2013-06-26 58 views
2

爲什麼我得到錯誤的答案,在做算術運算:gdb python:如何對gdb.value進行算術運算?

(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. 

是否有任何替代做到這一點?

+0

'python diff = long(address) - long(offset)'有效嗎? – user4815162342

+0

謝謝。:-)它正是我想要的。 –

回答

1

您正在減去指針類型的兩個值。這意味着結果(如C中)除以對象的大小。

相反,請確保「偏移」具有整數類型,而不是指針類型。

在最後一個例子中,你試圖減去字符串。你不能那樣做。將電話從計算轉移到「十六進制」到打印,它將工作。

+0

再次感謝Tromey :-) –

+0

tromey,在這裏寫的腳本'print-struct-follow-pointers.py'這裏寫的http://stackoverflow.com/questions/16787289/gdb-python-parsing-structures-each-field-如果與腳本中的is_pointer(v)條件類似,我正在通過if檢查另一個條件:if_typedef(v):' 'type_ = v.type.strip_typedefs ()' 'print'type =%s'%(type_)' 在某些結構體的情況下,它會輸出正確的類型,但對於某些結構體,它會輸出'type = struct {...}'。我無法找到確切的原因.. –

+0

解決。:-)這是因爲匿名結構..!是否有沒有支持python關鍵字'與'和'as'內gdb?爲什麼它不起作用?代碼在這裏http://stackoverflow.com/questions/17448736/gdb-python-why-below-code-is-not-working-under-gdb –