• Stars
    star
    105
  • Rank 327,391 (Top 7 %)
  • Language
    Python
  • Created over 13 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

attach to running Python process

pydbattach - attach to running Python process

It probably should have been called pydbgattach or so but I didn't noticed until I went to bed so I keep this name now. :)

This works in several steps:

  1. We are attaching to the running Python process and want to inject some C-code pyinjectcode.c.

    Originally I planned to develop a small tool for this by myself based on ptrace. For Mac, I also found mach_inject which may have been useful.

    However, to keep things simple for now, I just use GDB for this.

    See the file pydbattach.sh which basically does this step.

  2. pyinjectcode.c creates a new Python thread and runs a Python script. In our case, it runs pyinjectcode.py.

  3. pyinjectcode.py starts a Pdb instance and attaches it to another already running Python process (just the first it founds for now). This is done via a more generic sys.settrace function which is implemented in pythreadhacks.py.

  4. pythreadhacks.py heavily uses (_)ctypes to access the underlying CPython objects for the thread state. This way, it reimplements PyEval_SetTrace in a more general way.


Similar alternatives, to attach to running CPython process without having it prepared beforehand. Those usually use the OS debugging capabilities (or use gdb/lldb) to attach to the native CPython process and then inject some code or just analyze the native CPython thread stacks.

There are other alternatives where you prepare your Python script beforehand to listen on some (tcp/file) socket to provide an interface for remote debugging and/or just a Python shell / REPL.

Some overviews and collected code examples:

-- Albert Zeyer, http://www.az2000.de

More Repositories

1

music-player

Music player - endlessly plays your music
Python
489
star
2

PyCParser

C parser and interpreter written in Python with automatic ctypes interface generation
Python
345
star
3

openlierox

OpenLieroX - Liero clone / Worms realtime / 2D shooter
C++
261
star
4

mouse-scroll-wheel-acceleration-userspace

Mouse scroll wheel acceleration in user space
Python
129
star
5

python-embedded

Python build script to create a small embeddable static Python lib
C
82
star
6

music-player-core

Music player core Python module
C++
75
star
7

RandomFtpGrabber

Random FTP grabber - downloads all the interesting stuff
Python
59
star
8

playground

some space to try things out
Python
50
star
9

py_better_exchook

nice Python exception hook replacement
Python
40
star
10

commandergenius

Commander Genius - Commander Keen compatible clone -- main repo here: https://github.com/gerstrong/Commander-Genius
C++
40
star
11

python-preloaded

Bundle Python executable with preloaded modules
Python
40
star
12

Pyjector

inject PyTerminal into any running application via SIMBL
Objective-C
36
star
13

simbl

SIMBL is the SIMple Bundle Loader.
C
34
star
14

helpers

some small helper tools
Python
34
star
15

timecapture

captures your current topmost application and the URL/file with timestamps
Python
32
star
16

PySDL

PySDL is a pure Python SDL binding via on-the-fly ctypes generation
Python
29
star
17

wiki

some useful information
28
star
18

PythonHotswap

Hotswap Python functions. And persistence of runtime.
Python
28
star
19

grub-fuse

GRUB filesystem support <-> FUSE bridge
C
26
star
20

Xee

lightweight, fast and convenient image viewer and browser for MacOSX
C
24
star
21

png-db

Database for PNG images
C
23
star
22

dotfiles

my dotfiles
Shell
23
star
23

google-books-export

export PDF from Google Books preview
Python
21
star
24

Coding-Game-Intro

Coding Introduction / Tutorial, mostly by writing small little fun games
21
star
25

PyTerminal

Python Terminal framework - embed a Python GUI Terminal in your app
Objective-C
20
star
26

chrome-ext-google-takeout-downloader

JavaScript
19
star
27

learn-midi

Learn-midi learns to procude MIDI output based on any audio input
Python
17
star
28

background-zmq-ipython

Background ZMQ IPython/Jupyter kernel
Python
17
star
29

PyCPython

interpret CPython in pure Python
Python
16
star
30

IdeaResolveSymlinks

Java
14
star
31

mac-suppress-mouse-keyboard

Suppress mouse & keyboard events on MacOSX. Baby-proof my Mac!
Objective-C
14
star
32

PyLua

interpret Lua (the C source code) in pure Python with PyCParser
C
14
star
33

binstruct

binary structure serialization (like binary JSON or BSON) with cryptography
Python
10
star
34

FScriptAnywhereSIMBL

FScript Anywhere via SIMBL
Objective-C
10
star
35

RemoteControl

secure remote control of your computer from your iPhone
Python
10
star
36

PyOverheadGame

simple Python game with 2D top-down perspective
Scilab
9
star
37

smf-openid-server

SMF OpenID server
PHP
9
star
38

navit

Navit is a car navigation system with routing engine
C
9
star
39

planet_wars-cpp

Google AI challenge: Planet Wars game engine and co, C++ implementation
C++
9
star
40

timidity-macosx

TiMidity++ for MacOSX
C
8
star
41

pyclibrary

pure-python C parser + ctypes automation library
Python
8
star
42

literal-python-to-pickle

Convert literal Python code to binary Pickle -- faster ast.literal_eval
C++
7
star
43

backup_system

Python
6
star
44

TeXShop

Objective-C
5
star
45

system-tools

Python
5
star
46

pynsh

the Python dynamic shell
JavaScript
4
star
47

AutoSleep

automatically suspend not-needed processes - safe battery
Python
4
star
48

iphone-ssh-relay

iPhone port/SSH relay daemon and client for USB connections
C
4
star
49

CPython

fork of CPython - to try out own hacks
Python
4
star
50

chromehacking

Chrome hacking
Python
4
star
51

screenshooting

scripts for handling screenshots and analyzing them
Python
4
star
52

textmate

TextMate is a graphical text editor for OS X 10.7+
C
4
star
53

foreground_app_info

get details about the application and opened url which is in foreground
Python
3
star
54

ChromeWebApps

turn a website into a native-like app on your Mac
Python
3
star
55

SaveTheRock

Save The Rock! is a 2D platformer bundled with a user-friendly in-game level editor
C++
3
star
56

py-tui-editor

Simple Python terminal (TUI) multi-line editor
Python
3
star
57

iphone-backup

iPhone backup scripts
Shell
3
star
58

intelligent-patcher

scripts to patch source code in an intelligent way
Python
3
star
59

clang

copy of git://repo.or.cz/clang.git
C++
3
star
60

WebAudioPlayer

audio player in HTML/JS
JavaScript
3
star
61

getraenke

Getränkeabrechnungssystem fürs Wohnheim
Python
3
star
62

9eyes-fetcher

fetches all images from 9-eyes.com
Python
3
star
63

ImageViewer

view images
C++
2
star
64

play-with-the-brain

PyBrain experiments
Python
2
star
65

openlierox-distrib

OpenLieroX - distrib scripts & files
C++
2
star
66

planetwars

PlanetWars Google AI Challenge 2010
Python
2
star
67

reiserfs-fuse

ReiserFS for FUSE
C
2
star
68

PictureSlider

MacOSX picture screen saver
Objective-C
2
star
69

turingmachine

Non-deterministic Turing machine simulator framework
C++
2
star
70

MathExercisesJavaAppletBase

Java (Applet) code base for math exercises
Java
2
star
71

zinssaetze

Python
2
star
72

tor

C
2
star
73

automatic_object_locking

demonstrating how the GIL could be removed in Python
C++
2
star
74

google-contacts-sync

Google contact syncing and merging
Python
2
star
75

diplom-thesis-automata

Diplom thesis for Computer Science in Automata theory
TeX
1
star
76

memos

manage memos
Python
1
star
77

ParseOggVorbis

C++ OGG Vorbis decoder
C
1
star
78

errordb

error message database - with list of solutions to each error
1
star
79

live-helper

Python
1
star
80

PyJitDev

Just-in-Time Python development
Python
1
star
81

ffmpeg

build tools, Xcode project
C
1
star
82

google-breakpad

OpenLieroX Google Breakpad copy
C++
1
star
83

diplom-thesis-math

Diplom thesis for Mathematics
Python
1
star
84

sdl

copy of SDL svn trunk
C
1
star
85

itunes-scripts

some simple Python scripts for iTunes and the iTunes music library
Python
1
star
86

streamplay

Objective-C
1
star
87

Sport-RWTH-Aachen

exaktes Kursprogramm (nach Zeit) von Sportkursen vom Hochschulsport der RWTH Aachen
1
star