How to view internal smart pointer data inside gdb?

I have a test program as shown below:

#include<memory> #include<iostream> using namespace std; int main() { shared_ptr<int> si(new int(5)); return 0; } 

Debug it:

 (gdb) l 1 #include<memory> 2 #include<iostream> 3 using namespace std; 4 5 int main() 6 { 7 shared_ptr<int> si(new int(5)); 8 return 0; 9 } 10 (gdb) b 8 Breakpoint 1 at 0x400bba: file testshare.cpp, line 8. (gdb) r Starting program: /home/x/cpp/x01/a.out Breakpoint 1, main () at testshare.cpp:8 8 return 0; (gdb) p si $1 = std::shared_ptr (count 1, weak 0) 0x614c20 

It displays only information about the type of the si pointer, but how to get the value stored in it (in this case 5 )? How to check si internal content during debugging?

+5
source share
2 answers

Try the following:

 p *si._M_ptr 

Now this assumes you are using libstdc++.so , given the output for p si .

Alternatively, you can directly use the value 0x614c20 (from your output):

 p {int}0x614c20 

Both should display a value of 5 .

+1
source

but how to get the value stored in it

You will need to overlay the original pointer on the actual pointer type stored in std::shared_ptr . Use whatis to find out what the actual type of pointer is.

 (gdb) p si $8 = std::shared_ptr (count 1, weak 0) 0x614c20 (gdb) whatis si type = std::shared_ptr<int> (gdb) p *(int*)0x614c20 $9 = 5 
0
source

Source: https://habr.com/ru/post/1262154/


All Articles