The Basics of The GDB Debugger
The GDB debugger allows you to step through a program line-by-line, note variable values, set breakpoints where the execution pauses, and finally, pause and continue execution of a program. The GDB debugger supports many languages such as Assembly, C, and C++. To get started with the GDB debugger, first the program will need to be compiled with gcc for debugging. This can be done using this command and flags bellow.
gcc -Wall -g program.c
- gcc: This is the GNU Compiler Collection, a command that compiles programs from multiple languages. This is needed to compile the program before it is used in the GDB.
- -Wall: This will check for warnings and errors in the program and it’s good practice to use this flag with compiling the code.
- -g: This flag is important as it preserves identifiers and symbols so you can get a better visual debugging it later with the GDB debugger.
- program.c: A sample program name, where you would put your program title that you’d like to debug.
After this you will need to start the GDB using the executable created from the previous compiling.
gdb a.out
You can also give it arguments if your program takes arguments.
gdb --args a.out arg1 arg2
Now that you’re in the gdb with your executable, you can start debugging it. Here are the basic commands that can help you get started.
- Refresh: refresh the display.
- Run: Run the program.
- n (next): Step through to the next instruction.
- break POINT: Setting a break point.
- Continue: Continues to the next breakpoint if set.
- print VARIABLE: prints a variable’s value.
- watch VARIABLE: watches for a variable when it changes.
These commands can help get you out of the gate to start debugging your own programs. The gdb is very helpful with runtime errors and more specifically, logical errors. Thank you.