Jump to content

GCC Debugging/gdb

From Wikibooks, open books for an open world

Preping for the debugger

[edit | edit source]
program must first be compiled for debugging:
gcc -g <filname.c> -o <output filename>
eg: gcc -g debug_me.c -o debug_me
the "-g" flag is important (To create symbol table)

Starting the debugger

[edit | edit source]
start the debugger with:
gdb <program name>
eg: gdb debug_me

common commands

[edit | edit source]
run, print, where, break, cond, step, next, set, ignore
runs the program
to pass arguments to the program on run:
run <arg1> <arg2>
eg: run 12 35
eg: run "hello world" "goodbye world"

break

[edit | edit source]
sets a "breakpoint" at a certain area \ function
break <function name>
eg: break main
or
eg: b main
break <filename>:<line of code to stop in>
eg: break debug_me.c:5
shorthand for break
execute next line of code
eg: next
next line of code, stepping into functions
eg: step

continue

[edit | edit source]
go to next breakpoint or end of program
shorthand for continue

print

[edit | edit source]
print out a variables \ expressions contents
print <variable name>
eg: print x
prints out a variable \ expression value every step
eg: disp x

undisplay

[edit | edit source]
cancel a display command
eg: undisplay 3
would stop the var at pos 3 from displaying every time

where

[edit | edit source]
move within the call stack
...

up and down

[edit | edit source]
up: previous step inside call stack
down: next step inside call stack
shows code at a certain location
list <no args>
code at current location and then at next location if typed again
eg: list
list -
shows code at previous location and then at the location before that if typed again
list <function name>
shows code in function definition
eg: list func1
conditional
cond <breakpoint> <condition>
eg: cond 1 val>0
stop at breakpoint "1" when "val" becomes greater than 0
change a value, eg
set variable x = 12
will change x's value to 12

ignore

[edit | edit source]

ignore a breakpoint for a user specified number of times.

usage: ignore <id> <count>

ignore 2 20 --> Breakpoint 2 will not be hit for 20 times.

exits the gdb
shorthand for quit