最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

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 You cannot cast structs. But you could create a union holding that struct and a 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
  • When you mention 2 results from Google, how were they not useful? You might add what doubts are left after reading them. Or what they mentioned, to start with. – Gerhardh Commented Feb 7 at 15:05
  • yes I know they are implementation defined. I checked the memory layout and adapted the bitfields accordingly. – Max Sedlusch Commented Feb 7 at 15:14
Add a comment  | 

2 Answers 2

Reset to default 3

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

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论