HowTo: Debugging with GDB

Since some month I am working with MMORPG emulators again, written in C++. Because of this I needed a powerful debugging tool and so I chose GDB.
I will show you some nice-to-knows for debugging with GDB on a simple code sample.

I recommend at least basic knowledge of C++, Linux and debugging to understand most of the things explained in the following part.

Assume we have this small C++ code block:

int MyDivide(int, int);

int main()
    int x, y, result;
    x = 10, y = 2;
    result = MyDivide(x, y);
    x = 4; y = 0;
    result = MyDivide(x, y); // Division by zero
    return result;       

int MyDivide(int _x, int _y)
    return _x / _y;

Now put this in a file, called “divide.cpp”, and compile it to a binary, called “divide”:

$ g++ -g divide.cpp -o divide

After doing this, let us start with the GDB magic and load the “divide” binary:

$ gdb divide 

Everything is prepared, so here are some useful things you can do with GDB:

  1. Start application:
  2. (gdb) run
    Starting program: /home/dennis/Entwicklung/Misc/divide 
    Program received signal SIGFPE, Arithmetic exception.
    0x0000000000400513 in MyDivide (_x=4, _y=0) at divide.cpp:15
    15          return _x / _y;

    In our case the startup of the application results in a crash, due to division by zero.

  3. Backtraces:
  4. Because of the crash, we now can get more detailed information with the backtrace.

    (gdb) bt
    #0  0x0000000000400513 in MyDivide (_x=4, _y=0) at divide.cpp:15
    #1  0x00000000004004f9 in main () at divide.cpp:9
    (gdb) bt full
    #0  0x0000000000400513 in MyDivide (_x=4, _y=0) at divide.cpp:15
    No locals.
    #1  0x00000000004004f9 in main () at divide.cpp:9
            x = 4
            y = 0
            result = 5
  5. Breakpoints:
  6. (gdb) break divide.cpp:7
    Breakpoint 1 at 0x4004ca: file divide.cpp, line 7.
    (gdb) break divide.cpp:9
    Breakpoint 2 at 0x4004ea: file divide.cpp, line 9.
  7. Step debugging:
  8. (gdb) run
    Starting program: /home/dennis/Entwicklung/Misc/divide 
    Breakpoint 1, main () at divide.cpp:7
    7           result = MyDivide(x, y);
    (gdb) next
    8           x = 4; y = 0;
    (gdb) continue
    Breakpoint 2, main () at divide.cpp:9
    9           result = MyDivide(x, y); // Division by zero

    With the “next” command, GDB will go to the next line, which is executed by the application. As opposed to this the “continue” command will simply continue the application.

  9. Variable manipulation:
  10. (gdb) print y
    $1 = 0
    (gdb) set y = 1
    (gdb) print y
    $2 = 1
  11. Exiting application:
  12. (gdb) quit

I hope this tutorial helps you to work with GDB and to get some personal achievements with debugging your applications.

If you have feedback, regards, corrections or questions please let me know and do not hesitate to comment!

2 thoughts on “HowTo: Debugging with GDB

Leave a Reply

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



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>