This is a brief description of some of the most commonly used features of gdb.
-gflag. So, if your program is in a source file called
memsim.cand you want to put the executable in the file
memsim, then you would compile with the following command:
gcc -g -o memsim memsim.c
gdbat the unix prompt. Gdb will give you a prompt that looks like this:
(gdb). From that prompt you can run your program, look at variables, etc., using the commands listed below (and others not listed). Or, you can start gdb and give it the name of the program executable you want to debug by saying
To exit the program
quit at the
(gdb) prompt (actually
q is good enough).
helpwill give you a list of topics. Then you can type
help topicto get information about that topic (or it will give you more specific terms that you can ask for help about). Or you can just type
help commandand get information about any other command.
file executablespecifies which program you want to debug.
runwill start the program running under gdb. (The program that starts will be the one that you have previously selected with the
filecommand, or on the unix command line when you started gdb. You can give command line arguments to your program on the gdb command line the same way you would on the unix command line, except that you are saying
runinstead of the program name:
You can even do input/output redirection:run 2048 24 4
run > outfile.txt.
break function sets the breakpoint at the
function. If your code is in
multiple files, you might need to specify
break linenumber or
filename:linenumber sets the breakpoint to the given
line number in the source file. Execution will stop before that line
has been executed.
deletewill delete all breakpoints that you have set.
delete number will delete breakpoint numbered
number. You can find out what number each
breakpoint is by doing
info breakpoints. (The command
info can also be used to find out a lot of other stuff.
help info for more information.)
clear functionwill delete the breakpoint set at that function. Similarly for
continuewill set the program running again, after you have stopped it at a breakpoint.
stepwill go ahead and execute the current source line, and then stop execution again before the next source line.
nextwill continue until the next source line in the current function (actually, the current innermost stack frame, to be precise). This is similar to
step, except that if the line about to be executed is a function call, then that function call will be completely executed before execution stops again, whereas with
stepexecution will stop at the first line of the function that is called.
next, except that if you are at the end of a loop,
untilwill continue execution until the loop is exited, whereas
nextwill just take you back up to the beginning of the loop. This is convenient if you want to see what happens after the loop, but don't want to step through every iteration.
list linenumberwill print out some lines from the source code around
linenumber. If you give it the argument
functionit will print out lines from the beginning of that function. Just
listwithout any arguments will print out the lines just after the lines that you printed out with the previous
print expressionwill print out the value of the expression, which could be just a variable name. To print out the first 25 (for example) values in an array called