從某些版本的 GDB(我猜是 7 個左右),可以以互動或非互動方式從 GDB 呼叫 python。
下面是一些例子:
(gdb) python print("gdb".capitalize())
Gdb
(gdb)
是否可以將 GDB 中使用的變數傳遞給 Python?我嘗試過這樣的事情,但沒有運氣:
嘗試傳遞名為 c_variable 的 C 變數
(gdb) python print(c_variable.capitalize())
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'c_variable' is not defined
Error while executing Python code.
嘗試傳遞名為 $1 的 GDB 變數
(gdb) python print($1.capitalize())
File "<string>", line 1
print($1.capitalize())
^
SyntaxError: invalid syntax
Error while executing Python code.
編輯
幾乎在我的問題之后,我發現這個問題通過 gdb 將 c 變數傳遞給 python
所以我最終得到以下結果:
(gdb) whatis p_char
type = char *
(gdb) ptype p_char
type = char *
(gdb) p p_char
$1 = 0x8002004 "hello world"
(gdb) python x=str(gdb.parse_and_eval('p_char')); print(x.split(" "))
['0x8002004', '"hello', 'world"']
這是我可以使用的東西,但我需要做一些額外的清理作業(洗掉引號、地址等),有沒有更好的方法?而且我仍然不知道是否可以通過$1。
uj5u.com熱心網友回復:
嘗試傳遞名為 c_variable 的 C 變數
嘗試傳遞名為 $1 的 GDB 變數
py print(gdb.parse_and_eval("c_variable"))
py print(gdb.parse_and_eval("$1"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/406016.html
標籤:
