Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

xcode

gdb

qt

qlist

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.

like image 577
user805547 Avatar asked Jun 24 '12 02:06

user805547


3 Answers

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.

like image 74
apouche Avatar answered Oct 20 '22 05:10

apouche


For strings, p my_string.toStdString().c_str() works

(gdb) p my_string.toStdString().c_str()
$5 = 0x55556d10d7d0 "abc 123 test string"
like image 33
XMB5 Avatar answered Oct 20 '22 04:10

XMB5


I read the source and came up with this suboptimal approach, I leave it to the community to improve on this:

    QString s1("This should be easy");

    QList<QString> s;
    s.push_back("Can you debug me?");

(gdb) print/c s1.d.data[0]@30
$2 = {84 'T', 104 'h', 105 'i', 115 's', 32 ' ', 115 's', 104 'h', 111 'o', 117 'u', 108 'l', 100 'd', 32 ' ', 98 'b', 101 'e', 32 ' ', 101 'e', 97 'a', 115 's', 121 'y', 0 '\0', 0 '\0', 0 '\0', 0 '\0', 0 '\0', 0 '\0', 0 '\0', 0 '\0', 14 '\016', 0 '\0', 0 '\0'}

(gdb) print/c ((QString*)s.d.array).d.data[0]@20
$12 = {67 'C', 97 'a', 110 'n', 32 ' ', 121 'y', 111 'o', 117 'u', 32 ' ', 100 'd', 101 'e', 98 'b', 117 'u', 103 'g', 32 ' ', 109 'm', 101 'e', 63 '?', 0 '\0', 0 '\0', 0 '\0'}
like image 24
user805547 Avatar answered Oct 20 '22 03:10

user805547