27
votes

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?

4
Alberteddu have u worked on this. cos i am unable to understand the documentation given in the link. Can you please guide me. - Sagar Gupta M.
You did not give enough information in your question. Where did you hear about this file, and what are you expecting it to do? What are you trying to achieve? - Sven Marnach
To use the gdb in python as described in sourceware.org/gdb/current/onlinedocs/gdb/Python.html - Sagar Gupta M.
What operating system are you using? You probably need to install the python GDB package. - n s

4 Answers

38
votes

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.

Explanation

  • 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 running gdb -x MY-SCRIPT.py).

Example Program

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')

run:

$ gdb -q -x t.py 

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 <[email protected]>
   0x0000000000401ae6 <+6>:  pushq  $0x3d
   0x0000000000401aeb <+11>: jmpq   0x401700
End of assembler dump.

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

3
votes

You can follow this tutorial to install PythonGDB. The Python code depends on a C extension.

For Windows, there is a recent enough gdb build in MinGW, but it doesn't seem to include the Python module you can import (still supports Python scripting in gdb). You have to install MinGW and then install the gbd package using 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.

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

2
votes

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

I hope that helps.

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

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

You have to configure GDB using that option:

--with-python=location

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

1
votes

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

$ 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

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

(gdb) source Tools/gdb/webkit.py
(gdb) p run
$1 = (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}

Hope this helps.