5

When compiling with g++, -fdump-class-hierarchy exports the program's vtables in a (more or less) human-readable format. However, the resulting file only contains information about the vtable but not about the class layout itself. I'd like to get a comprehensive list of the layout of all my program's classes.

clang offers the -cc1 -fdump-record-layouts arguments to achieve this. The MS compiler can be called using -d1reportAllClassLayout. Is there any g++ switch that does this?

Eadilu
  • 91
  • 1
  • 5
  • Clang and g++ produce binary-compatible output, don't they? So if Clang can produce it... – Tristan Brindle Oct 20 '14 at 14:48
  • How does human-readable output describing the class layout differ from the input? Is it padding & alignment info you're after, or something to flatten out the base-class & member subobjects? – Useless Oct 20 '14 at 15:41
  • @Useless The class layout might be spread out over a number of different files, and with multiple and virtual inheritance thrown into the mix, you really do have to have some understanding of the compiler internals to figure it out. – James Kanze Oct 20 '14 at 16:01
  • Related: http://stackoverflow.com/questions/2549618/is-there-any-g-option-to-dump-class-layout-and-vtables?rq=1, http://stackoverflow.com/questions/2123823/dump-class-struct-member-variables-in-g?rq=1 – ecatmur Oct 20 '14 at 16:31
  • @JamesKanze Oh, sure - I was just asking about the motivation, not questioning the utility. – Useless Oct 20 '14 at 21:11
  • @ecatmur I saw those questions before... The former one refers to fdump-class-hierarchy (which does *not* contain the class layout, only the vtables), the latter relies on third party tools like gccxml that are a pain to set up as they aren't using the same parameters as g++ or do not contain the information I need such as the byte offset of field variables. – Eadilu Oct 21 '14 at 09:13
  • @Eadilu it's good practice to mention related questions and explain why their answers don't satisfy your requirements - this helps answerers and other people with similar problems, and makes it less likely that your question will be erroneously closed as a duplicate. – ecatmur Oct 21 '14 at 09:21
  • @TristanBrindle: This seems to be the way to go. I simply haven't worked with clang yet, project setup is gcc. Guess I'll take this as an incentive to finally take a look at clang. – Eadilu Oct 21 '14 at 09:22
  • @Useless It doesn't even have to be human-readable... If I can write something in less than a few days work I'll be happy to take anything machine-readable as well. It just doesn't seem to be that easy for g++. – Eadilu Oct 21 '14 at 09:23
  • @ecatmur I'll keep that in mind and add related questions I came across to my next question, thank you. – Eadilu Oct 21 '14 at 09:25

1 Answers1

1

If the program is compiled with debugging information, you can use pahole to dump the struct and vtable layouts from the debugging information:

g++ -ggdb3 -c program.cpp
pahole program.o

Note that g++ will only generate debugging information for structs that are actually used within the program, so you may have to add some dummy functions to create or otherwise use objects of your struct types.

ecatmur
  • 152,476
  • 27
  • 293
  • 366
  • Good answer... However, I already tried pahole and it has problems extracting the corrent layout for ancestor classes ( http://article.gmane.org/gmane.comp.debugging.dwarves/109 ). Unfortunately, this leads to my example classes consisting mainly of large wholes according to pahole. – Eadilu Oct 21 '14 at 09:20