thermostat / itrace (http://cs.virginia.edu/~dww4s/tools/itrace/itrace.html)
Instuction Tracing Program
Clone this repository (size: 826.4 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/thermostat/itrace/
| commit 22: | 25f709641fbe |
| parent 21: | b03540a5df70 |
| branch: | itrace |
cleaned up code, added only-syms option
20 months ago
Changed (Δ1.4 KB):
raw changeset »
itrace.c (35 lines added, 82 lines removed)
| … | … | @@ -100,6 +100,7 @@ unsigned int gzip_output = 0; |
100 |
100 |
/* symtab */ |
101 |
101 |
symbol_table_t symtab; |
102 |
102 |
unsigned int use_symtab = 1; |
103 |
unsigned int ignore_non_sym_insn = 0; |
|
103 |
104 |
|
104 |
105 |
FILE* gzopen_w(char * fname) { |
105 |
106 |
char buf[MAX_FNAME_LEN]; |
| … | … | @@ -112,13 +113,6 @@ void gzclose_w(FILE* f) { |
112 |
113 |
pclose(f); |
113 |
114 |
} |
114 |
115 |
|
115 |
void print_instruction(iaddr_t eip, |
|
116 |
iaddr_t esp, |
|
117 |
unsigned char * bin_buf, |
|
118 |
char* dis_buf, |
|
119 |
unsigned int text_size); |
|
120 |
||
121 |
void print_from_address(iaddr_t addr); |
|
122 |
116 |
void print_insn(insn_entry_t * insn); |
123 |
117 |
|
124 |
118 |
|
| … | … | @@ -254,35 +248,35 @@ void control_child() { |
254 |
248 |
memcpy(&(insn.binary), bin_buf, BINARY_BUFFER_SIZE); |
255 |
249 |
memcpy(&(insn.disassembly), dis_buf, DISASSEMBLY_BUFFER_SIZE); |
256 |
250 |
cache_insn(&insn); |
257 |
/*print_instruction(user_struct.regs.eip, |
|
258 |
user_struct.regs.esp, |
|
259 |
|
|
251 |
/* defer is used for only printing the last N instructions. |
|
252 |
if this is the case, we put them in a queue, and only print |
|
253 |
at the end.*/ |
|
260 |
254 |
if ( defer ) { |
261 |
if ( (circ_queue_head+1) % circ_queue_size == circ_queue_tail ) { |
|
262 |
circ_queue_tail++; |
|
263 |
if (circ_queue_tail == circ_queue_size) { |
|
264 |
circ_queue_tail = 0; |
|
265 |
} |
|
266 |
} |
|
267 |
circ_queue_head++; |
|
268 |
if ( circ_queue_head == circ_queue_size ) { |
|
269 |
circ_queue_head = 0; |
|
270 |
} |
|
271 |
||
272 |
circ_queue[circ_queue_head] = insn; |
|
273 |
if (ptrace(PTRACE_SINGLESTEP, pid, 0, 0) != 0) { |
|
274 |
perror("ptrace"); |
|
275 |
} |
|
276 |
wait(&wait_val); |
|
277 |
continue; |
|
255 |
if ( (circ_queue_head+1) % circ_queue_size == circ_queue_tail ) { |
|
256 |
circ_queue_tail++; |
|
257 |
if (circ_queue_tail == circ_queue_size) { |
|
258 |
circ_queue_tail = 0; |
|
259 |
} |
|
260 |
} |
|
261 |
circ_queue_head++; |
|
262 |
if ( circ_queue_head == circ_queue_size ) { |
|
263 |
circ_queue_head = 0; |
|
264 |
} |
|
265 |
||
266 |
circ_queue[circ_queue_head] = insn; |
|
267 |
if (ptrace(PTRACE_SINGLESTEP, pid, 0, 0) != 0) { |
|
268 |
perror("ptrace"); |
|
269 |
} |
|
270 |
wait(&wait_val); |
|
271 |
continue; |
|
278 |
272 |
} |
279 |
273 |
else { |
280 |
|
|
274 |
print_insn(&insn); |
|
281 |
275 |
} |
282 |
276 |
} |
283 |
277 |
else { |
284 |
278 |
if ( ! defer ) { |
285 |
|
|
279 |
print_insn(cached_insn); |
|
286 |
280 |
} |
287 |
281 |
} |
288 |
282 |
|
| … | … | @@ -306,7 +300,6 @@ void control_child() { |
306 |
300 |
circ_queue_size, circ_queue_head, |
307 |
301 |
circ_queue_tail); |
308 |
302 |
while ( circ_queue_head != circ_queue_tail ) { |
309 |
/*print_from_address(circ_queue[circ_queue_tail]); */ |
|
310 |
303 |
print_insn(&(circ_queue[circ_queue_tail])); |
311 |
304 |
circ_queue_tail++; |
312 |
305 |
if ( circ_queue_tail == circ_queue_size ) { |
| … | … | @@ -314,63 +307,14 @@ void control_child() { |
314 |
307 |
} |
315 |
308 |
} |
316 |
309 |
print_insn(&(circ_queue[circ_queue_tail])); |
317 |
/*print_from_address(circ_queue[circ_queue_tail]);*/ |
|
318 |
310 |
} |
319 |
311 |
|
320 |
312 |
} |
321 |
313 |
|
322 |
||
323 |
void print_from_address(iaddr_t addr) { |
|
324 |
char dis_buf[DISASSEMBLY_BUFFER_SIZE]; |
|
325 |
char bin_buf[16]; |
|
326 |
int text_size; |
|
327 |
text_size = get_inst_disassembly(addr, |
|
328 |
dis_buf,DISASSEMBLY_BUFFER_SIZE, |
|
329 |
(int*)bin_buf); |
|
330 |
/* get info out of the struct */ |
|
331 |
print_instruction(addr, 0, bin_buf, dis_buf, text_size); |
|
332 |
} |
|
333 |
||
334 |
314 |
/* |
335 |
* print_instruction - |
|
336 |
* prints the conents of bin_buf (if the print_hex |
|
337 |
* option is set) and the contents of dis_buf, along |
|
338 |
* with the eip from user_struct. |
|
339 |
*/ |
|
340 |
||
341 |
void print_instruction(/*struct user * user_struct,*/ |
|
342 |
iaddr_t eip, |
|
343 |
iaddr_t esp, |
|
344 |
unsigned char * bin_buf, |
|
345 |
char* dis_buf, |
|
346 |
unsigned int text_size) { |
|
347 |
unsigned int ind; |
|
348 |
fprintf(out, "0x%08x: ", eip); |
|
349 |
if ( print_hex ) { |
|
350 |
for ( ind = 0; ind < 10; ind++ ) { |
|
351 |
/* print the first 8 bytes of the hex */ |
|
352 |
if (ind < text_size ) { |
|
353 |
fprintf(out, "%02x ", (unsigned char)bin_buf[ind]); |
|
354 |
} |
|
355 |
else { |
|
356 |
fprintf(out, " "); |
|
357 |
} |
|
358 |
} |
|
359 |
} |
|
360 |
fprintf(out, "%s", dis_buf); |
|
361 |
if ( watch_esp && esp && |
|
362 |
((strncmp("call", dis_buf, 4) == 0) || |
|
363 |
(strncmp("ret", dis_buf, 3) ==0)) ) { |
|
364 |
fprintf(out, " (esp=0x%08x; *esp=0x%08x)", |
|
365 |
esp, |
|
366 |
ptrace(PTRACE_PEEKTEXT, pid, |
|
367 |
esp, 0)); |
|
368 |
} |
|
369 |
||
370 |
fprintf(out, "\n"); |
|
371 |
} |
|
372 |
||
373 |
||
315 |
print an insn_entry_t structure to the out |
|
316 |
FD. |
|
317 |
*/ |
|
374 |
318 |
void print_insn(insn_entry_t * insn) { |
375 |
319 |
unsigned int ind; |
376 |
320 |
|
| … | … | @@ -380,6 +324,9 @@ void print_insn(insn_entry_t * insn) { |
380 |
324 |
if ( sym_name != NULL && (strcmp(sym_name, "") != 0)) { |
381 |
325 |
fprintf(out, "<%s>:\n", sym_name); |
382 |
326 |
} |
327 |
else if ( ignore_non_sym_insn ) { |
|
328 |
return; |
|
329 |
} |
|
383 |
330 |
} |
384 |
331 |
fprintf(out, "0x%08x: ", insn->eip); |
385 |
332 |
if ( print_hex ) { |
| … | … | @@ -456,6 +403,8 @@ void exit_usage(int rc) { |
456 |
403 |
"\n" |
457 |
404 |
"--no-syms Do not print symbols\n" |
458 |
405 |
"\n" |
406 |
"--only-syms Only print instructions which are associated with symbols\n" |
|
407 |
"\n" |
|
459 |
408 |
"--range LOWER UPPER Only print in instructions that execute in the\n" |
460 |
409 |
"-r given range.\n" |
461 |
410 |
"\n" |
| … | … | @@ -524,6 +473,10 @@ char** set_opts(int argc, char* argv[]) |
524 |
473 |
else if ( strcmp(cur_arg, "--no-syms") == 0 ) { |
525 |
474 |
use_symtab = 0; |
526 |
475 |
} |
476 |
else if ( strcmp(cur_arg, "--only-syms") == 0 ) { |
|
477 |
ignore_non_sym_insn = 1; |
|
478 |
use_symtab = 1; |
|
479 |
} |
|
527 |
480 |
else if ( strcmp(cur_arg, "-h") == 0 || |
528 |
481 |
strcmp(cur_arg, "--help") == 0 ) { |
529 |
482 |
exit_usage(0); |
