🌐
GNU Project
sourceware.org › gdb › current › onlinedocs › gdb.html › Python-API.html
Python API (Debugging with GDB)
You can get quick online help for GDB’s Python API by issuing the command python help (gdb).
🌐
Red Hat
developers.redhat.com › blog › 2017 › 11 › 10 › gdb-python-api
The GDB Python API | Red Hat Developer
December 27, 2023 - It's been written for GDB, using the Python API, and is maintained by the developers of the libstdc++ library. The API it uses and implements is the GDB Python pretty printer interface.
🌐
GitHub
github.com › jefftrull › gdb_python_api
GitHub - jefftrull/gdb_python_api: Experiments with the GDB Python API
It examines the AST at the current line using libClang's Python API, and advances execution until a user function is hit. The finishu command returns you to the point right after where stepu was executed, as though you had typed next instead. Due to the use of libClang, an extra environment variable is required: PYTHONPATH=/path/to/gdb_python_api LD_LIBRARY_PATH=/usr/lib/llvm-5.0/lib gdb ...
Starred by 70 users
Forked by 15 users
Languages   Python 88.3% | C++ 7.3% | CMake 4.4%
🌐
Python documentation
docs.python.org › 3 › howto › gdb_helpers.html
Debugging C API extensions and CPython Internals with GDB — Python 3.14.2 documentation
This document explains how the Python GDB extension, python-gdb.py, can be used with the GDB debugger to debug CPython extensions and the CPython interpreter itself. When debugging low-level proble...
🌐
Embedded Artistry
embeddedartistry.com › home › blog › metal.gdb: controlling gdb through python scripts with the gdb python api
Metal.GDB: Controlling GDB through Python Scripts with the GDB Python API - Embedded Artistry
December 16, 2021 - This API allows us to automate communication with our embedded target. Note that the standard arm-none-eabi build has a separate executable called arm-none-eabi-gdb-py that requires python-2.7.
🌐
Desy
zeuthen.desy.de › dv › documentation › unixguide › infohtml › gdb › Python-API.html
Python API - Debugging with GDB
At startup, gdb overrides Python's sys.stdout and sys.stderr to print using gdb's output-paging streams. A Python program which outputs to one of these streams may have its output interrupted by the user (see Screen Size). In this situation, a Python KeyboardInterrupt exception is thrown. Basic Python: Basic Python Functions. Exception Handling: How Python exceptions are translated. Values From Inferior: Python representation of values. Types In Python: Python representation of types. Pretty Printing API: Pretty-printing values.
🌐
Medium
shadowintel.medium.com › binary-analysis-with-python-gdb-api-8c5c8b0439aa
Binary Analysis With Python GDB API | by Ahmet Göker | Medium
February 3, 2024 - The script automates the debugging process by setting breakpoints at specific locations, defining an event handler to print values, and controlling the flow between breakpoints. It demonstrates the use of the GDB Python API to interact with GDB programmatically.
🌐
Python Developer's Guide
devguide.python.org › gdb
Python
You should have been redirected · If not, click here to continue
Find elsewhere
🌐
Villanova
ece.villanova.edu › VECR › doc › gdb › Python-API.html
Python API - Debugging with GDB
Example: gdb.some_function ('foo', bar = 1, baz = 2). Basic Python: Basic Python Functions. Exception Handling: How Python exceptions are translated. Values From Inferior: Python representation of values. Types In Python: Python representation of types. Pretty Printing API: Pretty-printing values.
🌐
Red Hat
developers.redhat.com › articles › 2024 › 02 › 05 › how-cache-data-using-gdbs-python-api
How to cache data using GDB's Python API | Red Hat Developer
February 5, 2024 - The GNU Debugger (GDB) has an extensive, and growing Python API which can be used to customize and extend GDB as needed. Different object types exist within the Python API to represent different parts
🌐
Python Developer's Guide
devguide.python.org › development-tools › gdb
GDB support
Page moved: Information on debugging CPython using GDB is now in the main Python documentation, since it is relevant for C extension modules as well. Please read it first: Debugging C API extension...
Top answer
1 of 4
13

FYI recent gdb versions are scriptable in Python. You can call python code from the gdb command line. This opens a whole new world, check the relevant documentation. From the command line run:

 dnf/yum/apt-get install gdb-doc
 info gdb extending python

If you do not like the text-based info browser, here is one (among many?) alternative, graphical browser:

yelp 'info:gdb' # , go to "Extending"

Here is a sample gdb-python script. It attaches gdb to the first "your_program" found running.

#!/usr/bin/python

import subprocess
import string

def backquotes(cmdwords):
        output = subprocess.Popen(cmdwords, stdout=subprocess.PIPE).communicate()[0]
        return output.strip()

pid = backquotes(['pgrep', 'your_program'])

gdb.execute("attach " + str(pid))
2 of 4
13

A reduced example that I'm currently using:

class DebugPrintingBreakpoint(gdb.Breakpoint):
    debugging_IDs = frozenset({37, 153, 420})
    def stop(self):
        top = gdb.newest_frame()
        someVector = top.read_var('aVectorVar')
        # Access the begin() & end() pointer of std::vector in GNU Standard C++ lib
        first = someVector['_M_impl']['_M_start']
        last = someVector['_M_impl']['_M_finish']
        values = []
        while first != last:
            values.append(int(first.dereference()['intID']))
            first = first + 1
        if not set(values) & debugging_IDs:
            return False # skip: none of the items we're looking for can be found by ID in the vector on the stack
        print("Found other accompanying IDs: {}".format(values))
        return True # drop to gdb's prompt
# Ensure shared libraries are loaded already
gdb.execute("start")
# Set our breakpoint, which happens to reside in some shared lib, hence the "start" previously
DebugPrintingBreakpoint("source.cpp:42")
gdb.execute("continue")

You can execute this script from gdb's prompt like this:

(gdb) source script.py

Or from the command-line:

$ gdb --command script.py ./executable.elf

See the complete GDB Python API docs for further info.

🌐
Deepan Seeralan
deepanseeralan.com › tech › gdb-macros-commands-with-python
Using Python APIs in GDB - Deepan Seeralan
July 30, 2021 - Python APIs are available in the module gdb. This module is automatically imported by gdb for use in scripts evaluated by the python command.
🌐
Stack Overflow
stackoverflow.com › questions › 63149644 › using-gdb-python-api-how-to-fetch-the-value-of-a-c-variable-during-debug-sessio
using gdb python api, how to fetch the value of a c variable during debug session - Stack Overflow
The v variable that you get in the gdb's python interpreter is a gdb.Value object that you can print, get the value of a field (if the original v variable is an object), etc.
🌐
GitHub
github.com › cyrus-and › gdb-dashboard
GitHub - cyrus-and/gdb-dashboard: Modular visual interface for GDB in Python
GDB dashboard is a standalone .gdbinit file written using the Python API that enables a modular interface showing relevant information about the program being debugged.
Starred by 12K users
Forked by 819 users
Languages   Python