Skip to main content

Parse gdb machine interface output with Python

Project description

https://travis-ci.org/cs01/pygdbmi.svg?branch=master https://img.shields.io/badge/pypi-v0.7.2.0-blue.svg https://img.shields.io/badge/python-2.7,3.3,3.4,3.5,pypy-blue.svg https://img.shields.io/badge/SayThanks.io-☼-blue.svg https://img.shields.io/gratipay/cs01.svg

pygdbmi - Get Structured Output from GDB’s Machine Interface

Parse gdb machine interface string output and return structured data types (Python dicts) that are JSON serializable. Useful for writing the backend to a gdb frontend. For example, gdbgui uses pygdbmi on the backend.

Also implements a class to control gdb, GdbController, which allows programmatic control of gdb using Python, which is also useful if creating a front end.

To get machine interface output from gdb, run gdb with the --interpreter=mi2 flag.

Installation

pip install pygdbmi

Examples

gdb mi has the following type of ugly, but structured, example output:

-> -break-insert main
<- ^done,bkpt={number="1",type="breakpoint",disp="keep",
    enabled="y",addr="0x08048564",func="main",file="myprog.c",
    fullname="/home/nickrob/myprog.c",line="68",thread-groups=["i1"],
    times="0"}
<- (gdb)

Use pygdbmi.gdbmiparser.parse_response to turn that string output into a JSON serializable dictionary

from pygdbmi import gdbmiparser
from pprint import pprint
response = gdbmiparser.parse_response('^done,bkpt={number="1",type="breakpoint",disp="keep", enabled="y",addr="0x08048564",func="main",file="myprog.c",fullname="/home/nickrob/myprog.c",line="68",thread-groups=["i1"],times="0"')
pprint(response)
> {'message': 'done',
 'payload': {'bkpt': {'addr': '0x08048564',
                      'disp': 'keep',
                      'enabled': 'y',
                      'file': 'myprog.c',
                      'fullname': '/home/nickrob/myprog.c',
                      'func': 'main',
                      'line': '68',
                      'number': '1',
                      'thread-groups': ['i1'],
                      'times': '0',
                      'type': 'breakpoint'}},
 'type': 'result'}

Ain’t that better?

But how do you get the gdb output into Python in the first place? If you want, pygdbmi also has a class to control gdb as subprocess. You can write commands, and get structured output back:

from pygdbmi.gdbcontroller import GdbController
from pprint import pprint

# Start gdb process
gdbmi = GdbController()

# Load binary a.out and get structured response
response = gdbmi.write('-file-exec-file a.out')
pprint(response)
[{'message': u'thread-group-added',
  'payload': {u'id': u'i1'},
  'type': 'notify'},
 {'message': u'done', 'payload': None, 'type': 'result'}]

Now do whatever you want with gdb. All gdb commands, as well as gdb machine interface commands are acceptable. gdb mi commands give better structured output that is machine readable, rather than gdb console output. mi commands begin with a -.

response = gdbmi.write('-break-insert main')
response = gdbmi.write('-exec-run')
response = gdbmi.write('next')
response = gdbmi.write('next')
response = gdbmi.write('continue')
response = gdbmi.exit()

API

There are two main functions/classes of interest.

  • function pygdbmi.gdbmiparser.parse_response(gdb_mi_text)

Parse gdb mi text and turn it into a dictionary. See Parsed Output for more information.

  • Class pygdbmi.gdbcontroller.GdbController

Run gdb as a subprocess using the GdbController class. Send commands and recieve structured output that is JSON serializable. See source code for methods and documentation.

Parsed Output Description

Each parsed gdb response consists of a list of dictionaries. Each dictionary has keys type, message, and payload.

The type is defined based on gdb’s various mi output record types, and can be

  • result - the result of a gdb command, such as done, running, error, etc.

  • notify - additional async changes that have occurred, such as breakpoint modified

  • console - textual responses to cli commands

  • log - debugging messages from gdb’s internals

  • output - output from target

  • target - output from remote target

  • done - when gdb has finished its output

In addition to the type key, pygdbmi also adds two more fields:

  • message contains a textual message from gdb, which is not always present. When missing, this is None.

  • payload contains the content of gdb’s output, which can contain any of the following: dictionary, list, string. This too is not always present, and can be None depending on the response.

Development

To get started with development, set up a new virtual environment, then clone this repo and run pip install -r requirements.txt. Test changes are still working with python setup.py test. Add to tests at pygdbmi/tests/test_app.py

See Also

  • gdbgui implements a browser-based frontend to gdb, using pygdbmi on the backend

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pygdbmi-0.7.2.1.tar.gz (13.8 kB view details)

Uploaded Source

File details

Details for the file pygdbmi-0.7.2.1.tar.gz.

File metadata

  • Download URL: pygdbmi-0.7.2.1.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pygdbmi-0.7.2.1.tar.gz
Algorithm Hash digest
SHA256 4051e3b0dce1e8be123ac76961c56dfa957a453c12dedfa8b377b1cdd2b45b2f
MD5 3bb11855f29e21fb809496bd37f8a626
BLAKE2b-256 6f4ccbc00bd3c69f0a65168e3af90df9f71427ee362d70fe14afd595906644dd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page