Skip to main content

A simple method to interactively preempt a running script to run a debugger

Project description

preemptive_debugger

A simple method to interactively preempt a running script to run a debugger

preemptive_debugger

Installing

Install:

pip install preemptive_debugger

Usage

In one terminal, create a file called test.py with the following:

import preemptive_debugger

import time

for i in range(10_000):
    print(f"i = {i}")
    time.sleep(0.1)

Run this code with:

python3 test.py

In another terminal, run:

kill -SIGUSR1 $(pgrep -f "python3 test.py")

This will preempt the running script and start a debugger in the original terminal, which can be used to analyze what is happening. This is very useful when you have a script that is stuck or doing strange things. Without this package, if you want to see what is going on in the script, you would typically need to stop the script, modify the code, and try your best to reproduce the result (or wait a long time to have the issue come back up). With this package, adding just one line (import preemptive_debugger), you can easily start a debugger whenever you want to look into what is happening in a python program.

Notes

I have tried some alternative methods to interactively choose when to start a debugger, but they didn't work well. Some pip packages I have tried:

  • keyboard: Tried to start a debugger when a keyboard key was pressed, but I got this error which was very inconvenient:
ImportError: You must be root to use this library on linux.
  • pynput: Tried to start a debugger when a keyboard key was pressed, but I realized this starts a new thread to run the handler. This does not allow me to properly analyze the state of the program I am interested in.

  • madbg: Tried attaching to the script with madbg attach <pid>, but this only worked for very simple scripts. For any nontrivial script, I got this error:

pyinjector.api.LinuxInjectorPermissionError: Injector failed with -8 calling injector_attach: PTRACE_ATTACH error : Operation not permitted
Failed attaching to process due to permission error.
This is most likely due to ptrace scope limitations applied to the kernel for security purposes.
Possible solutions:
 - Rerun as root
 - Temporarily remove ptrace scope limitations usin
`echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope`
 - Persistently remove ptrace scope limitations by editing /etc/sysctl.d/10-ptrace.conf
More details can be found here: https://stackoverflow.com/q/19215177/2907819')

Running with sudo, I got this error:

pyinjector.api.InjectorError: Injector failed with -5 calling injector_inject: dlopen failed

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

preemptive_debugger-0.0.1.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

preemptive_debugger-0.0.1-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file preemptive_debugger-0.0.1.tar.gz.

File metadata

  • Download URL: preemptive_debugger-0.0.1.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.12

File hashes

Hashes for preemptive_debugger-0.0.1.tar.gz
Algorithm Hash digest
SHA256 d4f3d834070785ebbb9ad0feed9f0c627ab461966683d71e2d7c189929cbf493
MD5 751f9f9a1d7e1bbe9fce43b69d550133
BLAKE2b-256 d97f1e43a0e96867de8e5678556230da2f4e836d3d19bf4c6236894767e56428

See more details on using hashes here.

File details

Details for the file preemptive_debugger-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for preemptive_debugger-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 46a04ea4c9065dbcd682bb7c4535dd1122bd793409c3bdbfa0e9258e4227ac94
MD5 18b3cbeb3c5543aca7eea94fa90680d0
BLAKE2b-256 74e9cb01c1ea0f8d04f84c253f6078e19a3374deabba103c45a733e5869ef38a

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