Printing QStrings in gdb

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!


2 Responses to “Printing QStrings in gdb”

  1. Rob Says:

    1. Where is the kdesdk module?
    2. I have the kde-devel-gdb file contents – what extension do I give it when I create the file?
    3. I don’t have a.gdbinit file. If I make a gdbinit file, what do I call it? You only give what the extension should be.

    Thanks in advance.

  2. randomguy3 Says:

    1. The kdesdk module is where I linked to it. If you want to get a local copy of it, you need to do
    svn checkout

    2. You don’t have to give it any extensions. Linux doesn’t care about extensions in the same way that Windows does.

    3. Similarly, “gdbinit” is not the extension, “.gdbinit” is the whole filename. Just do something like
    echo ‘source ~/kde-devel-gdb’ >> ~/.gdbinit

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: