Gdb: Difference between revisions

From miki
Jump to navigation Jump to search
Line 141: Line 141:
|-
|-
|
|
<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''.
|-
|-
|
|
Line 152: Line 152:
|-
|-
|
|
<code>cl _location_</code><br/>
<code>cl LOCATION</code><br/>
<code>clear _location_</code>
<code>clear LOCATION</code>
|Clear breakpoint by ''_location_''
|Clear breakpoint by ''LOCATION''
|-
|-
|
|
Line 162: Line 162:
|-
|-
|
|
<code>d _number_</code><br/>
<code>d NUMBER</code><br/>
<code>delete _number_</code>
<code>delete NUMBER</code>
|Clear breakpoint by ''_number_'' (as listed by <code>i b</code>)
|Clear breakpoint by ''NUMBER'' (as listed by <code>i b</code>)
|-
|-
|
|
<code>dis _number_</code><br/>
<code>dis NUMBER</code><br/>
<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>)
|}
|}


Line 194: Line 194:
|-
|-
|
|
<code>advance _location_</code>
<code>advance LOCATION</code>
|Run until temporary breakpoint set at ''_location_''.
|Run until temporary breakpoint set at ''LOCATION''.
|}
|}


Line 202: Line 202:
|-
|-
|
|
<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>undisplay _number_</code>
<code>undisplay NUMBER</code>
|Undisplay expression by ''_number_''.
|Undisplay expression by ''NUMBER''.
|}

;List and disassemble
{|
|-
|
<code>l</code><br/>
<code>list</code>
|List (10 by default) lines of current frame
|-
|
<code>disassemble /m</code><br/>
<code>disassemble /s</code><br/>
<code>disassemble 'foo.c'::bar</code>
|Disassemble a specified section of memory
|}
|}



Revision as of 10:30, 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:


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 current frame
print VAR
Print value of variable VAR
list
List specified function or line (see help line for more information)
kill
Kill current program
b FILE:LINE
break FILE: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
repeat last command
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
n like s, but don't step into functions
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

b LOCATION
break LOCATION

Set breakpoint at LOCATION.

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)
Execute program

run

Start execution (or restart)

s
step

Step into current line

n
next

Run to next line (step over current line)

finish

Execute till returning from current selected frame.

advance LOCATION

Run until temporary breakpoint set at LOCATION.
Display and print

display EXPR

Display EXPR at each prompt (if within scope).

undisplay NUMBER

Undisplay expression by NUMBER.
List and disassemble

l
list

List (10 by default) lines of current frame

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

Disassemble a specified section of memory

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