Print C++ vtables using GDB

后端 未结 5 2214
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-02 16:09

I\'m trying to print an object\'s vtable using gdb; I found the

show print vt bl on

setting, but I still don\'t actually know how to print

相关标签:
5条回答
  • 2020-12-02 16:14

    A more compact solution:

    p /a (*(void ***)obj)[0]@10
    
    0 讨论(0)
  • 2020-12-02 16:14

    In the actual gdb 7.5.1 the command is not info vtable!

    Use info vtbl

    0 讨论(0)
  • 2020-12-02 16:16

    If you have a sufficiently new version of gdb, you may want to look at the "info vtbl" command.

    I only noticed the feature when googling for an answer to this question and I noticed posts to the gdb mailing list circa 2012, notably this one from March 2012:

    http://permalink.gmane.org/gmane.comp.gdb.patches/73957

    0 讨论(0)
  • 2020-12-02 16:35

    For the example at http://en.cppreference.com/w/cpp/language/virtual

    Without using 'info vtbl'

    (gdb) p b
    $1 = {_vptr.Base = 0x400a60 <vtable for Base+16>}
    
    (gdb) x/16x 0x400a60
    0x400a60 <_ZTV4Base+16>:    0x0040094c  0x00000000  0x72654437  0x64657669
    
    (gdb) x/16x 0x0040094c
    0x40094c <Base::f()>:   0xe5894855  0x10ec8348  0xf87d8948  0x400a15be
    0x40095c <Base::f()+16>:    0x10c0bf00  0xf9e80060  0xc9fffffd  0x485590c3
    0x40096c <Derived::f()+2>:  0x8348e589  0x894810ec  0x1bbef87d  0xbf00400a
    0x40097c <Derived::f()+18>: 0x006010c0  0xfffddbe8  0x66c3c9ff  0x00841f0f
    
    0 讨论(0)
  • 2020-12-02 16:38
      (gdb) set $i = 0
      (gdb) while $i < 10
         >print $i
         >p /a (*(void ***)obj)[$i]
         >set $i = $i + 1
         >end
    

    Where "obj" is the object whose vtable you'd like to print, and 10 is the number of methods.

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