140

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

Vijay
  • 65,327
  • 90
  • 227
  • 319
  • I found this documentation is good. https://scc.ustc.edu.cn/zlsc/sugon/intel/debugger/cl/index.htm#commandref/gdb_mode/cmd_condition.htm – Rick Mar 31 '20 at 04:50

12 Answers12

140
  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
grokus
  • 18,046
  • 9
  • 29
  • 35
artagnon
  • 3,609
  • 3
  • 23
  • 26
  • 7
    `info locals` -- View all local variables; list -- view source; rbreak -- break on function matching regular expression. – Paul Biggar Sep 27 '09 at 10:53
  • source /path/to/macro/file And all of my nifty macros are there to help me debug in seconds. – Sudhanshu Jan 13 '10 at 05:19
  • 1
    `set print object on` for polymorphic elements and `set print elements 0` are two commands I use very often. Pretty useful. – Kiril Kirov Jul 17 '14 at 09:04
  • 1
    Also, `t a a bt` (meaning `thread apply all backtrace`). Could be used with (almost) all other commands. Especially useful with `bt full`. – Kiril Kirov Jul 17 '14 at 09:10
  • Very useful information received in this thread , thank you all. – user28186 Jun 14 '22 at 22:26
103

Start gdb with a textual user interface

gdb -tui
Amro
  • 123,847
  • 25
  • 243
  • 454
45

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.
thegreendroid
  • 3,239
  • 6
  • 31
  • 40
Paul Biggar
  • 27,579
  • 21
  • 99
  • 152
21

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

martin
  • 1,106
  • 2
  • 9
  • 11
  • 3
    Ctrl-a a to switch back to 'normal' command line view ! – Kevin Feb 16 '11 at 21:26
  • 2
    "-' can be used instaed of 'wh'. shorter the better..:) – raj_gt1 May 05 '15 at 13:55
  • 6
    `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](http://stackoverflow.com/questions/8409540/how-to-close-layout-src-windows-in-gdb) per [docs](https://sourceware.org/gdb/onlinedocs/gdb/TUI-Keys.html#TUI-Keys): `C-x C-a`, `C-x a`, `C-x A`. – x-yuri Oct 08 '15 at 21:49
  • 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). – Sam Oct 16 '17 at 13:06
16

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

Earlz
  • 62,085
  • 98
  • 303
  • 499
Olof
  • 533
  • 3
  • 12
6

For example the macros defined in stl-views.gdb

grigy
  • 6,696
  • 12
  • 49
  • 76
6

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
  • 7,372
  • 8
  • 38
  • 46
6

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

toasted_flakes
  • 2,502
  • 1
  • 23
  • 38
raghava
  • 236
  • 4
  • 12
4
  • 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
  • 947
  • 1
  • 7
  • 14
4

Instead of starting gdb with the option -tui to see a child process that contains a screen that highlights where the executing line of code is in your program, jump in and out of this feature with C-x o and C-x a. This is useful if you're using the feature and what to temporarily not use it so you can use the up-arrow to get a previous command.

Rose Perrone
  • 61,572
  • 58
  • 208
  • 243
  • 3
    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`. – Nathan Fellman Aug 11 '14 at 11:14
3

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
  • 31,633
  • 21
  • 64
  • 68
2

To debug STL, add content to .gdbinit, follow these instructions:

http://www.yolinux.com/TUTORIALS/GDB-Commands.html#STLDEREF

Bob Yoplait
  • 2,421
  • 1
  • 23
  • 35