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

(1) print shared_ptr
(2) print variables:
(3) change call stack frame:

No comments: