views:

1799

answers:

10

Hi All,

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

+2  A: 

For example the macros defined in stl-views.gdb

grigy
+22  A: 
  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. list: view source
  13. rbreak: break on function matching regular expression
Ramkumar Ramachandra
`info locals` -- View all local variables; list -- view source; rbreak -- break on function matching regular expression.
Paul Biggar
source /path/to/macro/fileAnd all of my nifty macros are there to help me debug in seconds.
Sudhanshu
+4  A: 

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

Olof
+15  A: 

Start gdb with a textual user interface

gdb -tui
Amro
I cannot believe this feature escaped me for all these years. Thank you, thank you, thank you!
DevSolar
or just C-x C-a
elmarco
+9  A: 

Starting in gdb 7.0, there is reversible debugging, so your new favourite command 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.
Paul Biggar
+4  A: 

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

martin
How do you switch back?
Matt Joiner
+2  A: 

scripting gdb is a good trick, other than that I like set scheduler locking on / off to prevent the running of other threads when you are stepping in one.

Ben
How do you set scheduler locking?
caspin
`set scheduler-locking on` inside gdb
Ben
A: 
  • Using .gdbinit (start up file where you can write macros and call from gdb). Place .gdbinit in your home directory so that it is picked up every time gdb is loaded
  • info threads to list all the active threads, and f(#) -> # thread number you want to switch to

  • sometime i use gdb to convert from hex to decimal or binary, its very handy instead of opening up a calculator

    • p/d 0x10 -> gives decimal equivalent of 0x10
    • p/t 0x10 -> binary equivalent of 0x10
    • p/x 256 -> hex equivalent of 256
subbul
+1  A: 

Using the -command= option while firing up gdb. Same as -x . This command file can contain gdb commands [few breakpoints, few options etc]. Useful in case a particular exe needs to be put through successive debug runs using gdb.

raghava
A: 

This can be useful, I am sure it could be improved though, help welcome:

define mallocinfo
  set $__f = fopen("/dev/tty", "w")
  call malloc_info(0, $__f)
  call fclose($__f)
elmarco