Thursday, June 9, 2016

How to print std::shared_ptr in GDB

GDB is a very useful debugging tool for C++, especially when your program crashes due to unknown errors. At such case, you could enable core dump and then debug your program with the core dump generated when your program crashes.

One thing to discuss here is how to print std::shared_ptr variables:
by default, if we print a std::shared_ptr variable "msg", we could see the contents that the "msg" is pointing to:
(gdb) print msg
$1 = std::shared_ptr (count 1, weak 0) 0x7f2ba8002740

what we could do instead is to:
(gdb) print (*msg._M_ptr)
which would print the contents


References:
(1) print shared_ptr
http://stackoverflow.com/questions/24917556/how-to-access-target-of-stdtr1shared-ptr-in-gdb
(2) print variables:
http://ftp.gnu.org/old-gnu/Manuals/gdb/html_chapter/gdb_9.html
(3) change call stack frame:
http://www.unknownroad.com/rtfm/gdbtut/gdbstack.html

No comments: