GNU includes software called a debugger, which allows you to monitor the execution of your programs to locate and remove logic errors.
The GNU debugger works only with executable files that were compiled with the -g compiler option, which generates information that is used by the debugger to help you debug your programs.
The gdb command will start the GNU debugger and enable you to use its features. The run command will run a program through the debugger.
Breakpoints are markers that can be set at any executable line of code. When program execution reaches a breakpoint, execution pauses.
The break command inserts a breakpoint at the line number specified after the command.
When the program runs, it suspends execution at any line that contains a breakpoint and is said to be in break mode.
The continue command causes the program to continue running until the next breakpoint is reached.
The print command allows you to peek inside the computer at the value of one of your variables.
When the print command is used, the result is stored in a convenience variable such as $1. Convenience variables are temporary variables that can be used in the debugging process to perform arithmetic and evaluate boolean expressions.
You can display a list of all of the breakpoints in the program by typing info break.
To remove a breakpoint, type delete, followed by a space and the number of the breakpoint to remove.
Use the quit command to end the debugging session.
The set command allows the programmer to assign new values to variables.
The step command executes the next statement in the program. If the next statement to execute is a function call, control transfers to the called function. The step command enables you to enter a function and study the individual statements of that function.
The finish command executes the remaining statements in the function and returns control to the place where the function was called.
The next command behaves like the step command, except when the next statement to execute contains a function call. In that case, the called function executes in its entirety and the program advances to the next executable line after the function call.
The watch command sets a watch on any variable or data member of an object currently in scope during execution of the debugger. Whenever the value of a watched variable changes, the debugger enters break mode and notifies you that the value has changed.