GCC Debugging/gdb

From Wikibooks, open books for an open world
Jump to: navigation, search

Preping for the debugger[edit]

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]

start the debugger with:
gdb <program name>
eg: gdb debug_me

common commands[edit]

run, print, where, break, cond, step, next, set

run[edit]

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]

sets a "breakpoint" at a certain area \ funtion
break <function name>
eg: break main
or
eg: b main
break <filename>:<line of code to stop in>
eg: break debug_me.c:5

b[edit]

shorthand for break

next[edit]

execute next line of code
eg: next

step[edit]

next line of code, stepping into functions
eg: step

continue[edit]

go to next breakpoint or end of program

cont[edit]

shorthand for continue

print[edit]

print out a variables \ expressions contents
print <variable name>
eg: print x

disp[edit]

prints out a variable \ expression value every step
eg: disp x

undisplay[edit]

cancel a display command
eg: undisplay 3
would stop the var at pos 3 from displaying every time

where[edit]

move within the call stack
...

up and down[edit]

up: previous step inside call stack
down: next step inside call stack

list[edit]

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 funtion definition
eg: list func1

cond[edit]

conditional
cond <breakpoint> <condition>
eg: cond 1 val>0
stop at breakpoint "1" when "val" becomes greater then 0

set[edit]

change a value, eg
set variable x = 12
will change x's value to 12

quit[edit]

exits the gdb

q[edit]

shorthand for quit