Summary

The debugging process is an essential thing that we can do to analyse our program by running it step-by-step. When our program produces unexpected results or it crashes in the middle of an execution, there is no other choice than to run the debugging process. GDB is our choice since it is compatible with the C++ language, as it comes with MinGW-w64 installer packages and is lightweight when loaded.

GDB can only run an executable file that compiles using the -g option. This option will add the debugging information and symbol, which are important in the debugging process. You will be unable to debug the executable files that are compiled without the -g option.

After we successfully load our program under GDB, we can choose either the run or start command to execute the debugging process. The run command will execute our program as usual but will stop if the debugger finds a breakpoint, while the start command will stop at the main block of program at the first execution.

When the debugger stops at certain line, we have to decide whether to continue the debugging process. We have the option to run the program until it exits or if the breakpoint is found using the continue command. Alternatively, we can run the debugger step-by-step using the next command.

To make the debugger stop at the execution of the debugging process, call the break [linenumber] command to set the breakpoint. If we want to ensure that we set the correct line number, call the list command to print the source code. Calling the delete N command will then delete the breakpoint where N can find the info break command.

Retrieving the value of a variable is also important when finding an error. If the program produces unexpected output, we can trace the value of a variable by printing it. We can do this by using the print [variablename] command. At the variable we suspect is causing an error, we can reassign the value of that variable with a new one using the set var [variablename]=[newvalue] command. We can then run the debugger again until we obtain the expected output. When we have fixed all the errors, and are sure that everything is perfect, we can recompile our program by calling the compiling command inside GDB prompt using the shell [Windows shell command] command.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset