Repository navigation
Expand file tree
/
Copy pathmi_parse.cpp
More file actions
493 lines (479 loc) · 13.8 KB
/
Copy pathmi_parse.cpp
File metadata and controls
493 lines (479 loc) · 13.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
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/* parseur de donnees MI produites par GDB
- maintient une pile de l'ascendance de l'element courant
*/
#include <stdio.h>
using namespace std;
#include <string>
#include <vector>
#include <map>
#include "target.h"
#include "mi_parse.h"
/*
etats :
<0 : vu fin de conteneur, depilage immediat requis puis aller en (-e)
1 : debut de ligne, attendre ^, etc...
2 : vu ^, attendre texte ou ',' ou fin de ligne
10 : a l'interieur d'un conteneur, attendre texte, quoted-string ou autre conteneur ou fin de conteneur
11 : vu 1er char d'un nom, attendre la suite ou '='
12 : vu '=', attendre valeur i.e. " ou conteneur i.e. '[' ou '{'
13 : vu '"', attendre char ou '"' ou '\'
14 : vu '\', attendre char
15 : vu '"' (fin de valeur) ou fin de conteneur, attendre ',' ou fin de conteneur
70 : vu '~', stream-output, attendre texte ou fin de ligne
74 : vu '\', attendre char ou fin de ligne
N.B. 15 precede 10 sauf au debut du conteneur
valeurs de retour :
0 : R.A.S.
1 : fin de valeur
2 : debut de conteneur nomme
3 : debut de conteneur anonyme
4 : fin de conteneur nomme
5 : fin de conteneur anonyme
6 : debut de conteneur report
7 : debut et fin de conteneur report vide
8 : fin de conteneur report
9 : fin de stream-output
*/
// cette fonction parse un caractere et rend zero sauf si on a une fin de valeur
// si la valeur de retour est >= 0, le processus peut continuer (e est a jour)
int mi_parse::proc1char( int c )
{
if ( e < 0 ) // depilage eventuel avant lecture char
{
if ( stac.size() )
stac.pop_back();
else return(-6661); // depilage impossible
e = -e;
}
if ( c < ' ' )
{ // ci-dessous: liste des etats gerant eux-memes la fin de ligne
if ( ( e != 1 ) && ( e != 2 ) && ( e != 10 ) && ( e != 15 ) && ( e != 70 ) )
return(-66631); // fin de ligne inattendue
}
switch ( e )
{ // les reports hierarchises (kinda serialized objects)
// reply status exec status notif
case 1: if ( ( c == '^' ) || ( c == '+' ) || ( c == '*' ) || ( c == '=' ) )
{
e = 2;
nam = char(c); // le '^' est inclus dans le nom de conteneur
}
// les streams 'human readable'
// console log target out (gdb) prompt (echo cmd)
else if ( ( c == '~' ) || ( c == '&' ) || ( c == '@' ) || ( c == '(' ) || ( c == '-' ) )
{
e = 70; // raw stream
nam = char(c); val = ""; // le nom du stream est simplement ~, & ou @
}
else if ( c > ' ' ) // accepter les residus de newline
{
return(-6662); // debut de report illegal
} break;
case 2: if ( c == ',' )
{
stac.push_back( conteneur( nam, c ) );
e = 10;
return( 6 ); // debut conteneur report, le nom est dans nam
}
else if ( c < ' ' )
{
e = 1;
return( 7 ); // debut et fin de report court, le nom est dans nam
}
else {
nam += char(c);
} break;
case 10: if ( ( c == '{' ) || ( c == '[' ) )
{
stac.push_back( conteneur( "", c ) );
e = 10;
return( 3 ); // debut conteneur anonyme
}
else if ( ( c == '}' ) || ( c == ']' ) )
{ // cet etat -15 sert a differer le depilage du conteneur avant etat 15
e = -15; // afin que celui-ci puisse etre lu juste apres ce return
if ( stac.back().nam.size() )
return( 4 ); // fin de conteneur nomme
else return( 5 ); // fin de conteneur anonyme
}
else if ( c == '"' )
{
val = ""; nam = ""; // debut de valeur anonyme
e = 13;
}
else if ( c < ' ' )
{
c = stac.back().nam[0];
if ( ( c == '^' ) || ( c == '+' ) || ( c == '*' ) || ( c == '=' ) )
{
e = -1;
return( 8 ); // fin de conteneur report
}
else return(-66632); // fin de ligne inattendue
}
else {
nam = char(c);
e = 11;
} break;
case 11: if ( c == '=' )
{
val = "";
e = 12;
}
else {
nam += char(c);
} break;
case 12: if ( ( c == '{' ) || ( c == '[' ) )
{
stac.push_back( conteneur( nam, c ) ); // debut conteneur
e = 10;
return( 2 ); // debut conteneur
}
else if ( c == '"' )
{
val = "";
e = 13; // debut de valeur simple
}
else return(-6664); // signe egal mal suivi
break;
case 13: if ( c == '\\' )
e = 14;
else if ( c == '"' )
{
e = 15;
return( 1 ); // fin de valeur simple
}
else {
val += char(c);
} break;
case 14: {
e = 13;
val += char(c);
} break;
case 15: if ( c == ',' )
{
e = 10;
}
else if ( ( c == '}' ) || ( c == ']' ) )
{ // cet etat -15 sert a differer le depilage du conteneur avant etat 15
e = -15; // afin que celui-ci puisse etre lu juste apres ce return
if ( stac.back().nam.size() )
return( 4 ); // fin de conteneur nomme
else return( 5 ); // fin de conteneur anonyme
}
else if ( c < ' ' )
{
c = stac.back().nam[0];
if ( ( c == '^' ) || ( c == '+' ) || ( c == '*' ) || ( c == '=' ) )
{
e = -1;
return( 8 ); // fin de conteneur report
}
else return(-66633); // fin de ligne inattendue
}
else return(-6665); // garbage apres conteneur
break;
case 70 : if ( c < ' ' )
{
e = 1; // rien a depiler
return( 9 ); // fin de stream, le contenu est dans val
}
else if ( c == '\\' )
{
e = 74;
}
else if ( c != '"' )
{
val += char(c);
} break;
case 74 : if ( c == 'n' )
val += char(10);
else if ( c == 't' )
val += char(8);
else val += char(c);
e = 70; break;
}
return 0;
}
// formatter un dump texte indente, en fonction de la valeur retournee par mi_parse::proc1char()
// seulement si celle-ci est positive - retourne le nombre de chars mis dans buf
int mi_parse::dump( int retval, char * buf, int size )
{
int pos = 0;
static int level = 0;
switch ( retval )
{
case 1: if ( level > 0 )
pos += snprintf( buf, size, "%*s", level*3, " " ); // indent!
if ( nam.size() )
pos += snprintf( buf+pos, size, "%s = \"%s\"", nam.c_str(), val.c_str() );
else pos += snprintf( buf+pos, size, "\"%s\"", val.c_str() );
break;
case 2: if ( level > 0 )
pos += snprintf( buf, size, "%*s", level*3, " " ); // indent!
// pos += snprintf( buf+pos, size, "debut conteneur %s %c",
pos += snprintf( buf+pos, size, "conteneur %s %c",
stac.back().nam.c_str(), stac.back().type );
++level;
break;
case 3: ++level;
break;
case 4: --level;
if ( level > 0)
pos += snprintf( buf, size, "%*s", level*3, " " ); // indent!
// pos += snprintf( buf+pos, size, "fin conteneur %s !%c",
// stac.back().nam.c_str(), stac.back().type );
// un hack bien sale : '{' + 2 donne '}', '[' + 2 donne ']'
pos += snprintf( buf+pos, size, "%c", stac.back().type + 2 );
break;
case 5: --level;
break;
case 6: pos += snprintf( buf, size, "debut report %s", nam.c_str() );
level = 1;
break;
case 7: pos += snprintf( buf, size, "report court %s", nam.c_str() );
level = 0;
break;
case 8: pos += snprintf( buf, size, "fin report %s", stac.back().nam.c_str() );
level = 0;
break;
case 9: if ( nam[0] == '(' )
pos += snprintf( buf, size, "(%s", val.c_str() );
else pos += snprintf( buf, size, "stream %c%s", nam[0], val.substr(0,72).c_str() );
break;
}
return pos;
}
// extraire et ranger les donnees dans la target, en fonction de la valeur retournee par mi_parse::proc1char()
/* 0 : R.A.S.
1 : fin de valeur
2 : debut de conteneur nomme
3 : debut de conteneur anonyme
4 : fin de conteneur nomme
5 : fin de conteneur anonyme
6 : debut de conteneur report
7 : fin de conteneur report vide
8 : fin de conteneur report
9 : fin de stream-output
*/
int mi_parse::extract( int retval, target * targ )
{
// static variables holding pieces of partially parsed elements
static asmline curasm;
static string relfile;
static string absfile;
static int curline0 = -1;
static int curline1;
static unsigned int number;
int ss = stac.size();
switch ( retval )
{ // N.B. il faudra optimiser l'ordre des tests en fonction de la frequence des cas ;-)
// refactoriser et mutualiser
case 1: // fin de valeur
if ( ( ss ) && ( stac.back().nam == string("register-names") ) ) // short circuit eval ;-)
targ->regs.add_reg_name( val );
else if ( ( ss ) && ( stac.back().nam == string("src_and_asm_line") ) )
{
if ( nam == string("line") )
{
if ( curline0 < 0 )
curline0 = curline1 = strtoul( val.c_str(), NULL, 10 );
else curline1 = strtoul( val.c_str(), NULL, 10 );
}
else if ( nam == string("file") )
relfile = val;
else if ( nam == string("fullname") )
absfile = val;
}
else if ( ( ss ) && ( stac.back().nam == string("bkpt") ) )
{
if ( nam == string("number") )
number = strtoul( val.c_str(), NULL, 0 );
else if ( nam == string("addr") )
targ->add_break( number, strtoull( val.c_str(), NULL, 0 ) );
}
else if ( ( ss >= 2 ) && ( stac[stac.size()-2].nam == string("register-values") ) )
{
if ( nam == string("number") )
number = strtoul( val.c_str(), NULL, 0 );
else if ( nam == string("value") )
targ->regs.set_reg( number, strtoull( val.c_str(), NULL, 0 ) );
}
else if ( ( ss >= 2 ) && (
( stac[stac.size()-2].nam == string("line_asm_insn") ) ||
( stac[stac.size()-2].nam == string("asm_insns") )
)
)
{
if ( nam == string("address") ) curasm.set_adr( val );
else if ( nam == string("opcodes") )
{
// curasm.count_the_bytes( val );
if ( targ->option_binvis )
curasm.parse_the_bytes( val.c_str() );
else curasm.count_the_bytes( val );
}
else if ( nam == string("inst") ) curasm.asmsrc = val;
}
else if ( ( ss >= 2 ) && ( stac[stac.size()-2].nam == string("memory") ) )
{
if ( nam == string("begin") )
{
targ->ramstock[0].w32.clear();
targ->ramstock[0].adr0 = strtoull( val.c_str(), NULL, 0 );
}
else if ( nam == string("offset") )
targ->ramstock[0].adr0 += strtoull( val.c_str(), NULL, 0 );
else if ( nam == string("contents") )
{
targ->ramstock[0].txt2w32( val.c_str() );
}
}
else if ( ( ss ) && ( stac.back().nam == string("*stopped") ) )
{
if ( nam == "signal-meaning" )
{
if ( targ->reason == "signal-received" )
{ targ->reason += " : "; targ->reason += val; }
}
if ( nam == "reason" )
{
targ->reason = val;
}
}
else if ( ( ss ) && ( stac.back().nam == string("^error") ) )
{
if ( nam == "msg" )
targ->error_msg = val;
}
break;
case 2: // debut de conteneur nomme
if ( ( ss ) && ( stac.back().nam == string("register-names") ) )
targ->regs.start_reg_names();
if ( ( ss ) && ( stac.back().nam == string("BreakpointTable") ) )
targ->breakpoints.clear();
break;
case 3: // debut de conteneur anonyme
if ( ( ss >= 2 ) && (
( stac[stac.size()-2].nam == string("line_asm_insn") ) ||
( stac[stac.size()-2].nam == string("asm_insns") )
)
)
curasm.init();
break;
case 4: // fin de conteneur nomme
if ( ( ss ) && ( stac.back().nam == string("register-names") ) )
targ->job_reset_running(RegNames);
else if ( ( ss ) && ( stac.back().nam == string("asm_insns") ) )
targ->job_reset_running(Disass);
else if ( ( ss ) && ( stac.back().nam == string("register-values") ) )
targ->job_reset_running(RegVal);
else if ( ( ss ) && ( stac.back().nam == string("BreakpointTable") ) )
targ->job_reset_running(BreakList);
else if ( ( ss ) && ( stac.back().nam == string("memory") ) )
targ->job_reset_running(RAMRead);
else if ( ( ss >= 2 ) &&
(
( stac.back().nam == string("bkpt") ) &&
( stac[stac.size()-2].nam == string("^done") )
)
)
targ->job_reset_running(BreakSetKill);
break;
case 5: // fin de conteneur anonyme
if ( ( ss >= 2 ) && (
( stac[stac.size()-2].nam == string("line_asm_insn") ) ||
( stac[stac.size()-2].nam == string("asm_insns") )
)
)
{
if ( targ->asmmap.count(curasm.adr) )
{ // asm deja vu a cette adresse
}
else {
if ( stac[stac.size()-2].nam == string("line_asm_insn") )
{
curasm.src0 = curline0;
curasm.src1 = curline1;
curline0 = -1;
if ( targ->filemap.count(relfile) == 0 )
{
srcfile curfile;
targ->filemap[relfile] = targ->filestock.size();
targ->filestock.push_back( curfile );
targ->filestock.back().relpath = relfile;
targ->filestock.back().abspath = absfile;
if ( targ->filestock.back().status == 0 )
targ->filestock.back().readfile();
}
curasm.isrc = targ->filemap[relfile];
}
targ->asmmap[curasm.adr] = targ->asmstock.size();
targ->asmstock.push_back( curasm );
}
}
break;
case 6: // debut de conteneur report
break;
case 7: // fin de conteneur report vide
if ( nam == string("^done") )
{
targ->job_reset_running(BreakSetKill);
targ->job_reset_running(GDBSet);
}
break;
case 8: // fin de conteneur report
if ( ( ss ) && ( stac.back().nam == string("*stopped") ) )
{
targ->job_reset_running(Run);
targ->job_reset_running(Continue);
if ( ((int)targ->reason.find("egmentation fault")) > 0 )
{
targ->error_msg = targ->reason;
targ->job_set_error( Run );
}
else if ( ((int)targ->reason.find("nknown signal")) > 0 )
{
targ->error_msg = targ->reason;
#ifdef __amd64
targ->error_msg += "\nThis tool is for 64-bit targets only";
#else
targ->error_msg += "\nThis tool is for 32-bit targets only";
#endif
targ->job_set_error( Run );
}
}
else if ( ( ss ) && ( stac.back().nam == string("^error") ) )
{
int ijob = targ->job_running();
if ( ijob >= 0 )
{
if ( ((int)targ->error_msg.find("rror creating process")) > 0 )
#ifdef __amd64
targ->error_msg += "\nThis tool is for 64-bit targets only";
#else
targ->error_msg += "\nThis tool is for 32-bit targets only";
#endif
targ->job_set_error((job_enum)ijob);
}
}
break;
case 9: // fin de stream-output
if ( nam[0] == '&' )
{
if ( ((int)val.find("o such file")) > 0 )
{
targ->job_set_error(File);
targ->error_msg = val;
}
else if ( ((int)val.find("ot in executable")) > 0 )
{
targ->job_set_error(File);
targ->error_msg = val;
}
printf("%s\n", val.c_str() ); fflush(stdout);
}
break;
}
return 0;
}