Basically, I want to get typeid(*this).name()
, i.e. the real type of this
.
I want to get this in GDB (without modifying the source code). I
This question may be related: vtable in polymorphic class of C++ using gdb:
(gdb) help set print object
Set printing of object's derived type based on vtable info.
It's not exactly typeid() but it should show the real object type when inspecting a polymorphic pointer (e.g. this
in a base class). Naturally works only for classes with a vtable (i.e. at least one virtual method) but so does typeid
.
Use ptype
command, like this:
(gdb) ptype 42
type = int
The 'ptype [ARG]' command will print the type.
As @Star Brilliant says here, this:
ptype my_var
returns things like type = unsigned short
, but I want it to return type = uint16_t
instead, so I can truly know how many bytes it is when inspecting memory. The best I can figure out to get this effect is to do:
print &my_var
which prints (uint16_t *) 0x7ffffffefc2c
, thereby revealing that its pointer type is uint16_t*
, meaning its type is uint16_t
.
I find this to be more-useful than ptype my_var
, but a more direct way to get this effect is desired in case you have any suggestions.
Sample gdb commands and output:
(gdb) ptype my_var
type = unsigned short
(gdb) print &my_var
$27 = (uint16_t *) 0x7ffffffefc2c
Again, notice ptype my_var
reveals it is an unsigned short
, whereas print &my_var
reveals the more-detailed and desired answer which is that it is a uint16_t
.