如何在Visual C ++中检查vtable? [英] How can one inspect a vtable in Visual C++?

查看:217
本文介绍了如何在Visual C ++中检查vtable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一个继承了一个复杂的代码库(在Visual C + +,假设2003年或更高版本)与一个大而复杂的继承图。假设它很深,并且有很多虚拟函数,甚至可能有多个继承。 (是的,一点维修的噩梦)。任何试图将这个类层次结构重构为某些saner将需要知道每个类使用的每个虚函数的实现。

Suppose one had inherited a complex codebase (in Visual C++, assume 2003 or perhaps later) with a large and complex inheritance graph. Suppose it's deep, and there's lots of virtual functions and possibly even multiple inheritance as well. (Yes, a bit of a maintenance nightmare). Any attempt to refactor this class hierarchy into something saner will need to know which implementation of each virtual function each class uses.

如果我们采用任意叶类L1 - 从基类B1,从基类B2派生的,等等 - 它将显然有一个类的vtable将显示类似(伪vtable):

If we take an arbitrary leaf class L1 - which derives from base class B1, which derives from base class B2, etc. - it will clearly have a vtable for the class which will show something like (pseudo-vtable):

L1::F1
B3::F2
B1::F3
L1::F4
etc.

...取决于哪些虚拟函数被哪个类覆盖。

...depending on exactly which virtual functions have been overridden by which class.

怎么能看到这样的vtable在一个很像这样的形式?通过阅读代码可以手动重建它,但是这是容易出错和费力的。也许,在调试器中打入类的一个对象可以允许你通过vtable指针检查一个Watch窗口中的vtable,但这是一个尴尬的解决方案,特别是如果你也想看到vtables为L2, L3,... LN。

How could one see such a vtable in a form much like that? It would be possible to reconstruct it by hand by reading through the code, but that's error-prone and laborious. Presumably also, breaking into an object of the class in the debugger could allow you to inspect the vtable in a Watch window via the vtable pointer for that one class, but that's an awkward solution particularly if you want to also see the vtables for L2, L3, ... LN.

DbgHelp.dll是否提供以编程方式检查vtables的设施(允许以任何形式输出)?或者有其他方法吗?

Does DbgHelp.dll provide facilities to inspect the vtables programmatically (allowing output in whatever form is required)? Or is there some other method?

推荐答案

使用Visual Studio 2005有两个未记录的标志,它们是 reportAllClassLayout reportSingleClassLayout 标记。例如,在cl.exe命令行上尝试/ d1 reportAllClassLayout。它将向您显示包括虚拟表格的完整类布局,以下是示例。另请参阅 http://blogs.msdn.com/vcblog/archive/2007/05/17/diagnosing-hidden-odr-violations-in-visual-c-and-fixing-lnk2022.aspx 没有太关于这些标志的许多信息,因为他们现在没有记录,但也许微软将正式支持他们在未来的视觉工作室版本。

With Visual Studio 2005 there are two undocumented flags that does exactly what you need. They are the reportAllClassLayout and reportSingleClassLayout flags. For example try "/d1 reportAllClassLayout" on the cl.exe commandline. It will show you the full class layout including virtual tables, here's an Example. Also see http://blogs.msdn.com/vcblog/archive/2007/05/17/diagnosing-hidden-odr-violations-in-visual-c-and-fixing-lnk2022.aspx There isn't too much information on these flags because they are undocumented for now but maybe Microsoft will officially support them in future versions of visual studio.

另一种方法,以使用 IDA Pro 交互式反汇编程序。有一个巨大的学习曲线,但IDA是聪明的,以帮助您构建VTables,并链接到你的类。它用于反向工程二进制文件,你没有传统的符号,但它确实使用visual studio pdb文件。这样,您会看到完全 所有的vtables。哪些虚拟表被用于什么方法,或什么方法被覆盖,所有同时遍历代码。换句话说,你实际上在运行时调试期间看到你的方法调用被跟踪到vtable 。像VS调试器这样的典型调试器不会跟踪到您注意到的虚拟表。

Another approach, and actually what I prefer, is to use the IDA Pro Interactive disassembler. There is a huge learning curve, but IDA is smart enough to help you build VTables and links them to your classes. It's used to reverse engineer binaries that you don't have symbols for traditionally but it does use visual studio pdb files too. Doing so you will see exactly what all your vtables look like. Which virtual tables are being used for what methods, or what methods are being overridden, all whilst stepping through the code. In other words you actually see your method calls being traced into the vtable during runtime debugging. Typical debuggers like VS debugger don't trace into virtual tables as you've noticed.

这篇关于如何在Visual C ++中检查vtable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆