Lance Chao
Lance Chao

Reputation: 79

GDB - show last n lines executed

Sometimes in GDB I want to see the control flow that got the program to where it is now. Simply put, how do I make GDB print the last x lines executed?

Upvotes: 3

Views: 6747

Answers (2)

ks1322
ks1322

Reputation: 35716

This is yet another use case for Reverse Debugging.

  1. You should start process record and replay at some point:

    (gdb) record
    
  2. When you want to see last executed lines you can go backwards like this:

    (gdb) reverse-step 3
    

    or

    (gdb) reverse-next 3
    
  3. Use this answer https://stackoverflow.com/a/1545732/72178 to actually print the next N executed lines.

Upvotes: 5

You simply cannot do that (easily) in gdb, because the execution trace of any program is not kept (and keeping it would be very expensive : it would slow down a lot the execution, and it would use a lot of resources - memory & disk space).

You can however use the backtrace or bt command of gdb to show the call stack, that is the current instruction pointer in the current function, the calling function, the calling function of the calling function, and so forth

BTW, if you really wanted it, you might script recent gdb using Python or Guile to give you such information. You certainly could keep the entire trace (by scripting around the step & backtrace functionalities).

Upvotes: 2

Related Questions