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

情到浓时终转凉″ 提交于 2019-12-09 16:55:19

问题


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 object itself (p sharedPtr) I get something like this:

$1 = std::tr1::shared_ptr (count 2) 0x13c2060

With a normal pointer I can do p *ptr and get all the data or p ptr->variable for just one variable.

I'm on Centos 6.5, GCC 4.4.7-4.el6 and GDB 7.2-64.el6_5.2.


回答1:


Try with

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

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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/24917556/how-to-access-target-of-stdtr1shared-ptr-in-gdb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!