Dump class/struct member variables in g++

不打扰是莪最后的温柔 提交于 2019-12-18 12:19:14

问题


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.



来源:https://stackoverflow.com/questions/2123823/dump-class-struct-member-variables-in-g

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!