Payloads.rst 3.21 KB
Newer Older
Luke Macken's avatar
Luke Macken committed
1 2 3 4 5 6
Payloads
========

Viewing the largest objects in your process
-------------------------------------------

7 8 9 10
Pyrasite provides a tool to view object memory usage statistics, and the
live value, of largest objects in your process. This requires `urwid
<http://pypi.python.org/pypi/urwid>`_ and `meliae
<https://launchpad.net/meliae>`_ to be installed.
11

12
::
Luke Macken's avatar
Luke Macken committed
13

14
    $ pyrasite-memory-viewer <PID>
Luke Macken's avatar
Luke Macken committed
15 16


17
.. image:: http://lewk.org/img/pyrasite-memory-viewer.png
Luke Macken's avatar
Luke Macken committed
18

19
.. note:: We recommend using python-meliae from your OS distribution, if available.  If it is not, you will need to first install Cython, and then meliae seperately. If pip/easy_install does not work, you may need to use the tarball from the upstream website.
Luke Macken's avatar
Luke Macken committed
20

21
This tool automatically injects the following payload:
Luke Macken's avatar
Luke Macken committed
22

23 24 25
.. literalinclude:: ../pyrasite/payloads/dump_memory.py
   :language: python
   :start-after: html
Luke Macken's avatar
Luke Macken committed
26

27
You can easily dump the object memory usage JSON data by hand, if you wish:
Luke Macken's avatar
Luke Macken committed
28

29
::
Luke Macken's avatar
Luke Macken committed
30

31
    $ pyrasite <PID> pyrasite/payloads/dump_memory.py
Luke Macken's avatar
Luke Macken committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84


Call Graph
----------

Pyrasite comes with a payload that generates an image of your processes call
graph using `pycallgraph <http://pycallgraph.slowchop.com>`_.

.. literalinclude:: ../pyrasite/payloads/start_callgraph.py
   :language: python
   :start-after: http
.. literalinclude:: ../pyrasite/payloads/stop_callgraph.py
   :language: python
   :start-after: http

::

    $ pyrasite <PID> pyrasite/payloads/start_callgraph.py
    $ pyrasite <PID> pyrasite/payloads/stop_callgraph.py

The callgraph is then generated using `graphviz <http://www.graphviz.org>`_ and
saved to `callgraph.png`. You can see an example callgraph `here <http://pycallgraph.slowchop.com/pycallgraph/wiki/RegExpExample>`_.


Viewing loaded modules
----------------------

.. literalinclude:: ../pyrasite/payloads/dump_modules.py
   :language: python

::

    $ pyrasite <PID> pyrasite/payloads/dump_modules.py

Dumping thread stacks
---------------------

.. literalinclude:: ../pyrasite/payloads/dump_stacks.py
   :language: python

::

    $ pyrasite <PID> pyrasite/payloads/dump_stacks.py

Forcing garbage collection
---------------------------

.. literalinclude:: ../pyrasite/payloads/force_garbage_collection.py
   :language: python

::

    $ pyrasite <PID> pyrasite/payloads/force_garbage_collection.py
Luke Macken's avatar
Luke Macken committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

Reverse Subprocess Shell
------------------------

.. literalinclude:: ../pyrasite/payloads/reverse_shell.py
   :language: python
   :start-after: Copyright

::

    $ pyrasite <PID> pyrasite/payloads/reverse_shell.py

::

    $ nc -l 9001
    Linux tomservo 2.6.40.3-0.fc15.x86_64 #1 SMP Tue Aug 16 04:10:59 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux
    % ls

Reverse Python Shell
--------------------

.. deprecated:: 2.0
   Use the `pyrasite-shell <http://readthedocs.org/docs/pyrasite/en/latest/Shell.html>`_ instead

This lets you easily introspect or alter any objects in your running process.

.. literalinclude:: ../pyrasite/payloads/reverse_python_shell.py
   :language: python
   :start-after: Copyright

::

    $ python
    >>> x = 'foo'

::

    $ pyrasite <PID> pyrasite/payloads/reverse_python_shell.py

::

    $ nc -l 9001
    Python 2.7.1 (r271:86832, Apr 12 2011, 16:15:16)
    [GCC 4.6.0 20110331 (Red Hat 4.6.0-2)]
    >>> print x
    foo
    >>> globals()['x'] = 'bar'