开发者

Print C++ vtables using GDB

开发者 https://www.devze.com 2023-03-10 00:15 出处:网络
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 the vtable - p *开发者_运维百科object still doesn\'t prin

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 the vtable - p *开发者_运维百科object still doesn't print it out.

How do I print the vtable?


A more compact solution:

p /a (*(void ***)obj)[0]@10


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


  (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.


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

Use info vtbl


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

精彩评论

暂无评论...
验证码 换一张
取 消