Skip to main content

GDB-like Python Debugger in the Trepan family

Project description

CircleCI PyPI - Downloads License Documentation Status Supported Python Versions

packagestatus

Abstract

This is a gdb-like debugger for Python. It is a rewrite of pdb from the ground up. I was disappointed with the flakiness, imprecision, and poor quality of coding, modularity, and level of documentation when I first looked at pdb.

pdb has gotten better since then. But a complete debugger is way more complex than what you’d expect from a Standard Python module; it requires a larger set of supporting packages than is found in the Standard Python library.

trepan3k is both a high-level debugger as well as a lower-level bytecode debugger inspector. The code understands a lot about byte code and the Python code object. The debugger makes use of this knowledge to get more precise and accurate results and provide more reliable operations.

A command-line interface (CLI) is provided as well as remote access interface over TCP/IP.

See the entry-exit for the various ways you can enter the debugger.

This code supports versions of Python back to version 3.0 using different git branches. See trepan2 for the same code modified to work with Python 2.

Features

Since this debugger is similar to other trepanning debuggers and gdb in general, knowledge gained by learning this is transferable to those debuggers and vice versa.

There’s a lot of cool stuff here that’s not in the stock Python debugger pdb, or any other Python debugger that I know about.

More Exact location information

Python reports line information on the granularity of a line. For Python versions up to 3.8. To get more precise information, we can (de)parse into Python the bytecode around a bytecode offset, such as the place you are stopped at.

So far as I know, there is no other debugger that decompiles code at runtime to narrow the position down to the specific bytecode instruction.

See the deparse command for details on getting this kind of information.

The problem with deparsing after 3.8 is that there is no decompiler that can deparse code and give associations to bytecode instructions. I am slowly working on that, though.

We use information in Python’s code object line number table in bytes to understand which lines are breakpointable, and in which module or function the line appears. Use info-line to see this information. Most, if not all, other debuggers do go to such lengths, and as a result, it is possible to request stopping on a line number that can never occur without complaint.

In the future, we may allow specifying an offset to indicate which offset to stop at when there are several choices for a given line number.

Debugging Python bytecode (no source available)

You can pass the debugger the name of Python bytecode, and many times, the debugger will merrily proceed. This debugger tries very hard to find the source code. Either by using the current executable search path (e.g. PATH) or for some by looking inside the bytecode for a filename in the main code object (co_filename) and applying that with a search path that takes into account the directory where the bytecode lives.

Failing to find source code this way, and in other situations where source code can’t be found, the debugger will decompile the bytecode and use that for showing the source text. This allows us to debug ``eval``’d or ``exec``’d code.

But if you happen to know where the source code is located, you can associate a file source code with the current name listed in the bytecode. See the set-substitute command for details here.

Source-code Syntax Colorization

Terminal source code is colorized via pygments. And with that, you can set the pygments color style, e.g., “colorful”, “paraiso-dark”. See set-style . Furthermore, we make use of terminal bold and emphasized text in debugger output and help text. Of course, you can also turn this off. You can use your own pygments_style, provided you have a terminal that supports 256 colors. If your terminal supports the basic ANSI color sequences only, we support that too in both dark and light themes.

Command Completion

Command completion is available for GNU readline and prompt_toolkit. While prompt_toolkit is new, command completion for GNU Readline is not just a simple static list but varies depending on the context. For example, for frame-changing commands that take optional numbers, the list of valid numbers is considered.

In time (and perhaps with some volunteers), prompt_toolkit will be as good as GNU Readline completion.

Terminal Handling

We can adjust the debugger output depending on the line width of your terminal. If it changes, or you want to adjust it, see set-width.

Signal Handling

Following gdb, we provide its rich set of signal handling. From the gdb documentation:

GDB has the ability to detect any occurrence of a signal in your program. You can tell GDB in advance what to do for each kind of signal.

Better Support for Thread Debugging

When you are stopped inside a thread, the thread name is shown to make this fact clearer, and you can see and switch between frames in different threads. See frame for more information.

And following gdb, you can list the threads too. See info-threads for more information.

Smart Eval

If you want to evaluate the current source line before it is run in the code, use eval. To evaluate the text of a common fragment of a line, such as the expression part of an if statement, you can do that with eval?. See eval for more information.

Function Breakpoints

Many Python debuggers only allow setting a breakpoint at a line event, and functions are treated like line numbers. But functions and lines are fundamentally different. If I write:

def five(): return 5

this line contains three different kinds of things. First, there is the code in Python that defines the function five() for the first time. Then there is the function itself, and then there is some code inside that function.

In this debugger, you can give the name of a function by surrounding adding () at the end:

break five()

Also five could be a method of an object that is currently defined when the breakpoint command is given:

self.five()

More Stepping Control

Sometimes you want small steps, and sometimes large steps.

This fundamental issue is handled in a couple of ways:

Step Granularity

There are now step event and next event commands with aliases to s+, s>, and so on. The plus-suffixed commands force a different line on a subsequent stop; the dash-suffixed commands don’t. Suffixes >, <, and ! specify call, return and exception events respectively. And without a suffix, you get the default; this is set by the set different command.

Event Filtering and Tracing

By default, the debugger stops at every event: call, return, line, exception, c-call, c-exception. If you just want to stop at line events (which is largely what happens in pdb), you can. If, however, you just want to stop at calls and returns, that’s possible too. Or pick some combination.

In conjunction with handling all events by default, the event status is shown when stopped. The reason for stopping is also available via info program.

Event Tracing of Calls and Returns

I’m not sure why this was not done before. Probably because of the lack of the ability to set and move by different granularities, tracing calls and returns leads to too many uninteresting stops (such as at the same place you just were). Also, stopping on function definitions probably added to this tedium.

Because we’re handling return events, we can stop on the return. This is a little more precise than pdb’s retval command.

Debugger Macros via Python Lambda Expressions

There are debugger macros. In gdb, there is a macro debugger command to extend debugger commands.

However, Python has its own rich programming language, so it seems silly to recreate the macro language that is in gdb. Simpler and more powerful is just to use Python here. A debugger macro here is just a lambda expression that returns a string or a list of strings. Each string returned should be a debugger command.

We also have aliases for the extremely simple situation where you want to give an alias to an existing debugger command. But beware: Some commands, like step inspect command suffixes, and change their behavior accordingly.

We also provide a means to extend the debugger through additional Python packages.

Byte-code Instruction Introspection

We do more in the way of looking at the byte codes to give better information. Through this, we can provide:

  • A skip command. It is like the jump command, but you don’t have to deal with line numbers.

  • Disassembly of code fragments. You can now disassemble relative to the stack frames you are currently stopped at.

  • Better interpretation of where you are when inside execfile or exec. (But really, though this is probably a Python compiler misfeature.)

  • Check that breakpoints are set only where they make sense.

  • A more accurate determination of whether you are at a function-defining def or class statements (because the caller’s instruction contains MAKE_FUNCTION or BUILD_CLASS.)

Even without “deparsing” mentioned above, the ability to disassemble where the PC is currently located (see info-pc), by line number range or byte-offset range lets you tell exactly where you are and code is getting run.

Some Debugger Command Arguments can be Variables and Expressions

Commands that take integer arguments like up, list, or disassemble allow you to use a Python expression that may include local or global variables that evaluate to an integer. This eliminates the need in gdb for special “dollar” debugger variables. (Note, however, because of shlex parsing, expressions can’t have embedded blanks.)

Out-of-Process Debugging

You can now debug your program in a different process or even on a different computer on a different network!

Related is flexible support for remapping path names from the file system, e.g., the filesystem seen inside a Docker container or on a remote filesystem with locally-installed files. See subst for more information.

Egg, Wheel, and Tarballs

Can be installed via the usual pip or easy_install. There is a source tarball. How To Install has full instructions and installation using git or by other means.

Modularity

Because this debugger is modular, I have been able to use it as the basis for debuggers in other projects. In particular, it is used as a module in trepanxpy, a debugger for Python interpreter, x-python, written in Python.

It is also used as a module inside an experimental open-source Wolfram Mathematica interpreter, Mathics3.

Using pytracer, the Debugger plays nicely with other trace hooks. You can have several debugger objects.

Many of the things listed below do not directly impact end-users, but it does eventually by way of more robust and featureful code. And keeping developers happy is a good thing.(TM)

  • Commands and subcommands are individual classes now, not methods in a class. This means they now have properties like the context in which they can be run, minimum abbreviation names, or alias names. To add a new command, you add a file under the command directory.

  • I/O is its own layer. This simplifies interactive readline behavior from reading commands over a TCP socket.

  • An interface is its own layer. Local debugging, remote debugging, and running debugger commands from a file (source) are different interfaces. This means, for example, that we are able to give better error reporting if a debugger command file has an error.

  • There is an experimental Python-friendly interface for front-ends

  • more testable. Much more unit and functional tests.

Documentation

Documentation: http://python3-trepan.readthedocs.org

See Also

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

trepan3k-1.5.1.tar.gz (431.5 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

trepan3k-1.5.1-311-none-any.whl (386.2 kB view details)

Uploaded

trepan3k-1.5.1-310-none-any.whl (386.3 kB view details)

Uploaded

trepan3k-1.5.1-39-none-any.whl (386.3 kB view details)

Uploaded

trepan3k-1.5.1-38-none-any.whl (386.2 kB view details)

Uploaded

trepan3k-1.5.1-37-none-any.whl (386.2 kB view details)

Uploaded

trepan3k-1.5.1-36-none-any.whl (386.2 kB view details)

Uploaded

File details

Details for the file trepan3k-1.5.1.tar.gz.

File metadata

  • Download URL: trepan3k-1.5.1.tar.gz
  • Upload date:
  • Size: 431.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for trepan3k-1.5.1.tar.gz
Algorithm Hash digest
SHA256 c1480c5a78b0c426cde9fc9887190531b4e314dbda12d40b9a88b426b340b681
MD5 5f551091b4145fe12e10d9a4c09b5c42
BLAKE2b-256 0ce015fc03c2d7d19ddf6f21658e80288fffec54e7086cf92d19d81f89d3a7aa

See more details on using hashes here.

File details

Details for the file trepan3k-1.5.1-311-none-any.whl.

File metadata

  • Download URL: trepan3k-1.5.1-311-none-any.whl
  • Upload date:
  • Size: 386.2 kB
  • Tags:
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for trepan3k-1.5.1-311-none-any.whl
Algorithm Hash digest
SHA256 e813317a55e73dfceed2dce943a696bd8c4f2c52a151c452fdb61793ade5274c
MD5 c49b69748faa0b209e665c79ffdfd64e
BLAKE2b-256 d1fdbb3c674dad5aca5526cc90a6b886a37100af9db9e38a900fd7d6857eb4b6

See more details on using hashes here.

File details

Details for the file trepan3k-1.5.1-310-none-any.whl.

File metadata

  • Download URL: trepan3k-1.5.1-310-none-any.whl
  • Upload date:
  • Size: 386.3 kB
  • Tags:
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for trepan3k-1.5.1-310-none-any.whl
Algorithm Hash digest
SHA256 2a6e7fa9a96b8e301d180e91f16caa0ae4bd5b3056992a5be356f6b1b2a05d7e
MD5 611160da10de32cdf8d44c25ae66d1ca
BLAKE2b-256 562b1069561d1ceacf4b1ba9c1d33aa94a94b8f4ae6f178fccb67d66b306ae79

See more details on using hashes here.

File details

Details for the file trepan3k-1.5.1-39-none-any.whl.

File metadata

  • Download URL: trepan3k-1.5.1-39-none-any.whl
  • Upload date:
  • Size: 386.3 kB
  • Tags:
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for trepan3k-1.5.1-39-none-any.whl
Algorithm Hash digest
SHA256 7f93c9b15564db01eced424606ba68e7a8d1be9f89746f8144cf3ed89e4a1614
MD5 adf582ab739a90688c487ca8b3d7c3c2
BLAKE2b-256 17a622456e71d0402c55ab64e06f1fc339b017b9238ee98ab0d7e08d512c4a68

See more details on using hashes here.

File details

Details for the file trepan3k-1.5.1-38-none-any.whl.

File metadata

  • Download URL: trepan3k-1.5.1-38-none-any.whl
  • Upload date:
  • Size: 386.2 kB
  • Tags:
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for trepan3k-1.5.1-38-none-any.whl
Algorithm Hash digest
SHA256 ec8463a76324d6d829c6599d42bb912417a92367a2ddc8600bb6fa759f0114da
MD5 789ac1a5de34f866c6bfb3f1adc4e1b9
BLAKE2b-256 2c1b629cf058f417451b527881f0f01028237fa580356930782859be3dd134af

See more details on using hashes here.

File details

Details for the file trepan3k-1.5.1-37-none-any.whl.

File metadata

  • Download URL: trepan3k-1.5.1-37-none-any.whl
  • Upload date:
  • Size: 386.2 kB
  • Tags:
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for trepan3k-1.5.1-37-none-any.whl
Algorithm Hash digest
SHA256 777fe119ba9eff70153f72e948ec293e4d66dca234e1597b699aabb56d40bdb4
MD5 42b75dde97d3c1edf50e1e628f339a9e
BLAKE2b-256 bdbe7490bd26132d92b0ed77e183e532ae72777cda71ef50002ee76de94a692c

See more details on using hashes here.

File details

Details for the file trepan3k-1.5.1-36-none-any.whl.

File metadata

  • Download URL: trepan3k-1.5.1-36-none-any.whl
  • Upload date:
  • Size: 386.2 kB
  • Tags:
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for trepan3k-1.5.1-36-none-any.whl
Algorithm Hash digest
SHA256 aba78b5d8aad1d8c820add7ce0ade3f966143320b3b4eed1a0f673805a5e252f
MD5 4be029efa2a131a9fc0cb1f6d7442ad3
BLAKE2b-256 9abcc136cc46ea5ceb0b7a9e9c1381ee8b9f025615a21aed9e832a984ed17d0c

See more details on using hashes here.

Supported by

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