Dojo
Dojo

Reputation: 5684

GCC - Print the definition of a struct

Is it possible for a C program to print the definition of a struct? Member names would be great to have, but just an ordered list of data types (and array length in case of arrays) would be sufficient.

I was looking for preprocessor directives to achieve this, but could not find any. Is there a preprocessor directive that can grab an annotated part of code and use it as a #define variable. If so I could initialize some sting variables with to hold the value of the #define variables.

For example a structure like this get printed as-is

struct foo{
int a;
char b;
short arr[6];
}

or like this

struct s{
int m1;
char m2;
short m3[6];
}

The formatting is not important as long as the structure can be recreated from the data. So something like this is also fine:

s{int,char,short[10]}

Just FYI this is a resource constrained ARM based device.

I don't want to manually copy paste the struct code into a print statement. If the struct code changes, and the print statement is not changed, it would yield wrong results.

Upvotes: 2

Views: 1337

Answers (2)

Peter - Reinstate Monica
Peter - Reinstate Monica

Reputation: 16026

There are object file formats which contain the information you need, and allow for programmatic access. One of them is Dwarf. Debuggers typically use this information.

I don't think (but I'm not sure) that it is possible to access the debug information for the running code. This is a difference to programs in languages which run in an elaborate run time system like Java or the .net family. Those can use reflection on themselves.

In programs written in traditionally compiled languages you probably would open an object file -- possibly the running executable itself! -- and examine it programmatically, much like a debugger does. Considering that, it is conceivable that it would be easier to analyze the source code (which in C and C++ must be available for many type definitions in the form of header files). But for non-trivial uses that idea may be deceiving because the compiler has lexed and parsed the source for you and put all the information in neat data structures where they can be easily accessed. This part of the compilation process -- i.e. a considerable part of the front end -- would have to be done by you if you operate on the sources.

Upvotes: 1

Paul Ogilvie
Paul Ogilvie

Reputation: 25286

Why would you want to do that if you already have the source code?

But in general, C compilers don't offer an option to e.g. "list" all the structures it has seen during compilation. The compiler will keep internal symbol tables, but no-one has thought of any need to be able to print these. There also may be hundreds of structs and types in all the source and header files read during the compilation.

Note also that names of types, fields and variables are primarily there to give meaning to the programmer. After compilation, many names will be gone. Only global variables and missing external variables will retain their name in the object file to be able to resolve these during linking, and after linking, neither these names are needed anymore. The compiler has for example translated a->b into the offset of b relatieve to the place in memory where a resides.

Upvotes: 0

Related Questions