KM GDB
KM GDB
What is GDB?
• “GNU Debugger”
• A debugger for several languages, including C and C++
• It allows you to inspect what the program is doing at a certain point
during execution.
• Errors like segmentation faults may be easier to find with the help
of gdb.
Additional step when compiling program
• Normally, you would compile a program like:
gcc [flags] <source files> -o <output file>
For example:
gcc hello.c -o hello.x
• Now you add a -g option to enable built-in debugging support
(which gdb needs):
gcc [other flags] -g <source files> -o <output file>
For example:
gcc -g hello.c -o hello_debug
Starting up gdb
• Just try “gdb” or “gdb a.out.” You’ll get a prompt that looks like this:
(gdb)
• If you didn’t specify a program to debug, you’ll have to load it in
now:
(gdb) file a.out
Here, a.out is the program you want to load, and “file” is the
command to load it.
gdb help
• gdb has an interactive shell, much like the one you use as soon as
you log into the linux grace machines. It can recall history with the
arrow keys, auto-complete words (most of the time) with the TAB
key, and has other nice features.