-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathdebugadapter.h
More file actions
359 lines (251 loc) · 10.7 KB
/
debugadapter.h
File metadata and controls
359 lines (251 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
Copyright 2020-2025 Vector 35 Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#pragma once
#include <cstdint>
#include <utility>
#include <vector>
#include <optional>
#include <string>
#include <stdexcept>
#include <functional>
#include <unordered_map>
#include <array>
#include "binaryninjaapi.h"
#include <fmt/format.h>
#include "../api/ffi.h"
#include "ffi_global.h"
#include "debuggercommon.h"
#include "debuggerevent.h"
#include "../vendor/intx/intx.hpp"
DECLARE_DEBUGGER_API_OBJECT(BNDebugAdapter, DebugAdapter);
using namespace BinaryNinja;
namespace BinaryNinjaDebugger {
enum StopReason
{
UnknownStopReason,
StdoutMessageReason,
ProcessExitedReason,
BackendDisconnectedReason,
SingleStepStopReason,
BreakpointStopReason,
ExceptionStopReason
};
// Used by the DebuggerState to query the capacities of the DebugAdapter, and take different actions accordingly.
enum DebugAdapterCapacity
{
DebugAdapterSupportStepOver,
DebugAdapterSupportStepReturn,
DebugAdapterSupportStepOverReverse,
DebugAdapterSupportModules,
DebugAdapterSupportThreads,
DebugAdapterSupportTTD,
};
struct LaunchConfigurations
{
bool requestTerminalEmulator;
std::string inputFile;
bool connectedToDebugServer;
LaunchConfigurations() : requestTerminalEmulator(true) {}
LaunchConfigurations(bool terminal, const std::string& file, bool debugServer) :
requestTerminalEmulator(terminal), inputFile(file), connectedToDebugServer(debugServer)
{}
};
struct DebugProcess
{
std::uint32_t m_pid {};
std::string m_processName {};
DebugProcess() {}
DebugProcess(std::uint32_t pid) : m_pid(pid) {}
DebugProcess(std::uint32_t pid, std::string name) : m_pid(pid), m_processName(name) {}
bool operator==(const DebugProcess& rhs) const
{
return (m_pid == rhs.m_pid) && (m_processName == rhs.m_processName);
}
bool operator!=(const DebugProcess& rhs) const { return !(*this == rhs); }
};
struct DebugThread
{
std::uint32_t m_tid {};
std::uintptr_t m_rip {};
bool m_isFrozen {};
DebugThread() {}
DebugThread(std::uint32_t tid) : m_tid(tid) {}
DebugThread(std::uint32_t tid, std::uintptr_t rip) : m_tid(tid), m_rip(rip) {}
bool operator==(const DebugThread& rhs) const { return (m_tid == rhs.m_tid) && (m_rip == rhs.m_rip); }
bool operator!=(const DebugThread& rhs) const { return !(*this == rhs); }
};
struct DebugBreakpoint
{
std::uintptr_t m_address {};
unsigned long m_id {};
bool m_is_active {};
DebugBreakpoint(std::uintptr_t address, unsigned long id, bool active) :
m_address(address), m_id(id), m_is_active(active)
{}
DebugBreakpoint(std::uintptr_t address) : m_address(address) {}
DebugBreakpoint() {}
bool operator==(const DebugBreakpoint& rhs) const { return this->m_address == rhs.m_address; }
bool operator!() const { return !this->m_address && !this->m_id && !this->m_is_active; }
};
struct DebugRegister
{
std::string m_name {};
intx::uint512 m_value {};
std::size_t m_width {}, m_registerIndex {};
std::string m_hint {};
DebugRegister() = default;
DebugRegister(std::string name, intx::uint512 value, std::size_t width, std::size_t register_index) :
m_name(std::move(name)), m_value(value), m_width(width), m_registerIndex(register_index)
{}
};
struct DebugModule
{
std::string m_name {}, m_short_name {};
std::uintptr_t m_address {};
std::size_t m_size {};
bool m_loaded {};
DebugModule() : m_name(""), m_short_name(""), m_address(0), m_size(0) {}
DebugModule(std::string name, std::string short_name, std::uintptr_t address, std::size_t size, bool loaded) :
m_name(std::move(name)), m_short_name(std::move(short_name)), m_address(address), m_size(size),
m_loaded(loaded)
{}
// These are useful for remote debugging. Paths can be different on the host and guest systems, e.g.,
// /usr/bin/ls, and C:\Users\user\Desktop\ls. So we must compare the base file name, rather than the full path.
bool IsSameBaseModule(const DebugModule& other) const;
bool IsSameBaseModule(const std::string& name) const;
static bool IsSameBaseModule(const std::string& module, const std::string& module2);
static std::string GetPathBaseName(const std::string& path);
};
struct DebugFrame
{
size_t m_index = 0;
uint64_t m_pc = 0;
uint64_t m_sp = 0;
uint64_t m_fp = 0;
std::string m_functionName;
uint64_t m_functionStart = 0;
std::string m_module = "<unknown>";
DebugFrame() = default;
DebugFrame(size_t index, uint64_t pc, uint64_t sp, uint64_t fp, const std::string& functionName,
uint64_t functionStart, const std::string& module) :
m_index(index),
m_pc(pc), m_sp(sp), m_fp(fp), m_functionName(functionName), m_functionStart(functionStart), m_module(module)
{}
};
struct DebugMemoryRegion
{
std::uintptr_t m_start = 0;
std::uintptr_t m_end = 0;
uint32_t m_permissions = 0; // flags for read/write/execute permissions
std::string m_name;
std::string m_module; // associated module/file if applicable
// Permission flags
static constexpr uint32_t PermRead = 1;
static constexpr uint32_t PermWrite = 2;
static constexpr uint32_t PermExecute = 4;
DebugMemoryRegion() = default;
DebugMemoryRegion(std::uintptr_t start, std::uintptr_t end, uint32_t permissions,
const std::string& name = "", const std::string& module = "") :
m_start(start), m_end(end), m_permissions(permissions), m_name(name), m_module(module)
{}
std::size_t GetSize() const { return m_end > m_start ? m_end - m_start : 0; }
bool IsReadable() const { return (m_permissions & PermRead) != 0; }
bool IsWritable() const { return (m_permissions & PermWrite) != 0; }
bool IsExecutable() const { return (m_permissions & PermExecute) != 0; }
bool Contains(std::uintptr_t address) const { return address >= m_start && address < m_end; }
};
class DebuggerController;
class DebugAdapter
{
IMPLEMENT_DEBUGGER_API_OBJECT(BNDebugAdapter);
private:
// Function to call when the DebugAdapter wants to notify the front-end of certain events
// TODO: we should not use a vector here; only the DebuggerController should register one here;
// Other components should register their callbacks to the controller, who is responsible for notify them.
std::function<void(const DebuggerEvent& event)> m_eventCallback;
DebuggerController* m_controller;
protected:
uint64_t m_entryPoint;
bool m_hasEntryFunction;
uint64_t m_start;
uint64_t m_originalImageBase;
std::string m_defaultArchitecture;
public:
DebugAdapter(BinaryView* data);
virtual ~DebugAdapter() {}
virtual bool Init() { return true; }
virtual void SetEventCallback(std::function<void(const DebuggerEvent& event)> function)
{
m_eventCallback = function;
}
[[nodiscard]] virtual bool Execute(const std::string& path, const LaunchConfigurations& configs = {}) = 0;
[[nodiscard]] virtual bool ExecuteWithArgs(const std::string& path, const std::string& args,
const std::string& workingDir, const LaunchConfigurations& configs = {}) = 0;
[[nodiscard]] virtual bool Attach(std::uint32_t pid) = 0;
[[nodiscard]] virtual bool Connect(const std::string& server, std::uint32_t port) = 0;
virtual bool ConnectToDebugServer(const std::string& server, std::uint32_t port);
virtual bool DisconnectDebugServer();
virtual bool Detach() = 0;
virtual bool Quit() = 0;
virtual std::vector<DebugProcess> GetProcessList() = 0;
virtual std::vector<DebugThread> GetThreadList() = 0;
virtual DebugThread GetActiveThread() const = 0;
virtual std::uint32_t GetActiveThreadId() const = 0;
virtual bool SetActiveThread(const DebugThread& thread) = 0;
virtual bool SetActiveThreadId(std::uint32_t tid) = 0;
virtual bool SuspendThread(std::uint32_t tid) = 0;
virtual bool ResumeThread(std::uint32_t tid) = 0;
virtual std::vector<DebugFrame> GetFramesOfThread(std::uint32_t tid);
virtual DebugBreakpoint AddBreakpoint(const std::uintptr_t address, unsigned long breakpoint_type = 0) = 0;
virtual DebugBreakpoint AddBreakpoint(const ModuleNameAndOffset& address, unsigned long breakpoint_type = 0) = 0;
virtual bool RemoveBreakpoint(const DebugBreakpoint& breakpoint) = 0;
virtual bool RemoveBreakpoint(const ModuleNameAndOffset& address) { return false; }
virtual std::vector<DebugBreakpoint> GetBreakpointList() const = 0;
virtual std::unordered_map<std::string, DebugRegister> ReadAllRegisters() = 0;
virtual DebugRegister ReadRegister(const std::string& reg) = 0;
virtual bool WriteRegister(const std::string& reg, intx::uint512 value) = 0;
virtual DataBuffer ReadMemory(std::uintptr_t address, std::size_t size) = 0;
virtual bool WriteMemory(std::uintptr_t address, const DataBuffer& buffer) = 0;
virtual std::vector<DebugModule> GetModuleList() = 0;
virtual std::vector<DebugMemoryRegion> GetMemoryRegions() { return {}; }
virtual std::string GetTargetArchitecture() = 0;
virtual DebugStopReason StopReason() = 0;
virtual uint64_t ExitCode() = 0;
virtual bool BreakInto() = 0;
virtual bool Go() = 0;
virtual bool GoReverse();
virtual bool StepInto() = 0;
virtual bool StepIntoReverse();
virtual bool StepOver() = 0;
virtual bool StepOverReverse();
// virtual bool RunTo(std::uintptr_t address) = 0;
virtual bool StepReturn();
virtual bool StepReturnReverse();
virtual std::string InvokeBackendCommand(const std::string& command) = 0;
virtual uint64_t GetInstructionOffset() = 0;
virtual uint64_t GetStackPointer();
virtual bool SupportFeature(DebugAdapterCapacity feature) = 0;
// This is implemented by the (base) DebugAdapter class.
// Sub-classes should use it to post debugger events directly (only when needed).
void PostDebuggerEvent(const DebuggerEvent& event);
virtual void WriteStdin(const std::string& msg);
virtual BinaryNinja::Ref<BinaryNinja::Metadata> GetProperty(const std::string& name);
virtual bool SetProperty(const std::string& name, const BinaryNinja::Ref<BinaryNinja::Metadata>& value);
// The debug adapter some times need to have access to the controller and the binary view. However, it is
// better NOT to directly keep a reference to the binary view, since the rebasing can make the view
void SetController(DebuggerController* controller) { m_controller = controller; }
DebuggerController* GetController() { return m_controller; }
Ref<BinaryView> GetData();
virtual Ref<Settings> GetAdapterSettings();
};
}; // namespace BinaryNinjaDebugger