Qt5 Pretty Printers for GDB

If you’ve ever debugged a Qt program in GDB and been frustrated when attempting to print a QString gives you an unhelpful result like

$1 = {d = 0x6e7c40}

then kde-dev-scripts has some useful tools for you. For some time now, there has been the kde-devel-gdb script, which you can include in your ~/.gdbinit. This gives you commands like “printq4string” and “printq5string” which will dump the actual string contents of a QString.

However, GDB 7 has the ability to add pretty-printers written in Python. This means that you can control how a value is output when you use “print” and when backtraces are printed. So “print someQString” will actually write out the string for you.

The gdb directory of kde-dev-scripts now has some of these pretty-printers for Qt5. Currently, it covers most of the main value classes and collection classes in QtCore (QByteArray, QString, QStringList, QList, QMap, etc). QHash and QSet are the most notable ones that are missing.

To use these pretty-printers, you either need to source the “load-qt5printers.py” file in your GDB session, or use the “create-qt5-autoloaders.py” script to generate files that will allow GDB to auto-load the printers when it loads the Qt5 libraries.

I plan to eventually submit these to Qt, so that pretty-printing of Qt value classes will work out of the box. For now, do let me know if you run into any issues with them (don’t forget to compile your application with debugging symbols!), and feel free to extend what’s there, or use it as a template for some Qt4 pretty-printers.

About these ads

Tags: , , , ,

10 Responses to “Qt5 Pretty Printers for GDB”

  1. Harald Says:

    Thank you thank you! I’ve really been waiting for someone to pick up these printers again, missing them deeply since moving to qt5. I have a problem though, if I try to print a QStringList I get:

    (gdb) p codes
    $1 = Python Exception unorderable types: int() > gdb.Type():

  2. Milian Wolff Says:

    You are aware that there are GDB printers for that already in KDevelop? Please contribute to those.

    https://projects.kde.org/projects/extragear/kdevelop/kdevelop/repository/revisions/master/show/debuggers/gdb/printers

    • randomguy3 Says:

      I wasn’t, as I don’t use KDevelop. From what people have said to me, I certainly wasn’t the only one that didn’t realise that these scripts were available there (and even if I had, the filename suggest they don’t work for Qt5, although from reading the code, it at least attempts to support Qt5, although that support is not entirely correct).

      I’ll submit improvements to those scripts and note their existence in kde-dev-scripts. However, I still want to push them upstream to Qt itself, so I’m going to keep my separate Qt5-only ones as well.

  3. Milian Wolff Says:

    See: http://lmgtfy.com/?q=qt+gdb+pretty+printers

    Anyhow, see esp. the mailing list thread above about the inclusion of these pretty printers. I’d also love to get that done but wasn’t met by much help from the Qt side before. I’d welcome if we could merge the effort and get it all upstreamed in a good way. We only ship the stuff with KDevelop b/c it’s not upstream, not because we want to ship it.

    • randomguy3 Says:

      Yeah, for some reason I only read the first couple of hits last time. But the ones I’ve written have the advantage of not having to hunt down a whole load of contributors to sort out licensing before submitting upstream.

      I’ve got a handful more to implement (QHash, QSet, QUrl, maybe the date/time ones) before it’s in a position where I can try merging it into the Qt5 code base.

  4. Milian Wolff Says:

    Its 10 committers according to git log, only 3 in the copyright headers. Also, there is the relicensecheck.pl in kde-dev-scripts…

    Really, that should never be a reason to reinvent the wheel.

    • randomguy3 Says:

      Well, no, and if I’d spotted it first, I’d have adapted the KDevelop ones. But it’s a silver lining.

      Also, relicensecheck.pl doesn’t actually help, because it doesn’t record any licenses that are acceptable for Qt submissions.

  5. Kevin Funk Says:

    Regarding upstreaming: QtCreator obviously already supports all of these. So I strongly advise you to coordinate a bit with Andre Poenitz from QtCreator fame in case you plan to improve the GDB pretty-printers for Qt (even before even trying to send a change-request) — he is probably the best contact regarding GDB pretty-printers. I still don’t really fully understand if the pretty-printers contained in qt-creator.git are fully usable in the GDB CLI, probably worth asking, too.

  6. Diana Says:

    Thanks a ton! My final .gdbinit file was:

    python
    import sys
    sys.path.insert(0, ‘/path_to_downloaded_gdb_dir/gdb/qt5printers’)
    end
    source /path_to_downloaded_gdb_dir/gdb/load-qt5printers.py

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: