forked from Vector35/binaryninja-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debuginfo.cpp
396 lines (301 loc) · 11.3 KB
/
debuginfo.cpp
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Copyright (c) 2015-2023 Vector 35 Inc
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
// TODO : Documentation
#include "binaryninjaapi.h"
using namespace BinaryNinja;
using namespace std;
///////////////
// DebugInfo //
///////////////
DebugInfo::DebugInfo(BNDebugInfo* debugInfo)
{
m_object = debugInfo;
}
vector<string> DebugInfo::GetParsers() const
{
size_t count;
char** parsers = BNGetDebugParserNames(m_object, &count);
vector<string> result;
for (size_t i = 0; i < count; ++i)
{
result.emplace_back(parsers[i]);
}
BNFreeStringList(parsers, count);
return result;
}
vector<NameAndType> DebugInfo::GetTypes(const string& parserName) const
{
size_t count;
BNNameAndType* nameAndTypes =
BNGetDebugTypes(m_object, parserName.size() == 0 ? nullptr : parserName.c_str(), &count);
vector<NameAndType> result;
for (size_t i = 0; i < count; ++i)
{
result.emplace_back(nameAndTypes[i].name,
Confidence<Ref<Type>>(new Type(BNNewTypeReference(nameAndTypes[i].type)), nameAndTypes[i].typeConfidence));
}
BNFreeDebugTypes(nameAndTypes, count);
return result;
}
vector<DebugFunctionInfo> DebugInfo::GetFunctions(const string& parserName) const
{
size_t count;
BNDebugFunctionInfo* functions =
BNGetDebugFunctions(m_object, parserName.size() == 0 ? nullptr : parserName.c_str(), &count);
vector<DebugFunctionInfo> result;
for (size_t i = 0; i < count; ++i)
{
result.emplace_back(functions[i].shortName ? functions[i].shortName : "",
functions[i].fullName ? functions[i].fullName : "", functions[i].rawName ? functions[i].rawName : "",
functions[i].address,
functions[i].type ? new Type(BNNewTypeReference(functions[i].type)) : nullptr,
functions[i].platform ? new Platform(BNNewPlatformReference(functions[i].platform)) : nullptr);
}
BNFreeDebugFunctions(functions, count);
return result;
}
vector<DataVariableAndName> DebugInfo::GetDataVariables(const string& parserName) const
{
size_t count;
BNDataVariableAndName* variableAndNames =
BNGetDebugDataVariables(m_object, parserName.size() == 0 ? nullptr : parserName.c_str(), &count);
vector<DataVariableAndName> result;
for (size_t i = 0; i < count; ++i)
{
result.emplace_back(variableAndNames[i].address,
Confidence(new Type(BNNewTypeReference(variableAndNames[i].type)), variableAndNames[i].typeConfidence),
variableAndNames[i].autoDiscovered, variableAndNames[i].name);
}
BNFreeDataVariablesAndName(variableAndNames, count);
return result;
}
// May return nullptr
Ref<Type> DebugInfo::GetTypeByName(const string& parserName, const string& name) const
{
BNType* result = BNGetDebugTypeByName(m_object, parserName.c_str(), name.c_str());
if (result)
return Ref<Type>(new Type(result));
return nullptr;
}
optional<tuple<uint64_t, Ref<Type>>> DebugInfo::GetDataVariableByName(
const string& parserName, const string& name) const
{
BNDataVariableAndName* result = BNGetDebugDataVariableByName(m_object, parserName.c_str(), name.c_str());
if (result)
{
BNFreeString(result->name);
return {{result->address, Ref<Type>(new Type(result->type))}};
}
return {};
}
optional<tuple<string, Ref<Type>>> DebugInfo::GetDataVariableByAddress(
const string& parserName, const uint64_t address) const
{
BNDataVariableAndName* nameAndVar = BNGetDebugDataVariableByAddress(m_object, parserName.c_str(), address);
if (nameAndVar)
{
const tuple<string, Ref<Type>> result = {nameAndVar->name, Ref<Type>(new Type(nameAndVar->type))};
BNFreeString(nameAndVar->name);
return {result};
}
return {};
}
// The tuple is (DebugInfoParserName, type)
vector<tuple<string, Ref<Type>>> DebugInfo::GetTypesByName(const string& name) const
{
size_t count;
BNNameAndType* namesAndTypes = BNGetDebugTypesByName(m_object, name.c_str(), &count);
vector<tuple<string, Ref<Type>>> result;
for (size_t i = 0; i < count; ++i)
{
result.emplace_back(namesAndTypes[i].name, Ref<Type>(new Type(BNNewTypeReference(namesAndTypes[i].type))));
}
BNFreeNameAndTypeList(namesAndTypes, count);
return result;
}
// The tuple is (DebugInfoParserName, address, type)
vector<tuple<string, uint64_t, Ref<Type>>> DebugInfo::GetDataVariablesByName(const string& name) const
{
size_t count;
BNDataVariableAndName* variablesAndName = BNGetDebugDataVariablesByName(m_object, name.c_str(), &count);
vector<tuple<string, uint64_t, Ref<Type>>> result;
for (size_t i = 0; i < count; ++i)
{
result.emplace_back(variablesAndName[i].name, variablesAndName[i].address,
Ref<Type>(new Type(BNNewTypeReference(variablesAndName[i].type))));
}
BNFreeDataVariablesAndName(variablesAndName, count);
return result;
}
// The tuple is (DebugInfoParserName, TypeName, type)
vector<tuple<string, string, Ref<Type>>> DebugInfo::GetDataVariablesByAddress(const uint64_t address) const
{
size_t count;
BNDataVariableAndNameAndDebugParser* variablesAndName = BNGetDebugDataVariablesByAddress(m_object, address, &count);
vector<tuple<string, string, Ref<Type>>> result;
for (size_t i = 0; i < count; ++i)
{
result.emplace_back(variablesAndName[i].parser, variablesAndName[i].name,
Ref<Type>(new Type(BNNewTypeReference(variablesAndName[i].type))));
}
BNFreeDataVariableAndNameAndDebugParserList(variablesAndName, count);
return result;
}
bool DebugInfo::RemoveParserInfo(const string& parserName)
{
return BNRemoveDebugParserInfo(m_object, parserName.c_str());
}
bool DebugInfo::RemoveParserTypes(const string& parserName)
{
return BNRemoveDebugParserTypes(m_object, parserName.c_str());
}
bool DebugInfo::RemoveParserFunctions(const string& parserName)
{
return BNRemoveDebugParserFunctions(m_object, parserName.c_str());
}
bool DebugInfo::RemoveParserDataVariables(const string& parserName)
{
return BNRemoveDebugParserDataVariables(m_object, parserName.c_str());
}
bool DebugInfo::RemoveTypeByName(const string& parserName, const string& name)
{
return BNRemoveDebugTypeByName(m_object, parserName.c_str(), name.c_str());
}
bool DebugInfo::RemoveFunctionByIndex(const string& parserName, const size_t index)
{
return BNRemoveDebugFunctionByIndex(m_object, parserName.c_str(), index);
}
bool DebugInfo::RemoveDataVariableByAddress(const string& parserName, const uint64_t address)
{
return BNRemoveDebugDataVariableByAddress(m_object, parserName.c_str(), address);
}
bool DebugInfo::AddType(const string& name, Ref<Type> type)
{
return BNAddDebugType(m_object, name.c_str(), type->GetObject());
}
bool DebugInfo::AddFunction(const DebugFunctionInfo& function)
{
BNDebugFunctionInfo* input = new BNDebugFunctionInfo();
input->shortName = function.shortName.size() ? BNAllocString(function.shortName.c_str()) : nullptr;
input->fullName = function.fullName.size() ? BNAllocString(function.fullName.c_str()) : nullptr;
input->rawName = function.rawName.size() ? BNAllocString(function.rawName.c_str()) : nullptr;
input->address = function.address;
input->type = function.type ? function.type->GetObject() : nullptr;
input->platform = function.platform ? function.platform->GetObject() : nullptr;
bool result = BNAddDebugFunction(m_object, input);
BNFreeString(input->shortName);
BNFreeString(input->fullName);
BNFreeString(input->rawName);
return result;
}
bool DebugInfo::AddDataVariable(uint64_t address, Ref<Type> type, const string& name)
{
if (name.size() == 0)
return BNAddDebugDataVariable(m_object, address, type->GetObject(), nullptr);
return BNAddDebugDataVariable(m_object, address, type->GetObject(), name.c_str());
}
/////////////////////
// DebugInfoParser //
/////////////////////
DebugInfoParser::DebugInfoParser(BNDebugInfoParser* parser)
{
m_object = parser;
}
Ref<DebugInfoParser> DebugInfoParser::GetByName(const string& name)
{
BNDebugInfoParser* parser = BNGetDebugInfoParserByName(name.c_str());
if (parser)
return new DebugInfoParser(BNNewDebugInfoParserReference(parser));
return nullptr;
}
vector<Ref<DebugInfoParser>> DebugInfoParser::GetList()
{
size_t count = 0;
BNDebugInfoParser** parsers = BNGetDebugInfoParsers(&count);
vector<Ref<DebugInfoParser>> result;
for (size_t i = 0; i < count; ++i)
{
result.emplace_back(new DebugInfoParser(BNNewDebugInfoParserReference(parsers[i])));
}
BNFreeDebugInfoParserList(parsers, count);
return result;
}
vector<Ref<DebugInfoParser>> DebugInfoParser::GetListForView(const Ref<BinaryView> data)
{
size_t count = 0;
BNDebugInfoParser** parsers = BNGetDebugInfoParsersForView(data->GetObject(), &count);
vector<Ref<DebugInfoParser>> result;
for (size_t i = 0; i < count; ++i)
{
result.emplace_back(new DebugInfoParser(BNNewDebugInfoParserReference(parsers[i])));
}
BNFreeDebugInfoParserList(parsers, count);
return result;
}
string DebugInfoParser::GetName() const
{
return BNGetDebugInfoParserName(m_object);
}
Ref<DebugInfo> DebugInfoParser::Parse(Ref<BinaryView> view, Ref<DebugInfo> existingDebugInfo, std::function<bool(size_t, size_t)> progress) const
{
ProgressContext ctxt;
if (progress)
ctxt.callback = progress;
else
ctxt.callback = [=](size_t, size_t) { return true; };
BNDebugInfo* info = nullptr;
if (existingDebugInfo)
{
info = BNParseDebugInfo(m_object, view->GetObject(), existingDebugInfo->GetObject(), ProgressCallback, &ctxt);
if (!info)
return nullptr;
info = BNNewDebugInfoReference(info);
}
else
{
info = BNParseDebugInfo(m_object, view->GetObject(), nullptr, ProgressCallback, &ctxt);
if (!info)
return nullptr;
}
return new DebugInfo(info);
}
bool DebugInfoParser::IsValidForView(const Ref<BinaryView> view) const
{
return BNIsDebugInfoParserValidForView(m_object, view->GetObject());
}
//////////////////////////////
// Plugin registration APIs //
//////////////////////////////
bool CustomDebugInfoParser::IsValidCallback(void* ctxt, BNBinaryView* view)
{
CustomDebugInfoParser* parser = (CustomDebugInfoParser*)ctxt;
return parser->IsValid(new BinaryView(view));
}
bool CustomDebugInfoParser::ParseCallback(void* ctxt, BNDebugInfo* debugInfo, BNBinaryView* view, bool (*progress)(void*, size_t, size_t), void* progressCtxt)
{
CustomDebugInfoParser* parser = (CustomDebugInfoParser*)ctxt;
return parser->ParseInfo(new DebugInfo(debugInfo), new BinaryView(view), [=](size_t cur, size_t max) {
return progress(progressCtxt, cur, max);
});
}
CustomDebugInfoParser::CustomDebugInfoParser(const string& name) :
DebugInfoParser(
BNNewDebugInfoParserReference(BNRegisterDebugInfoParser(name.c_str(), IsValidCallback, ParseCallback, this)))
{}