The dynamic debugger utility, gdb
, has a
large number of capabilities. This quick guide lists a small but
useful subset of the gdb
commands.
Example. Compile the program printch.cpp:
hawk% g++ -g printch.cpp -o printch
Example. Run gdb on the printch program and set the number of source lines to list at a time to 28.
hawk% gdb -xdb -tui printch
GNU gdb 4.17.1
Copyright 1998 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License
(gdb) set listsize 28
Breakpoints are points in your code at which gdb will stop and allow executing other gdb commands.
Example. Set a breakpoint at the beginning of main
.
(gdb) b main
Example. Set a breakpoint at line 35 while in file printch.cpp.
(gdb) b 35
Example. Set a breakpoint at the beginning of member function erase of the class list.
(gdb) b list::erase
Example. List all breakpoints which have been set so far in a debugging session.
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x0040104f in main at printch.cpp:27
2 breakpoint keep y 0x004010a7 in main at printch.cpp:35
Example. Delete the breakpoint at line 35.
(gdb) delete 2
Example 1. The program is printch, which can take an optional command line argument. Start it running with no command line argument.
(gdb) r
Example 2. Start printch running with command line argument A.
(gdb) r A
(gdb) s
(gdb) n
(gdb) c
(gdb) finish
Example 1. Print the value of a variable count
(gdb) p count
Example 2. Print the value of the expression fname[i+1]
(gdb) p fname[i+1]
Example. List the next listsize number of lines of code. Note that listsize's value can be change with the set command.
(gdb) l
Example. List the lines centered around line 41.
(gdb) l 41
(gdb) where
#0 mystrcpy (copyto=0x259fc6c "*", copyfrom=0x259fddc "ABC") at printch.cpp:27
#1 0x4010c8 in main (argc=3, argv=0x25b0cb8) at printch.cpp:40
The statement at line 27 of the function mystrcpy
is the
next statement and the function mystrcpy was called by main
.
There is a help command, h
and the command
to quit gdb is q
.
Gdb Command | Description |
---|---|
set listsize n | Set the number of lines listed by the list command to n [set listsize] |
b function | Set a breakpoint at the beginning of function [break] |
b line number | Set a breakpoint at line number of the current file. [break] |
info b | List all breakpoints [info] |
delete n | Delete breakpoint number n [delete] |
r args | Start the program being debugged, possibly with command line arguments args. [run] |
s count | Single step the next count statments (default is 1). Step into functions. [step] |
n count | Single step the next count statments (default is 1). Step over functions. [next] |
finish | Execute the rest of the current function. Step out of the current function. [finish] |
c | Continue execution up to the next breakpoint or until termination if no breakpoints are encountered. [continue] |
p expression | print the value of expression [print] |
l optional_line | List next listsize lines. If optional_line is given, list the lines centered around optional_line. [list] |
where | Display the current line and function and the stack of calls that got you there. [where] |
h optional_topic | help or help on optional_topic [help] |
q | quit gdb [quit] |