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 | |
Raymond Hettinger | 1048094 | 2011-01-10 03:26:08 +0000 | [diff] [blame^] | 7 | **Source code:** :source:`Lib/trace.py` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 14 | .. _trace-cli: |
| 15 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 16 | Command-Line Usage |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | ------------------ |
| 18 | |
| 19 | The :mod:`trace` module can be invoked from the command line. It can be as |
| 20 | simple as :: |
| 21 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 22 | python -m trace --count -C . somefile.py ... |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 23 | |
Éric Araujo | 67f0b6c | 2010-12-15 19:30:15 +0000 | [diff] [blame] | 24 | The above will execute :file:`somefile.py` and generate annotated listings of |
| 25 | all Python modules imported during the execution into the current directory. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 27 | .. program:: trace |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 28 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 29 | .. cmdoption:: --help |
| 30 | |
| 31 | Display usage and exit. |
| 32 | |
| 33 | .. cmdoption:: --version |
| 34 | |
| 35 | Display the version of the module and exit. |
| 36 | |
| 37 | Main options |
| 38 | ^^^^^^^^^^^^ |
| 39 | |
Éric Araujo | 67f0b6c | 2010-12-15 19:30:15 +0000 | [diff] [blame] | 40 | At least one of the following options must be specified when invoking |
| 41 | :mod:`trace`. The :option:`--listfuncs <-l>` option is mutually exclusive with |
| 42 | the :option:`--trace <-t>` and :option:`--counts <-c>` options . When |
| 43 | :option:`--listfuncs <-l>` is provided, neither :option:`--counts <-c>` nor |
| 44 | :option:`--trace <-t>` are accepted, and vice versa. |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 45 | |
| 46 | .. program:: trace |
| 47 | |
| 48 | .. cmdoption:: -c, --count |
| 49 | |
| 50 | Produce a set of annotated listing files upon program completion that shows |
Éric Araujo | 67f0b6c | 2010-12-15 19:30:15 +0000 | [diff] [blame] | 51 | how many times each statement was executed. See also |
| 52 | :option:`--coverdir <-C>`, :option:`--file <-f>` and |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 53 | :option:`--no-report <-R>` below. |
| 54 | |
| 55 | .. cmdoption:: -t, --trace |
| 56 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 57 | Display lines as they are executed. |
| 58 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 59 | .. cmdoption:: -l, --listfuncs |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 60 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 61 | Display the functions executed by running the program. |
| 62 | |
| 63 | .. cmdoption:: -r, --report |
| 64 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 65 | Produce an annotated list from an earlier program run that used the |
Éric Araujo | 67f0b6c | 2010-12-15 19:30:15 +0000 | [diff] [blame] | 66 | :option:`--count <-c>` and :option:`--file <-f>` option. This does not |
| 67 | execute any code. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 68 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 69 | .. cmdoption:: -T, --trackcalls |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 70 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 71 | Display the calling relationships exposed by running the program. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 72 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 73 | Modifiers |
| 74 | ^^^^^^^^^ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 75 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 76 | .. program:: trace |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 77 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 78 | .. cmdoption:: -f, --file=<file> |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 79 | |
Éric Araujo | 67f0b6c | 2010-12-15 19:30:15 +0000 | [diff] [blame] | 80 | Name of a file to accumulate counts over several tracing runs. Should be |
| 81 | used with the :option:`--count <-c>` option. |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 82 | |
| 83 | .. cmdoption:: -C, --coverdir=<dir> |
| 84 | |
Éric Araujo | 67f0b6c | 2010-12-15 19:30:15 +0000 | [diff] [blame] | 85 | Directory where the report files go. The coverage report for |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 86 | ``package.module`` is written to file :file:`{dir}/{package}/{module}.cover`. |
| 87 | |
| 88 | .. cmdoption:: -m, --missing |
| 89 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 90 | When generating annotated listings, mark lines which were not executed with |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 91 | ``>>>>>>``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 92 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 93 | .. cmdoption:: -s, --summary |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 94 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 95 | When using :option:`--count <-c>` or :option:`--report <-r>`, write a brief |
| 96 | summary to stdout for each file processed. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 97 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 98 | .. cmdoption:: -R, --no-report |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 99 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 100 | Do not generate annotated listings. This is useful if you intend to make |
| 101 | several runs with :option:`--count <-c>`, and then produce a single set of |
| 102 | annotated listings at the end. |
| 103 | |
| 104 | .. cmdoption:: -g, --timing |
| 105 | |
Éric Araujo | 67f0b6c | 2010-12-15 19:30:15 +0000 | [diff] [blame] | 106 | Prefix each line with the time since the program started. Only used while |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 107 | tracing. |
| 108 | |
| 109 | Filters |
| 110 | ^^^^^^^ |
| 111 | |
| 112 | These options may be repeated multiple times. |
| 113 | |
| 114 | .. program:: trace |
| 115 | |
| 116 | .. cmdoption:: --ignore-module=<mod> |
| 117 | |
Éric Araujo | 67f0b6c | 2010-12-15 19:30:15 +0000 | [diff] [blame] | 118 | Ignore each of the given module names and its submodules (if it is a |
| 119 | package). The argument can be a list of names separated by a comma. |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 120 | |
| 121 | .. cmdoption:: --ignore-dir=<dir> |
| 122 | |
| 123 | Ignore all modules and packages in the named directory and subdirectories. |
| 124 | The argument can be a list of directories separated by :data:`os.pathsep`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 125 | |
| 126 | .. _trace-api: |
| 127 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 128 | Programmatic Interface |
| 129 | ---------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 130 | |
Éric Araujo | 67f0b6c | 2010-12-15 19:30:15 +0000 | [diff] [blame] | 131 | .. class:: Trace(count=1, trace=1, countfuncs=0, countcallers=0, ignoremods=(),\ |
| 132 | ignoredirs=(), infile=None, outfile=None, timing=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 133 | |
Éric Araujo | 67f0b6c | 2010-12-15 19:30:15 +0000 | [diff] [blame] | 134 | Create an object to trace execution of a single statement or expression. All |
| 135 | parameters are optional. *count* enables counting of line numbers. *trace* |
| 136 | enables line execution tracing. *countfuncs* enables listing of the |
| 137 | functions called during the run. *countcallers* enables call relationship |
| 138 | tracking. *ignoremods* is a list of modules or packages to ignore. |
| 139 | *ignoredirs* is a list of directories whose modules or packages should be |
| 140 | ignored. *infile* is the name of the file from which to read stored count |
| 141 | information. *outfile* is the name of the file in which to write updated |
| 142 | count information. *timing* enables a timestamp relative to when tracing was |
| 143 | started to be displayed. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 144 | |
Éric Araujo | 67f0b6c | 2010-12-15 19:30:15 +0000 | [diff] [blame] | 145 | .. method:: run(cmd) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 146 | |
Éric Araujo | 67f0b6c | 2010-12-15 19:30:15 +0000 | [diff] [blame] | 147 | Execute the command and gather statistics from the execution with |
| 148 | the current tracing parameters. *cmd* must be a string or code object, |
| 149 | suitable for passing into :func:`exec`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 150 | |
Éric Araujo | 67f0b6c | 2010-12-15 19:30:15 +0000 | [diff] [blame] | 151 | .. method:: runctx(cmd, globals=None, locals=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 152 | |
Éric Araujo | 67f0b6c | 2010-12-15 19:30:15 +0000 | [diff] [blame] | 153 | Execute the command and gather statistics from the execution with the |
| 154 | current tracing parameters, in the defined global and local |
| 155 | environments. If not defined, *globals* and *locals* default to empty |
| 156 | dictionaries. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 157 | |
Éric Araujo | 67f0b6c | 2010-12-15 19:30:15 +0000 | [diff] [blame] | 158 | .. method:: runfunc(func, *args, **kwds) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 159 | |
Éric Araujo | 67f0b6c | 2010-12-15 19:30:15 +0000 | [diff] [blame] | 160 | Call *func* with the given arguments under control of the :class:`Trace` |
| 161 | object with the current tracing parameters. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 162 | |
Éric Araujo | 67f0b6c | 2010-12-15 19:30:15 +0000 | [diff] [blame] | 163 | .. method:: results() |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 164 | |
Éric Araujo | 67f0b6c | 2010-12-15 19:30:15 +0000 | [diff] [blame] | 165 | Return a :class:`CoverageResults` object that contains the cumulative |
| 166 | results of all previous calls to ``run``, ``runctx`` and ``runfunc`` |
| 167 | for the given :class:`Trace` instance. Does not reset the accumulated |
| 168 | trace results. |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 169 | |
| 170 | .. class:: CoverageResults |
| 171 | |
Éric Araujo | 67f0b6c | 2010-12-15 19:30:15 +0000 | [diff] [blame] | 172 | A container for coverage results, created by :meth:`Trace.results`. Should |
| 173 | not be created directly by the user. |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 174 | |
Éric Araujo | 67f0b6c | 2010-12-15 19:30:15 +0000 | [diff] [blame] | 175 | .. method:: update(other) |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 176 | |
Éric Araujo | 67f0b6c | 2010-12-15 19:30:15 +0000 | [diff] [blame] | 177 | Merge in data from another :class:`CoverageResults` object. |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 178 | |
Éric Araujo | 67f0b6c | 2010-12-15 19:30:15 +0000 | [diff] [blame] | 179 | .. method:: write_results(show_missing=True, summary=False, coverdir=None) |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 180 | |
Éric Araujo | 67f0b6c | 2010-12-15 19:30:15 +0000 | [diff] [blame] | 181 | Write coverage results. Set *show_missing* to show lines that had no |
| 182 | hits. Set *summary* to include in the output the coverage summary per |
| 183 | module. *coverdir* specifies the directory into which the coverage |
| 184 | result files will be output. If ``None``, the results for each source |
| 185 | file are placed in its directory. |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame] | 186 | |
| 187 | A simple example demonstrating the use of the programmatic interface:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 188 | |
| 189 | import sys |
| 190 | import trace |
| 191 | |
| 192 | # create a Trace object, telling it what to ignore, and whether to |
| 193 | # do tracing or line-counting or both. |
| 194 | tracer = trace.Trace( |
| 195 | ignoredirs=[sys.prefix, sys.exec_prefix], |
| 196 | trace=0, |
| 197 | count=1) |
| 198 | |
| 199 | # run the new command using the given tracer |
| 200 | tracer.run('main()') |
| 201 | |
| 202 | # make a report, placing output in /tmp |
| 203 | r = tracer.results() |
| 204 | r.write_results(show_missing=True, coverdir="/tmp") |
| 205 | |