Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to go to the previous line in GDB?

Tags:

debugging

gdb

Is it possible in gdb to go to a line before the currently executing line. e.g:

 void my_fun( somePtrType** arr,int start,int end) {  // arr is an array of pointers to somePtrType   //line a  ... some assignments  swap(&arr[ind1] , &arr[ind2] ) ;  //line b (current line ) } 

I am at line b currently and can examine the arr values there but I want to go back to line a and examine the contents of arr at that time.

I think it might not be possible because a debugger can run a code in slow motion,but can't make it execute backwards.
Any more insights..

like image 565
sud03r Avatar asked Jul 30 '09 14:07

sud03r


People also ask

Can you go back a line in GDB?

If the target environment supports it, gdb can allow you to “rewind” the program by running it backward. A target environment that supports reverse execution should be able to “undo” the changes in machine state that have taken place as the program was executing normally.

How do you jump a line in GDB?

So just type skip in gdb to skip a line.

Can a debugger go backwards?

yes, this is not true backward debugging. but it is simple and very effective. to perform true backward stepping, the debugger would need to reverse all operations, typically with a rather heavy state machine and data recording.

What does jump do in GDB?

jump * address. Resume execution at the instruction at address address . makes the next continue command or stepping command execute at address 0x485 , rather than at the address where your program stopped. See section Continuing and stepping.


1 Answers

Yes! With the new version 7.0 gdb, you can do exactly that!

The command would be "reverse-step", or "reverse-next".

You can get gdb-7.0 from ftp.gnu.org:/pub/gnu/gdb

If you run into the error: Target child does not support this command. then try adding target record at the beginning of execution, after starting run.

Edit: Since GDB 7.6 target record is deprecated, use target record-full instead.

like image 156
Michael Snyder Avatar answered Sep 22 '22 03:09

Michael Snyder