Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`trace` --- Trace or track Python statement execution |
| 3 | ========================================================== |
| 4 | |
| 5 | .. module:: trace |
| 6 | :synopsis: Trace or track Python statement execution. |
| 7 | |
| 8 | |
| 9 | The :mod:`trace` module allows you to trace program execution, generate |
| 10 | annotated statement coverage listings, print caller/callee relationships and |
| 11 | list functions executed during a program run. It can be used in another program |
| 12 | or from the command line. |
| 13 | |
Raymond Hettinger | e0e0822 | 2010-11-06 07:10:31 +0000 | [diff] [blame] | 14 | .. seealso:: |
| 15 | |
| 16 | Latest version of the `trace module Python source code |
| 17 | <http://svn.python.org/view/python/branches/release27-maint/Lib/trace.py?view=markup>`_ |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 18 | |
| 19 | .. _trace-cli: |
| 20 | |
| 21 | Command Line Usage |
| 22 | ------------------ |
| 23 | |
| 24 | The :mod:`trace` module can be invoked from the command line. It can be as |
| 25 | simple as :: |
| 26 | |
| 27 | python -m trace --count somefile.py ... |
| 28 | |
| 29 | The above will generate annotated listings of all Python modules imported during |
| 30 | the execution of :file:`somefile.py`. |
| 31 | |
| 32 | The following command-line arguments are supported: |
| 33 | |
| 34 | :option:`--trace`, :option:`-t` |
| 35 | Display lines as they are executed. |
| 36 | |
| 37 | :option:`--count`, :option:`-c` |
| 38 | Produce a set of annotated listing files upon program completion that shows how |
| 39 | many times each statement was executed. |
| 40 | |
| 41 | :option:`--report`, :option:`-r` |
| 42 | Produce an annotated list from an earlier program run that used the |
| 43 | :option:`--count` and :option:`--file` arguments. |
| 44 | |
| 45 | :option:`--no-report`, :option:`-R` |
| 46 | Do not generate annotated listings. This is useful if you intend to make |
| 47 | several runs with :option:`--count` then produce a single set of annotated |
| 48 | listings at the end. |
| 49 | |
| 50 | :option:`--listfuncs`, :option:`-l` |
| 51 | List the functions executed by running the program. |
| 52 | |
| 53 | :option:`--trackcalls`, :option:`-T` |
| 54 | Generate calling relationships exposed by running the program. |
| 55 | |
| 56 | :option:`--file`, :option:`-f` |
| 57 | Name a file containing (or to contain) counts. |
| 58 | |
| 59 | :option:`--coverdir`, :option:`-C` |
| 60 | Name a directory in which to save annotated listing files. |
| 61 | |
| 62 | :option:`--missing`, :option:`-m` |
| 63 | When generating annotated listings, mark lines which were not executed with |
| 64 | '``>>>>>>``'. |
| 65 | |
| 66 | :option:`--summary`, :option:`-s` |
| 67 | When using :option:`--count` or :option:`--report`, write a brief summary to |
| 68 | stdout for each file processed. |
| 69 | |
| 70 | :option:`--ignore-module` |
Facundo Batista | 873c985 | 2008-01-19 18:38:19 +0000 | [diff] [blame] | 71 | Accepts comma separated list of module names. Ignore each of the named |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 72 | module and its submodules (if it is a package). May be given |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 73 | multiple times. |
| 74 | |
| 75 | :option:`--ignore-dir` |
Facundo Batista | 873c985 | 2008-01-19 18:38:19 +0000 | [diff] [blame] | 76 | Ignore all modules and packages in the named directory and subdirectories |
| 77 | (multiple directories can be joined by os.pathsep). May be given multiple |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 78 | times. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 79 | |
| 80 | |
| 81 | .. _trace-api: |
| 82 | |
| 83 | Programming Interface |
| 84 | --------------------- |
| 85 | |
| 86 | |
Neal Norwitz | ca37661 | 2008-02-26 08:21:28 +0000 | [diff] [blame] | 87 | .. class:: Trace([count=1[, trace=1[, countfuncs=0[, countcallers=0[, ignoremods=()[, ignoredirs=()[, infile=None[, outfile=None[, timing=False]]]]]]]]]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 88 | |
| 89 | Create an object to trace execution of a single statement or expression. All |
| 90 | parameters are optional. *count* enables counting of line numbers. *trace* |
| 91 | enables line execution tracing. *countfuncs* enables listing of the functions |
| 92 | called during the run. *countcallers* enables call relationship tracking. |
| 93 | *ignoremods* is a list of modules or packages to ignore. *ignoredirs* is a list |
| 94 | of directories whose modules or packages should be ignored. *infile* is the |
| 95 | file from which to read stored count information. *outfile* is a file in which |
Neal Norwitz | ca37661 | 2008-02-26 08:21:28 +0000 | [diff] [blame] | 96 | to write updated count information. *timing* enables a timestamp relative |
| 97 | to when tracing was started to be displayed. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 98 | |
| 99 | |
| 100 | .. method:: Trace.run(cmd) |
| 101 | |
| 102 | Run *cmd* under control of the Trace object with the current tracing parameters. |
| 103 | |
| 104 | |
| 105 | .. method:: Trace.runctx(cmd[, globals=None[, locals=None]]) |
| 106 | |
| 107 | Run *cmd* under control of the Trace object with the current tracing parameters |
| 108 | in the defined global and local environments. If not defined, *globals* and |
| 109 | *locals* default to empty dictionaries. |
| 110 | |
| 111 | |
| 112 | .. method:: Trace.runfunc(func, *args, **kwds) |
| 113 | |
| 114 | Call *func* with the given arguments under control of the :class:`Trace` object |
| 115 | with the current tracing parameters. |
| 116 | |
| 117 | This is a simple example showing the use of this module:: |
| 118 | |
| 119 | import sys |
| 120 | import trace |
| 121 | |
| 122 | # create a Trace object, telling it what to ignore, and whether to |
| 123 | # do tracing or line-counting or both. |
| 124 | tracer = trace.Trace( |
| 125 | ignoredirs=[sys.prefix, sys.exec_prefix], |
| 126 | trace=0, |
| 127 | count=1) |
| 128 | |
| 129 | # run the new command using the given tracer |
| 130 | tracer.run('main()') |
| 131 | |
| 132 | # make a report, placing output in /tmp |
| 133 | r = tracer.results() |
| 134 | r.write_results(show_missing=True, coverdir="/tmp") |
| 135 | |