Archive for the 'Programming' Category

Need ldd on windows?

Hey there,

if you might ever need something like ldd on windows to figure out the dependencies on dynamic libraries of some binary, the following link could interest you:

http://www.dependencywalker.com/

GDB command

Hey folks,

I’ve just discovered another neat feature of gdb: command.
I was in a situation where I needed to watch a certain variable on each breakpoint hit.
Generally, this is pretty easy in gdb using its watch-feature. Unfortunately, the variable under consideration was a QString.
Now, we already have some experience with those little brothers. If we only would have a way to call functions on each breakpoint hit…

… and here it is: command:
Once you have a breakpoint, you can assign a command to it, which will be executed each time the breakpoint is hit. For example:

1
2
3
4
5
6
7
8
9
(gdb) b main
Breakpoint 1 at 0x804db90: file main.cpp, line 11.
(gdb) command 1
>p argc
>end
(gdb) r
[Thread debugging using libthread_db enabled]
Breakpoint 1, main (argc=1, argv=0xbffff184) at main.cpp:11
$1 = 1

In the example above, the command p argc is assigned to breakpoint #1, which will simply print the number of arguments. If a QString variable is considered, one would’ve to replace the p with some gdb macro that is able to print a QString. If no breakpoint number is given, gdb uses the number of the last created one. To tell gdb to leave the command mode, just type end

Print QStrings in GDB

If you will ever have to go through the hell of debugging Qt-applications, you might face the problem of printing the contents of QString objects. If you call print on a QString object in gdb, the output will most probably look like the following:

{static null = {<No data fields>}, static shared_null = {ref = {_q_value = 1}, 
alloc = 0, size = 0, data = 0x80e6d0a, clean = 0, simpletext = 0, righttoleft = 0, 
asciiCache = 0, capacity = 0, reserved = 0, array = {0}}, 
static shared_empty = {ref = {_q_value = 2}, alloc = 0, size = 0, data = 0xb734deb2, 
clean = 0, simpletext = 0, righttoleft = 0, asciiCache = 0, capacity = 0, 
reserved = 0, array = {0}}, d = 0x8105138, static codecForCStrings = 0x0}

This is (in most cases) not quite what we want to know about our QString object. Fortunately, GDB can interpret certain routines, so that we can implement a function that prints QStrings. These routines are stored in GDB’s init-file ~/.gdbinit. Unfortunately, the internal data representation of a string has changed in Qt4, such that we most certainly need two versions of the desired function. I will call the functions qprint3 and qprint4, respectively.

Qt3 Version

The Qt3 version was originally posted by David Faure to the KDE maillist in 2001:

define qprint3
    set $i=0
    while $i < $arg0.d->len
	print $arg0.d->unicode[$i++].cl
    end
end

Qt4 Version

define qprint4
    printf "(QString)0x%x (length=%i): \"",&$arg0,$arg0.d->size
    set $i=0
    while $i < $arg0.d->size
        set $c=$arg0.d->data[$i++]
        if $c < 32 || $c > 127
                printf "\\u0x%04x", $c
        else
                printf "%c", (char)$c
        end
    end
    printf "\"\n"
end

Further reading

The KDE source repository has some further pretty neat GDB macros.