Debugging with GDB

Code to debug

We then use in this article this program which gives the number of people having a common birthday.

  • Compiling with the -g flag will generate debugging symbols

g++ -g3 birthday_problem.cpp -o birthday_problem

Debugging step by step

Add breakpoint

  • Add breakpoint on the main function

breakpoint on the main function
  • Add breakpoint on a specific line like birthday_problem.cpp:32

breakpoint on a specific line

Run the program

  • Run the program using the command run. And the debugger stops on the first breakpoint.

launch the program
  • Debuggers allow you to inspect the program’s state, including variables and functions, at any point during execution. You can then choose to either step through the code line by line or run (see the below example it until the next breakpoint is reached.

continue and break
  • Print a variable

continue and break

Scripting

Two sample scripts: