打印/调试libc++ STL与Xcode/LLDB

Printing/Debugging libc++ STL with Xcode/LLDB

本文关键字:Xcode LLDB libc++ 调试 打印 STL      更新时间:2023-10-16

我试图在Xcode 8中使用LLDB来调试非常基本的STL。我以前可以像这样打印一个向量:

p myvector[0]

查看第一个向量索引中的内容。当我这样做的时候,我得到了这个错误:

error: Couldn't lookup symbols:
  __ZNSt3__16vectorI9my_classNS_9allocatorIS1_EEEixEm

相反,我必须输入这个:

p myvector.__begin_[0]

,以获得任何输出。

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

有没有人能够得到任何有用的输出从LLDB与libc++?

[]std::vector上的操作符方法,因此要打印您想要的表达式,lldb必须能够调用[]方法。这里的问题是,OS X上的STL非常积极地内联它所能内联的一切,而不是浪费空间产生相同函数的行外副本。这对于优化代码来说非常好,但对于调试来说不是很好,因为它使调试器没有[]操作符可调用。这就是你看到的错误信息。

如果您只是想查看这个向量中的元素,您可以使用lldb "STL数据格式化器"来为您完成这项工作。它们知道大多数STL类型是如何布局的,并且可以打印大多数容器类型的元素。例如:

(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)
}

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

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

您甚至可以使用框架变量的-L选项来定位向量的元素,然后您可以强制转换地址以将其传递给其他函数:

(lldb) frame var -L my_vec[1]
0x0000000100100348: (Foo) my_vec[1] = {
0x0000000100100348:   var1 = 10
0x000000010010034c:   var2 = 20
}
(lldb) expr printf("%dn", ((class Foo *) 0x0000000100100348)->var1)
10
(int) $3 = 3
如果你使用的是c++ 11,另一种解决这个问题的方法是:
template class std::vector<MyClass>
代码中的

。这将指示编译器发出用于此专门化的所有模板函数的行外副本。这不是一个很好的通用解决方案,您只希望在调试构建时这样做,但它确实允许您调用这些函数并在复杂的表达式中使用它们。

我也遇到了类似的问题:error: Couldn't lookup symbols:

我的解决方案是在源代码的某个地方显式地使用被质疑的函数。

#include <vector>
template<typename T>
struct Vector : std::vector<T>
{
    Vector(size_t n)
    : std::vector<T>{n}
    {}
    T& operator[](size_t n)
    { return std::vector<T>::operator[](n); }
};
struct XXX
{
    int x;
};
void func()
{
    std::vector<XXX> a{10};
    Vector<XXX> b{10};
    auto x = b[0]; // gcc will produce an assembler code of operator[] for debug purpose
    1;  // as a break point
}

在第1行设置断点;然后运行它

(lldb) p a[0]
error: Couldn't lookup symbols:
  __ZNSt3__16vectorI3XXXNS_9allocatorIS1_EEEixEm
(lldb) p b[0]
(XXX) $0 = (x = 0)

宾果! !函数是否存在于TEXT块中?

(lldb) image lookup -r -n 'XXX.*operator'
1 match found in /Users/xxx/Library/Developer/Xcode/DerivedData/xxx:
        Address: sandbox[0x00000001000011f0] (sandbox.__TEXT.__text + 256)
        Summary: sandbox`Vector<XXX>::operator[](unsigned long) at main.cpp:19

我不确定,但我以前学过这个。在调试阶段,而不是生产阶段。如果我们在模板函数的一行上设置了断点,调试器会怎么做?设置断点,实际上取代一些现有的汇编代码与陷阱或跳转,这里和那里到处模板应用?或者只是在函数中设置一个断点?它是作为模板编写的。所以它应该在生产阶段内联。但是,在调试阶段,该函数不会内联,也不会作为普通函数编写。请不要简单地相信我在这里说的话。请您自己确认。参考gcc, clang,lldb.的文档

MacOS 10.13.6的#include <vector>, Xcode版本9.4.1有一个宏_LIBCPP_INLINE_VISIBILITY:

template <class _Tp, class _Allocator>
inline _LIBCPP_INLINE_VISIBILITY
typename vector<_Tp, _Allocator>::reference
vector<_Tp, _Allocator>::operator[](size_type __n)
{
    _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
    return this->__begin_[__n];
}

_LIBCPP_INLINE_VISIBILITY#include <__config>中定义为:

#define _LIBCPP_INLINE_VISIBILITY __attribute__ ((__visibility__("hidden"), __always_inline__))

这些关键字hidden__always_inline__似乎控制着行为。

当我将inline _LIBCPP_INLINE_VISIBILITY添加到上面的示例解决方案代码时:

    inline _LIBCPP_INLINE_VISIBILITY
    T& operator[](size_t n)
    { return std::vector<T>::operator[](n); }

导致:

(lldb) p b[0]
error: Couldn't lookup symbols:
  __ZN6VectorI3XXXEixEm

我希望能有所帮助,有人能更深入地调查。