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