Debug using GNU Debugger (GDB)

Metin Cakircali

1 min read

The GNU Debugger (GDB) is a powerful tool that allows software developers to see what is happening inside a program while it runs or what it was doing at the moment it crashed. This guide provides an overview of the basic commands needed to start using GDB for debugging C and C++ programs.

Starting GDB

To start GDB, use the following command:

gdb <your_program>

Setting Breakpoints

Breakpoints allow you to pause the execution of your program at specific points. To set a breakpoint, use:

break <function_name>

or

break <file_name>:<line_number>

Running the Program

To run the program within GDB, use:

run

Stepping Through Code

To step through your code line by line, use:

step

or to step over function calls, use:

next

Inspecting Variables

To inspect the value of a variable, use:

print <variable_name>

Continuing

To continue running the program until the next breakpoint, use:

continue

Exiting

To exit GDB, use:

quit

Conclusion

GDB is an essential tool for debugging C and C++ programs. By mastering its basic commands, you can significantly improve your debugging efficiency.