使用GDB打印C++ vtable

Nat*_*ath 27 c++ gdb

我正在尝试使用gdb打印对象的vtable; 我找到了

show print vt bl on
Run Code Online (Sandbox Code Playgroud)

设置,但我实际上还不知道如何打印vtable - p*对象仍然不打印出来.

如何打印vtable?

Jas*_*cos 43

更紧凑的解决方案:

p /a (*(void ***)obj)[0]@10
Run Code Online (Sandbox Code Playgroud)

  • 或者甚至更紧凑:`x/10a*(void**)obj`,虽然它也会打印方法指针的地址(即`&ptr`). (3认同)

pnk*_*lix 35

如果您有足够新版本的gdb,您可能需要查看"info vtbl"命令.

我在谷歌搜索这个问题的答案时才注意到这个功能,我注意到大约2012年的gdb邮件列表的帖子,特别是2012年3月的这个帖子:

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


Wil*_*ley 10

  (gdb) set $i = 0
  (gdb) while $i < 10
     >print $i
     >p /a (*(void ***)obj)[$i]
     >set $i = $i + 1
     >end

其中"obj"是您要打印的vtable的对象,10是方法的数量.


Kla*_*aus 9

在实际的 gdb 7.5.1 中,命令不是info vtable!

使用信息 vtbl


小智 5

例如http://en.cppreference.com/w/cpp/language/virtual

不使用“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
Run Code Online (Sandbox Code Playgroud)