需要一个C结构解析器

Lets say I have a file system.h in which I have the following structures

typedef struct Foo {
  int count;
  uint32 world;
  Temp id;
} Foo;

typedef struct Temp {
  uint64 id;
} Temp;

Foo bar;

Now I need a macro, something like DUMP_STRUCT(bar), that prints all the members of bar (of type Foo) recursively.

Is there any parser/script (preferably in php/python) out there that can parse the C file or take information from dwarf from the executable and create a function similar to DUMP_STRUCT?

Seems there is Python bindings for libclang, which should be able to do what you want. Also there's the pycparser project.

If your C header file contains the typical stuff that C header files contains (macros, preprocessor conditionals, includes, bit fields) you'll need a full C parser to process the header file.

GCC-XML might produce what you need (it doesn't do "C" exactly, rather it does C++) in the sense of dumping the various structure slots and their types as XML text; you'll have to massage that in code that walks the data structures. To do that, you'll likely have to match the substructure of the type declarations (as XML subtrees of interesting shape/content).

Our DMS Software Rengineering Toolkit with its C Front End does full preprocess/parse/build symbol tables. You could generate GCC-XML's output from that, but it is easier to simply customize DMS to walk the various symbol tables and ASTs to extract what you want. The matching process is easier because the symbol table is designed for convenient access to the structure of the namespaces, the symbol entries in them, and their type information (already set up as data structures). DMS also provides surface-syntax pattern matching which can be use both to recognize ASTs of interest, and as code-generators to produce code that cannot be syntactically wrong.

You can use GCCXML, it can parse C (with some restrictions) and C++ headers with output in XML.

You may get some mileage from examining how pstruct works. (Its in perl I think, but uses the compilers stab debugging info to output information about the structure layout).