Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dump class/struct member variables in g++

Tags:

c++

gcc

g++

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).

like image 991
kennytm Avatar asked Jan 23 '10 16:01

kennytm


2 Answers

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.

like image 155
Kornel Kisielewicz Avatar answered Sep 19 '22 08:09

Kornel Kisielewicz


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.

like image 23
jfawcett Avatar answered Sep 19 '22 08:09

jfawcett