Skip to main content
 首页 » 编程设计

python之gdb python : How to do arithmetic operation on a gdb. 值

2025年05月04日8birdshome

为什么我在做算术运算时得到错误的答案:

(gdb) python address = gdb.parse_and_eval('&(((struct my_struct *)next->priv).wait_list)') 
(gdb) python print address 
0x410027a00728 
(gdb) python offset = gdb.parse_and_eval('&((struct wait_list_t *)0)->list') 
(gdb) python print offset 
0x0 
(gdb) python diff = address - offset 
gdb) python print diff 
0x410027a0072 

而输出应该是 0x410027a00728。 我检查了地址类型和偏移量

(gdb) python print address.type 
struct list_head * 
(gdb) python print offset.type 
struct list_head * 

我也试过

(gdb) python y = hex(long(address)) 
(gdb) python print y 
0x410027A14FF0L 
(gdb) python z = hex(long(offset)) 
(gdb) python print z 
0x0L 
(gdb) python diff = y - z 
Traceback (most recent call last): 
File "<string>", line 1, in ? 
TypeError: unsupported operand type(s) for -: 'str' and 'str' 
Error while executing Python code. 

有没有其他方法可以做到这一点?

请您参考如下方法:

您正在减去两个指针类型的值。这意味着结果与 C 中一样,除以对象的大小。

相反,请确保“offset”具有整数类型,而不是指针类型。

在上一个示例中,您尝试减去字符串。你不能那样做。将对“hex”的调用从计算转移到打印,它将起作用。