33

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?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Nathaniel Flath
  • 15,477
  • 19
  • 69
  • 94
  • Looking at the assembly, wo virtual function calls to the same function are going through different offsets to the vtable, causing a segfault. I'm trying to debug this issue. – Nathaniel Flath May 31 '11 at 18:41
  • Can you post some ambient code? Perhaps it's a bug that we can spot without going through the assembly. – Kerrek SB Jul 29 '11 at 17:11

5 Answers5

49

A more compact solution:

p /a (*(void ***)obj)[0]@10
Jasancos
  • 711
  • 1
  • 5
  • 11
  • 3
    Or even more compact: `x/10a *(void**)obj`, although it'll also print addresses of the method pointers (i.e. `&ptr`). – Ruslan Jan 01 '16 at 16:55
46

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

pnkfelix
  • 3,770
  • 29
  • 45
11

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

Use info vtbl

Klaus
  • 24,205
  • 7
  • 58
  • 113
11
  (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.

Will Bradley
  • 1,733
  • 15
  • 27
5

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
smh
  • 81
  • 1
  • 1