Extract the fields of a C struct

后端 未结 7 740
盖世英雄少女心
盖世英雄少女心 2020-12-24 08:56

I often have to write code in other languages that interact with C structs. Most typically this involves writing Python code with the struct or ctypes modules.

So I

7条回答
  •  旧时难觅i
    2020-12-24 09:23

    If you compile your C code with debugging (-g), pahole (git) can give you the exact structure layouts being used.

    $ pahole /bin/dd
    …
    struct option {
            const char  *              name;                 /*     0     8 */
            int                        has_arg;              /*     8     4 */
    
            /* XXX 4 bytes hole, try to pack */
    
            int *                      flag;                 /*    16     8 */
            int                        val;                  /*    24     4 */
    
            /* size: 32, cachelines: 1, members: 4 */
            /* sum members: 24, holes: 1, sum holes: 4 */
            /* padding: 4 */
            /* last cacheline: 32 bytes */
    };
    …
    

    This should be quite a lot nicer to parse than straight C.

提交回复
热议问题