How to access target of std::tr1::shared_ptr in GDB

后端 未结 3 1318
执笔经年
执笔经年 2021-02-14 12:09

How can I access target of a std::tr1::shared_ptr in GDB. This doesn\'t work:

(gdb) p sharedPtr->variableOfTarget

If I try with the pointer

相关标签:
3条回答
  • 2021-02-14 12:30

    Try with

    (gdb) p (*sharedPtr.get())
    

    that function returns the a pointer to the object owned by the smart pointer.

    0 讨论(0)
  • 2021-02-14 12:34

    ptr->get() not always work.

    when i try ptr->get(), gdb complains for: can not resolve method ***:get() to any overloaded instance

    I eventually go to /usr/include/ to find the source code of shared_ptr to see the private member.

    It turns out to be

    ptr._M_ptr

    It works for me. Source code works for everyone.

    0 讨论(0)
  • 2021-02-14 12:48

    Answer first:

    p *frame._M_ptr # frame is the shared_ptr's name
    

    I tried p (*frame.get()), but it didn't work(frame is my shared_ptr name)

    (gdb) p frame
    $4 = std::shared_ptr (count 2, weak 0) 0x2ea3080
    (gdb) p (*frame.get())
    Cannot evaluate function -- may be inlined
    

    then I tried to get what's in this shared_ptr, then I found this

    (gdb) p frame.
    _M_get_deleter  __shared_ptr    operator*       reset           unique          ~shared_ptr     
    _M_ptr          get             operator->      shared_ptr      use_count       
    _M_refcount     operator bool   operator=       swap            ~__shared_ptr   
    

    I used it's _M_ptr field, it worked.

    (gdb) p *frame._M_ptr 
    $5 = {
    ...
    }
    

    I used std::shared_ptr, and gdb 7.6.

    0 讨论(0)
提交回复
热议问题