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