Dump class/struct member variables in g++

2020-02-25 08:15发布

问题:

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:

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.



回答2:

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.



标签: c++ gcc g++