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