Debugging with GDB

The GNU Debugger, usually called just GDB and named gdb as an executable file, is the standard debugger for the GNU operating system. However, its use is not strictly limited to the GNU operating system; it is a portable debugger that runs on many Unix-like systems and works for many programming languages, including Ada, C, C++, Objective-C, Free Pascal etc.
GDB offers extensive facilities for tracing and altering the execution of computer programs. The user can monitor and modify the values of programs’ internal variables, and even call functions independently of the program’s normal behavior.

The debugger does not contain its own graphical user interface, and defaults to a command-line interface. Several front-ends have been built for it, such as Xxgdb, Data Display Debugger (DDD), Nemiver, KDbg, Xcode debugger etc.
A debugger lets you pause a program, examine and change variables, and step through code.

Getting Started: Starting and Stopping

1. Compile your C++ program with -g option. -g is the debugging option.
You still get an executable file but it contains debugging information that lets you use variables and function namee inside gdb.
g ++ -g file_name -o a
// where a is name-of-executable, you can use any other name too

2. Once you have compiled your file , now you need to open gdb debugger
It could be opened by one of the following ways :

    gdb name-of-executable
    gdb -e name-of-executable -c name-of-core-file
    gdb name-of-executable -pid process-id

You’ll see a prompt (gdb) – all examples are from this prompt. You need not type (gdb) in the examples discussed below. It is the prompt you get when you enter the debugger.

3. Once you have opened gdb, you have many options to proceed further.

a. You could run or execute the code.
To execute, type ‘r’ in front of the gdb prompt as :
(gdb) r
This is not the only way to run.
You can run it directly (r), pass arguments (r arg1 arg2), or feed in a file.

Breakpoints and Watchpoints

b. Set breakpoints before running
Breakpoints are one of the keys to debugging.
They pause (break) a program when it reaches a certain location.
You can examine and change variables, then resume execution.
This is helpful when seeing why certain inputs fails,or testing inputs.
break line_number

c. Set Watchpoints before running.
Set a watchpoint, which pauses the program when a condition changes.
Watchpoints are great for certain inputs without having to break on function call.
For Example : watch x == 3
This would pause the program when x == 3 changes.

Stepping Through Code

Stepping lets you trace the path of your program, and zero in on the code that is crashing or returning invalid input.

After being paused by a breakpoint/watchpoint, you can do one of the following :

    i. continue
    Resume execution after being paused by a breakpoint/watchpoint.
    The program will continue until it hits the next breakpoint/watchpoint.
    Use: (gdb) c or (gdb) continue.

    ii. next
    Run program until next line, then pause.
    If the current line is a function, execute the entire function, then pause.
    Next is good for walking through your code quickly.
    Use: (gdb) n or (gdb) next

    iii. step
    Run the next instruction, not line.
    If the current instructions is setting a variable, it is the same as next.
    If it’s a function,it will jump into the function,execute the first statement,then,pause.
    Step is good for diving into the details of your code.
    Use: (gdb) s or (gdb) step

    iv. finish
    Finish executing the current function, then pause (also called step out).
    Useful if you accidentally stepped into a function.
    Use: (gdb) function

Setting Variables and Calling Functions

Viewing and changing variables at run-time is a huge part of debugging. Try giving functions invalid inputs or running other test cases to find the root of problems. Typically, you will view/set variables when the program is paused.

    1. print x
    Print current value of variable x. Being able to use the original variable names is why the (-g) flag is needed; programs compiled regularly have this information removed.
    set x = 3
    set x = y
    //Set x to a set value (3) or to another variable (y)

    2. call myfunction()
    call myotherfunction(x)
    // calls function independently

    3. display x
    undisplay x

    //Constantly display value of variable x, which is shown after every step or pause. Useful if you are constantly checking for a certain value. Use undisplay to remove the constant display.

In this way you can debug your programs and know where the fault lies at.

To Quit From GDB:
(gdb) q


One thought on “Debugging with GDB

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: