tags:

views:

340

answers:

5

I need to trace all instrutions of a program using gdb. After every execution of a instruction, I want gdb invokes a specified function.

Is it a possiable work? How to achieve this?

I searched internet and found "stepi arg" command in gdb could step arg instructions. But how to find total number of instructions? After every instruction, how to make gdb to invoke my function automately?

A: 

for example:

int main() { int x=1; int y=2; int z=x+y; printf("%d",z); return 0; }

after each instruction, I will invoke "info registers" and save all the infomation in a file.

programmer
Are you debugging, or just curious? If you are debugging, there are better ways to do that. If you're just curious, are you interested in the computer's operation, or in messing with gdb? What is your hardware and OS?There is a way to do this on some platforms, but it will take some effort on both sides.
gary
A: 

I need to save all the registers to a file after each instruction. Gdb could trace instrucion step by step manually, but I intend to make it run automaticlly.

Os is linux and processor is intel.

programmer
A: 
cat t.c
int main() { int x=1; int y=2; int z=x+y; printf("%d",z); return 0; }

gcc t.c
gdb -q ./a.out
break main
run
(no debugging symbols found)...
Breakpoint 1, 0x0000000000400488 in main ()
set logging on
while 1
 >stepi
 >info registers
end
quit

Now examine gdb.log: it should contain the info you are seeking.

P.S. This isn't a discussion forum. Please don't append questions as "answers". Instead edit your original question to clarify it, or use comments.

Employed Russian
A: 

Thanks, Employed Russian. But gdb alway display "--Type to continue, or q to quit". Could you tell me how to avoid this? I need gdb run with no break until end.

programmer
A: 

GDB always prints "---Type to continue, or q to quit---" during execution because of the height or pagination parameter.

In order to avoid or disable this you have to give the following command either in gdb prompt or .gdbinit file

set height 0 or set pagination off

Dhiman Saha