Gdb: Difference between revisions

From miki
Jump to navigation Jump to search
Line 38: Line 38:
* https://beej.us/guide/bggdb/
* https://beej.us/guide/bggdb/



;help
:Get help on commands

;run [ARGS]
:Start debugged program. Arguments may include wildcards (*) and redirections (<, <<...)

;backtrace [COUNT]
;bt [COUNT]
;where [COUNT]
:Print backtrace of all stack frames, or innermost (outermost) COUNT frames if COUNT>0 (COUNT<0)

;frame [FRAME]
:Select and print stack frame

;info locals
;info args
:Print information on local variables / function arguments in the <u>current</u> frame

;print VAR
:Print value of variable VAR

;list
:List specified function or line (see <code>help line</code> for more information)

;kill
:Kill current program

;b FILE&#58;LINE
;break FILE&#58;LINE
:Insert a breakpoint at file FILE, line LINE

;watch ''expr''
:stop execution when expr. changes

;awatch ''expr''
:stop execution when expression is accessed (read or write)

;next
:Step to next instruction

;cont
:continue execution

;help ''command''
;apropos ''word''
:type '''help''' followed by command name for full documentation
:type '''apropos wrod''' to serrch for commands related to word
:Command name abbreviations are allowed if unambiguous

;source ''script''
:source given script

;save breakpoints ''file''
;save b ''file''
:save current breakpoints as script


;RETURN
;RETURN
:repeat last command
:repeat last command


;Break points and watch points
{| class=wikitable
|-
{|
|b main||Put a breakpoint at the beginning of the program
|-
|b||Put a breakpoint at the current line
|-
|b N||Put a breakpoint at line N
|-
|b +N||Put a breakpoint N lines down from the current line
|-
|b fn||Put a breakpoint at the beginning of function "fn"
|-
|d N||delete breakpoint number N
|-
|r||Run the program until a breakpoint or error
|-
|c||continue running the program until the next breakpoint or error
|-
|f||Run until the current function is finished
|-
|s||run the next line of the program
|-
|-
|
|s N||run the next N lines of the program
<code>b [+-][NUMBER]</code><br/>
|-
<code>break [+-][NUMBER]</code>
|n||like s, but don't step into functions
|Set a breakpoint at current line, at given line ''NUMBER'' or ''NUMBER'' lines after/before current line.
|-
|u N||run until you get N lines in front of the current line
|-
|p var||print the current value of the variable "var"
|-
|bt||print a stack trace
|-
|u||go up a level in the stack
|-
|d||go down a level in the stack
|-
|q||Quit gdb
|}

;Break points
{|
|-
|-
|
|
<code>b LOCATION</code><br/>
<code>b LOCATION</code><br/>
<code>break LOCATION</code><br/>
<code>break LOCATION</code><br/>
|Set breakpoint at ''LOCATION''.
|Set breakpoint at ''LOCATION''.
:<code>b main</code> sets a breakpoint at beginning of function <code>main()</code>.
:<code>b foo.c:42</code> sets a breakpoint at file {{file|foo.c}}, line 42.
|-
|
<code>watch EXPR</code>
|Stop execution when ''EXPR'' changes
|-
|
<code>awatch EXPR</code>
|Stop execution when ''EXPR'' is accessed
|-
|-
|
|
Line 170: Line 90:
<code>disable NUMBER</code>
<code>disable NUMBER</code>
|Disable breakpoint by ''NUMBER'' (as listed by <code>i b</code>)
|Disable breakpoint by ''NUMBER'' (as listed by <code>i b</code>)
|-
|
<code>save b FILE</code><br/>
<code>save breakpoints FILE</code>
|Save current breakpoints as script FILE. Use <code>source</code> to reload.
|}
|}


Line 177: Line 102:
|
|
<code>run</code>
<code>run</code>
<code>run [ARGS]</code>
|Start execution (or restart)
| Start (or restart) program. Arguments may include wildcards (*) and redirections (&lt;, &lt;&lt;...)
|-
|-
|
|
<code>s</code><br/>
<code>kill</code>
|Kill current program.
<code>step</code>
|-
|Step ''into'' current line
|
<code>c</code>
<code>cont</code>
| Continue and interrupted program.
|-
|
<code>s [NUMBER]</code><br/>
<code>step [NUMBER]</code>
|Step (''into'') current line, or ''NUMBER'' lines.
|-
|-
|
|
<code>n</code><br/>
<code>n</code><br/>
<code>next</code>
<code>next</code>
|Run to next line (''step over'' current line)
|Run to next line (''over'' current line)
|-
|-
|
|
<code>f</code><br/>
<code>finish</code>
<code>finish</code>
|Execute till returning from current selected frame.
|Execute till returning from current selected frame.
Line 198: Line 134:
|}
|}


;View stack
;Display and print
{|
|-
|
<code>bt</code><br/>
<code>bt [COUNT]</code><br/>
<code>backtrace [COUNT]</code><br/>
<code>where [COUNT]</code>
|Print backtrace of all stack frames, or innermost (outermost) ''COUNT'' frames if ''COUNT''>0 (''COUNT''<0)
|-
|
<code>f [FRAME]</code><br/>
<code>frame [FRAME]</code>
|Select frame ''FRAME'' and print stack frame
|-
|
<code>u</code><br/>
<code>up</code>
|Go up a level in the stack
|-
|
<code>d</code><br/>
<code>down</code>
|Go down a level in the stack
|}

;View memory
{|
{|
|-
|-
Line 204: Line 166:
<code>display EXPR</code>
<code>display EXPR</code>
|Display EXPR at each prompt (if within scope).
|Display EXPR at each prompt (if within scope).
|-
|
<code>i locals</code><br/>
<code>i args</code><br/>
<code>info locals</code>
<code>info args</code>
|Print information on local variables / function arguments in the <u>current</u> frame
|-
|
<code>print EXPR</code>
|print ''EXPR''.
|-
|-
|
|
Line 210: Line 183:
|}
|}


;View code
;List and disassemble
{|
{|
|-
|-
Line 223: Line 196:
<code>disassemble 'foo.c'::bar</code>
<code>disassemble 'foo.c'::bar</code>
|Disassemble a specified section of memory
|Disassemble a specified section of memory
|}

;Miscellaneous
{|
|-
|
<code>q</code><br/>
<code>quit</code>
|Quit gdb.
|-
|
<code>help COMMAND</code><br/>
<code>apropos WORD</code>
|Get help on ''COMMAND'', or search commands related to ''WORD''.
|-
|
<code>source FILE</code>
|Source script ''FILE''.
|}
|}



Revision as of 10:55, 22 November 2016

References

GDB front-ends

There is also the built-in Text User Interface to GDB (C-x C-a: http://davis.lbl.gov/Manuals/GDB/gdb_21.html

Prepare debug session

  • Compile with debug symbols, use option -g:
gcc -g program.c               # -g : debug symbols
gcc -g -O0 program.c           #  ... -O0: disable optimization
  • Force core dumps (see bash help ulimit):
ulimit -c unlimited
./a.out
# Segmentation fault (core dumped)

GDB invocation

gdb a.out
gdb a.out core.1234           # If coredump available

GDB commands

Reference:


RETURN
repeat last command
Break points and watch points

b [+-][NUMBER]
break [+-][NUMBER]

Set a breakpoint at current line, at given line NUMBER or NUMBER lines after/before current line.

b LOCATION
break LOCATION

Set breakpoint at LOCATION.
b main sets a breakpoint at beginning of function main().
b foo.c:42 sets a breakpoint at file foo.c, line 42.

watch EXPR

Stop execution when EXPR changes

awatch EXPR

Stop execution when EXPR is accessed

i b
info b
info break

list breakpoints

cl LOCATION
clear LOCATION

Clear breakpoint by LOCATION

d
delete

Delete all breakpoints

d NUMBER
delete NUMBER

Clear breakpoint by NUMBER (as listed by i b)

dis NUMBER
disable NUMBER

Disable breakpoint by NUMBER (as listed by i b)

save b FILE
save breakpoints FILE

Save current breakpoints as script FILE. Use source to reload.
Execute program

run run [ARGS]

Start (or restart) program. Arguments may include wildcards (*) and redirections (<, <<...)

kill

Kill current program.

c cont

Continue and interrupted program.

s [NUMBER]
step [NUMBER]

Step (into) current line, or NUMBER lines.

n
next

Run to next line (over current line)

f
finish

Execute till returning from current selected frame.

advance LOCATION

Run until temporary breakpoint set at LOCATION.
View stack

bt
bt [COUNT]
backtrace [COUNT]
where [COUNT]

Print backtrace of all stack frames, or innermost (outermost) COUNT frames if COUNT>0 (COUNT<0)

f [FRAME]
frame [FRAME]

Select frame FRAME and print stack frame

u
up

Go up a level in the stack

d
down

Go down a level in the stack
View memory

display EXPR

Display EXPR at each prompt (if within scope).

i locals
i args
info locals info args

Print information on local variables / function arguments in the current frame

print EXPR

print EXPR.

undisplay NUMBER

Undisplay expression by NUMBER.
View code

l
list

List (10 by default) lines of current frame

disassemble /m
disassemble /s
disassemble 'foo.c'::bar

Disassemble a specified section of memory
Miscellaneous

q
quit

Quit gdb.

help COMMAND
apropos WORD

Get help on COMMAND, or search commands related to WORD.

source FILE

Source script FILE.

GDB examples

Simple Segmentation Fault Example

(From [1])

Example program segfault.c:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
  char *buf;

  buf = malloc(1<<31);

  fgets(buf, 1024, stdin);
  printf("%s\n", buf);

  return 1;
}
Compile and launch gdb:
gcc -g segfault.c
gdb a.out

The debug session

run
backtrace
frame 3
print buf
kill
break segfault.c:8
run
print buf
next
print buf

Fix the bug, then start again, watching now buf:

watch buf
# Start again, answer 'y' when asked to start from beginning
run
# Break at watch point, let's _c_ontinue
c