Skip to main content

pygdb2 is a python module which allows you to send commands to the underlying gdb process. For example, it can be used to automatically and programmatically add breakpoints and watchpoints.

How to install

Make sure that pygdb2 is installed:

$ pip install pygdb2

Then, you need to activate the GDB integration by putting this line inside your ~/.gdbinit:

python import pygdb2

How to use it

You need to launch your python program inside gdb using the special pyrun command:

$ gdb --args python myscript.py
...
(gdb) pyrun
...

From the python program, you can use pygdb2.set_trace() to enter the gdb prompt, or pygdb2.execute() to send commands to gdb.

At the (gdb) prompt, it is possible to invoke the pdb command to enter in the corresponding debugger at the Python level.

Example

For example, the following code adds a watchpoint for a particular region of memory created with ctypes:

import ctypes
import pygdb2

def main():
    buf = ctypes.c_int()
    buf.value = 42
    adr = ctypes.cast(ctypes.pointer(buf), ctypes.c_void_p)
    # enter gdb when we write to this memory
    pygdb2.execute("watch *(int*)%d" % adr.value)

    i = 0
    while i < 5:
        print i
        i += 1
        if i == 2:
            buf.value = 43 # GDB stops here

Here is an example of a gdb/pdb session:

$ gdb --args python set_watchpoint.py
...
(gdb) # lines prefixed "pygdb2:" contain the command coming from pygdb2.execute()
(gdb) pyrun
...
pygdb2: watch *(int*)14079984
Hardware watchpoint 1: *(int*)14079984
0
1
Hardware watchpoint 1: *(int*)14079984

Old value = 42
New value = 43
i_set (ptr=0xd6d7f0, value=<value optimized out>, size=4) at /build/buildd/python2.7-2.7.1/Modules/_ctypes/cfield.c:663
663 /build/buildd/python2.7-2.7.1/Modules/_ctypes/cfield.c: No such file or directory.
    in /build/buildd/python2.7-2.7.1/Modules/_ctypes/cfield.c

(gdb) # now we are debugging at the C level
(gdb) # in particular, we are inside function of _ctypes
(gdb) # which sets the value of the buffer

(gdb) # Let's jump to the Python level
(gdb) pdb
Signal received, entering pdb
Traceback:
  File "set_watchpoint.py", line 20, in <module>
    main()
  File "set_watchpoint.py", line 16, in main
    buf.value = 43 # we should enter gdb here
> /home/antocuni/env/src/pygdb2/pygdb2/test/set_watchpoint.py(12)main()
-> while i < 5:
(Pdb++) print i
2
(Pdb++) c
2
3
4

Program exited normally.
(gdb) q

Using signals

pygdb2 uses both SIGUSR1 and SIGUSR2 to communicate with gdb, so if your program also needs those, you might have conflicts.

Release files for pygdb2 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pygdb2 0.1
File Size Uploaded
pygdb2-0.1.tar.gz 3.5 kB Details

Release files / pygdb2-0.1.tar.gz

Download URL pygdb2-0.1.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
2a35f5a25ca7bb18c03dfd4a0ff94798ab613faedf3f4015c2d162a9e26a3c61
BLAKE2b-256 checksum
How to use checksums
4d066c8ad759e3fbc07f064071db9ff306cc6cd2546daa56dc868c788a624b77
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page