开发者

How do I dump an arbitrary struct in C?

开发者 https://www.devze.com 2023-03-20 08:32 出处:网络
I don\'t know which direction to go,perhaps something like reflecti开发者_开发技巧on will help?If you\'re using Clang 8 or newer, you can now use the built-in compiler function __builtin_dump_struct t

I don't know which direction to go,perhaps something like reflecti开发者_开发技巧on will help?


If you're using Clang 8 or newer, you can now use the built-in compiler function __builtin_dump_struct to dump a struct. It uses the information that's naturally available at compile time to generate code that pretty-prints a struct.

Example code demonstrating the function:

dumpstruct.c:

#include <stdio.h>

struct nested {
    int aa;
};

struct dumpme {
    int a;
    int b;
    struct nested n;
};

int main(void) {
    struct nested n;
    n.aa = 12;
    struct dumpme d;
    d.a = 1;
    d.b = 2;
    d.n = n;
    __builtin_dump_struct(&d, &printf);
    return 0;
}

Example compile-and-run:

$ clang dumpstruct.c -o dumpstruct
$ ./dumpstruct 
struct dumpme {
int a : 1
int b : 2
struct nested n : struct nested {
    int aa : 12
    }
}

If you're not using Clang >= 8 but you are using GCC, it's pretty easy to switch. Just install the clang-8 or clang-9 package and replace invocations of gcc with clang.


The answer of @Kerrek SB works realy well, I just post how to use it in a function using a void pointer.

int dump(void *myStruct, long size)
{
    unsigned int i;
    const unsigned char * const px = (unsigned char*)myStruct;
    for (i = 0; i < size; ++i) {
        if( i % (sizeof(int) * 8) == 0){
            printf("\n%08X ", i);
        }
        else if( i % 4 == 0){
            printf(" ");
        }
        printf("%02X", px[i]);
    }

    printf("\n\n");
    return 0;
}

int main(int argc, char const *argv[])
{
    OneStruct data1, data2;

    dump(&data1, sizeof(OneStruct));

    dump(&data2, sizeof(OneStruct));

    return 0;
}


Here's a hex dump, about as general as you can get:

struct Foo x;

unsigned int i;
const unsigned char * const px = (unsigned char*)&x;

for (i = 0; i < sizeof(x); ++i) printf("%02X ", px[i]);

Note that the result of this is entirely implementation-defined; presumably there'll be plenty of padding, and you won't know what any of the printed values mean. (Most of them will probably just be pointers to some other part of space.)

As Etienne says, C is a statically typed language and does not have reflection, so you have to know the declaration of Foo in order to interpret the content of x.


What do you want to do with your file once you've got it? If it's going to be read back in at a later time just use fread and fwrite, like

struct foo * bar;
fwrite(bar,sizeof(*bar),1,stdout);

...

fread(bar,sizeof(*bar),1,stdin);

This will give binary output that's dependant on your compiler/platform, as long as those are unchanged you should be fine. From there you can also feed the file into a hex reader etc., though you'll need to know the layout of the struct to do anything useful with it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号