Introduction to debugging

wxDev-C++

<  Previous   <           = Home =             > Next >

Introduction to debugging

    All developers make mistakes, but some mistakes are harder to detect than others. Compile-time mistakes are usually the easiest to spot because wxDev-C++ will provide a list of those errors (including line numbers and suggested corrections) in the Compiler Output window. Runtime errors (or "bugs") are more insidious because the developer has no way of knowing what line of code caused the dreaded infinite loop or "Access Violation" or "Segmentation Fault" or "Blue Screen of Death". Thus debuggers were created to help developers investigate their program while it is running.

    A debugger is a program that runs your program inside of it. It keeps track of your program's functions, variables and instructions. It is capable of pausing your program at a given moment (aka breakpointing), allowing you to view (and even modify) the values of your variables at that moment, and then continuing the execution of the program either one instruction at a time (stepping) or to the end of execution. You can set breakpoints anywhere in your code : once your program reaches that  code at runtime, the debugger will pause your program and let you examine its variables at that time.

    This tutorial explains the use of the MingW GNU debugger (gdb). It can only be used with the MingW gcc compiler. Note that gdb is intended to be a command-line interface. It runs as its own shell allowing the user to run gcc-compiled programs from within that shell. All we do in wxDev-C++ is send messages to the gdb shell and parse the output as it occurs. 

    With gdb (as with all debuggers), we can:

  • specify places within the program to halt execution (aka breakpoints)
  • step through the program one line of source code at a time
  • view the values of variables in realtime as the program executes
  • evaluate the memory stack when a program throws an error
Note that a web search will reveal many gdb tutorials out there that are far more comprehensive than this one in terms of scope (for example, http://dirac.org/linux/gdb/). Please consider and review them if you really want to learn the in's and out's of using gdb.
There are, in fact, other graphical programs, which also use gdb in a similar manner. For instance, the Data Display Debugger (ddd) is a popular GUI interface for gdb debugging. You may, in fact, prefer to use these 3rd party programs instead of the built in wxDev-C++ interface.