Posts Tagged 'trick'

Easily debug unit tests with GDB

A while ago I developed an ANSI C based unittest framework that gives me the same comfort as the Qt’s unit test framework in C++, but small and portable for embedded ANSI C applications.

You can find the framework here on github :

This framework also supports data-driven tests, which means you write one test case and can execute the same test n times with different data sets. When something goes wrong you need to debug this. Therefor you can pass the name of the testcase as commandline argument, which means only this testcase gets executed. If it’s a data-driven test this still gets called n times, so there is a second command line argument which contains the dataset name. Now the testcase gets executed only one time with the data set causing the problem.

This is already quiet cool and worked good. But I’m lazy. I didn’t want to open the correct file in the debugger, set the breakpoint, start the application with the correct arguments and let it run to the problematic line. So now I automated also this.

Now you can simply invoke the test with the option “-g” (like debugging in GCC), nothing more. Then the test will abort on the first error and creates a file called ‘test.gdb’ with all instructions necessary to debug this problem.
Then you start GDB with this file and you are there where you want.

Here is an example of the unittest output:

Unit test output

When you run the cgdb as shown in the output you jump right into the debugger with one breakpoint set at the start of the test case and one breakpoint at the error location. You can decide if you want to step to the error or if you simply hit ‘c’ (continue) to run to the error location. Sometimes it also helps to start history recording before pressing continue, so you can use reverse-stepping if you have missed an important line.

Unit test debugging

The generated test.gdb file which does the magic for you only contains three lines:

break test_strtouint16
break ../src/test/utiltest/strtoint.c:277
run test_strtouint16 padding

This new feature is not yet on github, but I’ll upload it soon.
I think it’s a really useful and simple feature.

It would be cool to see this in other unit test frameworks too in the future.

GIT bash cheats

When you install GIT you should also install the GIT bash completion with it. (On gentoo you just need to set the bash-completion USE flag when emerging git). This makes your life much easier. This can complete git commands like “git stat[TAB]” to “git status”. Additionally it offers the GIT bash function __get_ps1 for your command prompt. You can integrate this into your “PS1” variable to show the current branch of your working dir on your command prompt. That’s just great, isn’t it?

Add this to your ~/.bashrc to activate the GIT bash completion and the new command prompt.

# load git bash completion
source /usr/share/bash-completion/git
# Cool trick to show current git branch in the command prompt in gentoo colors.
export PS1='\[33[01;32m\]\u@\h\[33[01;34m\] \w$(__git_ps1 " (%s)") \$\[33[00m\] '

(Attention: wordpress removes for any reason my “\ 0 3 3” sequences (without space) so that you see only 33 instead.)
If you enter your working dir the prompt will look like this.

gergap@lt_gergap ~/work/helisim (master) $

If you are wondering what this “\[33…]” is for, this is just to set some nice colors for the prompt. You can remove that if you don’t like colors.