tags:

views:

361

answers:

2

Is there a flag in g++ or tools to dump the member variables of a struct/class? To illustrate, consider source code like this

struct A { virtual void m() {}; };
struct B : public A { int b; virtual void n() = 0; };
struct C : public B { int c1, c2; void o(); };
struct D : public C { virtual void n() {}; A d; };

I want to get something similar to

A:   0 = (vptr)

B:   0 = (vptr)
     4 = b

C:   0 = (vptr)
     4 = b
     8 = c1
    12 = c2

D:   0 = (vptr)
     4 = b
     8 = c1
    12 = c2
    16 = d

(-fdump-class-hierarchy does not work. It only prints the member functions.)

(Assume I don't know the classes A to D, or there are so many classes that I don't want to list them out myself.)

(Specifically, I want to dump the member variables of http://www.opensource.apple.com/source/xnu/xnu-1456.1.26/iokit/IOKit/IOUserClient.h).

+1  A: 

I would use ddd, then display the structs in the data pane. You can expand an individual struct that points to another struct and the UI will follow from one to the other. A very powerful debugging tool.

jfawcett
Thanks. But that requires me to list all the structs. Furthermore, some structs are unfortunately abstract so I can't `new` them.
KennyTM
+4  A: 

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.

Kornel Kisielewicz
Thanks! `gcc-xml` works for me, although I still need to write a not-so-small script to convert it back to the form I need. (http://networkpx.googlecode.com/svn/etc/print-struct-members.py in case anyone interested.)
KennyTM