til: docker_gdb-python-docker.md
This data as json
| path | topic | title | url | body | html | shot | created | created_utc | updated | updated_utc | shot_hash | slug |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| docker_gdb-python-docker.md | docker | Running gdb against a Python process in a running Docker container | https://github.com/simonw/til/blob/main/docker/gdb-python-docker.md | While investigating [Datasette issue #1268](https://github.com/simonw/datasette/issues/1268) I found myself with a Python process that was hanging, and I decided to try running `gdb` against it based on tips in [Debugging of CPython processes with gdb](https://www.podoliaka.org/2016/04/10/debugging-cpython-gdb/) Here's the recipe that worked: 1. Find the Docker container ID using `docker ps` - in my case it was `16197781a7b5` 2. Attach a new bash shell to that process in privileged mode (needed to get `gdb` to work): `docker exec --privileged -it 16197781a7b5 bash` 3. Install `gdb` and the Python tooling for using it: `apt-get install gdb python3-dbg` 4. Use `top` to find the pid of the running Python process that was hanging. It was `20` for me. 5. Run `gdb /usr/bin/python3 -p 20` to launch `gdb` against that process 6. In the `(gdb)` prompt run `py-bt` to see a backtrace. I'm sure there's lots more that can be done in `gdb` at this point, but that's how I got to a place where I could interact with the Python process that was running in the Docker container. | <p>While investigating <a href="https://github.com/simonw/datasette/issues/1268">Datasette issue #1268</a> I found myself with a Python process that was hanging, and I decided to try running <code>gdb</code> against it based on tips in <a href="https://www.podoliaka.org/2016/04/10/debugging-cpython-gdb/" rel="nofollow">Debugging of CPython processes with gdb</a></p> <p>Here's the recipe that worked:</p> <ol> <li>Find the Docker container ID using <code>docker ps</code> - in my case it was <code>16197781a7b5</code> </li> <li>Attach a new bash shell to that process in privileged mode (needed to get <code>gdb</code> to work): <code>docker exec --privileged -it 16197781a7b5 bash</code> </li> <li>Install <code>gdb</code> and the Python tooling for using it: <code>apt-get install gdb python3-dbg</code> </li> <li>Use <code>top</code> to find the pid of the running Python process that was hanging. It was <code>20</code> for me.</li> <li>Run <code>gdb /usr/bin/python3 -p 20</code> to launch <code>gdb</code> against that process</li> <li>In the <code>(gdb)</code> prompt run <code>py-bt</code> to see a backtrace.</li> </ol> <p>I'm sure there's lots more that can be done in <code>gdb</code> at this point, but that's how I got to a place where I could interact with the Python process that was running in the Docker container.</p> | <Binary: 96,623 bytes> | 2021-03-21T22:48:21-07:00 | 2021-03-22T05:48:21+00:00 | 2021-03-21T22:48:21-07:00 | 2021-03-22T05:48:21+00:00 | 7b9f40a0c261e3d630b151d72313cabc | gdb-python-docker |