Gdb

Debug sessions with gdb and ddd

A graphical frontend for gdb is for example DDD. Type in a empty line for repeating the last command.

To debug program with a bigger symbol table which can be used by gdb use gcc option -g

 gcc -g ...
 gcc -ggdb3
further information for gdb3

Start debug session:

 gdb program [corefile]
The corefile is a file from a ealier run and maybe a core dump file. To end gdb use "quit" or "q".

commands inside gdb

 list
lists the commands around the point where the program stops at this time. If you repeat the command "list" you get the lines after these which you have already seen. You can repeat that until you reach the end of the program.
 list 1,5
lists lines 1 until 5.
 run 
 r
runs the program.
 run arg1 arg2
runs the program with parameters
 set args arg1 arg2
gives arguments after run.
 show args
shows arguments.
 break
 b
sets breakpoint
 break linenumber
 break filename:linenumber
 break linenumber if expression
 break function
 break filename:function
 break function if expression

 continue
continues execution of the program.
 info breakpoints
overview of the breakpoints you have defined.
 delete 2
deletes breakpoint number 2.
 clear
 cl
removes all breakpoints.
 disable 1 
disables breakpoint 1
 enable 1
enables it again
 step
 s
executes the next assignment or steps into a function dependent where the program is currently.
 next
 n
executes next assignment or steps over a function if current position is a function call.
 print
 p
shows values of variables.
 print myfunction(2, 4711)
calls function with these parameters.
 print pmydatastructure
 print *mydatastructure

You get an output with $1, $2, ... . These variables can be used:

 print $1 + $2
 $3 = 4711

 print arr@10
prints first ten addresses of the array.
 print arr[0]@10 
gives 10 values beginning with the first position.
 print arr[20]@10
gives 10 values beginning with the twentieth position.
 whatis i
returns type of i e.g. "type = int"
 ptype
gives more information what is helpful with structures. "whatis" gives only "type = structure" but "ptype" gives the whole definition of that structure.
 where
 bt
find position where the program breaks its execution due to SIGSEGV.