file <prog> | use prog as the program to be debugged |
run <arg1> <arg2> | run the process with the specified arguments |
run | run the process with the last specified arguments
to run the process without the last specified arguments, use before set args |
set args <arg1> <arg2> | set the run arguments |
show args | display the run arguments |
kill | kill the process being debugged |
cont | continue to run the debugged process |
cont <n> | when stopped at a breakpoint, continue to run until the same breakpoint is reached for the nth time |
step | continue to run the debugged process until it reaches a different line of code including subroutine calls (i.e. goes in subroutines) |
step <n> | perform n step s |
stepi | continue to run the debugged process up to the next instruction (i.e. goes in subroutines) |
stepi <n> | perform n stepti s |
next | continue to run the debugged process until it reaches a different line of code in the same routine (i.e. does not go in subroutines) |
next <n> | perform n next s |
nexti | continue to run the debugged process up to the next instruction of the routine (i.e. does not go in subroutines) |
nexti <n> | perform n nexti s |
return | continue to run the debugged process until it reaches the calling routine |
return <value> | continue to run the debugged process until it reaches the calling routine, value is the returned value |
stack
bt
where
info stack | print stack trace |
up | select and display stack frame of calling routine |
up <n> | select and display nth upper stack frame |
down | select and display stack frame of called routine |
down <n> | select and display nth lower stack frame |
frame | display current stack frame |
frame <n> | select and display stack frame number n |
select-frame <n> | select stack frame number n (there is no display) |
list <n> | display 10 lines around line number n |
examination
info stack | display the addresses of frame, called frame, calling frame, frame arguments, frame local variables and also the language, program counter and saved register values |
info args | display the arguments |
info locals | display the local variables |
whatis <expr> | print the type of expression expr |
ptype <expr> | print a description of the type of expression expr
ptype differs from whatis by printing a detailed description, instead of just the name of the type |
ptype <type> | print a description of type |
print [/<fmt>] <expr> | print the value of expression expr with format fmt |
display [/<fmt>] <expr> | print the value of expression expr with format fmt each time the program stops |
display | print the values of the expressions to be displayed |
undisplay <expr> | stop to display the expression expr |
source
dir <path> | add path at the beginning of the source path
the directories must be separated by : (; on Windows)
adding a directory already present in the source path moves it at the beginning $cdir is the compilation directory
$cwd is the current working directory |
show directories | print the source path |
breakpoints
break file.c:105 | set a breakpoint at the line 105 of file file.c |
break foobar | set a breakpoint at the beginning of routine foobar
but, in order to get access to the list of all symbols, we have to start the program ( break main then run ) before |
break 'ValueHandler2::getSt Tab | autocompletion of the routine name |
info breakpoints | list the breakpoints |
info watchpoints | list the watchpoints |
delete | remove all the breakpoints and watchpoints |
delete <n> | remove the breakpoint/watchpoint number n |
info
list | display ten lines around the current line |
disassemble <routine> | disassemble a routine |
info source | show the name of the current source file |
info sources | show all the source files which have debugging information |
info functions | show signatures of all functions |
info function <regexp> | show the signatures of functions whose name matches a regexp |
interaction with the shell
pwd | print working directory |
shell <command> | execute a shell command |
set environment <var> <value> | set an environment variable for the next run of the application |
unset environment <var> | remove a variable from the environment |
show environment | show the environment given to the next run of the application |
show environment <var> | show the value of the environment variable for the next run of the application |
misceallaneous
quit | exit |
source <file> | execute the commands of file file |
automated launching
When debugging a process forked from another, the usual solution is to
-
add an infinite loop
volatile int i=0; while (i==0);
in the code, - recompile,
- start the parent process,
-
wait for the
fork
, - attach gdb/ddd to it,
-
change the value of
i
, and - continue the process.
-
add the following code just before the line where you want to start the debug of the child process,
char buffer[128];
long pid = getpid();
sprintf(buffer,"echo break %s:%d > /tmp/ddd_cmd_%ld; echo cont >> /tmp/ddd_cmd_%ld; ddd -pid=%ld --command=/tmp/ddd_cmd_%ld &",__FILE__,__LINE__+3,pid,pid,pid,pid);
system(buffer);
sleep(3); - recompile, and
- start the parent process.