GCC Debugging/gdb

Preping for the debuggerEdit

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 debuggerEdit

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

common commandsEdit

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

runEdit

runs the program
to pass arguments to the program on run:
run <arg1> <arg2>
eg: run 12 35
eg: run "hello world" "goodbye world"

breakEdit

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

bEdit

shorthand for break

nextEdit

execute next line of code
eg: next

stepEdit

next line of code, stepping into functions
eg: step

continueEdit

go to next breakpoint or end of program

contEdit

shorthand for continue

printEdit

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

dispEdit

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

undisplayEdit

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

whereEdit

move within the call stack
...

up and downEdit

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

listEdit

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

condEdit

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

setEdit

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

quitEdit

exits the gdb

qEdit

shorthand for quit