9

我试图调试我的小型词法分析器并遇到了这个问题:QtCreator-Debugger 不显示我的 std::string-variable 的任何内容。我尝试在控制台中对其进行调试,得到了相同的结果,只是简单的结构信息。

我几天前使用的QtCreator版本确实显示了字符串的内容。所有其他 STL 元素(如 std::vector、std::map、std::multimap 等)都显示正确的数据,只是 std::string 类不正确。

经过几个小时的谷歌搜索,我发现了很多描述漂亮打印机创建的网页,但我解决这个问题的非常愚蠢的方法并没有帮助。有什么想法可以摆脱这个错误吗?

注意:字符串变量的“内容”将始终显示为“不可访问”。我将 QtCreator 2.6 (QT5) 用于 64 位 Linux 操作系统。

编辑(1):我重新安装了从操作系统到编译器和 IDE 的所有东西......奇怪的是,当我使用优化级别 3(-O3)构建我的项目时,QT 可以显示 std::strings。

命令行如下:clang++ -std=c++11 -O3 -g -c foo.cpp

当我删除 -O3 时,std::strings <不可访问>。有任何想法吗?

4

3 回答 3

10

您可以尝试修复“/usr/share/qtcreator/debugger/stdtypes.py”。由于他们使用“成员位置的硬编码假设”,因此似乎并非在任何地方都有效。就我而言 - Linux x64, gcc 9.1 它的工作方式与您描述的完全一样: 字符串不可访问

所以找函数def qdumpHelper_std__string(d, value, charType, format):

并更改 (size, alloc, refcount) = d.split("ppp", data - 3 * d.ptrSize())(size, alloc, refcount) = d.split("ppp", value.address() + d.ptrSize())

还可以评论d.check(0 <= size and size <= alloc and alloc <= 100*1000*1000)或将其更改为类似

if size > 1000:
   size = 1000

在我的系统上 std::string 有下一个结构

pointer 8 byte
size    8 byte
union   16 byte

并且这个union字段可以改变它的含义取决于字符串的大小。所以我们需要评论那张size < alloc支票。 value.address()- 字符串对象的地址,所以会不时value.address() + d.ptrSize()指向大小,并value.address() + 2 * d.ptrSize()指向包含值的那个联合。alloc size

只需查看您的std::string类声明,您就会了解系统的结构。修复后: 修复了调试器视图

两者都适用 - 当“系统 GDB 漂亮打印机”检查并清除时

于 2019-07-24T16:22:43.030 回答
6

试试这个,它对我有用。
在 Qt Creator 菜单栏中:

Tools -> Options -> Debugger
Uncheck the option (Load system GDB pretty printers)
于 2014-10-15T18:15:49.817 回答
2

To clarify and sum up AstoBasto post: In file: /usr/share/qtcreator/debugger/stdtypes.py replace this function:

def qdumpHelper_std__string(d, value, charType, format):
[...]

With this:

def qdumpHelper_std__string(d, value, charType, format):
if d.isQnxTarget():
    qdumpHelper__std__string__QNX(d, value, charType, format)
    return
if d.isMsvcTarget():
    qdumpHelper__std__string__MSVC(d, value, charType, format)
    return

data = value.extractPointer()
# We can't lookup the std::string::_Rep type without crashing LLDB,
# so hard-code assumption on member position
# struct { size_type _M_length, size_type _M_capacity, int _M_refcount; }

(size, alloc, refcount) = d.split("ppp", value.address() + d.ptrSize())
refcount = refcount & 0xffffffff
d.check(refcount >= -1) # Can be -1 according to docs.

if size > 4002:
    size = 4002
d.putCharArrayHelper(data, size, charType, format)

And this works (at least on Kubuntu 19.10).

于 2019-12-31T18:11:40.430 回答