Most tricky/useful commands for gdb debugger

39,875

Solution 1

  1. backtrace full: Complete backtrace with local variables
  2. up, down, frame: Move through frames
  3. watch: Suspend the process when a certain condition is met
  4. set print pretty on: Prints out prettily formatted C source code
  5. set logging on: Log debugging session to show to others for support
  6. set print array on: Pretty array printing
  7. finish: Continue till end of function
  8. enable and disable: Enable/disable breakpoints
  9. tbreak: Break once, and then remove the breakpoint
  10. where: Line number currently being executed
  11. info locals: View all local variables
  12. info args: View all function arguments
  13. list: view source
  14. rbreak: break on function matching regular expression

Solution 2

Start gdb with a textual user interface

gdb -tui

Solution 3

Starting in gdb 7.0, there is reversible debugging, so your new favourite commands are:

* reverse-continue ('rc') -- Continue program being debugged but run it in reverse
* reverse-finish -- Execute backward until just before the selected stack frame is called
* reverse-next ('rn') -- Step program backward, proceeding through subroutine calls.
* reverse-nexti ('rni') -- Step backward one instruction, but proceed through called subroutines.
* reverse-step ('rs') -- Step program backward until it reaches the beginning of a previous source line
* reverse-stepi -- Step backward exactly one instruction
* set exec-direction (forward/reverse) -- Set direction of execution.

Solution 4

Instead of launching GDB with "-tui" param you can also switch to text mode after a while using by typing "wh".

Solution 5

thread apply all bt or thread apply all print $pc: For finding out quickly what all threads are doing.

Share:
39,875
Vijay
Author by

Vijay

http://theunixshell.blogspot.com/

Updated on July 08, 2022

Comments

  • Vijay
    Vijay almost 2 years

    Can you post your most tricky and useful commands while you run a debugger like gdb or dbx.

  • Paul Biggar
    Paul Biggar almost 15 years
    info locals -- View all local variables; list -- view source; rbreak -- break on function matching regular expression.
  • DevSolar
    DevSolar over 14 years
    I cannot believe this feature escaped me for all these years. Thank you, thank you, thank you!
  • Sudhanshu
    Sudhanshu over 14 years
    source /path/to/macro/file And all of my nifty macros are there to help me debug in seconds.
  • deft_code
    deft_code about 14 years
    How do you set scheduler locking?
  • Ben
    Ben about 14 years
    set scheduler-locking on inside gdb
  • Kevin
    Kevin over 13 years
    Ctrl-a a to switch back to 'normal' command line view !
  • tothphu
    tothphu about 11 years
    That was the command I was looking for a long-long time! It is really nasty to check all 30 threads one by one!
  • sujin
    sujin about 10 years
    Thanks You saved lot of my time.. Wonderfull....
  • Kiril Kirov
    Kiril Kirov almost 10 years
    set print object on for polymorphic elements and set print elements 0 are two commands I use very often. Pretty useful.
  • Kiril Kirov
    Kiril Kirov almost 10 years
    Also, t a a bt (meaning thread apply all backtrace). Could be used with (almost) all other commands. Especially useful with bt full.
  • Nathan Fellman
    Nathan Fellman almost 10 years
    you can change the focus to the command window using focus cmd so that the up/down arrows work. You switch back using focus src.
  • Omry Yadan
    Omry Yadan over 9 years
    how do you make this the default? I tried it .gdbinit but gdb prints /home/omry/.gdbinit:1: Error in sourced command file: Target 'None' cannot support this command.
  • raj_gt1
    raj_gt1 about 9 years
    "-' can be used instaed of 'wh'. shorter the better..:)
  • x-yuri
    x-yuri over 8 years
    Ctrl-a a? Is it a joke? Looks more like tmux/screen command. And doesn't work for me. It must be one of as per docs: C-x C-a, C-x a, C-x A.
  • SullX
    SullX over 8 years
    And another: <minus> RET
  • Patryk
    Patryk about 8 years
    or use cgdb
  • doug65536
    doug65536 about 7 years
    You can also use the -iex option to add individual commands on the gdb command line.
  • PraveenMax
    PraveenMax about 7 years
    very nice features. Missed this so far...
  • Sam
    Sam over 6 years
    It's actually C-x a. You can also switch views with C-x 1 and C-x 2 when in tui mode to see assembly as well (if need be).
  • user28186
    user28186 about 2 years
    Very useful information received in this thread , thank you all.
  • user28186
    user28186 about 2 years
    Very useful reply