Skip to content

Instantly share code, notes, and snippets.

@GavinRay97
Last active February 17, 2024 05:06
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save GavinRay97/700ff1631d7e5ac460efd0780759c908 to your computer and use it in GitHub Desktop.
Save GavinRay97/700ff1631d7e5ac460efd0780759c908 to your computer and use it in GitHub Desktop.
Dump C/C++ vtable & record layout information (clang + msvc + gcc)
# Requirements:
# clang - The classes/structs you want to dump must be used in code at least once, not just defined.
# MSVC - The classes/structs you want to dump must have "MEOW" in the name for "reportSingleClass" to work.
# Usage:
# $ make dump_vtables file=test.cpp
dump_vtables:
clang -cc1 -fdump-record-layouts -emit-llvm $(file) > clang-vtable-layout-$(file).txt
clang -cc1 -fdump-vtable-layouts -emit-llvm $(file) > clang-record-layout-$(file).txt
g++ -fdump-lang-class=$(file).txt $(file)
cl.exe $(file) /d1reportSingleClassLayoutMEOW > msvc-single-class-vtable-layout-$(file).txt
cl.exe $(file) /d1reportAllClassLayout > msvc-all-class-vtable-layout-$(file).txt
@jemmy512
Copy link

Hi, how to read the outputs of -fdump-record-layouts and -fdump-vtable-layouts -emit-llvm? I don't find any official doc about these two commands.

@GavinRay97
Copy link
Author

Heya,

On the lefthand side are the offsets from the start of the struct for each field, and on the right is the description of the field itself.
At the bottom is summary information:

*** Dumping AST Record Layout
   0 | class StarObject
   0 |   class SkyObject (primary base)
   0 |     class SkyPoint (primary base)
   0 |       (SkyPoint vtable pointer)
   0 |       (SkyPoint vftable pointer)
  16 |       long double lastPrecessJD
  32 |       class dms RA0
  32 |         double D
     |       [sizeof=8, dsize=8, align=8
     |        nvsize=8, nvalign=8]
  ...(snipped)...
 184 |   float B
 188 |   float V
     | [sizeof=192, dsize=192, align=16
     |  nvsize=192, nvalign=16]

One other useful bit of information is there is another form of this flag, -fdump-record-layouts-simple, which gives output like below:

Layout: <ASTRecordLayout
  Size:64
  DataSize:64
  Alignment:32
  FieldOffsets: [0, 32]>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment