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!

Gitlog Parser

Sometimes I needed a possibility to convert the output of the `git log` command in a useful format. Because of that I have written a tool in Perl, which is currently able to convert this output to different file formats.

Here is a short overview of this tool:

  1. Link:

  3. Get it:
  4. # Clone from github
    $ git clone git://

  5. Current supported formats:
    • SQL-Dump
    • RSS-Feed
    • HTML-Page

  6. Documentation:
  7. You can find the documentation in the README file of my github repository.

  8. Sample usage:
  9. # Change to GitParser directory
    $ cd YOURPATH/GitlogParser
    # Show help
    $ ./scripts/ --help
    # Execute script for RSS output
    $ ./scripts/ --repo PATH-TO-GIT-REPO

    The –repo option is the mandatory option to state the git repository. Of course there are other options to customize the output for your purposes. For example you can specify the title of your RSS-Feed with the –title option. If you want to know more about the other options please take a look at the documentation or use the output of the –help option.

  10. Sample output:
  11. RSS

This tool is not finished completely and I will probably implement support for more file formats. If you have suggestions or any feature request please do not hesitate to contact me via github!