CPSC 213: gdb Tutorial
The purpose of gdb is to allow for easy debugging. Although you may
argue that you can debug a program easily through visual checks, this is often
not the case when there are multiple calls and dependencies. This is especially
true when programs are thousands of lines long.
What is gdb?
gdb is a debugging program for code written in C/C++ that is
executed through the command line. It works the same way as many other debugging
programs. For example, it has a lot of the functionality available in the Eclipse
IDE. However, as there isn't a GUI, it is best for you to get acquainted to using
commands within the terminal: it follows a simple command-response model
How To Start - Compilation
You should now be familiar with compiling C programs with gcc. A
typical command that you may use is
gcc -g -o executable_name source.c
where the -o flag specifies a different executable filename than the
default a.out. However, to allow the debugger to output additional
information for the end user, you need to add the -g flag to allow the
compiler to include additional debugging information into the executable.
When you write any code, it is suggested that you keep the -g flag
on to allow for easy debugging. The tradeoff is a small increase in compilation
time and size of the executable. For the remainder of the CS213 course, please
compile with the flag on.
To learn more about other flags for gcc, please see the
official online documentation.
How To Start - Executing
Executing gdb is as simple as
You need not supply any other arguments. It's easy! You should see a (gdb)
prompt once it has loaded your program, indicating that it is ready for use.
The interface of gdb works like a usual command-line interface,
i.e. you type commands at the prompt and press Enter to execute them. Some
useful features to know are:
- Command completion - gdb can complete your command if it has
enough characters to determine which command you want to enter. The automated
completion occurs when you hit the tab key.
- History - You can see a history of the commands you have entered.
This can achieved with the arrow keys or Ctrl+P and Ctrl+N.
- Help - Just type help if you need general help or
help command to learn more about a specific gdb command.
- Info - This command allows you get relevant information about various
things. For example info locals will tell you the local variables that
you have access to. info break will tell you the breakpoints you have set.
To run the program, type
if you want to keep the same arguments as the previous run.
If the program executed successfully, it will tell you the program exited
normally, else it will give you an error message.
About Error Messages
Error messages tell you the error that occurred and the line where it
occurred. They are very useful for narrowing down where exactly the
execution faulted. A common error message you will see is
SIGSEGV, Segmentation fault.. It simply means that you have tried to
access an invalid memory address. This can mean you are accessing an array out
of bounds! Google is always helpful to learn more about your error message.
- print expression - Prints almost all C expressions. Try these:
- print x - Prints the value of x
- print *x - Prints the value stored in the memory location pointed
to by x. (x must be of pointer type.)
- print 3 + 4 * 5 - Prints the calculation of 3 + 4 * 5.
- backtrace - Shows the call stack. Let's say main() called
helperFunc(), which called printf. Since printf is a
library function, the error message will show you that a line within the library
caused an error, but through the call stack, you can see that main()
called helperFunc() on line x, and helperFunc() called
printf on line y.
- break - Sets a breakpoint. This can be done with a line number in
the source file using break source_file.c:line_number,
or with a function name using break helper_func. To delete a
breakpoint, first list all the breakpoints with info break, find the
number of the desired breakpoint, and delete it with del breakpoint_number.
- watch - Watches a variable. Halts execution when variable is changed.
- continue, cont - Continues execution until the next
breakpoint, watch, or an error occurs.
- step - Steps over the next source line (OVER function calls)
- next - Steps into the source line (INTO function calls)
These are some shortcuts to help you debug faster.
*some conditions apply.
- b is break
- bt is backtrace
- d is delete
- n is next
- p is print
- s is step
- Just hitting Enter at the prompt, with nothing typed, will repeat the previous command.*
- Conditional breakpoints - You can make breakpoints conditional, so
that they break when a condition is true, using e.g. breakpoint foo if x==0
- Advanced printing
- Printing structs - print linkedlist->next
- Printing the value that a pointer is pointing to - print *r
- Printing in hex - print/x variable
- Casting a struct - print *(struct_name) variable
Last modified 2012-10-04 11:07