-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathdebugadapter.cpp
More file actions
283 lines (208 loc) · 8 KB
/
debugadapter.cpp
File metadata and controls
283 lines (208 loc) · 8 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
/*
Copyright 2020-2026 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.
*/
#include <binaryninjacore.h>
#include <binaryninjaapi.h>
#include <lowlevelilinstruction.h>
#include <mediumlevelilinstruction.h>
#include <highlevelilinstruction.h>
#ifndef WIN32
#include "libgen.h"
#endif
#include "debugadapter.h"
#include "debuggercontroller.h"
using namespace BinaryNinjaDebugger;
DebugModule::DebugModule() : m_name(""), m_short_name(""), m_address(0), m_size(0), m_loaded(false)
{
m_caseInsensitive = Settings::Instance()->Get<bool>("debugger.caseInsensitiveModuleName");
}
DebugModule::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)
{
m_caseInsensitive = Settings::Instance()->Get<bool>("debugger.caseInsensitiveModuleName");
}
DebugAdapter::DebugAdapter(BinaryView* data)
{
INIT_DEBUGGER_API_OBJECT();
// These are just a workaround to avoid holding a reference to the data. Note, when DebugAdapter is constructed,
// the binary view passed in is the one *before* rebasing. After the debugger launches the target and rebases the
// binary view, the un-rebased binary view is released by the core and the debugger becomes the only place where
// we hold a reference to it.
// Though, a better approach would be to add a function SetData() to the DebugAdapter class and update the binary
// view after the launch, similar to what we do for the DebuggerController. However, we will need to add new APIs
// to get the original image base of the binary view, because LLDB requires the breakpoint address be relative to
// the original image base, and it does not work with a rebased one.
m_entryPoint = data->GetEntryPoint();
// For shared libraries which do not have a valid entry point, the GetEntryPoint will return 0x0 anyways.
// Here we check if there is actually a function at the entry point, to determine if the entry point is real.
m_hasEntryFunction = (data->GetAnalysisEntryPoint() != nullptr);
m_start = data->GetStart();
m_originalImageBase = data->GetOriginalImageBase();
if (data->GetDefaultArchitecture())
m_defaultArchitecture = data->GetDefaultArchitecture()->GetName();
}
void DebugAdapter::PostDebuggerEvent(const DebuggerEvent& event)
{
if (m_eventCallback)
m_eventCallback(event);
}
std::string DebugModule::GetPathBaseName(const std::string& path)
{
#ifdef WIN32
// TODO: someone please write it on Windows!
char baseName[MAX_PATH];
char ext[MAX_PATH];
_splitpath_s(path.c_str(), NULL, 0, NULL, 0, baseName, MAX_PATH, ext, MAX_PATH);
return std::string(baseName) + std::string(ext);
#else
return basename(strdup(path.c_str()));
#endif
}
static bool StringsEqual(const std::string& str1, const std::string& str2, bool caseInsensitive)
{
if (!caseInsensitive)
return str1 == str2;
if (str1.size() != str2.size())
return false;
return std::equal(str1.begin(), str1.end(), str2.begin(),
[](char c1, char c2) { return std::tolower(c1) == std::tolower(c2); });
}
bool DebugModule::IsSameBaseModule(const DebugModule& other) const
{
return (StringsEqual(m_name, other.m_name, m_caseInsensitive)
|| StringsEqual(m_short_name, other.m_short_name, m_caseInsensitive)
|| StringsEqual(GetPathBaseName(m_name), GetPathBaseName(other.m_name), m_caseInsensitive)
|| StringsEqual(GetPathBaseName(m_short_name), GetPathBaseName(other.m_short_name), m_caseInsensitive));
}
bool DebugModule::IsSameBaseModule(const std::string& name) const
{
return (StringsEqual(m_name, name, m_caseInsensitive)
|| StringsEqual(m_short_name, name, m_caseInsensitive)
|| StringsEqual(GetPathBaseName(m_name), GetPathBaseName(name), m_caseInsensitive)
|| StringsEqual(GetPathBaseName(m_short_name), GetPathBaseName(name), m_caseInsensitive));
}
bool DebugModule::IsSameBaseModule(const std::string& module1, const std::string& module2)
{
// Cache the setting value on first call. Note: This will not update if the setting changes during runtime,
// but settings rarely change during a debugging session, and this avoids expensive Settings::Instance() calls.
static bool caseInsensitive = Settings::Instance()->Get<bool>("debugger.caseInsensitiveModuleName");
return (StringsEqual(module1, module2, caseInsensitive)
|| StringsEqual(GetPathBaseName(module1), GetPathBaseName(module2), caseInsensitive));
}
bool DebugAdapter::GoReverse()
{
return false;
}
bool DebugAdapter::StepIntoReverse()
{
return false;
}
bool DebugAdapter::StepOverReverse()
{
return false;
}
bool DebugAdapter::StepReturn()
{
return false;
}
bool DebugAdapter::StepReturnReverse()
{
return false;
}
uint64_t DebugAdapter::GetStackPointer()
{
return 0;
}
void DebugAdapter::WriteStdin(const std::string& msg)
{
LogWarn("WriteStdin operation not supported");
}
std::vector<DebugFrame> DebugAdapter::GetFramesOfThread(std::uint32_t tid)
{
return {};
}
bool DebugAdapter::ConnectToDebugServer(const std::string& server, std::uint32_t port)
{
return false;
}
bool DebugAdapter::DisconnectDebugServer()
{
return true;
}
Ref<Metadata> DebugAdapter::GetProperty(const std::string& name)
{
return nullptr;
}
bool DebugAdapter::SetProperty(const std::string& name, const BinaryNinja::Ref<BinaryNinja::Metadata>& value)
{
return false;
}
Ref<BinaryView> DebugAdapter::GetData()
{
if (!m_controller)
return nullptr;
return m_controller->GetData();
}
Ref<Settings> DebugAdapter::GetAdapterSettings()
{
return nullptr;
}
// TTD (Time Travel Debugging) default implementations
std::vector<TTDMemoryEvent> DebugAdapter::GetTTDMemoryAccessForAddress(uint64_t startAddress, uint64_t endAddress, TTDMemoryAccessType accessType)
{
// Default implementation returns empty results for adapters that don't support TTD
return {};
}
std::vector<TTDPositionRangeIndexedMemoryEvent> DebugAdapter::GetTTDMemoryAccessForPositionRange(uint64_t startAddress, uint64_t endAddress, TTDMemoryAccessType accessType, const TTDPosition startTime, const TTDPosition endTime)
{
// Default implementation returns empty results for adapters that don't support TTD
return {};
}
std::vector<TTDCallEvent> DebugAdapter::GetTTDCallsForSymbols(const std::string& symbols, uint64_t startReturnAddress, uint64_t endReturnAddress)
{
// Default implementation returns empty results
return {};
}
std::vector<TTDEvent> DebugAdapter::GetTTDEvents(TTDEventType eventType)
{
// Default implementation returns empty results for adapters that don't support TTD
return {};
}
std::vector<TTDEvent> DebugAdapter::GetAllTTDEvents()
{
// Default implementation returns empty results for adapters that don't support TTD
return {};
}
TTDPosition DebugAdapter::GetCurrentTTDPosition()
{
// Default implementation returns an empty position for adapters that don't support TTD
return TTDPosition();
}
bool DebugAdapter::SetTTDPosition(const TTDPosition& position)
{
// Default implementation returns false for adapters that don't support TTD
return false;
}
std::pair<bool, TTDMemoryEvent> DebugAdapter::GetTTDNextMemoryAccess(uint64_t address, uint64_t size, TTDMemoryAccessType accessType)
{
// Default implementation returns failure for adapters that don't support TTD
return {false, TTDMemoryEvent()};
}
std::pair<bool, TTDMemoryEvent> DebugAdapter::GetTTDPrevMemoryAccess(uint64_t address, uint64_t size, TTDMemoryAccessType accessType)
{
// Default implementation returns failure for adapters that don't support TTD
return {false, TTDMemoryEvent()};
}
std::vector<TTDStringEntry> DebugAdapter::GetTTDStrings(const std::string& pattern, uint64_t maxResults)
{
return {};
}