Prerequisites
# Used by pyrasite to inject code into a running process sudo apt-get install gdb # Used to allow pyrasite to inspect the internals of a running process sudo apt-get install python-dbg # Make sure that gdb can find debugging symbols for the virtualenv python sudo mkdir -p /usr/lib/debug/opt/edx sudo ln -s /usr/lib/debug/{usr,opt/edx}/bin # Enable ptrace echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
Pyrasite
A python library that uses gdb to inject code into a running python process. Can be used for viewing memory statistics, and also other live process inspection.
Install Pyrasite
# Install the required python libraries virtualenv memory source memory/bin/activate pip install Cython pip install pyrasite meliae urwid
Interactively View Process Memory
sudo memory/bin/pyrasite-memory-viewer <PID>
Dump Process Memory
sudo -u www-data memory/bin/pyrasite <PID> dump_memory.py less /tmp/pyrasite-*-objects.json
Clean Up
echo 1 | sudo tee /proc/sys/kernel/yama/ptrace_scope sudo apt-get remove gdb python-dbg rm -rf memory sudo rm -rf /tmp/pyrasite-*-objects.json
GdbHeap
A gdb extension that allows inspection of memory used by many python internals
Install gdbheap
sudo apt-get install libc-dbg git clone http://git.fedorahosted.org/git/gdb-heap.git
Debug a Process
> sudo PYTHONPATH=gdb-heap/ gdb attach <PID> (gdb) python import gdbheap (gdb) heap