Frage

I need a way to have gdb like output for a pointer to a structure which shows all the fields and its addresses and the corresponding values. Is there a clean way to do this instead of printing it member by member. I cannot use any other external tool like pstruct or libgdb. I want to do it from within our C program. I do have gdb on the system and can use it. Thanks for your time.

War es hilfreich?

Lösung

Your options are:

a) Read the debug symbols from binary and interpret them in the same way as GDB does to determine what the structure members are, and pass that info to a generalized printing routine.

b) Compile metadata about your structures into your C program, either manually or with a custom build stage.

c) Print member by member.

Andere Tipps

You can write a function to do that?

void printStructPtr( struct name *p)
{
printf("{ ");

//print all members one by one separated by commas
printf("...", p->member1);
printf("...", p->member2);
printf("}");

}

If you want it to be generic then pass a receive a void pointer and cast it appropriately.

If you can link to a 3rd party library and build your application with the appropriate debugging symbols (gdb is quite flexible in that regard but you may be constrained in some other way) you could try out pulling the information you need from the debugging symbols.

libdwarf might help you out.

The specs for dwarf (and stabs/stabs+) are about on the web, but that's quite a lot of work from scratch.

You could perhaps customize the GCC compiler (e.g. with the MELT domain specific language to extend GCC) to add some new __attribute__ which would generate the printing code from the types (probably annotated). But such an approach would take several days of work, and I won't recommend it in general, unless you have a really big program to work on.

Notice that your problem is more complex than what you think at first: a pointer inside a struct may have to be printed and followed (or to be left out, if it is only for house-keeping). And complex data structures have shared components so you need to manage the set of already visited data. Notice also that printing union-s is in general not possible, unless you have some discriminator (and conventions about it, which are outside of the C declarations).

I would rather use (or develop) a tool which accepts the description of your data structures (with additional annotation) and output printing routines for it, and the C declarations of your data.

BTW, your problem is related to serialization

If all the structure members are the same you could increment a pointer to the first member to access the other members of the sturcture, although that's generally not a good idea because structures are sometimes padded. for example:

struct my_struct{
    int a;
    int b;
    int c;
};

struct my_struct t = {1, 2, 3};
int *p = &t.a;
for (i=0; i<sizeof(t)/sizeof(int); i++, p++) {
    printf("%d\n", *p);
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top