How to Trace Python Scripts using trace.py

More and more scripts for administrative tasks on Linux OS are written by Python. This post aims to introduce a tool to trace Python statement execution. Python is a dynamic object-oriented programming language which can be used to develop various types of software. It offers strong support for integration with other languages and tools and comes with extensive standard libraries. In Linux distributions, Python is widely used to write administrative tools, such as printer configuration package etc.

Tracing Python statement execution and record all the running codes line by line is very useful to locate the cause of an issue efficiently.

Fortunately, the python package comes with a tool trace.py, which can be used to meet those requirement. The trace.py resides in /user/lib/python2.x directory, where python2.x is the python version (e.g. python2.3 and python2.4 etc..)

# rpm -ql python |grep trace.py
/usr/lib/python2.3/trace.py
/usr/lib/python2.3/trace.pyc
/usr/lib/python2.3/trace.pyo

For example, to trace /usr/sbin/printconf-backend, the command is as follows:

# /usr/lib/python2.x/trace.py --trace /usr/sbin/printconf-backend

It will show all debug information and the python script source code on the console. We can record all the output as follows.

# script /tmp/printerconf.log
# /usr/lib/python2.x/trace.py --trace /usr/sbin/printconf-backend
# exit
#

Then check the /tmp/printerconf.log file.

Note: By default the trace.py has no execution permission. So it is needed to grant execution permission before performing the above instructions.

More Options of Trace.py

Using the option –help shows usage information in detail for trace.py. For example:

$ /usr/lib/python2.3/trace.py --help
Usage: /usr/lib/python2.3/trace.py [OPTIONS]  [ARGS]

Meta-options:
--help                Display this help then exit.
--version             Output version information then exit.

Otherwise, exactly one of the following three options must be given:
-t, --trace           Print each line to sys.stdout before it is executed.
-c, --count           Count the number of times each line is executed
                      and write the counts to .cover for each
                      module executed, in the module's directory.
                      See also `--coverdir', `--file', `--no-report' below.
-l, --listfuncs       Keep track of which functions are executed at least
                      once and write the results to sys.stdout after the
                      program exits.
-r, --report          Generate a report from a counts file; do not execute
                      any code.  `--file' must specify the results file to
                      read, which must have been created in a previous run
                      with `--count --file=FILE'.

Modifiers:
-f, --file=     File to accumulate counts over several runs.
-R, --no-report       Do not generate the coverage report files.
                      Useful if you want to accumulate over several runs.
-C, --coverdir=  Directory where the report files.  The coverage
                      report for . is written to file
                      //.cover.
-m, --missing         Annotate executable lines that were not executed
                      with '>>>>>> '.
-s, --summary         Write a brief summary on stdout for each file.
                      (Can only be used with --count or --report.)

Filters, may be repeated multiple times:
--ignore-module= Ignore the given module and its submodules
                      (if it is a package).
--ignore-dir=    Ignore files in the given directory (multiple
                      directories can be joined by os.pathsep).
Related Post