Course Content
C Tutorial
About Lesson

Using Debugging Tools (GDB)

To use GDB, ensure it’s installed on your system. Most Linux distributions come with GDB pre-installed, while for others, you might need to install it manually through package managers like apt, yum, or brew.

Compiling with Debug Symbols

When compiling your C code, include debug symbols (-g flag in GCC) to enable GDB to associate machine code with the original source. For instance:

gcc -g -o output_file input_file.c

Basic GDB Commands

Starting GDB

Run GDB by invoking it with the executable file as an argument:

gdb ./your_executable

Setting Breakpoints

Use breakpoints to pause execution at specific lines or functions:

break file.c:line_number
break function_name

Running the Program

Start executing the program within GDB:


Examining Variables

Inspect variable values during runtime:

print variable_name

Stepping Through Code

Step through the code line by line:

next (or 'n')
step (or 's')

Advanced GDB Features

Conditional Breakpoints

Set breakpoints to halt execution based on specified conditions:

break file.c:line_number if condition

Watching Variables

Track changes in variables:

watch variable_name


Retrieve the call stack to trace the sequence of function calls:

backtrace (or 'bt')

Tips for Effective Debugging with GDB

  1. Documentation: Familiarize yourself with GDB commands and features through online resources and documentation.

  2. Practice: Regularly use GDB to debug small snippets of code to enhance proficiency.

  3. Use Debugging Symbols: Ensure debug symbols are included during compilation for accurate debugging