GDB: The GNU Project Debugger
This article introduces the usage of GDB.
Overview
GDB, the GNU Project debugger, allows you to see what is going on inside another program while it executes – or what another program was doing at the moment it crashed.
GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:
- Start your program, specifying anything that might affect its behavior.
- Make your program stop on specified conditions.
- Examine what has happened, when your program has stopped.
- Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another.
The program being debugged can be written in Ada, C, C++, Objective-C, Pascal and many other languages. Those programs might be executing on the same machine as GDB (native) or on another machine (remote). GDB can run on most popular UNIX and Microsoft Windows variants.
GDB Quick Reference
Refer to GDB_Quick_Reference.
Example
Save the following source code to ~/example/ex-gdb.c:
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 16
#define COLUMN_SIZE 5
static int maxSize = ARRAY_SIZE;
int array[ARRAY_SIZE];
int (*pArray)[ARRAY_SIZE];
void setArray(void)
{
int i;
for (i = 0; i < maxSize; ++i)
{
array[i] = i + 1;
}
}
void printArray(void)
{
int i;
pArray = &array;
int halfMaxSize = (int)maxSize/2;
halfMaxSize = (halfMaxSize > COLUMN_SIZE) ? COLUMN_SIZE : halfMaxSize;
for (i = 0; i < maxSize; ++i)
{
if ((i > 0) && (i%halfMaxSize == 0))
{
printf("\n");
}
printf("%2d\t", (*pArray)[i]);
}
printf("\n");
}
int main(int argc, char *argv[])
{
if (argc == 2)
{
maxSize = atoi(argv[1]);
}
setArray();
printArray();
return 0;
}
Build the source code with the following command:
chenwx@chenwx ~/example $ gcc -g -o ex-gdb ex-gdb.c
chenwx@chenwx ~/example $ ll
-rwxrwxr-x 1 chenwx chenwx 10K Aug 15 08:34 ex-gdb
-rw-rw-r-- 1 chenwx chenwx 513 Aug 14 16:15 ex-gdb.c
Starting and Stoping GDB
Commands | Description |
---|---|
gdb | start GDB, with no debugging files |
gdb program | begin debugging program |
gdb program core | debug coredump core produced by program |
quit EOF (eg C-d) |
exit GDB |
INTERRUPT | (eg C-c) terminate current command, or send to running process |
chenwx@chenwx ~/example $ gdb
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
chenwx@chenwx ~/example $ gdb ex-gdb
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ex-gdb...done.
(gdb) q
chenwx@chenwx ~/example $
GDB Help
Commands | Description |
---|---|
help | list classes of commands |
help class | one-line descriptions for commands in class |
help command | describe command |
(gdb) h
List of classes of commands:
aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands
Type "help" followed by a class name for a list of commands in that class.
Type "help all" for the list of all commands.
Type "help" followed by command name for full documentation.
Type "apropos word" to search for commands related to "word".
Command name abbreviations are allowed if unambiguous.
(gdb) h aliases
Aliases of other commands.
List of commands:
ni -- Step one instruction
rc -- Continue program being debugged but run it in reverse
rni -- Step backward one instruction
rsi -- Step backward exactly one instruction
si -- Step one instruction exactly
stepping -- Specify single-stepping behavior at a tracepoint
tp -- Set a tracepoint at specified location
tty -- Set terminal for future runs of program being debugged
where -- Print backtrace of all stack frames
ws -- Specify single-stepping behavior at a tracepoint
Type "help" followed by command name for full documentation.
Type "apropos word" to search for commands related to "word".
Command name abbreviations are allowed if unambiguous.
(gdb) h ni
Step one instruction, but proceed through subroutine calls.
Usage: nexti [N]
Argument N means step N times (or till program stops for another reason).
Executing Program
Commands | Description |
---|---|
show args | display argument list |
set args arglist | specify arglist for next run |
set args | specify empty argument list |
show environment | show all environment variables |
show env var | show value of environment variable var |
set environment varname string | set environment variable var |
unset env var | remove var from environment |
run arglist | start your program with arglist |
run | start your program with current argument list |
run … < inf > outf | start your program with input, output redirected |
kill | kill running program |
tty dev | use dev as stdin and stdout for next run |
chenwx@chenwx ~/example $ gdb ex-gdb
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ex-gdb...done.
(gdb) set args 10
(gdb) show args
Argument list to give program being debugged when it is started is "10".
(gdb) r
Starting program: /home/chenwx/example/ex-gdb 10
1 2 3 4 5
6 7 8 9 10
[Inferior 1 (process 9323) exited normally]
(gdb) set args
(gdb) show args
Argument list to give program being debugged when it is started is "".
(gdb) r
Starting program: /home/chenwx/example/ex-gdb
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16
[Inferior 1 (process 9324) exited normally]
(gdb) r 13
Starting program: /home/chenwx/example/ex-gdb 13
1 2 3 4 5
6 7 8 9 10
11 12 13
[Inferior 1 (process 9373) exited normally]
(gdb) show args
Argument list to give program being debugged when it is started is "13".
(gdb) k
The program is not being run.
(gdb) show env HOME
HOME = /home/chenwx
Breakpoints and Watchpoints
Commands | Description |
---|---|
info break | show defined breakpoints |
info watch | show defined watchpoints |
break [file:]line | set breakpoint at line number [in file], eg: break main.c:37 |
break [file:]fun | set breakpoint at fun() [in file] |
break +offset break -offset |
set break at offset lines from current stop |
break *addr | set breakpoint at address addr |
break | set breakpoint at next instruction |
break … if expr | break conditionally on nonzero expr |
cond n [expr] | new conditional expression on breakpoint n; make unconditional if no expr |
tbreak … | temporary break; disable when reached |
rbreak regex | break on all functions matching regex |
watch expr | set a watchpoint for expression expr |
catch event | break at event, which may be catch, throw, exec, fork, vfork, load, or unload. |
clear | delete breakpoints at next instruction |
clear [file:]fun | delete breakpoints at entry to fun() |
clear [file:]line | delete breakpoints on source line |
delete [n] | delete breakpoints [ or breakpoint n ] |
disable [n] | disable breakpoints [ or breakpoint n ] |
enable [n] | enable breakpoints [ or breakpoint n ] |
enable once [n] | enable breakpoints [ or breakpoint n ]; disable again when reached |
enable del [n] | enable breakpoints [ or breakpoint n ]; delete when reached |
ignore n count | ignore breakpoint n, count times |
commands n [silent] command-list end |
execute GDB command-list every time breakpoint n is reached. [ silent ] suppresses default display ]. end of command-list |
(gdb) b 18 if i==5
Breakpoint 1 at 0x4005c3: file ex-gdb.c, line 18.
(gdb) b printArray
Breakpoint 2 at 0x4005ef: file ex-gdb.c, line 25.
(gdb) b ex-gdb.c:45
Breakpoint 3 at 0x400694: file ex-gdb.c, line 45.
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x00000000004005c3 in setArray at ex-gdb.c:18
stop only if i==5
2 breakpoint keep y 0x00000000004005ef in printArray at ex-gdb.c:25
3 breakpoint keep y 0x0000000000400694 in main at ex-gdb.c:45
(gdb) dis 3
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x00000000004005c3 in setArray at ex-gdb.c:18
stop only if i==5
2 breakpoint keep y 0x00000000004005ef in printArray at ex-gdb.c:25
3 breakpoint keep n 0x0000000000400694 in main at ex-gdb.c:45
(gdb) en 3
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x00000000004005c3 in setArray at ex-gdb.c:18
stop only if i==5
2 breakpoint keep y 0x00000000004005ef in printArray at ex-gdb.c:25
3 breakpoint keep y 0x0000000000400694 in main at ex-gdb.c:45
(gdb) del 3
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x00000000004005c3 in setArray at ex-gdb.c:18
stop only if i==5
2 breakpoint keep y 0x00000000004005ef in printArray at ex-gdb.c:25
(gdb) r
Starting program: /home/chenwx/example/ex-gdb
Breakpoint 1, setArray () at ex-gdb.c:18
18 array[i] = i + 1;
(gdb) p i
$1 = 5
(gdb) cond 1 i==10
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x00000000004005c3 in setArray at ex-gdb.c:18
stop only if i==10
breakpoint already hit 1 time
2 breakpoint keep y 0x00000000004005ef in printArray at ex-gdb.c:25
(gdb) c
Continuing.
Breakpoint 1, setArray () at ex-gdb.c:18
18 array[i] = i + 1;
(gdb) p i
$2 = 10
(gdb) tb 28
Temporary breakpoint 4 at 0x40060c: file ex-gdb.c, line 28.
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x00000000004005c3 in setArray at ex-gdb.c:18
stop only if i==10
breakpoint already hit 2 times
2 breakpoint keep y 0x00000000004005ef in printArray at ex-gdb.c:25
breakpoint already hit 1 time
4 breakpoint del y 0x000000000040060c in printArray at ex-gdb.c:28
(gdb) c
Continuing.
Temporary breakpoint 4, printArray () at ex-gdb.c:28
28 halfMaxSize = (halfMaxSize > COLUMN_SIZE) ? COLUMN_SIZE : halfMaxSize;
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x00000000004005c3 in setArray at ex-gdb.c:18
stop only if i==10
breakpoint already hit 2 times
2 breakpoint keep y 0x00000000004005ef in printArray at ex-gdb.c:25
breakpoint already hit 1 time
Program Stack
Commands | Description |
---|---|
backtrace [n] bt [n] |
print trace of all frames in stack; or of n frames — innermost if n > 0, outermost if n < 0 |
frame [n] | select frame number n or frame at address n; if no n, display current frame |
up n | select frame n frames up |
down n | select frame n frames down |
info frame [addr] | describe selected frame, or frame at addr |
info args | arguments of selected frame |
info locals | local variables of selected frame |
info reg [rn] … info all-reg [rn] |
register values [ for regs rn ] in selected frame; all-reg includes floating point |
(gdb) bt
#0 printArray () at ex-gdb.c:28
#1 0x00000000004006b7 in main (argc=1, argv=0x7fffffffe1c8) at ex-gdb.c:49
(gdb) frame
#0 printArray () at ex-gdb.c:28
28 halfMaxSize = (halfMaxSize > COLUMN_SIZE) ? COLUMN_SIZE : halfMaxSize;
(gdb) info frame
Stack level 0, frame at 0x7fffffffe0d0:
rip = 0x40060c in printArray (ex-gdb.c:28); saved rip = 0x4006b7
called by frame at 0x7fffffffe0f0
source language c.
Arglist at 0x7fffffffe0c0, args:
Locals at 0x7fffffffe0c0, Previous frame's sp is 0x7fffffffe0d0
Saved registers:
rbp at 0x7fffffffe0c0, rip at 0x7fffffffe0c8
(gdb) info args
No arguments.
(gdb) info locals
i = 0
halfMaxSize = 8
(gdb) info reg
rax 0x8 8
rbx 0x0 0
rcx 0x0 0
rdx 0x0 0
rsi 0x7fffffffe1c8 140737488347592
rdi 0x1 1
rbp 0x7fffffffe0c0 0x7fffffffe0c0
rsp 0x7fffffffe0b0 0x7fffffffe0b0
r8 0x400730 4196144
r9 0x7ffff7de78e0 140737351940320
r10 0x846 2118
r11 0x7ffff7a2e740 140737348036416
r12 0x4004c0 4195520
r13 0x7fffffffe1c0 140737488347584
r14 0x0 0
r15 0x0 0
rip 0x40060c 0x40060c <printArray+37>
eflags 0x202 [ IF ]
cs 0x33 51
ss 0x2b 43
ds 0x0 0
es 0x0 0
fs 0x0 0
gs 0x0 0
Execution Control
Commands | Description |
---|---|
continue [count] | continue running; if count specified this breakpoint next count times |
step [count] | execute until another line reached; repeat count times if specified |
stepi [count] si [count] |
step by machine instructions rather than source lines |
next [count] | execute next line, including any function calls |
nexti [count] ni [count] |
next machine instruction rather than source line |
until [location] | run until next instruction (or location) |
finish | run until selected stack frame returns |
return [expr] | pop selected stack frame without executing [ setting return value ] |
signal num | resume execution with signal s (none if 0) |
jump line jump *address |
resume execution at specified line number or address |
set var=expr | evaluate expr without displaying it; use for altering program variables |
Display
Commands | Description |
---|---|
print [/f] [expr] | show value of expr [ or last value $ ] according to format f in the following table |
call [/f] expr | like print but does not display void |
The format f of print and call commands:
- x - hexadecimal
- d - signed decimal
- u - unsigned decimal
- o - octal
- t - binary
- a - address, absolute and relative
- c - character
- f - floating point
For instances:
-
以十六进制输出
p /x expr
-
以有符号十进制输出
p /d expr
-
以无符号十进制输出
p /u expr
-
以八进制输出
p /o expr
-
以二进制输出
p /t expr
-
以地址格式输出
p /a expr
-
以单字符输出
p /c expr
-
以浮点数输出
p /f expr
-
打印局部变量i所占空间的大小
p /x sizeof(i)
(gdb) p i
$3 = 0
(gdb) p maxSize
$4 = 16
Commands | Description |
---|---|
x [/Nuf] expr | examine memory at address expr; optional format spec follows slash, see the following tables |
The N is the count of how many units to display. The u is the following format:
- b - individual bytes
- h - halfwords (two bytes)
- w - words (four bytes)
- g - giant words (eight bytes)
The f is printing format. Any print format, or the following options:
- s - null-terminated string
- i - machine instructions
For instances:
-
以十六进制输出
x /x expr
x /8bx expr
x /8hx expr
x /8wx expr
x /8gx expr
-
以有符号十进制输出
x /d expr
x /8bd expr
x /8hd expr
x /8wd expr
x /8gd expr
-
以无符号十进制输出
x /u expr
x /8bu expr
x /8hu expr
x /8wu expr
x /8gu expr
-
以八进制输出
x /o expr
x /8bo expr
x /8ho expr
x /8wo expr
x /8go expr
-
以二进制输出
x /t expr
x /8bt expr
x /8ht expr
x /8wt expr
x /8gt expr
-
以地址格式输出
x /a expr
x /8ba expr
x /8ha expr
x /8wa expr
x /8ga expr
-
以单字符输出
x /c expr
x /8bc expr
x /8hc expr
x /8wc expr
x /8gc expr
-
以浮点数输出
x /f expr
x /8bf expr
x /8hf expr
x /8wf expr
x /8gf expr
-
以字符串输出
x /s expr
x /8bs expr
x /8hs expr
x /8ws expr
x /8gs expr
-
反汇编
x /i expr
x /8bi expr
x /8hi expr
x /8wi expr
x /8gi expr
通常,使用 x /10i $ip-20 来查看当前的汇编($ip是指令寄存器).
(gdb) x /16d pArray
0x601080 <array>: 1 2 3 4
0x601090 <array+16>: 5 6 7 8
0x6010a0 <array+32>: 9 10 11 12
0x6010b0 <array+48>: 13 14 15 16
(gdb) x /16x pArray
0x601080 <array>: 0x00000001 0x00000002 0x00000003 0x00000004
0x601090 <array+16>: 0x00000005 0x00000006 0x00000007 0x00000008
0x6010a0 <array+32>: 0x00000009 0x0000000a 0x0000000b 0x0000000c
0x6010b0 <array+48>: 0x0000000d 0x0000000e 0x0000000f 0x00000010
Commands | Description |
---|---|
disassem [addr] | display memory as machine instructions |
Automatic Display
Commands | Description |
---|---|
info display | numbered list of display expressions |
display [/f] expr | show value of expr each time program stops [ according to format f ] |
display | display all enabled expressions on list |
undisplay n | remove number(s) n from list of automatically displayed expressions |
disable disp n | disable display for expression(s) number n |
enable disp n | enable display for expression(s) number n |
(gdb) b 18
Breakpoint 1 at 0x4005c3: file ex-gdb.c, line 18.
(gdb) b printArray
Breakpoint 2 at 0x4005ef: file ex-gdb.c, line 25.
(gdb) b ex-gdb.c:45
Breakpoint 3 at 0x400694: file ex-gdb.c, line 45.
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x00000000004005c3 in setArray at ex-gdb.c:18
2 breakpoint keep y 0x00000000004005ef in printArray at ex-gdb.c:25
3 breakpoint keep y 0x0000000000400694 in main at ex-gdb.c:45
(gdb) disp maxSize
1: maxSize = 16
(gdb) disp i
No symbol "i" in current context.
(gdb) info disp
Auto-display expressions now in effect:
Num Enb Expression
1: y maxSize
(gdb) r
Starting program: /home/chenwx/example/ex-gdb
Breakpoint 1, setArray () at ex-gdb.c:18
18 array[i] = i + 1;
1: maxSize = 16
(gdb) c
Continuing.
Breakpoint 1, setArray () at ex-gdb.c:18
18 array[i] = i + 1;
1: maxSize = 16
(gdb) c
Continuing.
Breakpoint 1, setArray () at ex-gdb.c:18
18 array[i] = i + 1;
1: maxSize = 16
Symbol Table
Commands | Description |
---|---|
info address s | show where symbol s is stored |
info func [regex] | show names, types of defined functions (all, or matching regex) |
info var [regex] | show names, types of global variables (all, or matching regex) |
whatis [expr] ptype [expr] |
show data type of expr [ or $ ] without evaluating; ptype gives more detail |
ptype type | describe type, struct, union, or enum |
(gdb) info addr pArray
Symbol "pArray" is static storage at address 0x6010a0.
(gdb) info addr array
Symbol "array" is static storage at address 0x601060.
(gdb) info func setArray
All functions matching regular expression "setArray":
File b.c:
void setArray(void);
(gdb) whatis setArray
type = void (void)
(gdb) whatis printArray
type = void (void)
(gdb) whatis array
type = int [16]
(gdb) whatis pArray
type = int (*)[16]
Expressions
Commands | Description |
---|---|
expr | an expression in C, C++, or Modula-2 (including function calls), or: |
addr@len | an array of len elements beginning at addr |
file::nm | a variable or function nm defined in file |
{type}addr | read memory at addr as specified type |
$ | most recent displayed value |
$n | nth displayed value |
$$ | displayed value previous to $ |
$$n | nth displayed value back from $ |
$_ | last address examined with x |
$__ | value at address $ |
$var | convenience variable; assign any value |
show values [n] | show last 10 values [ or surrounding $n ] |
show conv | display all convenience variables |
Signals
Commands | Description |
---|---|
info signals | show table of signals, GDB action for each |
handle signal act | specify GDB actions for signal |
act for handle signal act:
- print - announce signal
- noprint - be silent for signal
- stop - halt execution on signal
- nostop - do not halt execution
- pass - allow your program to handle signal
- nopass - do not allow your program to see signal
(gdb) handle SIG35 noprint nostop
Signal Stop Print Pass to program Description
SIG35 No No Yes Real-time event 35
(gdb) info sig
Signal Stop Print Pass to program Description
SIGHUP Yes Yes Yes Hangup
SIGINT Yes Yes No Interrupt
SIGQUIT Yes Yes Yes Quit
SIGILL Yes Yes Yes Illegal instruction
SIGTRAP Yes Yes No Trace/breakpoint trap
...
Working Files
Commands | Description |
---|---|
info files | display working files and targets in use |
file [file] | use file for both symbols and executable; with no arg, discard both |
core [file] | read file as coredump; or discard |
exec [file] | use file as executable only; or discard |
symbol [file] | use symbol table from file; or discard |
load file | dynamically link file and add its symbols |
add-sym file addr | read additional symbols from file, dynamically loaded at addr |
path dirs | add dirs to front of path searched for executable and symbol files |
show path | display executable and symbol file path |
info share | list names of shared libraries currently loaded |
(gdb) info files
Symbols from "/home/chenwx/example/ex-gdb".
Native process:
Using the running image of child process 13342.
While running this, GDB does not access memory from...
Local exec file:
`/home/chenwx/example/ex-gdb', file type elf64-x86-64.
Entry point: 0x4004c0
0x0000000000400238 - 0x0000000000400254 is .interp
0x0000000000400254 - 0x0000000000400274 is .note.ABI-tag
0x0000000000400274 - 0x0000000000400298 is .note.gnu.build-id
0x0000000000400298 - 0x00000000004002b4 is .gnu.hash
...
(gdb) show path
Executable and object file path: /home/chenwx/.rvm/gems/ruby-2.2.3/bin:/home/chenwx/.rvm/gems/ruby-2.2.3@global/bin:/home/chenwx/.rvm/rubies/ruby-2.2.3/bin:/home/chenwx/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/chenwx/.rvm/bin:/home/chenwx/.rvm/bin
(gdb) info share
From To Syms Read Shared Object Library
0x00007ffff7dd7ac0 0x00007ffff7df5640 Yes (*) /lib64/ld-linux-x86-64.so.2
0x00007ffff7a2d8b0 0x00007ffff7b80334 Yes (*) /lib/x86_64-linux-gnu/libc.so.6
(*): Shared library is missing debugging information.
Source Files
Commands | Description |
---|---|
show dir | show current source path |
dir names | add directory names to front of source path |
dir | clear source path |
info source | show name of current source file |
info sources | list all source files in use |
list | show next ten lines of source |
list - | show previous ten lines |
list lines [file:]num | display source surrounding lines, specified as line number num [ in named file ] |
list lines [file:]function | display source surrounding lines, specified as beginning of function [ in named file ] |
list lines +off | display source surrounding lines, specified as off lines after last printed |
list lines -off | display source surrounding lines, specified as off lines previous to last printed |
list lines *address | display source surrounding lines, specified as line containing address |
list f,l | from line f to line l |
info line num | show starting, ending addresses of compiled code for source line num |
forw regex | search following source lines for regex |
rev regex | search preceding source lines for regex |
(gdb) show dir
Source directories searched: $cdir:$cwd
(gdb) info source
Current source file is ex-gdb.c
Compilation directory is /home/chenwx/example
Located in /home/chenwx/example/ex-gdb.c
Contains 52 lines.
Source language is c.
Producer is GNU C11 5.4.0 20160609 -mtune=generic -march=x86-64 -g -fstack-protector-strong.
Compiled with DWARF 2 debugging format.
Does not include preprocessor macro info.
(gdb) list
23 {
24 int i;
25 pArray = &array;
26
27 int halfMaxSize = (int)maxSize/2;
28 halfMaxSize = (halfMaxSize > COLUMN_SIZE) ? COLUMN_SIZE : halfMaxSize;
29 for (i = 0; i < maxSize; ++i)
30 {
31 if ((i > 0) && (i%halfMaxSize == 0))
32 {
(gdb) list 30,35
30 {
31 if ((i > 0) && (i%halfMaxSize == 0))
32 {
33 printf("\n");
34 }
35
(gdb) info line 31
Line 31 of "ex-gdb.c" starts at address 0x400625 <printArray+62> and ends at 0x400638 <printArray+81>.
Shell Commands
Commands | Description |
---|---|
cd dir | change working directory to dir |
pwd | print working directory |
make … | call make |
shell cmd | execute arbitrary shell command string |
(gdb) pwd
Working directory /home/chenwx/example.
(gdb) shell rm ex-gdb
(gdb) shell ls -l
total 4
-rw-rw-r-- 1 chenwx chenwx 513 Aug 14 16:15 ex-gdb.c
(gdb) shell gcc -g -o ex-gdb ex-gdb.c
(gdb) shell ls -l
total 16
-rwxrwxr-x 1 chenwx chenwx 10176 Aug 15 08:38 ex-gdb
-rw-rw-r-- 1 chenwx chenwx 513 Aug 14 16:15 ex-gdb.c
GDB Scripts
source script
read, execute GDB commands from file script
define cmd command-list end
create new GDB command cmd; execute script defined by command-list.
document cmd help-text end
create online documentation for new GDB command cmd.
Repository
- Read-only git
You can check out a copy of the git repository directly using the command:
git clone git://sourceware.org/git/binutils-gdb.git
or, refe to gitweb interface of GDB.
References
- GDB: The GNU Project Debugger
- GDB Documentation
- GDB Debugger Command Cheat Sheet
- Debugging Under Unix: gdb Tutorial
- GDB wikipedia
- Gitweb Interface of GDB