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!!
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!!
Share Improve this question edited yesterday Max Sedlusch asked Feb 7 at 14:58 Max SedluschMax Sedlusch 1075 bronze badges 3 |2 Answers
Reset to default 3You 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
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.
uint32_t
. BTW: Are you aware that details of bitfields are implementation defined. You cannot rely on a certain order withint the integer value. – Gerhardh Commented Feb 7 at 15:04