Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 681
Pnog is not Pong Avatar asked Jul 23 '14 17:07

Pnog is not Pong


2 Answers

Try with

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

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

like image 193
Raydel Miranda Avatar answered Sep 17 '22 14:09

Raydel Miranda


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.

like image 28
boydc2011 Avatar answered Sep 17 '22 14:09

boydc2011