Printing/Debugging libc++ STL with Xcode/LLDB

后端 未结 2 1308
臣服心动
臣服心动 2020-12-23 02:33

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 whateve

2条回答
  •  忘掉有多难
    2020-12-23 03:19

    The similar problem also happens with me: error: Couldn't lookup symbols:

    My solution is to explicitly use the questioned function somewhere in a source code.

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

    Set a breakpoint on the line of 1; and run it.

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

    Bingo!! Does the function exist in a TEXT block?

    (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::operator[](unsigned long) at main.cpp:19
    

    I am not sure, but I had learned this before. In a debugging stage, instead of production stage. If we set a breakpoint on a line in a function of a template, what would a debugger do? Setting breakpoints, actually replacing some existing assembler code with trap or jump, here and there everywhere the template is applied? Or just setting a single breakpoint in a function? It is written as a template. So it should be inlined in a production stage. In a debugging stage, however, the function is not inlined and written as a normal function. Please do not simply believe what I say here. Please confirm by yourself. Consult documentation of gcc, clang, and lldb.

    #include of MacOS 10.13.6, Xcode Version 9.4.1 has a macro _LIBCPP_INLINE_VISIBILITY:

    template 
    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];
    }
    

    The _LIBCPP_INLINE_VISIBILITY is defined in #include <__config> as:

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

    Such keywords hidden and __always_inline__ seem to control behavior.

    When I added inline _LIBCPP_INLINE_VISIBILITY to the sample solution code above:

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

    resulted in:

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

    I hope that help and somebody look into much more deeply.

提交回复
热议问题