1

I have lots of c-structs (containing substructs containing unions containing substructs...)) and would like to print them in a similar way like gcc can do.

So I dont want to do things like described in How do I dump an arbitrary struct in C?, coz thats just a hexdump

and I also dont want to invent some kind of introspection (java/modern c++ like), but just want to use the knowledge gcc has/generates for gdb, and add some knowledge I have (like how to select the right variant of a union)

like proposed in Linux C: Easy & 'pretty' dump/printout of structs (like in gdb) - from source code?.

So my plan is: Take all that nice c-code I have, let gcc work on it and produce some meta-info, which in a second turn gets parsed/analyzed by something, which then is able to interpret/print a piece of memory according to that information.

There is a utility in Linux called pstruct/c2ph, whicht does something similar and seems to be at some level of knowledge which could be modified to do what I want..

So, basically Im looking for a tool which takes a file containing

struct X {
   int a;
   char *b;
 }

and produces a function lile printX (void *p); which then prints something like {a:1, b:"lala"}, if p points to the according X

So is there something out there which can already do that? I have the feeling that pstruct is very close...

Community
  • 1
  • 1
pbhd
  • 4,384
  • 1
  • 20
  • 26
  • If you already have your structs in some parseable format, you can simply use `sed` to generate printing functions for each one of them. Gets nicer if you use C11 generics in them, maybe have on type generic printing function as a result. – Gábor Buella Apr 03 '14 at 23:51
  • Humpfa. I have just c, which of course is parsable, but it is a real c-program with includes and defines and all that, so the utility should be somehow acting in the same class like a compiler does... – pbhd Apr 03 '14 at 23:53
  • Are you thinking of the Perl [`c2ph`](http://perldoc.perl.org/c2ph.html), which was once derived from a precursor program `pstruct`? – Jonathan Leffler Apr 03 '14 at 23:54
  • exactly. I have the feeling it could be modified to do what I need, but maybe there exists somethig like that already... – pbhd Apr 03 '14 at 23:57
  • The `sed` idea still seems fun to me, only one needs to work after the preprocessor, before the compiler. That should really be parsable enough, it wouldn't be really hard to handle typedefs as well, and ignore qualifiers like const, volatile etc.. – Gábor Buella Apr 04 '14 at 00:01
  • I dont want to discourage sed, I love that tool, but I think it can't handle e.g. nesting of structs in a way that is generic enough. – pbhd Apr 04 '14 at 00:04
  • Well, that is sad. Anyways, how about trying to attack it via yacc/bison http://stackoverflow.com/questions/2721071/c-grammar-in-gcc-source-code ; http://www.lysator.liu.se/c/ANSI-C-grammar-y.html This would still need some coding, but you would need to handle only structs, and ignore other stuff. – Gábor Buella Apr 04 '14 at 00:11

1 Answers1

1

The best and most precise approach is to rely on compiler plugin interface, like those provided by clang and gcc. Basically, the compiler will call callbacks supplied by your plugin on each interesting event (such as encountering declarations), giving you the chance to produce the necessary wrappers/metadata.

Alternative approaches include tools like gcc-xml (which will produce an xml representation of your program), swig and whatever tools capable of parsing DWARF debugging output (that's what gdb uses).

oakad
  • 6,945
  • 1
  • 22
  • 31
  • Thanks for that info, I'm currently busy examining gcc's plugin feature, think this can do the trick... – pbhd Apr 05 '14 at 19:53