Debugging the python VM

Question:

Is there a debugger that can debug the Python virtual machine while it is running Python code, similar to the way that GDB works with C/C++? I have searched online and have come across pdb, but this steps through the code executed by the Python interpreter, not the Python interpreter as its running the program.

Asked By: IT Ninja

||

Answers:

The reference implementation of Python, CPython, is written in C. You can use GDB to debug it as you would debug any other program written in C.

That said, Python does have a few little helpers for use in GDB buried under Misc/gdbinit. It’s got comments to describe what each command does, but I’ll repeat them here for convenience:

  • pyo: Dump a PyObject *.
  • pyg: Dump a PyGC_Head *.
  • pylocals: Print the local variables of the current Python stack frame.
  • lineno: Get the current Python line number.
  • pyframe: Print the source file name, line, and function.
  • pyframev: pyframe + pylocals
  • printframe: pyframe if within PyEval_EvalFrameEx; built-in frame otherwise
  • pystack: Print the Python stack trace.
  • pystackv: Print the Python stack trace with local variables.
  • pu: Print a Unicode string.

It looks like the Fedora project has also assembled their own collection of commands to assist with debugging which you may want to look at, too.

Answered By: icktoofay

If you’re looking to debug Python at the bytecode level, that’s exactly what pdb does.

If you’re looking to debug the CPython reference interpreter… as icktoofay’s answer says, it’s just a C program like any other, so you can debug it the same way as any other C program. (And you can get the source, compile it with extra debugging info, etc. if you want, too.)

You almost certainly want to look at EasierPythonDebugging, which shows how to set up a bunch of GDB helpers (which are Python scripts, of course) to make your life easier. Most importantly: The Python stack is tightly bound to the C stack, but it’s a big mess to try to map things manually. With the right helpers, you can get stack traces, frame dumps, etc. in Python terms instead of or in parallel with the C terms with no effort. Another big benefit is the py-print command, which can look up a Python name (in nearly the same way a live interpreter would), call its __repr__, and print out the result (with proper error handling and everything so you don’t end up crashing your gdb session trying to walk the PyObject* stuff manually).

If you’re looking for some level in between… well, there is no level in between. (Conceptually, there are multiple layers to the interpreter, but it’s all just C code, and it all looks alike to gdb.)

If you’re looking to debug any Python interpreter, rather than specifically CPython, you might want to look at PyPy. It’s written in a Python-like language called RPython, and there are various ways to use pdb to debug the (R)Python interpreter code, although it’s not as easy as it could be (unless you use a flat-translated PyPy, which will probably run about 100x too slow to be tolerable). There are also GDB debug hooks and scripts for PyPy just like the ones for CPython, but they’re not as complete.

Answered By: abarnert

Hoping my answer to this question is still relevant.

As rightly said in the above answers, you can debug the Cpython VM via any C debugger.

A solution I found was to debug via Visual Studio (not VS Code). (for Windows only)

(The below answer is a somewhat summary of the link
https://devguide.python.org/getting-started/setup-building/ )

You can clone the code from GitHub (https://github.com/python/cpython) and follow the instruction to build it in VS as stated here

(https://web.archive.org/web/20220907075854/https://cpython-core-tutorial.readthedocs.io/en/latest/build_cpython_windows.html)

Create a breakpoint inside python.c and you are good to go.

I hope it works out !!!

Answered By: daniel
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.