在 XCode 3.xx GDB 中打印 Qt 数据结构(QList、QString 等)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11174508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:40:43  来源:igfitidea点击:

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

c++xcodeqtgdbqlist

提问by user805547

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

我正在尝试在 XCode 中调试一些 Qt 容器,但从 GDB 返回的结果没有用:

    print l1
 = (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
 = {
  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.

如何从 l1 (QList) 和 strQuery (QString) 获得一些有用的输出?
我已经尝试过使用这个 .gdbinit,它添加了一些像“printq4string”这样的宏,但这些宏使用起来非常痛苦,因为在打印结构时我需要在每个成员变量上手动运行它。

采纳答案by user805547

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
 = {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 '
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
', 0 '##代码##', 0 '##代码##', 0 '##代码##', 0 '##代码##', 0 '##代码##', 0 '##代码##', 0 '##代码##', 14 '6', 0 '##代码##', 0 '##代码##'} (gdb) print/c ((QString*)s.d.array).d.data[0]@20 = {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 '##代码##'}

回答by apouche

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

首先确保您的项目设置为使用 GCC 4.2 编译而不是纯 LLVM,如下所示: 在此处输入图片说明

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

LLVM 现在被设置为 XCode 4 中的默认编译器,它不会为类内部的结构添加正确的调试信息。

Now in your ~/.gdbinit just add :

现在在你的 ~/.gdbinit 中添加:

##代码##

and you can now simply type pqts s1and it will dump your QStringnicely.

你现在可以简单地输入pqts s1,它会QString很好地转储你的。

回答by Nicholas Smith

I generally use p *varName.shdwhich dumps it as a QShared with the string data inside, but I'm not sure if that'll work for QList, definitely does for QString.

我通常使用p *varName.shdwhich 将其转储为带有字符串数据的 QShared,但我不确定这是否适用于 QList,绝对适用于 QString。