Max Sedlusch
Max Sedlusch

Reputation: 115

Is it possible to cast a struct consisting of bitfields to an unsigned int of the same length to print it in GDB?

On google I got 2 results for my question, on SO I did not find a question that would answer mine. I searched for "gdb cast struct to unsigned int c" on google and the results did not address gdb but were about casting between structs in general. I am debugging a piece of code that has structs like this:

typedef struct L1_valid_entry{
    uint32_t PXN:1;
    uint32_t index_1:1;
    uint32_t C_B:2;
    uint32_t XN:1;
    uint32_t DOMAIN:4;
    uint32_t IMPL:1;
    uint32_t AP_11_10:2;
    uint32_t TEX_14_12:3;
    uint32_t AP_15:1;
    uint32_t nG_S:2;
    uint32_t zero:1;
    uint32_t NS_19:1;
    uint32_t base_address:12;
}L1_valid_entry;

now in GDB I inspect such a struct instance and want to print it but instead of its bitfields as values I want to print it as an unsigned int. I tried p/x and p/t but it changes nothing.

Edit: p/x (unsigned int) works!!

Upvotes: 1

Views: 78

Answers (2)

Wiimm
Wiimm

Reputation: 3572

I use unions for this. Example:

typedef union combi
{
    uint32_t num;
    struct L1_valid_entry bit;
} combi;

int main()
{
  combi c;
  c.num = 2;
  c.bit.zero = 1;
  printf("%08x\n",c.num);
  return 0;
}

Warning: The bit order in num differs for little and big endian systems.

Upvotes: 2

dbush
dbush

Reputation: 224842

You can use the x command to print the contents of memory starting at that struct's address:

x/wx &e

Or alternately:

x/4bx &e

Upvotes: 3

Related Questions