Debugging running Python scripts with PDB via GDB
The blog post explains using GDB to debug running Python scripts, emphasizing the need for caution in production environments and suggesting further development for a more reliable debugging solution.
Read original articleThe blog post discusses a method for debugging running Python scripts using GDB (GNU Project Debugger) in conjunction with PDB (Python Debugger). It highlights that CPython, the reference implementation of Python, is written in C and can be debugged like any native program. The author provides an example of a Python script that can be debugged while it is running. By attaching GDB to the Python interpreter process, users can access detailed debugging information, especially when debug symbols are enabled. The author explains how to set a breakpoint on the PyEval_SaveThread function and execute arbitrary Python code in the current execution frame using PyRun_SimpleString. While this method has been successful in testing, the author cautions against using it in production environments due to potential risks of corrupting the CPython state or crashing the script. Additionally, if the script runs without a terminal, it may require redirecting stdin and stdout for interaction. The author expresses interest in further developing a more reliable solution for this debugging technique.
- GDB can be used to debug running Python scripts by attaching to the interpreter process.
- Debugging symbols enhance the information available during debugging sessions.
- Breakpoints can be set in GDB to execute Python code in the current execution frame.
- Caution is advised against using this method in production due to potential risks.
- Further development of a more reliable debugging solution is suggested.
Related
Making Python Less Random
Andrew Healey detailed his debugging journey with a Python game prototype facing randomness issues. He used ptrace to control system calls, ensuring consistent results without changing the code, showcasing advanced system call tracing techniques.
Making Python Less Random
Andrew Healey discussed debugging a Python game prototype with randomness bugs. He used ptrace to control system calls, ensuring consistent results for random functions without altering the code, enabling deterministic debugging.
Free-threaded CPython is ready to experiment with
CPython 3.13 introduces free-threading to enhance performance by allowing parallel threads without the GIL. Challenges like thread-safety and ABI compatibility are being addressed for future adoption as the default build.
Instrumenting Python GIL with eBPF
The Global Interpreter Lock (GIL) in Python simplifies memory management but hinders performance in multi-threaded, CPU-bound programs. Coroot explains GIL's impact on Python apps, measuring it with eBPF for insights and monitoring efficiency.
UndoDB Travel Debugging for C/C++
UDB is a time travel debugger for C/C++ on Linux, enabling live process debugging and execution replay. It aids in resolving complex bugs and integrates with popular IDEs, enhancing productivity.
import signal
def sigusr1_handler(signum, stack):
breakpoint()
signal.signal(signal.SIGUSR1, sigusr1_handler)
And then I can drop into pdb by sending a signal: $ kill -s sigusr1 $PID
I also want to highlight something non-obvious about debugging mixed native and Python code. Because Python is interpreted, it is possible to run both gdb and pdb on a process at the same time, and set breakpoints using both, without issue. This can be really nice when you're working on Python bindings.Related
Making Python Less Random
Andrew Healey detailed his debugging journey with a Python game prototype facing randomness issues. He used ptrace to control system calls, ensuring consistent results without changing the code, showcasing advanced system call tracing techniques.
Making Python Less Random
Andrew Healey discussed debugging a Python game prototype with randomness bugs. He used ptrace to control system calls, ensuring consistent results for random functions without altering the code, enabling deterministic debugging.
Free-threaded CPython is ready to experiment with
CPython 3.13 introduces free-threading to enhance performance by allowing parallel threads without the GIL. Challenges like thread-safety and ABI compatibility are being addressed for future adoption as the default build.
Instrumenting Python GIL with eBPF
The Global Interpreter Lock (GIL) in Python simplifies memory management but hinders performance in multi-threaded, CPU-bound programs. Coroot explains GIL's impact on Python apps, measuring it with eBPF for insights and monitoring efficiency.
UndoDB Travel Debugging for C/C++
UDB is a time travel debugger for C/C++ on Linux, enabling live process debugging and execution replay. It aids in resolving complex bugs and integrates with popular IDEs, enhancing productivity.