Archive for May, 2008

Printing QStrings in gdb

12th May 2008

I’m still learning the power of gdb. It’s an immensely useful tool for debugging programs (funny, that…).

Today I wanted to find out the contents of a QString. “print s”, where “s” is a QString, doesn’t do much for you, though. The data is stored as an array of ushorts inside a shared data member. But all I want is to print out the string as it would appear if I did “qDebug() << s”, for example.

Solution: head over to the kdesdk module, and into the scripts directory, where mountains of useful bits reside. The script you want is kde-devel-gdb.

Now copy this to your home directory and put the line “source ~/kde-devel-gdb” into your ~/.gdbinit file. Or, if you’re building KDE in the manner prescribed on techbase, you can just put “source /home/kde-devel/src/KDE/kdesdk/scripts/kde-devel-gdb” into your ~/.gdbinit file.

To use, check out “help user-defined”.  For example, to print a QString called s, type “printq4string s”.  Ta-da!