如何在Python中导入'GDB'
我使用Python 2.7和Python 3.1.3。但在我的Python中,我无法导入gdb。
I am using Python 2.7 and Python 3.1.3. But in my Python I am unable to "import gdb".
它给了我一个错误:
It is giving me an error as:
>>> import gdb
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
ImportError: No module named gdb
>>>
这是什么原因?我应该如何解决这个问题?
What's a reason for this? How should I solve this problem?
import gdb
你的Python代码在GDB进程中运行。它不应该从普通系统的Python解释器工作。
import gdb
only works when your Python code is running within the GDB process. It's not supposed to work from the regular system Python interpreter.
- GDB嵌入Python解释器,以便它可以使用Python作为扩展语言。 您不能只是
import gdb
$>从/ usr / bin / python
就像它是一个普通的Python库,因为GDB不是以库的形式存在的。 - 你可以在gdb中执行
来源MY-SCRIPT.py
(相当于运行gdb -x MY-SCRIPT.py $ c $
- GDB embeds the Python interpreter so it can use Python as an extension language.
- You can't just
import gdb
from/usr/bin/python
like it's an ordinary Python library because GDB isn't structured as a library. - What you can do is
source MY-SCRIPT.py
from within gdb (equivalent to runninggdb -x MY-SCRIPT.py
).
这是一个独立的示例。将下面的文件保存到 t.py
:
Here's a self contained example. Save the file below to t.py
:
import gdb
gdb.execute('file /bin/cat')
o = gdb.execute('disassemble exit', to_string=True)
print(o)
gdb.execute('quit')
运行:
$ gdb -q -x t.py
你会看到针对 exit()
的PLT存根被反汇编。在x86-64 Linux上:
and you'll see the PLT stub for exit()
disassembled. On x86-64 Linux:
Dump of assembler code for function exit@plt:
0x0000000000401ae0 <+0>: jmpq *0x20971a(%rip) # 0x60b200 <exit@got.plt>
0x0000000000401ae6 <+6>: pushq $0x3d
0x0000000000401aeb <+11>: jmpq 0x401700
End of assembler dump.
我已经收集了一些关于学习GDB Python API的资源这里。
I've collected some resources on learning the GDB Python API here.