How can I obtain a call graph of a Python program?
Environment
- Red Hat Enterprise Linux Server 5
- Red Hat Enterprise Linux Server 6
Issue
I would like to obtain a callgraph of a Python application as to find the sequence of calls leading to a particular error.
Resolution
Run the application with the Content from docs.python.org is not included.trace module which is part of the python package:
$ python -m trace --trace YOUR_APPLICATION
For example to write the call trace to the console:
$ python -m trace --trace /usr/share/system-config-users/system-config-users.py
--- modulename: threading, funcname: settrace
threading.py(90): _trace_hook = func
--- modulename: system-config-users, funcname: <module>
system-config-users.py(25): import sys
system-config-users.py(26): import signal
system-config-users.py(27): import libuser
system-config-users.py(29): signal.signal (signal.SIGINT, signal.SIG_DFL)
system-config-users.py(31): import gettext
--- modulename: gettext, funcname: <module>
gettext.py(11): """
....
gettext.py(448): if lang == 'C':
gettext.py(449): break
gettext.py(456): return result
gettext.py(468): if not mofiles:
gettext.py(469): if fallback:
gettext.py(471): raise IOError(ENOENT, 'No translation file found for domain', domain)
gettext.py(541): except IOError:
gettext.py(542): return message
system-config-users requires a currently running X server.
system-config-users.py(39): sys.exit(0)
For example to write the call trace to a file:
$ python -m trace --trace /usr/share/system-config-users/system-config-users.py > /tmp/scu.python-trace.txt
SBR
Product(s)
Components
Category
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.