-
Notifications
You must be signed in to change notification settings - Fork 213
/
debuginfo.cpp
466 lines (359 loc) · 13.8 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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// Copyright (c) 2015-2024 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;
}
TypeContainer DebugInfo::GetTypeContainer(const std::string& parserName)
{
return TypeContainer(BNGetDebugInfoTypeContainer(m_object, parserName.c_str()));
}
vector<NameAndType> DebugInfo::GetTypes(const string& parserName) const
{
size_t count;
BNNameAndType* nameAndTypes =
BNGetDebugTypes(m_object, parserName.size() == 0 ? nullptr : parserName.c_str(), &count);
if (nameAndTypes == nullptr)
return {};
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);
if (functions == nullptr)
return {};
vector<DebugFunctionInfo> result;
for (size_t i = 0; i < count; ++i)
{
vector<string> components;
for (size_t componentN = 0; componentN < functions[i].componentN; ++componentN)
components.emplace_back(functions[i].components[componentN]);
vector<VariableNameAndType> localVariables;
for (size_t localVariableN = 0; localVariableN < functions[i].localVariableN; ++localVariableN)
{
auto& bnVar = functions[i].localVariables[localVariableN];
VariableNameAndType vnt;
vnt.var = bnVar.var;
vnt.name = bnVar.name;
vnt.autoDefined = bnVar.autoDefined;
vnt.type = Confidence<Ref<Type>>(new Type(BNNewTypeReference(bnVar.type)), bnVar.typeConfidence);
localVariables.push_back(vnt);
}
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 CorePlatform(BNNewPlatformReference(functions[i].platform)) : nullptr,
components, localVariables);
}
BNFreeDebugFunctions(functions, count);
return result;
}
vector<DataVariableAndName> DebugInfo::GetDataVariables(const string& parserName) const
{
size_t count;
BNDataVariableAndName* variablesAndName =
BNGetDebugDataVariables(m_object, parserName.size() == 0 ? nullptr : parserName.c_str(), &count);
if (variablesAndName == nullptr)
return {};
vector<DataVariableAndName> result;
for (size_t i = 0; i < count; ++i)
{
result.emplace_back(variablesAndName[i].address,
Confidence(new Type(BNNewTypeReference(variablesAndName[i].type)), variablesAndName[i].typeConfidence),
variablesAndName[i].autoDiscovered, variablesAndName[i].name);
}
BNFreeDataVariablesAndName(variablesAndName, 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);
if (namesAndTypes == nullptr)
return {};
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);
if (variablesAndName == nullptr)
return {};
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);
if (variablesAndName == nullptr)
return {};
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, const vector<string>& components)
{
const char** const componentArray = new const char*[components.size()];
for (size_t i = 0; i < components.size(); ++i)
componentArray[i] = components[i].c_str();
return BNAddDebugType(m_object, name.c_str(), type->GetObject(), componentArray, components.size());
}
bool DebugInfo::AddFunction(const DebugFunctionInfo& function)
{
char** components = new char*[function.components.size()];
for (size_t i = 0; i < function.components.size(); ++i)
components[i] = (char*)function.components[i].c_str();
BNVariableNameAndType* localVariables = new BNVariableNameAndType[function.localVariables.size()];
for (size_t i = 0; i < function.localVariables.size(); i++)
{
auto v = function.localVariables[i];
localVariables[i].var = v.var;
localVariables[i].type = v.type.GetValue()->m_object;
localVariables[i].typeConfidence = v.type.GetConfidence();
localVariables[i].name = (char*)v.name.c_str();
localVariables[i].autoDefined = v.autoDefined;
}
BNDebugFunctionInfo input;
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;
input.components = components;
input.componentN = function.components.size();
input.localVariables = localVariables;
input.localVariableN = function.localVariables.size();
bool result = BNAddDebugFunction(m_object, &input);
BNFreeString(input.shortName);
BNFreeString(input.fullName);
BNFreeString(input.rawName);
delete[] components;
delete[] localVariables;
return result;
}
bool DebugInfo::AddDataVariable(uint64_t address, Ref<Type> type, const string& name, const vector<string>& components)
{
const char** const componentArray = new const char*[components.size()];
for (size_t i = 0; i < components.size(); ++i)
componentArray[i] = components[i].c_str();
if (name.size() == 0)
return BNAddDebugDataVariable(m_object, address, type->GetObject(), nullptr, componentArray, components.size());
return BNAddDebugDataVariable(m_object, address, type->GetObject(), name.c_str(), componentArray, components.size());
}
/////////////////////
// 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<BinaryView> debugFile, 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(), debugFile->GetObject(), existingDebugInfo->GetObject(),
ProgressCallback, &ctxt);
if (!info)
return nullptr;
info = BNNewDebugInfoReference(info);
}
else
{
info = BNParseDebugInfo(m_object, view->GetObject(), debugFile->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,
BNBinaryView* debugFile, BNProgressFunction progress, void* progressCtxt)
{
CustomDebugInfoParser* parser = (CustomDebugInfoParser*)ctxt;
return parser->ParseInfo(new DebugInfo(debugInfo), new BinaryView(view), new BinaryView(debugFile),
[=](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)))
{}