Skip to content

Gdb's Pretty Print feature for QString

Ernie Pasveer edited this page Nov 25, 2024 · 1 revision

Introduction

There are a few resources for setting up gdb to work with Qt objects. This page is an supplement to the previous Pretty Print page, but only for Qt's QString object.

https://github.com/epasveer/seer/wiki/Gdb's-Pretty-Print-feature

I'll try to collect as many resources and put them here. For now, this page will focus just on QString.

Preparation

Below is a good Pretty Print script that works for Qt5 and Qt6 QString.

import gdb
import itertools
import re
import time

class QStringPrinter:

    def __init__(self, val):
        self.val = val

    def to_string(self):
        ret = ""

        # The QString object may not be initialized yet. In this case 'size' is a bogus value
        # or in case of Qt5, 'd' is an invalid pointer and the following lines might throw memory
        # access error. Hence the try/catch.
        try:
            size = self.val['d']['size']
            if size == 0:
                return ret

            dataAsCharPointer = self.val['d']['ptr'].cast(gdb.lookup_type("char").pointer())

            ret = dataAsCharPointer.string(encoding = 'UTF-16', length = size * 2)

        except Exception as e:
            # swallow the exception and return empty string
            # pass
            return str(e)
        return ret

    def display_hint (self):
        return 'string'

def QStringPrinter_func(val):
    if str(val.type) == 'QString':
        return QStringPrinter(val)

gdb.pretty_printers.append(QStringPrinter_func)

Save this file to "QString_pp.py" somewhere on your system.

Test Program

Here's a simple test program to demonstrate simple QStrings. You can find it in the Seer release.

seer/tests/helloqstring

If you can get it to compile and link, you can debug the program with Seer.

Debugging

Debug the test program with Seer.

$ seergdb -s helloqstring a b c

When Seer comes up, set a breakpoint on 16 and hit "Continue". You should see this.

image

Now enter "str" in the Logger to display the contents of the QString "str". You should see this.

image

A lot of junk.

Debugging with the QString Pretty Printer

In Seer's gdb input field, source the QString_pp.py file. Note, you may need to include a path depending on where you initially put it.

image

Now when you enter "str" in the Logger area, you will see this. Much more comfortable.

image

What to do with lots of Pretty Print scripts?

With Pretty Print scripts, it's easy to end up with lots of them. Think one script per object/class.

Seer has a feature which auto-loads all these Pretty Print scripts. I should mention these can also be loaded in your .gdbinit file. It's your choice.

Seer maintains settings in a file in your home directory. This is for config settings and themes, etc.

$ ls -1 ~/.config/seergdb/
scripts
seergdb.conf

seergdb.conf holds all the settings for Seer. The scripts directory can be created by you and Pretty Print script can be moved there.

$ ls -1 ~/.config/seergdb/scripts/
QString_pp.py

Each time Seer is started, it will source all the scripts in that folder in alphabetical order.