Printing Qt data structures (QList, QString, etc.) in XCode 3.xx GDB

user805547 picture user805547 · Jun 24, 2012 · Viewed 7.8k times · Source

I am trying to debug some Qt containers in XCode and the results I get back from GDB are not useful:

    print l1
$1 = (QSharedPointer<QList<SNAPSHOT> > &) @0x102780650: {
  <QtSharedPointer::ExternalRefCount<QList<SNAPSHOT> >> = {
    <QtSharedPointer::Basic<QList<SNAPSHOT> >> = {
      value = 0x1161e47e0
    }, 
    members of QtSharedPointer::ExternalRefCount<QList<SNAPSHOT> >: 
    d = 0x1161ace00
  }, <No data fields>}
Current language:  auto; currently c++
(gdb) print strQuery
$2 = {
  d = 0x1161e2890

How do I get some useful out put from l1 (QList) and strQuery (QString)?
I've already tried using this .gdbinit which adds some macros like "printq4string" but those are quite painful to use as when printing out structs i need to manually run this on each member variable.

Answer

apouche picture apouche · Jul 7, 2012

Ok this drove me nuts but I got it.

First make sure your project is set to compile with GCC 4.2 and not pure LLVM as shown in : enter image description here

LLVM is now set as the default compiler in XCode 4 and it doesn't add correct debugging information for struct inside classes.

Now in your ~/.gdbinit just add :

define pqts
    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

and you can now simply type pqts s1 and it will dump your QString nicely.