Gdb: Difference between revisions

From miki
Jump to navigation Jump to search
No edit summary
Line 85: Line 85:
* https://beej.us/guide/bggdb/
* https://beej.us/guide/bggdb/


<div style="column-width:330pt;-webkit-column-width:330pt;-moz-column-width:330pt;">

{| width=100%
;RETURN
:repeat last command

;Break points and watch points
{|
|-
|-
!colspan=2|Break points and watch points
|
|-
|width=150pt|
<code>b [+-][NUMBER]</code><br/>
<code>b [+-][NUMBER]</code><br/>
<code>break [+-][NUMBER]</code>
<code>break [+-][NUMBER]</code>
|
|Set a breakpoint at current line, at given line ''NUMBER'' or ''NUMBER'' lines after/before current line.
Set a breakpoint at current line, at given line ''NUMBER'' or ''NUMBER'' lines after/before current line.
|-
|-
|
|
Line 144: Line 143:
|}
|}


{| width=100%
;Execute program
{|
|-
|-
!colspan=2|Execute program
|-
|width=150pt|
<code>r [ARGS]</code><br/>
<code>r [ARGS]</code>
|
|
Start (or restart) program. Arguments may include wildcards (*) and redirections (&lt;, &lt;&lt;...)
<code>run</code>
<code>run [ARGS]</code>
| Start (or restart) program. Arguments may include wildcards (*) and redirections (&lt;, &lt;&lt;...)
|-
|-
|
|
Line 185: Line 186:
|}
|}


{| width=100%
;View stack
{|
|-
|-
!colspan=2|View stack
|
|-
|width=150pt|
<code>bt</code><br/>
<code>bt</code><br/>
<code>bt [COUNT]</code><br/>
<code>bt [COUNT]</code><br/>
<code>backtrace [COUNT]</code><br/>
<code>backtrace [COUNT]</code><br/>
<code>where [COUNT]</code>
<code>where [COUNT]</code>
|
|Print backtrace of all stack frames, or innermost (outermost) ''COUNT'' frames if ''COUNT''>0 (''COUNT''<0)
Print backtrace of all stack frames, or innermost (outermost) ''COUNT'' frames if ''COUNT''>0 (''COUNT''<0)
|-
|-
|
|
Line 210: Line 213:
|}
|}


{| width=100%
;View memory
{|
|-
|-
!colspan=2|View memory
|
|-
|width=150pt|
<code>disp EXPR</code><br/>
<code>disp EXPR</code><br/>
<code>display EXPR</code>
<code>display EXPR</code>
|
|Display EXPR at each prompt (if within scope).
Display EXPR at each prompt (if within scope).
|-
|-
|
|
Line 234: Line 239:
|}
|}


{| width=100%
;View code
{|
|-
|-
!colspan=2|View code
|
|-
|width=150pt|
<code>l</code><br/>
<code>l</code><br/>
<code>list</code>
<code>list</code>
|
|List (10 by default) lines of current frame
List (10 by default) lines of current frame
|-
|-
|
|
Line 249: Line 256:
|}
|}


{| width=100%
;Miscellaneous
{|
|-
|-
!colspan=2|Miscellaneous
|
|-
|width=150pt|
<code>q</code><br/>
<code>q</code><br/>
<code>quit</code>
<code>quit</code>
|
|Quit gdb.
Quit gdb.
|-
|-
|
|
Line 265: Line 274:
<code>source FILE</code>
<code>source FILE</code>
|Source script ''FILE''.
|Source script ''FILE''.
|-
|{{kb|RETURN}}
|Repeat last command.
|}
|}
</div>


== GDB examples ==
== GDB examples ==

Revision as of 13:20, 23 November 2016

References

GDB front-ends

Reference list:

Ideally front-ends must use the GDB/MI2 interface.

These are using the old "annotation" mechanisms:

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

GDB configuratino

GDB reads file ~/.gdbinit at start.

Some references:

Bare minimum configuration

From StackOverflow [1]:

set history save on
set print pretty
set output-radix 16
set height 0

GDB dashboard

GDB dashboard is a modular visual interface for GDB in Python.

To install simply copy .gdbinit as ~/.gdbinit

cp gdb-dashboard/.gdbinit ~/.gdbinit


Alternatively, source it from ~/.gdbinit:

source ~/.gdbinit-dashboard
Install pygments

Install pygments to get source highlighting

sudo pip install Pygments        # Globally
pip install Pygments             # Locally

If GDB uses python3 (ldd $(which gdb))), you'll need to install with pip3:

sudo pip3 install Pygments       # Globally
pip3 install Pygments            # Locally

To get the list of available styles:

python from pygments.styles import get_all_styles as styles
python for s in styles(): print(s)

Alternative styles:

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:

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

r [ARGS]
r [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.

until [NUMBER]

Execute until the program reaches a source line greater than current.
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

up

Go up a level in the stack (frame calling current frame).

do
down

Go down a level in the stack (frame called by current frame).
View memory

disp EXPR
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.
RETURN Repeat last command.

GDB examples

Simple Segmentation Fault Example

(From [2])

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