我正在尝试使用GDB-Python Type API (gdb.types)探索数据类型的内存布局。具体地说,我希望找到一种方法来获得定义在父结构中的嵌套结构的所有成员的绝对偏移量。
在C中,我定义了:
typedef struct
{
int a;
short bf1:2;
char b:4;
struct //nested struct
{
long x;
long y;
};
} a_struct;在gdb中使用ptype命令,我得到:
(gdb) ptype /o a_struct
/* offset | size */ type = struct a_struct {
/* 0 | 4 */ int a;
/* 4:14 | 2 */ short bf1 : 2;
/* 4: 2 | 1 */ char b : 4;
/* XXX 2-bit hole */
/* XXX 3-byte hole */
/* 8 | 16 */ struct {
/* 8 | 8 */ long x;
/* 16 | 8 */ long y;
/* total size (bytes): 16 */
};
/* total size (bytes): 24 */
}上面的输出将嵌套匿名结构的字段的偏移量显示为父结构开头的绝对值,即x位于字节8,y位于字节16。
我正在尝试使用GDB Python Type API获得相同的结果,但没有成功。特别是,我使用的是gdb.types.deep_items(lookup_type)方法,它返回嵌套结构的字段的相对偏移量,即第一个字段(x)为0,第二个字段(y)为8。
有没有办法使用GDB Python API得到x的8和y的16 (如ptype输出所示)?
谢谢
发布于 2020-02-28 08:08:59
我现在不能检查,但如果我没记错的话,这个gdb插件提供了绝对的偏移量:https://blog.mozilla.org/sfink/2018/08/17/type-examination-in-gdb/。它会产生如下输出:
(gdb) pahole js::jit::ABIArg
offset size
0 16 : struct js::jit::ABIArg {
0 4 : kind_ : js::jit::ABIArg::Kind
4 4 : --> 32 bit hole in js::jit::ABIArg <--
8 8 : u : struct union {...} {
8 +0 1 : gpr_ : js::jit::Register::Code
8 +0 8 : fpu_ : js::jit::FloatRegister::Code
8 +0 4 : offset_ : uint32_t
} union {...}
} js::jit::ABIArghttps://stackoverflow.com/questions/60442223
复制相似问题