在python中使用gdb命令

时间:2012-01-07 20:50:28

标签: python gdb command

我想在gdb中使用python脚本,但我有一些问题,如何让这些命令结果重定向到我的python脚本?

我的意思是,当我使用“info f”时,在gdb中,它会打印有关ebp,eip信息的信息......现在我想让这些信息不显示在屏幕上,而是重定向到变量。

例如,在我的python脚本中,有一个名为“str1”的字符串,所以我想要str1 = gdb.command(“info f”),我尝试了一些方法,但信息将显示在屏幕上,如何将其从屏幕中删除,只需将其存储在字符串中?

2 个答案:

答案 0 :(得分:2)

以下是一个例子:

(gdb) info frame
Stack level 0, frame at 0x7fffffffd960:
 rip = 0x7ffff7dec680 in *__GI__dl_debug_state (dl-debug.c:77); saved rip 0x7ffff7de0731
 called by frame at 0x7fffffffdab0
 source language c.
 Arglist at 0x7fffffffd950, args: 
 Locals at 0x7fffffffd950, Previous frame's sp is 0x7fffffffd960
 Saved registers:
  rip at 0x7fffffffd958

(gdb) python str1 = gdb.execute("info frame", False, True)
(gdb) python print str1
Stack level 0, frame at 0x7fffffffd960:
 rip = 0x7ffff7dec680 in *__GI__dl_debug_state (dl-debug.c:77); saved rip 0x7ffff7de0731
 called by frame at 0x7fffffffdab0
 source language c.
 Arglist at 0x7fffffffd950, args: 
 Locals at 0x7fffffffd950, Previous frame's sp is 0x7fffffffd960
 Saved registers:
  rip at 0x7fffffffd958

文档here

答案 1 :(得分:1)

gdb.execute (command [, from_tty [, to_string]])就是你(我们:D)寻求的。你可以看一下function decription。第一个参数是引用 GDB 命令,第二个我没有真正理解,我想因​​为我不需要它,只需保持 false 。第三个确定命令输出将在何处流式传输。如果 false (默认),它只是打印,如果 true ,输出将被包装为字符串并返回给你,所以你可以将它分配给一个变量。

一个例子:

(gdb) py MyVar = gdb.execute("info f",False,True)
(gdb) py print(MyVar)
Stack level 0, frame at 0x7fffffffdda0:
 rip = 0x4006a6 in open@plt; saved rip 0x4007b9
 called by frame at 0x7fffffffddc0
 Arglist at 0x7fffffffdd90, args: 
 Locals at 0x7fffffffdd90, Previous frame's sp is 0x7fffffffdda0
 Saved registers:
  rip at 0x7fffffffdd98

(gdb) 

您可能也对gdb.parse_and_eval (expression)感兴趣。它提供了一种简单的方法来使用 GDB 所谓的方便变量

(gdb) set $MyVar = "PinkyPie is the best!"
(gdb) pi
>>> MyStr = gdb.parse_and_eval("$MyVar")
>>> print(MyStr)
"PinkyPie is the best!"
>>> 
(gdb)