| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="api.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="api.html" title='Python/C API Reference Manual' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="advanced-debugging.html" /> |
| 13 | <link rel="prev" href="threads.html" /> |
| 14 | <link rel="parent" href="initialization.html" /> |
| 15 | <link rel="next" href="advanced-debugging.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>8.2 Profiling and Tracing </title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="8.1 Thread State and" |
| 25 | href="threads.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="8. Initialization, Finalization, and" |
| 28 | href="initialization.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="8.3 Advanced Debugger Support" |
| 31 | href="advanced-debugging.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python/C API Reference Manual</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><img src='../icons/blank.png' |
| 38 | border='0' height='32' alt='' width='32' /></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="threads.html">8.1 Thread State and</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="initialization.html">8. Initialization, Finalization, and</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="advanced-debugging.html">8.3 Advanced Debugger Support</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION0010200000000000000000"></A><A NAME="profiling"></A> |
| 56 | <BR> |
| 57 | 8.2 Profiling and Tracing |
| 58 | </H1> |
| 59 | |
| 60 | <P> |
| 61 | |
| 62 | <P> |
| 63 | The Python interpreter provides some low-level support for attaching |
| 64 | profiling and execution tracing facilities. These are used for |
| 65 | profiling, debugging, and coverage analysis tools. |
| 66 | |
| 67 | <P> |
| 68 | Starting with Python 2.2, the implementation of this facility was |
| 69 | substantially revised, and an interface from C was added. This C |
| 70 | interface allows the profiling or tracing code to avoid the overhead |
| 71 | of calling through Python-level callable objects, making a direct C |
| 72 | function call instead. The essential attributes of the facility have |
| 73 | not changed; the interface allows trace functions to be installed |
| 74 | per-thread, and the basic events reported to the trace function are |
| 75 | the same as had been reported to the Python-level trace functions in |
| 76 | previous versions. |
| 77 | |
| 78 | <P> |
| 79 | <dl><dt><b><tt class="ctype"><a id='l2h-881' xml:id='l2h-881'>int (*Py_tracefunc)(PyObject *obj, |
| 80 | PyFrameObject *frame, int what, |
| 81 | PyObject *arg)</a></tt></b></dt> |
| 82 | <dd> |
| 83 | The type of the trace function registered using |
| 84 | <tt class="cfunction">PyEval_SetProfile()</tt> and <tt class="cfunction">PyEval_SetTrace()</tt>. |
| 85 | The first parameter is the object passed to the registration |
| 86 | function as <var>obj</var>, <var>frame</var> is the frame object to which the |
| 87 | event pertains, <var>what</var> is one of the constants |
| 88 | <tt class="constant">PyTrace_CALL</tt>, <tt class="constant">PyTrace_EXCEPTION</tt>, |
| 89 | <tt class="constant">PyTrace_LINE</tt>, <tt class="constant">PyTrace_RETURN</tt>, |
| 90 | <tt class="constant">PyTrace_C_CALL</tt>, <tt class="constant">PyTrace_C_EXCEPTION</tt>, |
| 91 | or <tt class="constant">PyTrace_C_RETURN</tt>, and <var>arg</var> |
| 92 | depends on the value of <var>what</var>: |
| 93 | |
| 94 | <P> |
| 95 | <div class="center"><table class="realtable"> |
| 96 | <thead> |
| 97 | <tr> |
| 98 | <th class="left" >Value of <var>what</var></th> |
| 99 | <th class="left" >Meaning of <var>arg</var></th> |
| 100 | </tr> |
| 101 | </thead> |
| 102 | <tbody> |
| 103 | <tr><td class="left" valign="baseline"><tt class="constant">PyTrace_CALL</tt></td> |
| 104 | <td class="left" >Always <tt class="constant">NULL</tt>.</td></tr> |
| 105 | <tr><td class="left" valign="baseline"><tt class="constant">PyTrace_EXCEPTION</tt></td> |
| 106 | <td class="left" >Exception information as returned by |
| 107 | <tt class="function">sys.exc_info()</tt>.</td></tr> |
| 108 | <tr><td class="left" valign="baseline"><tt class="constant">PyTrace_LINE</tt></td> |
| 109 | <td class="left" >Always <tt class="constant">NULL</tt>.</td></tr> |
| 110 | <tr><td class="left" valign="baseline"><tt class="constant">PyTrace_RETURN</tt></td> |
| 111 | <td class="left" >Value being returned to the caller.</td></tr> |
| 112 | <tr><td class="left" valign="baseline"><tt class="constant">PyTrace_C_CALL</tt></td> |
| 113 | <td class="left" >Name of function being called.</td></tr> |
| 114 | <tr><td class="left" valign="baseline"><tt class="constant">PyTrace_C_EXCEPTION</tt></td> |
| 115 | <td class="left" >Always <tt class="constant">NULL</tt>.</td></tr> |
| 116 | <tr><td class="left" valign="baseline"><tt class="constant">PyTrace_C_RETURN</tt></td> |
| 117 | <td class="left" >Always <tt class="constant">NULL</tt>.</td></tr></tbody> |
| 118 | </table></div> |
| 119 | </dl> |
| 120 | |
| 121 | <P> |
| 122 | <dl><dt>int <b><tt id='l2h-882' xml:id='l2h-882' class="cdata">PyTrace_CALL</tt></b></dt> |
| 123 | <dd> |
| 124 | The value of the <var>what</var> parameter to a <tt class="ctype">Py_tracefunc</tt> |
| 125 | function when a new call to a function or method is being reported, |
| 126 | or a new entry into a generator. Note that the creation of the |
| 127 | iterator for a generator function is not reported as there is no |
| 128 | control transfer to the Python bytecode in the corresponding frame. |
| 129 | </dd></dl> |
| 130 | |
| 131 | <P> |
| 132 | <dl><dt>int <b><tt id='l2h-883' xml:id='l2h-883' class="cdata">PyTrace_EXCEPTION</tt></b></dt> |
| 133 | <dd> |
| 134 | The value of the <var>what</var> parameter to a <tt class="ctype">Py_tracefunc</tt> |
| 135 | function when an exception has been raised. The callback function |
| 136 | is called with this value for <var>what</var> when after any bytecode is |
| 137 | processed after which the exception becomes set within the frame |
| 138 | being executed. The effect of this is that as exception propagation |
| 139 | causes the Python stack to unwind, the callback is called upon |
| 140 | return to each frame as the exception propagates. Only trace |
| 141 | functions receives these events; they are not needed by the |
| 142 | profiler. |
| 143 | </dd></dl> |
| 144 | |
| 145 | <P> |
| 146 | <dl><dt>int <b><tt id='l2h-884' xml:id='l2h-884' class="cdata">PyTrace_LINE</tt></b></dt> |
| 147 | <dd> |
| 148 | The value passed as the <var>what</var> parameter to a trace function |
| 149 | (but not a profiling function) when a line-number event is being |
| 150 | reported. |
| 151 | </dd></dl> |
| 152 | |
| 153 | <P> |
| 154 | <dl><dt>int <b><tt id='l2h-885' xml:id='l2h-885' class="cdata">PyTrace_RETURN</tt></b></dt> |
| 155 | <dd> |
| 156 | The value for the <var>what</var> parameter to <tt class="ctype">Py_tracefunc</tt> |
| 157 | functions when a call is returning without propagating an exception. |
| 158 | </dd></dl> |
| 159 | |
| 160 | <P> |
| 161 | <dl><dt>int <b><tt id='l2h-886' xml:id='l2h-886' class="cdata">PyTrace_C_CALL</tt></b></dt> |
| 162 | <dd> |
| 163 | The value for the <var>what</var> parameter to <tt class="ctype">Py_tracefunc</tt> |
| 164 | functions when a C function is about to be called. |
| 165 | </dd></dl> |
| 166 | |
| 167 | <P> |
| 168 | <dl><dt>int <b><tt id='l2h-887' xml:id='l2h-887' class="cdata">PyTrace_C_EXCEPTION</tt></b></dt> |
| 169 | <dd> |
| 170 | The value for the <var>what</var> parameter to <tt class="ctype">Py_tracefunc</tt> |
| 171 | functions when a C function has thrown an exception. |
| 172 | </dd></dl> |
| 173 | |
| 174 | <P> |
| 175 | <dl><dt>int <b><tt id='l2h-888' xml:id='l2h-888' class="cdata">PyTrace_C_RETURN</tt></b></dt> |
| 176 | <dd> |
| 177 | The value for the <var>what</var> parameter to <tt class="ctype">Py_tracefunc</tt> |
| 178 | functions when a C function has returned. |
| 179 | </dd></dl> |
| 180 | |
| 181 | <P> |
| 182 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void <b><tt id='l2h-889' xml:id='l2h-889' class="cfunction">PyEval_SetProfile</tt></b>(</nobr></td><td>Py_tracefunc <var>func</var>, PyObject *<var>obj</var>)</td></tr></table></dt> |
| 183 | <dd> |
| 184 | Set the profiler function to <var>func</var>. The <var>obj</var> parameter is |
| 185 | passed to the function as its first parameter, and may be any Python |
| 186 | object, or <tt class="constant">NULL</tt>. If the profile function needs to maintain state, |
| 187 | using a different value for <var>obj</var> for each thread provides a |
| 188 | convenient and thread-safe place to store it. The profile function |
| 189 | is called for all monitored events except the line-number events. |
| 190 | </dd></dl> |
| 191 | |
| 192 | <P> |
| 193 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void <b><tt id='l2h-890' xml:id='l2h-890' class="cfunction">PyEval_SetTrace</tt></b>(</nobr></td><td>Py_tracefunc <var>func</var>, PyObject *<var>obj</var>)</td></tr></table></dt> |
| 194 | <dd> |
| 195 | Set the tracing function to <var>func</var>. This is similar to |
| 196 | <tt class="cfunction">PyEval_SetProfile()</tt>, except the tracing function does |
| 197 | receive line-number events. |
| 198 | </dd></dl> |
| 199 | |
| 200 | <P> |
| 201 | |
| 202 | <DIV CLASS="navigation"> |
| 203 | <div class='online-navigation'> |
| 204 | <p></p><hr /> |
| 205 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 206 | <tr> |
| 207 | <td class='online-navigation'><a rel="prev" title="8.1 Thread State and" |
| 208 | href="threads.html"><img src='../icons/previous.png' |
| 209 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 210 | <td class='online-navigation'><a rel="parent" title="8. Initialization, Finalization, and" |
| 211 | href="initialization.html"><img src='../icons/up.png' |
| 212 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 213 | <td class='online-navigation'><a rel="next" title="8.3 Advanced Debugger Support" |
| 214 | href="advanced-debugging.html"><img src='../icons/next.png' |
| 215 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 216 | <td align="center" width="100%">Python/C API Reference Manual</td> |
| 217 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 218 | href="contents.html"><img src='../icons/contents.png' |
| 219 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 220 | <td class='online-navigation'><img src='../icons/blank.png' |
| 221 | border='0' height='32' alt='' width='32' /></td> |
| 222 | <td class='online-navigation'><a rel="index" title="Index" |
| 223 | href="genindex.html"><img src='../icons/index.png' |
| 224 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 225 | </tr></table> |
| 226 | <div class='online-navigation'> |
| 227 | <b class="navlabel">Previous:</b> |
| 228 | <a class="sectref" rel="prev" href="threads.html">8.1 Thread State and</A> |
| 229 | <b class="navlabel">Up:</b> |
| 230 | <a class="sectref" rel="parent" href="initialization.html">8. Initialization, Finalization, and</A> |
| 231 | <b class="navlabel">Next:</b> |
| 232 | <a class="sectref" rel="next" href="advanced-debugging.html">8.3 Advanced Debugger Support</A> |
| 233 | </div> |
| 234 | </div> |
| 235 | <hr /> |
| 236 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 237 | </DIV> |
| 238 | <!--End of Navigation Panel--> |
| 239 | <ADDRESS> |
| 240 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 241 | </ADDRESS> |
| 242 | </BODY> |
| 243 | </HTML> |