使用 GDB 打印 C++ vtables

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6191678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:39:56  来源:igfitidea点击:

Print C++ vtables using GDB

c++gdb

提问by Nathaniel Flath

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

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

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.

设置,但我实际上仍然不知道如何打印 vtable - p *object 仍然没有打印出来。

How do I print the vtable?

如何打印 vtable?

采纳答案by Will Bradley

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

其中“obj”是您要打印其 vtable 的对象,10 是方法的数量。

回答by Jasancos

A more compact solution:

更紧凑的解决方案:

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

回答by pnkfelix

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

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

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:

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

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

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

回答by Klaus

In the actual gdb 7.5.1 the command is notinfo vtable!

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

Use info vtbl

使用信息 vtbl

回答by smh

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

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

Without using 'info vtbl'

不使用'info vtbl'

(gdb) p b
 = {_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