How can I check a vtable in Visual C ++?

Suppose one inherited a complex code base (in Visual C ++, say 2003 or perhaps later) with a large and complex inheritance schedule. Suppose this is deep, and there are many virtual functions, and possibly even multiple inheritance. (Yes, a bit of a nightmare for maintenance). Any attempt to reorganize this class hierarchy into something more reasonable should know which implementation of each virtual function is used by each class.

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

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

... depending on which virtual functions were overridden by the class.

How can I see such a vtable in this form? It could be restored manually by reading the code, but error-prone and painstaking. Presumably also, by hacking a class object in the debugger, you can check the vtable in the Watch window using the vtable pointer for this one class, but this is an inconvenient solution, especially if you also want to see vtables for L2, L3, ... LN.

Does DbgHelp.dll provide capabilities for checking vtables software products (allowing output in any form)? Or is there some other method?

+4
source share
2 answers

Visual Studio 2005 has two undocumented flags that do exactly what you need. These are the reportAllClassLayout and reportSingleClassLayout flags . For example, try "/ d1 reportAllClassLayout" at the cl.exe command line. It will show you the full class, including virtual tables, here is an example . Also see http://blogs.msdn.com/vcblog/archive/2007/05/17/diagnosing-hidden-odr-violations-in-visual-c-and-fixing-lnk2022.aspx Information about these flags is wrong a lot, because so far they are undocumented, but perhaps Microsoft will officially support them in future versions of the visual studio.

Another approach and, in fact, what I prefer is to use the IDA Pro Interactive Disassembler. There is a huge learning curve, but the IDA is smart enough to help you build VTables and link them to your classes. It used to reverse-process binary files that you don't have characters for traditionally, but it also uses pdb files for visual studio. With this you will see exactly what all your vtables look like. Which virtual tables are used for which methods, or which methods are redefined, go through the code all the time. In other words, you actually see that your method calls are tracked in vtable during debugging at runtime. Typical debuggers, such as the VS debugger, do not trace to virtual tables, as you noticed.

+14
source

I would highly recommend using Doxygen as a tool for any code analysis like this. I don’t think that it has a quick way to find the final override for any function in the type, but it should provide a list of what is inherited and what is implemented so that you can quickly scan the tree to determine any given function (usually your the hierarchy is not large enough to make it a big deal). It can also generate call graphs, relationship graphs, and hyperlinks to source code, which is a fantastic tool.

+1
source

All Articles