gdb: show typeinfo of some data

前端 未结 4 1351
轻奢々
轻奢々 2020-12-14 05:53

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

相关标签:
4条回答
  • 2020-12-14 06:22

    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.

    0 讨论(0)
  • 2020-12-14 06:25

    Use ptype command, like this:

    (gdb) ptype 42
    type = int
    
    0 讨论(0)
  • 2020-12-14 06:37

    The 'ptype [ARG]' command will print the type.

    0 讨论(0)
  • 2020-12-14 06:43

    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.

    0 讨论(0)
提交回复
热议问题