如何在 Python 中导入“GDB”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4792483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 17:32:55  来源:igfitidea点击:

How to import 'GDB' in Python

pythongdb

提问by Sagar Gupta M.

I am using Python 2.7 and Python 3.1.3. But in my Python I am unable to "import gdb".

我正在使用 Python 2.7 和 Python 3.1.3。但是在我的 Python 中,我无法“导入 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?

这是什么原因?我应该如何解决这个问题?

回答by Donovan

I can't test now, but I think you need to configure and build a Python-enabled GDB. Take a look at this guide.

我现在无法测试,但我认为您需要配置和构建支持 Python 的 GDB。看看这个指南

I hope that helps.

我希望这有帮助。

This is outdated, I think. Anyway, you always need to build and configure a Python enabled GDB.

这已经过时了,我想。无论如何,您始终需要构建和配置启用 Python 的 GDB。

You can script GDB using the Python programming language. This feature is available only if GDB was configured using --with-python.

您可以使用 Python 编程语言编写 GDB 脚本。仅当 GDB 是使用--with-python.

You have to configure GDB using that option:

您必须使用该选项配置 GDB:

--with-python=location

Where locationis the location of python you would like to use GDB with.

location您希望与 GDB 一起使用的 python 的位置在哪里。

回答by TryPyPy

You can follow this tutorialto install PythonGDB. The Python codedepends on a C extension.

您可以按照本教程安装PythonGDB。在Python代码依赖于C扩展

For Windows, there is a recent enough gdb buildin MinGW, but it doesn't seem to include the Python module you can import (still supports Python scripting in gdb). You have to install MinGWand then install the gbd package using mingw-get install gdb.

对于 Windows,在MinGW 中有一个足够新的gdb 构建,但它似乎不包含您可以导入的 Python 模块(仍然支持 gdb 中的 Python 脚本)。您必须安装 MinGW,然后使用.mingw-get install gdb

If you use Cygwin, there's again a recent enough gdb in Cygwin Ports, without a Python module but with Python scripting support.

如果您使用Cygwin,则Cygwin Ports 中再次有足够新的 gdb ,没有 Python 模块但具有 Python 脚本支持。

I suppose it'd be possible to build gdb from source in either platform and get the Python module.

我想可以在任一平台上从源代码构建 gdb 并获取 Python 模块。

回答by scottt

import gdbonly works when your Python code is running within the GDB process. It's not supposed to work from the regular system Python interpreter.

import gdb仅当您的 Python 代码在 GDB 进程中运行时才有效。它不应该在常规系统 Python 解释器中工作。

Explanation

解释

  • GDB embeds the Python interpreter so it can use Python as an extension language.
  • You can'tjust import gdbfrom /usr/bin/pythonlike it's an ordinary Python library because GDB isn't structured as a library.
  • What you can do is source MY-SCRIPT.pyfrom within gdb (equivalent to running gdb -x MY-SCRIPT.py).
  • GDB 嵌入了 Python 解释器,因此它可以使用 Python 作为扩展语言。
  • 不能只是import gdb/usr/bin/python喜欢它的一个普通的Python库,因为GDB不规整为库。
  • 你可以做的是source MY-SCRIPT.py在 gdb 中(相当于 running gdb -x MY-SCRIPT.py)。

Example Program

示例程序

Here's a self contained example. Save the file below to t.py:

这是一个自包含的示例。将以下文件保存到t.py

import gdb
gdb.execute('file /bin/cat')
o = gdb.execute('disassemble exit', to_string=True)
print(o)
gdb.execute('quit')

run:

跑:

$ gdb -q -x t.py 

and you'll see the PLT stub for exit()disassembled. On x86-64 Linux:

你会看到exit()拆解的PLT存根。在 x86-64 Linux 上:

Dump of assembler code for function exit@plt:
   0x0000000000401ae0 <+0>:  jmpq   *0x20971a(%rip)    # 0x60b200 <[email protected]>
   0x0000000000401ae6 <+6>:  pushq  
$ python Tools/gdb/webkit.py
Traceback (most recent call last):
  File "Tools/gdb/webkit.py", line 38, in <module>
    import gdb
ImportError: No module named gdb
x3d 0x0000000000401aeb <+11>: jmpq 0x401700 End of assembler dump.

I've collected some resources on learning the GDB Python API here.

我一直在学习Python的GDB API收集了一些资源在这里

回答by Deqing

I just ran into the similar situation when trying to debug Webkit:

我在尝试调试 Webkit 时遇到了类似的情况:

(gdb) source Tools/gdb/webkit.py
(gdb) p run
 = (const WebCore::TextRun &) @0x7fffffffa450: {m_characters = "Plugin Testsa", m_len = 12, m_xpos = 0, 
  m_padding = 0, m_allowTabs = false, m_rtl = false, m_directionalOverride = false, 
  m_applyRunRounding = true, m_applyWordRounding = true, m_disableSpacing = false}

I then realized that this script should be invoked in gdb to make it working:

然后我意识到应该在 gdb 中调用这个脚本以使其工作:

##代码##

Hope this helps.

希望这可以帮助。