{"id":17644732,"url":"https://github.com/jschwinger233/pdbattach","last_synced_at":"2025-05-07T04:11:37.617Z","repository":{"id":39453314,"uuid":"451792368","full_name":"jschwinger233/pdbattach","owner":"jschwinger233","description":"Attach pdb to a running Python process.","archived":false,"fork":false,"pushed_at":"2022-06-26T07:46:15.000Z","size":41,"stargazers_count":22,"open_issues_count":3,"forks_count":0,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-05-07T04:11:29.566Z","etag":null,"topics":["attach","debug","gdb","pdb","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jschwinger233.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-01-25T08:19:46.000Z","updated_at":"2024-11-22T09:02:27.000Z","dependencies_parsed_at":"2022-09-20T03:03:24.787Z","dependency_job_id":null,"html_url":"https://github.com/jschwinger233/pdbattach","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jschwinger233%2Fpdbattach","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jschwinger233%2Fpdbattach/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jschwinger233%2Fpdbattach/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jschwinger233%2Fpdbattach/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jschwinger233","download_url":"https://codeload.github.com/jschwinger233/pdbattach/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252810274,"owners_count":21807759,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["attach","debug","gdb","pdb","python"],"created_at":"2024-10-23T10:39:47.157Z","updated_at":"2025-05-07T04:11:37.593Z","avatar_url":"https://github.com/jschwinger233.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pdbattach\n\nAttach pdb to a running Python process.\n\n## Install\n\n```\npython3.8 -mpip install git+https://github.com/jschwinger233/pdbattach.git\n```\n\n## Usage\n\n### check process stack\n\n```\nsudo pdbattach -p $pid -c 'import traceback; f=open(\"/tmp/bt\", \"w+\"); print(\"\".join(traceback.format_stack()), file=f, flush=True); f.close()'\n```\n\nThen you will find the process's stack backtrace at `/tmp/bt` like:\n\n```\n  File \"/usr/lib/python3.8/runpy.py\", line 194, in _run_module_as_main\n    return _run_code(code, main_globals, None,\n  File \"/usr/lib/python3.8/runpy.py\", line 87, in _run_code\n    exec(code, run_globals)\n  File \"/usr/lib/python3.8/http/server.py\", line 1294, in \u003cmodule\u003e\n    test(\n  File \"/usr/lib/python3.8/http/server.py\", line 1257, in test\n    httpd.serve_forever()\n  File \"/usr/lib/python3.8/socketserver.py\", line 232, in serve_forever\n    ready = selector.select(poll_interval)\n  File \"/usr/lib/python3.8/selectors.py\", line 415, in select\n    fd_event_list = self._selector.poll(timeout)\n  File \"\u003cstring\u003e\", line 1, in \u003cmodule\u003e\n```\n\n### interactive debugging using pdb\n\nSuppose we have a `python3.9` process running with command `python3.9 test.py`, where `test.py` is a simple script:\n\n```python\nimport time\nfor i in range(1000):\n    print(i)\n    time.sleep(10)\n```\n\nthen we can attach it:\n\n```bash\n$ sudo pdbattach -p $(pgrep -f test.py)\n--Return--\n\u003e \u003cstring\u003e(1)\u003cmodule\u003e()-\u003eNone\n(Pdb) b test.py:3\nBreakpoint 1 at /home/gray/Dropbox/mac.local/Documents/src/github.com/jschwinger233/pdbattach/test.py:3\n(Pdb) c\n\u003e /home/gray/Dropbox/mac.local/Documents/src/github.com/jschwinger233/pdbattach/test.py(3)\u003cmodule\u003e()-\u003eNone\n-\u003e print(i)\n(Pdb) p i\n5\n(Pdb) l\n  1  \timport time\n  2  \tfor i in range(1000):\n  3 B-\u003e\t   print(i)\n  4  \t   time.sleep(10)\n[EOF]\n(Pdb) q\n```\n\n### inject any code snippets\n\nSuppose we have a simple HTTP server causing memory leak, and we want to use [memray](https://github.com/bloomberg/memray) to do a memory profile by attaching.\n\nFirstly let's enable the tracker:\n\n```bash\nsudo pdbattach -p $(pidof python3.9) -c 'import memray; global t; t = memray.Tracker(\"out.bin\"); t.__enter__(); print(t)'\n```\n\nYou can also put the statements in a script file and run by:\n\n```bash\nsudo pdbattach -p $(pidof python3.9) -f mem_track.py\n```\n\nSuppose the output of the above injection is:\n\n```\nMemray WARNING: Correcting symbol for malloc from 0x421420 to 0x7f0400389110\nMemray WARNING: Correcting symbol for free from 0x421890 to 0x7f0400389700\n\u003cmemray._memray.Tracker object at 0x7f03ff6598d0\u003e\n```\n\nThen after a while, stop the tracker and inspect the outcomes:\n\n```bash\nsudo pdbattach -p $(pidof python3.9) -c 'import ctypes; ctypes.cast(0x7f03ff6598d0, ctypes.py_object).value.__exit__(None, None, None)'\n```\n\n## Known Issues\n\n1. pdb doesn't work properly under multi-thread scenarios. See [issue](https://bugs.python.org/issue41571).\n2. ptrace(2) relies on `struct user_regs_struct` whose definition varies across platforms, therefore running pdbattach inside a container (e.g. from docker.io/python:3 image) to attach a host process will cause segmentation fault. See [issue](https://github.com/jschwinger233/pdbattach/issues/4).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjschwinger233%2Fpdbattach","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjschwinger233%2Fpdbattach","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjschwinger233%2Fpdbattach/lists"}