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

后端 未结 3 1326
执笔经年
执笔经年 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: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.

提交回复
热议问题