• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to Trace Python Scripts using trace.py

by admin

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).

Filed Under: DevOps, Linux, Python

Some more articles you might also be interested in …

  1. kvm-img: command not found
  2. Apache HTTP server – most commonly used containers (special configuration directives)
  3. How to create partitions and file systems on DM-Multipath devices
  4. How to disable FIPS mode on CentOS/RHEL 7
  5. reboot Command Examples in Linux
  6. lslocks Command Examples in Linux
  7. pacman-key Command Examples
  8. How to Setup a squid proxy server on CentOS/RHEL 7
  9. How To Configure Separate Override.conf For Multiple MySQL Instances Using Systemd
  10. How to control resource (cgroup) with systemd for user process group in CentOS/RHEL 7

You May Also Like

Primary Sidebar

Recent Posts

  • qsub Command Examples in Linux
  • qsub: command not found
  • qrcp Command Examples in Linux
  • qmrestore Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright