Profile.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /////////////// Profile.proto ///////////////
  2. //@requires: Exceptions.c::PyErrFetchRestore
  3. //@substitute: naming
  4. // Note that cPython ignores PyTrace_EXCEPTION,
  5. // but maybe some other profilers don't.
  6. #ifndef CYTHON_PROFILE
  7. #if CYTHON_COMPILING_IN_PYPY || CYTHON_COMPILING_IN_PYSTON
  8. #define CYTHON_PROFILE 0
  9. #else
  10. #define CYTHON_PROFILE 1
  11. #endif
  12. #endif
  13. #ifndef CYTHON_TRACE_NOGIL
  14. #define CYTHON_TRACE_NOGIL 0
  15. #else
  16. #if CYTHON_TRACE_NOGIL && !defined(CYTHON_TRACE)
  17. #define CYTHON_TRACE 1
  18. #endif
  19. #endif
  20. #ifndef CYTHON_TRACE
  21. #define CYTHON_TRACE 0
  22. #endif
  23. #if CYTHON_TRACE
  24. #undef CYTHON_PROFILE_REUSE_FRAME
  25. #endif
  26. #ifndef CYTHON_PROFILE_REUSE_FRAME
  27. #define CYTHON_PROFILE_REUSE_FRAME 0
  28. #endif
  29. #if CYTHON_PROFILE || CYTHON_TRACE
  30. #include "compile.h"
  31. #include "frameobject.h"
  32. #include "traceback.h"
  33. #if CYTHON_PROFILE_REUSE_FRAME
  34. #define CYTHON_FRAME_MODIFIER static
  35. #define CYTHON_FRAME_DEL(frame)
  36. #else
  37. #define CYTHON_FRAME_MODIFIER
  38. #define CYTHON_FRAME_DEL(frame) Py_CLEAR(frame)
  39. #endif
  40. #define __Pyx_TraceDeclarations \
  41. static PyCodeObject *$frame_code_cname = NULL; \
  42. CYTHON_FRAME_MODIFIER PyFrameObject *$frame_cname = NULL; \
  43. int __Pyx_use_tracing = 0;
  44. #define __Pyx_TraceFrameInit(codeobj) \
  45. if (codeobj) $frame_code_cname = (PyCodeObject*) codeobj;
  46. #ifdef WITH_THREAD
  47. #define __Pyx_TraceCall(funcname, srcfile, firstlineno, nogil, goto_error) \
  48. if (nogil) { \
  49. if (CYTHON_TRACE_NOGIL) { \
  50. PyThreadState *tstate; \
  51. PyGILState_STATE state = PyGILState_Ensure(); \
  52. tstate = __Pyx_PyThreadState_Current; \
  53. if (unlikely(tstate->use_tracing) && !tstate->tracing && \
  54. (tstate->c_profilefunc || (CYTHON_TRACE && tstate->c_tracefunc))) { \
  55. __Pyx_use_tracing = __Pyx_TraceSetupAndCall(&$frame_code_cname, &$frame_cname, tstate, funcname, srcfile, firstlineno); \
  56. } \
  57. PyGILState_Release(state); \
  58. if (unlikely(__Pyx_use_tracing < 0)) goto_error; \
  59. } \
  60. } else { \
  61. PyThreadState* tstate = PyThreadState_GET(); \
  62. if (unlikely(tstate->use_tracing) && !tstate->tracing && \
  63. (tstate->c_profilefunc || (CYTHON_TRACE && tstate->c_tracefunc))) { \
  64. __Pyx_use_tracing = __Pyx_TraceSetupAndCall(&$frame_code_cname, &$frame_cname, tstate, funcname, srcfile, firstlineno); \
  65. if (unlikely(__Pyx_use_tracing < 0)) goto_error; \
  66. } \
  67. }
  68. #else
  69. #define __Pyx_TraceCall(funcname, srcfile, firstlineno, nogil, goto_error) \
  70. { PyThreadState* tstate = PyThreadState_GET(); \
  71. if (unlikely(tstate->use_tracing) && !tstate->tracing && \
  72. (tstate->c_profilefunc || (CYTHON_TRACE && tstate->c_tracefunc))) { \
  73. __Pyx_use_tracing = __Pyx_TraceSetupAndCall(&$frame_code_cname, &$frame_cname, tstate, funcname, srcfile, firstlineno); \
  74. if (unlikely(__Pyx_use_tracing < 0)) goto_error; \
  75. } \
  76. }
  77. #endif
  78. #define __Pyx_TraceException() \
  79. if (likely(!__Pyx_use_tracing)); else { \
  80. PyThreadState* tstate = __Pyx_PyThreadState_Current; \
  81. if (tstate->use_tracing && \
  82. (tstate->c_profilefunc || (CYTHON_TRACE && tstate->c_tracefunc))) { \
  83. tstate->tracing++; \
  84. tstate->use_tracing = 0; \
  85. PyObject *exc_info = __Pyx_GetExceptionTuple(tstate); \
  86. if (exc_info) { \
  87. if (CYTHON_TRACE && tstate->c_tracefunc) \
  88. tstate->c_tracefunc( \
  89. tstate->c_traceobj, $frame_cname, PyTrace_EXCEPTION, exc_info); \
  90. tstate->c_profilefunc( \
  91. tstate->c_profileobj, $frame_cname, PyTrace_EXCEPTION, exc_info); \
  92. Py_DECREF(exc_info); \
  93. } \
  94. tstate->use_tracing = 1; \
  95. tstate->tracing--; \
  96. } \
  97. }
  98. static void __Pyx_call_return_trace_func(PyThreadState *tstate, PyFrameObject *frame, PyObject *result) {
  99. PyObject *type, *value, *traceback;
  100. __Pyx_ErrFetchInState(tstate, &type, &value, &traceback);
  101. tstate->tracing++;
  102. tstate->use_tracing = 0;
  103. if (CYTHON_TRACE && tstate->c_tracefunc)
  104. tstate->c_tracefunc(tstate->c_traceobj, frame, PyTrace_RETURN, result);
  105. if (tstate->c_profilefunc)
  106. tstate->c_profilefunc(tstate->c_profileobj, frame, PyTrace_RETURN, result);
  107. CYTHON_FRAME_DEL(frame);
  108. tstate->use_tracing = 1;
  109. tstate->tracing--;
  110. __Pyx_ErrRestoreInState(tstate, type, value, traceback);
  111. }
  112. #ifdef WITH_THREAD
  113. #define __Pyx_TraceReturn(result, nogil) \
  114. if (likely(!__Pyx_use_tracing)); else { \
  115. if (nogil) { \
  116. if (CYTHON_TRACE_NOGIL) { \
  117. PyThreadState *tstate; \
  118. PyGILState_STATE state = PyGILState_Ensure(); \
  119. tstate = __Pyx_PyThreadState_Current; \
  120. if (tstate->use_tracing) { \
  121. __Pyx_call_return_trace_func(tstate, $frame_cname, (PyObject*)result); \
  122. } \
  123. PyGILState_Release(state); \
  124. } \
  125. } else { \
  126. PyThreadState* tstate = __Pyx_PyThreadState_Current; \
  127. if (tstate->use_tracing) { \
  128. __Pyx_call_return_trace_func(tstate, $frame_cname, (PyObject*)result); \
  129. } \
  130. } \
  131. }
  132. #else
  133. #define __Pyx_TraceReturn(result, nogil) \
  134. if (likely(!__Pyx_use_tracing)); else { \
  135. PyThreadState* tstate = __Pyx_PyThreadState_Current; \
  136. if (tstate->use_tracing) { \
  137. __Pyx_call_return_trace_func(tstate, $frame_cname, (PyObject*)result); \
  138. } \
  139. }
  140. #endif
  141. static PyCodeObject *__Pyx_createFrameCodeObject(const char *funcname, const char *srcfile, int firstlineno); /*proto*/
  142. static int __Pyx_TraceSetupAndCall(PyCodeObject** code, PyFrameObject** frame, PyThreadState* tstate, const char *funcname, const char *srcfile, int firstlineno); /*proto*/
  143. #else
  144. #define __Pyx_TraceDeclarations
  145. #define __Pyx_TraceFrameInit(codeobj)
  146. // mark error label as used to avoid compiler warnings
  147. #define __Pyx_TraceCall(funcname, srcfile, firstlineno, nogil, goto_error) if ((1)); else goto_error;
  148. #define __Pyx_TraceException()
  149. #define __Pyx_TraceReturn(result, nogil)
  150. #endif /* CYTHON_PROFILE */
  151. #if CYTHON_TRACE
  152. // see call_trace_protected() in CPython's ceval.c
  153. static int __Pyx_call_line_trace_func(PyThreadState *tstate, PyFrameObject *frame, int lineno) {
  154. int ret;
  155. PyObject *type, *value, *traceback;
  156. __Pyx_ErrFetchInState(tstate, &type, &value, &traceback);
  157. __Pyx_PyFrame_SetLineNumber(frame, lineno);
  158. tstate->tracing++;
  159. tstate->use_tracing = 0;
  160. ret = tstate->c_tracefunc(tstate->c_traceobj, frame, PyTrace_LINE, NULL);
  161. tstate->use_tracing = 1;
  162. tstate->tracing--;
  163. if (likely(!ret)) {
  164. __Pyx_ErrRestoreInState(tstate, type, value, traceback);
  165. } else {
  166. Py_XDECREF(type);
  167. Py_XDECREF(value);
  168. Py_XDECREF(traceback);
  169. }
  170. return ret;
  171. }
  172. #ifdef WITH_THREAD
  173. #define __Pyx_TraceLine(lineno, nogil, goto_error) \
  174. if (likely(!__Pyx_use_tracing)); else { \
  175. if (nogil) { \
  176. if (CYTHON_TRACE_NOGIL) { \
  177. int ret = 0; \
  178. PyThreadState *tstate; \
  179. PyGILState_STATE state = PyGILState_Ensure(); \
  180. tstate = __Pyx_PyThreadState_Current; \
  181. if (unlikely(tstate->use_tracing && tstate->c_tracefunc && $frame_cname->f_trace)) { \
  182. ret = __Pyx_call_line_trace_func(tstate, $frame_cname, lineno); \
  183. } \
  184. PyGILState_Release(state); \
  185. if (unlikely(ret)) goto_error; \
  186. } \
  187. } else { \
  188. PyThreadState* tstate = __Pyx_PyThreadState_Current; \
  189. if (unlikely(tstate->use_tracing && tstate->c_tracefunc && $frame_cname->f_trace)) { \
  190. int ret = __Pyx_call_line_trace_func(tstate, $frame_cname, lineno); \
  191. if (unlikely(ret)) goto_error; \
  192. } \
  193. } \
  194. }
  195. #else
  196. #define __Pyx_TraceLine(lineno, nogil, goto_error) \
  197. if (likely(!__Pyx_use_tracing)); else { \
  198. PyThreadState* tstate = __Pyx_PyThreadState_Current; \
  199. if (unlikely(tstate->use_tracing && tstate->c_tracefunc && $frame_cname->f_trace)) { \
  200. int ret = __Pyx_call_line_trace_func(tstate, $frame_cname, lineno); \
  201. if (unlikely(ret)) goto_error; \
  202. } \
  203. }
  204. #endif
  205. #else
  206. // mark error label as used to avoid compiler warnings
  207. #define __Pyx_TraceLine(lineno, nogil, goto_error) if ((1)); else goto_error;
  208. #endif
  209. /////////////// Profile ///////////////
  210. //@substitute: naming
  211. #if CYTHON_PROFILE
  212. static int __Pyx_TraceSetupAndCall(PyCodeObject** code,
  213. PyFrameObject** frame,
  214. PyThreadState* tstate,
  215. const char *funcname,
  216. const char *srcfile,
  217. int firstlineno) {
  218. PyObject *type, *value, *traceback;
  219. int retval;
  220. if (*frame == NULL || !CYTHON_PROFILE_REUSE_FRAME) {
  221. if (*code == NULL) {
  222. *code = __Pyx_createFrameCodeObject(funcname, srcfile, firstlineno);
  223. if (*code == NULL) return 0;
  224. }
  225. *frame = PyFrame_New(
  226. tstate, /*PyThreadState *tstate*/
  227. *code, /*PyCodeObject *code*/
  228. $moddict_cname, /*PyObject *globals*/
  229. 0 /*PyObject *locals*/
  230. );
  231. if (*frame == NULL) return 0;
  232. if (CYTHON_TRACE && (*frame)->f_trace == NULL) {
  233. // this enables "f_lineno" lookup, at least in CPython ...
  234. Py_INCREF(Py_None);
  235. (*frame)->f_trace = Py_None;
  236. }
  237. #if PY_VERSION_HEX < 0x030400B1
  238. } else {
  239. (*frame)->f_tstate = tstate;
  240. #endif
  241. }
  242. __Pyx_PyFrame_SetLineNumber(*frame, firstlineno);
  243. retval = 1;
  244. tstate->tracing++;
  245. tstate->use_tracing = 0;
  246. __Pyx_ErrFetchInState(tstate, &type, &value, &traceback);
  247. #if CYTHON_TRACE
  248. if (tstate->c_tracefunc)
  249. retval = tstate->c_tracefunc(tstate->c_traceobj, *frame, PyTrace_CALL, NULL) == 0;
  250. if (retval && tstate->c_profilefunc)
  251. #endif
  252. retval = tstate->c_profilefunc(tstate->c_profileobj, *frame, PyTrace_CALL, NULL) == 0;
  253. tstate->use_tracing = (tstate->c_profilefunc ||
  254. (CYTHON_TRACE && tstate->c_tracefunc));
  255. tstate->tracing--;
  256. if (retval) {
  257. __Pyx_ErrRestoreInState(tstate, type, value, traceback);
  258. return tstate->use_tracing && retval;
  259. } else {
  260. Py_XDECREF(type);
  261. Py_XDECREF(value);
  262. Py_XDECREF(traceback);
  263. return -1;
  264. }
  265. }
  266. static PyCodeObject *__Pyx_createFrameCodeObject(const char *funcname, const char *srcfile, int firstlineno) {
  267. PyObject *py_srcfile = 0;
  268. PyObject *py_funcname = 0;
  269. PyCodeObject *py_code = 0;
  270. #if PY_MAJOR_VERSION < 3
  271. py_funcname = PyString_FromString(funcname);
  272. py_srcfile = PyString_FromString(srcfile);
  273. #else
  274. py_funcname = PyUnicode_FromString(funcname);
  275. py_srcfile = PyUnicode_FromString(srcfile);
  276. #endif
  277. if (!py_funcname | !py_srcfile) goto bad;
  278. py_code = PyCode_New(
  279. 0, /*int argcount,*/
  280. #if PY_MAJOR_VERSION >= 3
  281. 0, /*int kwonlyargcount,*/
  282. #endif
  283. 0, /*int nlocals,*/
  284. 0, /*int stacksize,*/
  285. // make CPython use a fresh dict for "f_locals" at need (see GH #1836)
  286. CO_OPTIMIZED | CO_NEWLOCALS, /*int flags,*/
  287. $empty_bytes, /*PyObject *code,*/
  288. $empty_tuple, /*PyObject *consts,*/
  289. $empty_tuple, /*PyObject *names,*/
  290. $empty_tuple, /*PyObject *varnames,*/
  291. $empty_tuple, /*PyObject *freevars,*/
  292. $empty_tuple, /*PyObject *cellvars,*/
  293. py_srcfile, /*PyObject *filename,*/
  294. py_funcname, /*PyObject *name,*/
  295. firstlineno, /*int firstlineno,*/
  296. $empty_bytes /*PyObject *lnotab*/
  297. );
  298. bad:
  299. Py_XDECREF(py_srcfile);
  300. Py_XDECREF(py_funcname);
  301. return py_code;
  302. }
  303. #endif /* CYTHON_PROFILE */