Can you post your most tricky and useful commands while you run a debugger like gdb or dbx.
12 Answers
- backtrace full: Complete backtrace with local variables
- up, down, frame: Move through frames
- watch: Suspend the process when a certain condition is met
- set print pretty on: Prints out prettily formatted C source code
- set logging on: Log debugging session to show to others for support
- set print array on: Pretty array printing
- finish: Continue till end of function
- enable and disable: Enable/disable breakpoints
- tbreak: Break once, and then remove the breakpoint
- where: Line number currently being executed
- info locals: View all local variables
- info args: View all function arguments
- list: view source
- rbreak: break on function matching regular expression
-
7
info locals
-- View all local variables; list -- view source; rbreak -- break on function matching regular expression. Sep 27, 2009 at 10:53 -
source /path/to/macro/file And all of my nifty macros are there to help me debug in seconds. Jan 13, 2010 at 5:19
-
1
set print object on
for polymorphic elements andset print elements 0
are two commands I use very often. Pretty useful. Jul 17, 2014 at 9:04 -
1Also,
t a a bt
(meaningthread apply all backtrace
). Could be used with (almost) all other commands. Especially useful withbt full
. Jul 17, 2014 at 9:10
Start gdb with a textual user interface
gdb -tui
-
22I cannot believe this feature escaped me for all these years. Thank you, thank you, thank you!– DevSolarDec 15, 2009 at 13:55
-
-
1
-
1
-
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.
Instead of launching GDB with "-tui" param you can also switch to text mode after a while using by typing "wh".
-
3
-
2
-
6
-
It's actually
C-x a
. You can also switch views withC-x 1
andC-x 2
when in tui mode to see assembly as well (if need be).– SamOct 16, 2017 at 13:06
thread apply all bt
or thread apply all print $pc
: For finding out quickly what all threads are doing.
-
That was the command I was looking for a long-long time! It is really nasty to check all 30 threads one by one!– tothphuApr 28, 2013 at 23:57
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.
-
You can also use the
-iex
option to add individual commands on the gdb command line. Apr 30, 2017 at 10:21
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.
-
1
-
3
-
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. Nov 26, 2014 at 1:44
- 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
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.
-
3you can change the focus to the command window using
focus cmd
so that the up/down arrows work. You switch back usingfocus src
. Aug 11, 2014 at 11:14
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)
To debug STL, add content to .gdbinit, follow these instructions: