Is there any g++ option to dump class layout and vtables?
g++ -fdump-class-hierarchy -c source_file.cpp
Visual C++ Compiler Option To Dump Class Hierarchy
try
cl.exe /d1reportAllClassLayout test.cpp
The output is something like:
class request_handlerAttribute size(8):
+---
0 | name
4 | sdl
+---
class perfmonAttribute size(8):
+---
0 | name
4 | register
| (size=3)
+---
Found doing:
+ findstr /i class c1xx.dll > c1xx.txt
+ and then manually inspecting c1xx.txt
Hope it can help,
Benedetto
PS: This obviously is an undocumented and unsupported switch.
Look also here for a similar switch.
deciphering vtable dumps
I'm not 100% sure that this answer is correct, but here's my best guess.
When you have a class that inherits multiply and non-virtually, the layout of the class is usually a complete object of the first base type, then a complete object of the second base type, then the data for the object itself. If you look at B, you can see the vtable pointer for the A object, and if you look at C you can see that there's pointers into the vtable for both the A and B objects.
Because the objects are laid out this way, it means that if you have a B*
pointer pointing at a C
object, the pointer will actually not be at the base of the object; rather it will be pointing somewhere in the middle. This means that if you ever need to cast the object to an A*
, you'll need to adjust the B*
pointer some amount to skip it back to the start of the object. In order to do this, the compiler needs to encode somewhere the number of bytes you need to skip back to get to the start of the object. I think that the very first (int(*)(...))
is actually just a raw number of bytes you need to look at to get to the start of the object. If you'll notice, for the A
vtable this pointer is 0 (since the vtable for A is at the start of the object, and the same is true for the B
vtable (since it also lives at the start of the object. However, notice that the C
vtable has two parts - the first part is the vtable for A
, and its first crazy entry is zero as well (since if you're at the A
vtable, you don't need to do any adjustments). However, after the first half of this table is what appears to be the B
vtable, and notice that its first entry is hex value -0x10
. If you look at the C
object layout, you'll notice that the B
vtable pointer is 16 bytes after the A
vtable pointer. This -0x10
value might be the corrective offset you need to skip back over the B
vtable pointer to get back to the root of the object.
The second crazy entry of each vtable seems to be a pointer to the vtable itself. Notice that it's always equal to the address of the vtable object (compare the name of the vtable and what it's pointing at). This would be necessary if you wanted to do any sort of runtime type identification, since that usually involves looking at the address of the vtable (or at least something near the front of it).
Finally, as for why there's the cryptically-named setInt and getInt functions at the end of the C
vtable, I'm pretty sure that's because the C
type inherits two different sets of functions named setInt
and getInt
- one through A
and one through B
. If I had to guess, the mangling here is to ensure that the compiler internals can differentiate between the two virtual functions.
Hope this helps!
Dumping memory layout of C++ object does not work in clang
You need to make two changes:
- Add the
-emit-llvm
compiler switch. Without this, LLVM output is not required, so record layouts are never computed and so not dumped. - Use the classes in your code. If the classes are never used, no code generation is done for them, and their layouts are not dumped.
With these changes made, output like this is printed on stdout:
*** Dumping AST Record Layout
Type: struct Base
Layout: <ASTRecordLayout
Size:32
DataSize:32
Alignment:32
FieldOffsets: [0]>
*** Dumping AST Record Layout
Type: struct Derived
Layout: <ASTRecordLayout
Size:64
DataSize:64
Alignment:32
FieldOffsets: [32]>
...
Layout: <CGRecordLayout
LLVMType:%struct.Base = type { i32 }
NonVirtualBaseLLVMType:%struct.Base = type { i32 }
IsZeroInitializable:1
BitFields:[
]>
Layout: <CGRecordLayout
LLVMType:%struct.Derived = type { %struct.Base, i32 }
NonVirtualBaseLLVMType:%struct.Derived = type { %struct.Base, i32 }
IsZeroInitializable:1
BitFields:[
]>
How to display the VTABLE of a C++ class through GCC?
If the input file is say layout.cpp
, the command gcc -fdump-class-hierarchy layout.cpp
will produce a file layout.cpp.class
. This file will display the VTABLE along with some other useful information.
Dump class/struct member variables in g++
Use the right tool for the right job. g++ isn't much of a hierarchy viewing tool.
You can always use a external tool like doxygen, that can dump graphviz diagrams.
For power-solutions there is gcc-xml, that can dump your whole program into an xml file that you can parse at will.
Related Topics
Is Rebasing Dlls (Or Providing an Appropriate Default Load Address) Worth the Trouble
How to Asynchronously Copy Memory from the Host to the Device Using Thrust and Cuda Streams
Clean Up Your #Include Statements
Resolving a Circular Dependency Between Template Classes
Constructor Conditionally Marked Explicit
Memset for Initialization in C++
Is C++ Considered Weakly Typed? Why
Is There a Reason to Use Enum to Define a Single Constant in C++ Code
Decltype, Result_Of, or Typeof
How to Execute a Command and Get Return Code Stdout and Stderr of Command in C++
Understand Op Registration and Kernel Linking in Tensorflow
Concatenate Compile-Time Strings in a Template at Compile Time
Getprocaddress Function in C++
Converting Cv::Mat for Tesseract
What Can a 'Const' Method Change
How to Define Template Function Within Template Class in *.Inl File