pygdbmi

0.11.0.0 · active · verified Sat Apr 11

pygdbmi is a Python library that parses GDB's machine interface (MI) output into structured data (Python dictionaries) that are JSON serializable. It also provides a class, `GdbController`, to control GDB as a subprocess, allowing programmatic interaction for backend development of GDB frontends. It supports cross-platform debugging on Linux, macOS, and Windows. The current stable version is 0.11.0.0, with releases occurring periodically to introduce new features, fix bugs, and address breaking changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `GdbController` to interact with a GDB subprocess programmatically, including loading a binary, setting a breakpoint, running, and continuing execution. It also shows how to directly parse a raw GDB MI output string using `gdbmiparser.parse_response`. A simple C program is compiled and used as the debug target for the demonstration.

from pygdbmi.gdbcontroller import GdbController
from pprint import pprint
import os
import subprocess
import shutil

# NOTE: This example assumes 'make' and 'gdb' are available in your PATH.
# Create a dummy C program for debugging
if not os.path.exists('sample_app'):
    os.makedirs('sample_app')
with open('sample_app/main.c', 'w') as f:
    f.write('int main() { int x = 1; x++; return 0; }')

if shutil.which('gcc') and shutil.which('make'):
    subprocess.run(['gcc', 'main.c', '-o', 'pygdbmiapp', '-g'], cwd='sample_app', check=True)
    binary_path = os.path.abspath('sample_app/pygdbmiapp')
else:
    print("Warning: 'gcc' or 'make' not found. Quickstart will only demonstrate parsing.")
    binary_path = None

gdbmi = GdbController()

try:
    if binary_path:
        print(f"\nDebugging: {binary_path}")
        # Load the binary
        responses = gdbmi.write(f"-file-exec-and-symbols {binary_path}")
        print("Load binary response:")
        pprint(responses)

        # Set a breakpoint at main
        responses = gdbmi.write("-break-insert main")
        print("Breakpoint response:")
        pprint(responses)

        # Run the program
        responses = gdbmi.write("-exec-run")
        print("Run response:")
        pprint(responses)

        # Continue to finish
        responses = gdbmi.write("-exec-continue")
        print("Continue response:")
        pprint(responses)

    # Example of parsing raw MI output
    print("\nParsing raw GDB MI output:")
    raw_mi_output = '^done,bkpt={number="1",type="breakpoint",disp="keep",enabled="y",addr="0x08048564",func="main",file="myprog.c",fullname="/home/myprog.c",line="68",thread-groups=["i1"],times="0"}'
    parsed_response = gdbmi.gdbmiparser.parse_response(raw_mi_output)
    pprint(parsed_response)

finally:
    # Ensure gdb process is terminated
    gdbmi.exit()
    print("\nGDB process exited.")
    # Clean up dummy app
    shutil.rmtree('sample_app', ignore_errors=True)

view raw JSON →