使用Xcode/LLDB打印/调试libc ++ STL [英] Printing/Debugging libc++ STL with Xcode/LLDB

查看:99
本文介绍了使用Xcode/LLDB打印/调试libc ++ STL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Xcode 8中使用LLDB调试非常基本的STL.我曾经能够打印出这样的向量:

I'm trying to use LLDB within Xcode 8 to debug very basic STL. I used to be able to print a vector like this:

p myvector[0]

查看第一个向量索引中的内容.现在,当我这样做时,我会收到此错误:

to see whatever was in the first vector index. Now when I do that, I get this error:

error: Couldn't lookup symbols:
  __ZNSt3__16vectorI9my_classNS_9allocatorIS1_EEEixEm

相反,我必须输入以下内容:

Instead, I have to type this:

p myvector.__begin_[0]

以获取任何输出.

我尝试从LLDB svn存储库导入libcxx.py和unordered_multi.py脚本,但这似乎并没有改变.

I tried importing the libcxx.py and unordered_multi.py scripts from the LLDB svn repository but that doesn't seem to change anything.

有人能够使用libc ++从LLDB获得任何有用的输出吗?

Has anyone been able to get any useful output from LLDB with libc++?

推荐答案

[]std::vector上的运算符方法,因此要打印所需的表达式,lldb必须能够调用[]方法.这里的问题是OS X上的STL积极地内联所有内容,而不浪费空间来生产相同功能的行副本.这对优化代码非常有用,但对调试却不太好,因为它使调试器没有[]运算符可以调用.那就是您所看到的错误消息.

[] is an operator method on std::vector, so to print the expression you want, lldb would have to be able to call the [] method. The problem here is that the STL on OS X is aggressive about inlining everything it can, and not wasting space producing out of line copies of the same functions. That's great for optimized code, but not so good for debugging because it leaves the debugger with no [] operator to call. That's the error message you are seeing.

如果您只想查看此向量中的元素,则可以使用lldb "STL数据格式化程序" 为您完成此工作.他们知道如何布置大多数STL类型,并且可以打印大多数容器类型的元素.例如:

If you just want to see the elements in this vector, you can use the lldb "STL data formatters" to do this work for you. They know how most STL types are laid out, and can print the elements of most container types. For instance:

(lldb) expr my_vec[0]
error: Couldn't lookup symbols:
  __ZNSt3__16vectorI3FooNS_9allocatorIS1_EEEixEm

但是:

(lldb) expr my_vec
(std::__1::vector<Foo, std::__1::allocator<Foo> >) $0 = size=2 {
  [0] = (var1 = 10, var2 = 20)
  [1] = (var1 = 10, var2 = 20)
}

还有另一个命令框架变量" ,它可以检查静态对象,并与数据格式化程序挂钩.它不能调用函数并执行其他更复杂的表达式解析器任务,但是它知道如何使用STL数据格式化程序来检索单个元素:

There is also another command "frame variable" which can inspect static objects, and hooks into the data formatters. It can't call functions and do other more complex expression parser tasks, but it does know how to use the STL data formatters to retrieve individual elements:

(lldb) frame var my_vec[1]
(Foo) my_vec[1] = (var1 = 10, var2 = 20)

您甚至可以使用frame var的-L选项来定位矢量的元素,然后可以转换地址以将其传递给其他函数:

You can even use frame var's -L option to locate the elements of the vector, and then you can cast the address to pass it to other functions:

(lldb) frame var -L my_vec[1]
0x0000000100100348: (Foo) my_vec[1] = {
0x0000000100100348:   var1 = 10
0x000000010010034c:   var2 = 20
}
(lldb) expr printf("%d\n", ((class Foo *) 0x0000000100100348)->var1)
10
(int) $3 = 3

解决此问题的另一种方法(如果使用的是C ++ 11)是:

Another way to work around this for debugging - if you are using C++11 - is by putting:

template class std::vector<MyClass>

在您的代码中的某处.这将指示编译器为该专业化发出所有模板函数的离线副本.那不是一个很好的通用解决方案,您只想在调试版本中使用它,但是它确实允许您调用这些函数并在复杂的表达式中使用它们.

in your code somewhere. That will instruct the compiler to emit out-of-line copies of all the template functions for this specialization. That isn't a great general solution, and you only want to do it for debug builds, but it does let you call these functions and use them in complex expressions.

这篇关于使用Xcode/LLDB打印/调试libc ++ STL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆