-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
321 lines (258 loc) · 5.54 KB
/
main.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
/*
* main.c
*
* Created on: 15 févr. 2012
* Author: cor
*/
#include "Common.h"
#include "XmlVisitor/XmlExporter.h"
#include "XmlVisitor/XmlVisitor.h"
#include "XmlVisitor/XmlDieVisitor.h"
#define VERSION_MAJOR 0
#define VERSION_MINOR 1
using namespace std;
void printUsage(const char * name)
{
cout << name << " v" << VERSION_MAJOR << "." << VERSION_MINOR << endl;
cout << "---" << endl;
cout << "Query stuff from files with Dwarf debugging infos (.elf, .o, .exe, .dll, ...)" << endl;
cout << "Syntax: <source file> <options>" << endl;
cout << "Options:" << endl;
cout << " -o <output.xml> : File output as XML, to stdout if option is not specified" << endl;
cout << " -q <query + query options> : Add queries results to output" << endl;
cout << "Queries:" << endl;
cout << " 'cus' : List all files used in compilation" << endl;
cout << " 'sym' : Find symbols (types, members) in input file and dump the type as a tree" << endl;
cout << " down to base types" << endl;
cout << " => Format: sym <symbol> <verbose:0|1,optional>" << endl;
cout << "Example: " << endl;
cout << " " << name << " /path/to/my/file.elf -o dump.xml -q cus -q sym myBigVar" << endl;
}
namespace Queries
{
class Query
{
public:
virtual int ParseOptions(int argc, char * argv[]) = 0;
virtual void DoWork(DwarfObj & obj, Exporters::XmlExporter & exporter) = 0;
string Name;
protected:
Query(string name) :
Name(name)
{
}
};
class Cu: public Query
{
public:
static const char * Option;
Cu() :
Query("Cu")
{
}
int ParseOptions(int argc, char * argv[])
{
return 0;
}
void DoWork(DwarfObj & obj, Exporters::XmlExporter & exporter)
{
exporter.ExportCus(&obj);
}
};
const char * Cu::Option = "cus";
class Enum: public Query
{
public:
static const char * Option;
Enum() :
Query("Enum")
{
}
int ParseOptions(int argc, char * argv[])
{
return 0;
}
void DoWork(DwarfObj & obj, Exporters::XmlExporter & exporter)
{
exporter.ExportEnums(&obj);
}
};
const char * Enum::Option = "enums";
class Symbol: public Query
{
public:
static const char * Option;
Symbol() :
Query("Symbol"), Verbose(false)
{
}
string ExportedSymbol;
bool Verbose;
int ParseOptions(int argc, char * argv[])
{
if ((argc == 0) || (argv[0][0] == '-'))
{
cerr << "Query Symbol takes at least one argument" << endl;
throw exception();
}
ExportedSymbol = argv[0];
cout << "Symbol: Export Symbol is " << ExportedSymbol << endl;
if (argc <= 1)
return 1;
string verb(argv[1]);
cout << "Symbol: Verbose is " << verb << endl;
if (verb == "0")
Verbose = false;
else if (verb == "1")
Verbose = true;
else
return 1;
return 2;
}
void DoWork(DwarfObj & obj, Exporters::XmlExporter & exporter)
{
DwarfDie * root = obj.FindDie(ExportedSymbol.c_str());
exporter.ExportDie(root, Verbose);
if (root != NULL)
{
delete root;
}
}
};
const char * Symbol::Option = "sym";
}
class Options
{
public:
Options() :
HasOutput(false)
{
}
void Parse(int argc, char * argv[]);
string SourceFile;
/* Output */
bool HasOutput;
string Output;
/* Queries */
list<Queries::Query *> Queries;
};
void Options::Parse(int argc, char * argv[])
{
int lastArgc = 0;
int currentArgc = 1;
bool error = true;
/* Source file */
if (argc < 2)
return;
SourceFile = argv[currentArgc];
currentArgc++;
cout << "Found Source " << SourceFile << endl;
while (lastArgc != currentArgc)
{
lastArgc = currentArgc;
if (argc <= currentArgc)
break;
if (string(argv[currentArgc]) == "-o")
{
currentArgc++;
if (argc <= currentArgc)
break;
HasOutput = true;
Output = argv[currentArgc];
currentArgc++;
/* All required options have been found */
error = false;
cout << "Found Output " << Output << endl;
}
else if (string(argv[currentArgc]) == "-q")
{
currentArgc++;
if (argc <= currentArgc)
{
error = true;
break;
}
string queryStr(argv[currentArgc]);
Queries::Query * query = NULL;
if (queryStr == Queries::Cu::Option)
{
query = new Queries::Cu();
cout << "Found Query CU" << endl;
}
else if (queryStr == Queries::Symbol::Option)
{
query = new Queries::Symbol();
cout << "Found Query Symbol" << endl;
}
else if (queryStr == Queries::Enum::Option)
{
query = new Queries::Enum();
cout << "Found Query Enum" << endl;
}
else
{
cout << "Unable to find query for " << queryStr << endl;
}
currentArgc++;
if (query != NULL)
{
currentArgc += query->ParseOptions(argc - currentArgc, &argv[currentArgc]);
Queries.push_back(query);
}
}
}
if (error)
{
cerr << "Unable to parse options" << endl;
throw exception();
}
}
ostream & operator<<(ostream & os, DwarfCu * cu)
{
return *cu << os;
}
ostream & operator<<(ostream & os, DwarfDie * die)
{
os << "Die " << die->GetTag().ToString();
const char * name = die->GetAttrName();
if (name != NULL)
os << " [" << name << "]";
return os;
}
int main(int argc, char * argv[])
{
/* Usage */
if (argc <= 1)
{
printUsage(argv[0]);
return -1;
}
try
{
/* Handle options */
Options opts;
opts.Parse(argc, argv);
/* Process */
DwarfObj file(opts.SourceFile);
file.PrintFiles();
Exporters::XmlExporter exporter;
list<Queries::Query *>::iterator it;
for (it = opts.Queries.begin(); it != opts.Queries.end(); it++)
{
(*it)->DoWork(file, exporter);
}
/* Export */
if (opts.HasOutput)
{
exporter.Save(opts.Output);
}
else
{
exporter.Print();
}
} catch (exception & ex)
{
cerr << "Exception" << endl;
}
return 0;
}