How to access target of std::tr1::shared_ptr in GDB
Asked Answered
C

3

12

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.

Collazo answered 23/7, 2014 at 17:55 Comment(6)
Which compiler and stdlib are you using? Also which version of GDB?Parenthood
@Parenthood sorry! Updated my question.Collazo
have you tried (gdb) p sharedPtr.get()?Tomsk
@Raydel Miranda That worked! Thanks very much! I'll add answer in a moment.Collazo
I wasn't able to answer my own question yet, since I have too little reputation. This is what worked: p (*sharedPtr.get()) prints contents of the target object and p (*sharedPtr.get())->variableOfTarget will print a single variable. Funny thing is though that if you leave out the parentheses like this p *sharedPtr.get() it will also advance execution of the program. Can someone explain why is that?Collazo
unique_ptr: #22799101Appertain
T
12

Try with

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

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

Tomsk answered 23/7, 2014 at 18:53 Comment(2)
You have to use asterisk in front to get access to the pointer target and for some reason also parentheses like this: (*sharedPtr.get()). See my comment to the question.Collazo
If I use this solution I get (gdb) p (*self.get()) You can't do that without a process to debug.Watchtower
I
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.

Imperfective answered 22/12, 2014 at 8:20 Comment(3)
You meant ptr.get() right? I think the operator -> is not what you want use here.Tomsk
ptr->get() means *ptr.get().Imperfective
Nop, is not the same, ptr->get() is an attemp to call the get function from whatever the shared_ptr is pointing to, and *ptr.get() is dereferencing that pointer. The . operator has preference here. *ptr.get() is the same that *(ptr.get()).Tomsk
T
12

Try with

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

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

Tomsk answered 23/7, 2014 at 18:53 Comment(2)
You have to use asterisk in front to get access to the pointer target and for some reason also parentheses like this: (*sharedPtr.get()). See my comment to the question.Collazo
If I use this solution I get (gdb) p (*self.get()) You can't do that without a process to debug.Watchtower
D
9

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.

Dodeca answered 28/8, 2019 at 1:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.