Cast a struct member in GDB Pretty Print?

Viewed 33

I want to pretty print this struct struct MyStruct { char buffer[16]; }. Depending on buffer[15] I want to print buffer as a 10 byte string or treat it as a pointer. The 10 byte case is simple and works return self.val['buffer'].string(length = 10)

The second case I can't figure out. I want to do something like (*(char**)buffer[0]). I'm not sure how to do that. I was thinking parse_and_eval could be easy even if its not optimal but I couldn't figure out how to access buffer. I also need to cast the buffer to a 32bit int (len = *(int*)(bufer+4);) I couldn't figure that out either.

1 Answers

If I interpret your description correctly, I think your data is actually layed out like this:

struct MyStruct {
    union {
        char string[10];
        struct {
            char *p;
            int size;
        } ptr;
    }
    int flag;
}

So I think casting the buffer to a pointer of that type, and then choosing the format based on mystruct->flag would make life easier for you.

Even if my interpretation is not correct, try to find the correct version of that struct that captures the duality of the data.

Related