tracer.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. /* Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 */
  2. /* For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt */
  3. /* C-based Tracer for coverage.py. */
  4. #include "util.h"
  5. #include "datastack.h"
  6. #include "filedisp.h"
  7. #include "tracer.h"
  8. /* Python C API helpers. */
  9. static int
  10. pyint_as_int(PyObject * pyint, int *pint)
  11. {
  12. int the_int = MyInt_AsInt(pyint);
  13. if (the_int == -1 && PyErr_Occurred()) {
  14. return RET_ERROR;
  15. }
  16. *pint = the_int;
  17. return RET_OK;
  18. }
  19. /* Interned strings to speed GetAttr etc. */
  20. static PyObject *str_trace;
  21. static PyObject *str_file_tracer;
  22. static PyObject *str__coverage_enabled;
  23. static PyObject *str__coverage_plugin;
  24. static PyObject *str__coverage_plugin_name;
  25. static PyObject *str_dynamic_source_filename;
  26. static PyObject *str_line_number_range;
  27. int
  28. CTracer_intern_strings(void)
  29. {
  30. int ret = RET_ERROR;
  31. #define INTERN_STRING(v, s) \
  32. v = MyText_InternFromString(s); \
  33. if (v == NULL) { \
  34. goto error; \
  35. }
  36. INTERN_STRING(str_trace, "trace")
  37. INTERN_STRING(str_file_tracer, "file_tracer")
  38. INTERN_STRING(str__coverage_enabled, "_coverage_enabled")
  39. INTERN_STRING(str__coverage_plugin, "_coverage_plugin")
  40. INTERN_STRING(str__coverage_plugin_name, "_coverage_plugin_name")
  41. INTERN_STRING(str_dynamic_source_filename, "dynamic_source_filename")
  42. INTERN_STRING(str_line_number_range, "line_number_range")
  43. ret = RET_OK;
  44. error:
  45. return ret;
  46. }
  47. static void CTracer_disable_plugin(CTracer *self, PyObject * disposition);
  48. static int
  49. CTracer_init(CTracer *self, PyObject *args_unused, PyObject *kwds_unused)
  50. {
  51. int ret = RET_ERROR;
  52. if (DataStack_init(&self->stats, &self->data_stack) < 0) {
  53. goto error;
  54. }
  55. self->pdata_stack = &self->data_stack;
  56. self->cur_entry.last_line = -1;
  57. self->context = Py_None;
  58. ret = RET_OK;
  59. goto ok;
  60. error:
  61. STATS( self->stats.errors++; )
  62. ok:
  63. return ret;
  64. }
  65. static void
  66. CTracer_dealloc(CTracer *self)
  67. {
  68. int i;
  69. if (self->started) {
  70. PyEval_SetTrace(NULL, NULL);
  71. }
  72. Py_XDECREF(self->should_trace);
  73. Py_XDECREF(self->check_include);
  74. Py_XDECREF(self->warn);
  75. Py_XDECREF(self->concur_id_func);
  76. Py_XDECREF(self->data);
  77. Py_XDECREF(self->file_tracers);
  78. Py_XDECREF(self->should_trace_cache);
  79. Py_XDECREF(self->should_start_context);
  80. Py_XDECREF(self->switch_context);
  81. Py_XDECREF(self->context);
  82. DataStack_dealloc(&self->stats, &self->data_stack);
  83. if (self->data_stacks) {
  84. for (i = 0; i < self->data_stacks_used; i++) {
  85. DataStack_dealloc(&self->stats, self->data_stacks + i);
  86. }
  87. PyMem_Free(self->data_stacks);
  88. }
  89. Py_XDECREF(self->data_stack_index);
  90. Py_TYPE(self)->tp_free((PyObject*)self);
  91. }
  92. #if TRACE_LOG
  93. static const char *
  94. indent(int n)
  95. {
  96. static const char * spaces =
  97. " "
  98. " "
  99. " "
  100. " "
  101. ;
  102. return spaces + strlen(spaces) - n*2;
  103. }
  104. static BOOL logging = FALSE;
  105. /* Set these constants to be a file substring and line number to start logging. */
  106. static const char * start_file = "tests/views";
  107. static int start_line = 27;
  108. static void
  109. showlog(int depth, int lineno, PyObject * filename, const char * msg)
  110. {
  111. if (logging) {
  112. printf("%s%3d ", indent(depth), depth);
  113. if (lineno) {
  114. printf("%4d", lineno);
  115. }
  116. else {
  117. printf(" ");
  118. }
  119. if (filename) {
  120. PyObject *ascii = MyText_AS_BYTES(filename);
  121. printf(" %s", MyBytes_AS_STRING(ascii));
  122. Py_DECREF(ascii);
  123. }
  124. if (msg) {
  125. printf(" %s", msg);
  126. }
  127. printf("\n");
  128. }
  129. }
  130. #define SHOWLOG(a,b,c,d) showlog(a,b,c,d)
  131. #else
  132. #define SHOWLOG(a,b,c,d)
  133. #endif /* TRACE_LOG */
  134. #if WHAT_LOG
  135. static const char * what_sym[] = {"CALL", "EXC ", "LINE", "RET "};
  136. #endif
  137. /* Record a pair of integers in self->cur_entry.file_data. */
  138. static int
  139. CTracer_record_pair(CTracer *self, int l1, int l2)
  140. {
  141. int ret = RET_ERROR;
  142. PyObject * t = NULL;
  143. t = Py_BuildValue("(ii)", l1, l2);
  144. if (t == NULL) {
  145. goto error;
  146. }
  147. if (PyDict_SetItem(self->cur_entry.file_data, t, Py_None) < 0) {
  148. goto error;
  149. }
  150. ret = RET_OK;
  151. error:
  152. Py_XDECREF(t);
  153. return ret;
  154. }
  155. /* Set self->pdata_stack to the proper data_stack to use. */
  156. static int
  157. CTracer_set_pdata_stack(CTracer *self)
  158. {
  159. int ret = RET_ERROR;
  160. PyObject * co_obj = NULL;
  161. PyObject * stack_index = NULL;
  162. if (self->concur_id_func != Py_None) {
  163. int the_index = 0;
  164. if (self->data_stack_index == NULL) {
  165. PyObject * weakref = NULL;
  166. weakref = PyImport_ImportModule("weakref");
  167. if (weakref == NULL) {
  168. goto error;
  169. }
  170. STATS( self->stats.pycalls++; )
  171. self->data_stack_index = PyObject_CallMethod(weakref, "WeakKeyDictionary", NULL);
  172. Py_XDECREF(weakref);
  173. if (self->data_stack_index == NULL) {
  174. goto error;
  175. }
  176. }
  177. STATS( self->stats.pycalls++; )
  178. co_obj = PyObject_CallObject(self->concur_id_func, NULL);
  179. if (co_obj == NULL) {
  180. goto error;
  181. }
  182. stack_index = PyObject_GetItem(self->data_stack_index, co_obj);
  183. if (stack_index == NULL) {
  184. /* PyObject_GetItem sets an exception if it didn't find the thing. */
  185. PyErr_Clear();
  186. /* A new concurrency object. Make a new data stack. */
  187. the_index = self->data_stacks_used;
  188. stack_index = MyInt_FromInt(the_index);
  189. if (stack_index == NULL) {
  190. goto error;
  191. }
  192. if (PyObject_SetItem(self->data_stack_index, co_obj, stack_index) < 0) {
  193. goto error;
  194. }
  195. self->data_stacks_used++;
  196. if (self->data_stacks_used >= self->data_stacks_alloc) {
  197. int bigger = self->data_stacks_alloc + 10;
  198. DataStack * bigger_stacks = PyMem_Realloc(self->data_stacks, bigger * sizeof(DataStack));
  199. if (bigger_stacks == NULL) {
  200. PyErr_NoMemory();
  201. goto error;
  202. }
  203. self->data_stacks = bigger_stacks;
  204. self->data_stacks_alloc = bigger;
  205. }
  206. DataStack_init(&self->stats, &self->data_stacks[the_index]);
  207. }
  208. else {
  209. if (pyint_as_int(stack_index, &the_index) < 0) {
  210. goto error;
  211. }
  212. }
  213. self->pdata_stack = &self->data_stacks[the_index];
  214. }
  215. else {
  216. self->pdata_stack = &self->data_stack;
  217. }
  218. ret = RET_OK;
  219. error:
  220. Py_XDECREF(co_obj);
  221. Py_XDECREF(stack_index);
  222. return ret;
  223. }
  224. /*
  225. * Parts of the trace function.
  226. */
  227. static int
  228. CTracer_check_missing_return(CTracer *self, PyFrameObject *frame)
  229. {
  230. int ret = RET_ERROR;
  231. if (self->last_exc_back) {
  232. if (frame == self->last_exc_back) {
  233. /* Looks like someone forgot to send a return event. We'll clear
  234. the exception state and do the RETURN code here. Notice that the
  235. frame we have in hand here is not the correct frame for the RETURN,
  236. that frame is gone. Our handling for RETURN doesn't need the
  237. actual frame, but we do log it, so that will look a little off if
  238. you're looking at the detailed log.
  239. If someday we need to examine the frame when doing RETURN, then
  240. we'll need to keep more of the missed frame's state.
  241. */
  242. STATS( self->stats.missed_returns++; )
  243. if (CTracer_set_pdata_stack(self) < 0) {
  244. goto error;
  245. }
  246. if (self->pdata_stack->depth >= 0) {
  247. if (self->tracing_arcs && self->cur_entry.file_data) {
  248. if (CTracer_record_pair(self, self->cur_entry.last_line, -self->last_exc_firstlineno) < 0) {
  249. goto error;
  250. }
  251. }
  252. SHOWLOG(self->pdata_stack->depth, frame->f_lineno, frame->f_code->co_filename, "missedreturn");
  253. self->cur_entry = self->pdata_stack->stack[self->pdata_stack->depth];
  254. self->pdata_stack->depth--;
  255. }
  256. }
  257. self->last_exc_back = NULL;
  258. }
  259. ret = RET_OK;
  260. error:
  261. return ret;
  262. }
  263. static int
  264. CTracer_handle_call(CTracer *self, PyFrameObject *frame)
  265. {
  266. int ret = RET_ERROR;
  267. int ret2;
  268. /* Owned references that we clean up at the very end of the function. */
  269. PyObject * disposition = NULL;
  270. PyObject * plugin = NULL;
  271. PyObject * plugin_name = NULL;
  272. PyObject * next_tracename = NULL;
  273. /* Borrowed references. */
  274. PyObject * filename = NULL;
  275. PyObject * disp_trace = NULL;
  276. PyObject * tracename = NULL;
  277. PyObject * file_tracer = NULL;
  278. PyObject * has_dynamic_filename = NULL;
  279. CFileDisposition * pdisp = NULL;
  280. STATS( self->stats.calls++; )
  281. /* Grow the stack. */
  282. if (CTracer_set_pdata_stack(self) < 0) {
  283. goto error;
  284. }
  285. if (DataStack_grow(&self->stats, self->pdata_stack) < 0) {
  286. goto error;
  287. }
  288. /* Push the current state on the stack. */
  289. self->pdata_stack->stack[self->pdata_stack->depth] = self->cur_entry;
  290. /* See if this frame begins a new context. */
  291. if (self->should_start_context && self->context == Py_None) {
  292. PyObject * context;
  293. /* We're looking for our context, ask should_start_context if this is the start. */
  294. STATS( self->stats.start_context_calls++; )
  295. STATS( self->stats.pycalls++; )
  296. context = PyObject_CallFunctionObjArgs(self->should_start_context, frame, NULL);
  297. if (context == NULL) {
  298. goto error;
  299. }
  300. if (context != Py_None) {
  301. PyObject * val;
  302. Py_DECREF(self->context);
  303. self->context = context;
  304. self->cur_entry.started_context = TRUE;
  305. STATS( self->stats.pycalls++; )
  306. val = PyObject_CallFunctionObjArgs(self->switch_context, context, NULL);
  307. if (val == NULL) {
  308. goto error;
  309. }
  310. Py_DECREF(val);
  311. }
  312. else {
  313. Py_DECREF(context);
  314. self->cur_entry.started_context = FALSE;
  315. }
  316. }
  317. else {
  318. self->cur_entry.started_context = FALSE;
  319. }
  320. /* Check if we should trace this line. */
  321. filename = frame->f_code->co_filename;
  322. disposition = PyDict_GetItem(self->should_trace_cache, filename);
  323. if (disposition == NULL) {
  324. if (PyErr_Occurred()) {
  325. goto error;
  326. }
  327. STATS( self->stats.new_files++; )
  328. /* We've never considered this file before. */
  329. /* Ask should_trace about it. */
  330. STATS( self->stats.pycalls++; )
  331. disposition = PyObject_CallFunctionObjArgs(self->should_trace, filename, frame, NULL);
  332. if (disposition == NULL) {
  333. /* An error occurred inside should_trace. */
  334. goto error;
  335. }
  336. if (PyDict_SetItem(self->should_trace_cache, filename, disposition) < 0) {
  337. goto error;
  338. }
  339. }
  340. else {
  341. Py_INCREF(disposition);
  342. }
  343. if (disposition == Py_None) {
  344. /* A later check_include returned false, so don't trace it. */
  345. disp_trace = Py_False;
  346. }
  347. else {
  348. /* The object we got is a CFileDisposition, use it efficiently. */
  349. pdisp = (CFileDisposition *) disposition;
  350. disp_trace = pdisp->trace;
  351. if (disp_trace == NULL) {
  352. goto error;
  353. }
  354. }
  355. if (disp_trace == Py_True) {
  356. /* If tracename is a string, then we're supposed to trace. */
  357. tracename = pdisp->source_filename;
  358. if (tracename == NULL) {
  359. goto error;
  360. }
  361. file_tracer = pdisp->file_tracer;
  362. if (file_tracer == NULL) {
  363. goto error;
  364. }
  365. if (file_tracer != Py_None) {
  366. plugin = PyObject_GetAttr(file_tracer, str__coverage_plugin);
  367. if (plugin == NULL) {
  368. goto error;
  369. }
  370. plugin_name = PyObject_GetAttr(plugin, str__coverage_plugin_name);
  371. if (plugin_name == NULL) {
  372. goto error;
  373. }
  374. }
  375. has_dynamic_filename = pdisp->has_dynamic_filename;
  376. if (has_dynamic_filename == NULL) {
  377. goto error;
  378. }
  379. if (has_dynamic_filename == Py_True) {
  380. STATS( self->stats.pycalls++; )
  381. next_tracename = PyObject_CallMethodObjArgs(
  382. file_tracer, str_dynamic_source_filename,
  383. tracename, frame, NULL
  384. );
  385. if (next_tracename == NULL) {
  386. /* An exception from the function. Alert the user with a
  387. * warning and a traceback.
  388. */
  389. CTracer_disable_plugin(self, disposition);
  390. /* Because we handled the error, goto ok. */
  391. goto ok;
  392. }
  393. tracename = next_tracename;
  394. if (tracename != Py_None) {
  395. /* Check the dynamic source filename against the include rules. */
  396. PyObject * included = NULL;
  397. int should_include;
  398. included = PyDict_GetItem(self->should_trace_cache, tracename);
  399. if (included == NULL) {
  400. PyObject * should_include_bool;
  401. if (PyErr_Occurred()) {
  402. goto error;
  403. }
  404. STATS( self->stats.new_files++; )
  405. STATS( self->stats.pycalls++; )
  406. should_include_bool = PyObject_CallFunctionObjArgs(self->check_include, tracename, frame, NULL);
  407. if (should_include_bool == NULL) {
  408. goto error;
  409. }
  410. should_include = (should_include_bool == Py_True);
  411. Py_DECREF(should_include_bool);
  412. if (PyDict_SetItem(self->should_trace_cache, tracename, should_include ? disposition : Py_None) < 0) {
  413. goto error;
  414. }
  415. }
  416. else {
  417. should_include = (included != Py_None);
  418. }
  419. if (!should_include) {
  420. tracename = Py_None;
  421. }
  422. }
  423. }
  424. }
  425. else {
  426. tracename = Py_None;
  427. }
  428. if (tracename != Py_None) {
  429. PyObject * file_data = PyDict_GetItem(self->data, tracename);
  430. if (file_data == NULL) {
  431. if (PyErr_Occurred()) {
  432. goto error;
  433. }
  434. file_data = PyDict_New();
  435. if (file_data == NULL) {
  436. goto error;
  437. }
  438. ret2 = PyDict_SetItem(self->data, tracename, file_data);
  439. Py_DECREF(file_data);
  440. if (ret2 < 0) {
  441. goto error;
  442. }
  443. /* If the disposition mentions a plugin, record that. */
  444. if (file_tracer != Py_None) {
  445. ret2 = PyDict_SetItem(self->file_tracers, tracename, plugin_name);
  446. if (ret2 < 0) {
  447. goto error;
  448. }
  449. }
  450. }
  451. self->cur_entry.file_data = file_data;
  452. self->cur_entry.file_tracer = file_tracer;
  453. /* Make the frame right in case settrace(gettrace()) happens. */
  454. Py_INCREF(self);
  455. frame->f_trace = (PyObject*)self;
  456. SHOWLOG(self->pdata_stack->depth, frame->f_lineno, filename, "traced");
  457. }
  458. else {
  459. self->cur_entry.file_data = NULL;
  460. self->cur_entry.file_tracer = Py_None;
  461. SHOWLOG(self->pdata_stack->depth, frame->f_lineno, filename, "skipped");
  462. }
  463. self->cur_entry.disposition = disposition;
  464. /* A call event is really a "start frame" event, and can happen for
  465. * re-entering a generator also. f_lasti is -1 for a true call, and a
  466. * real byte offset for a generator re-entry.
  467. */
  468. if (frame->f_lasti < 0) {
  469. self->cur_entry.last_line = -frame->f_code->co_firstlineno;
  470. }
  471. else {
  472. self->cur_entry.last_line = frame->f_lineno;
  473. }
  474. ok:
  475. ret = RET_OK;
  476. error:
  477. Py_XDECREF(next_tracename);
  478. Py_XDECREF(disposition);
  479. Py_XDECREF(plugin);
  480. Py_XDECREF(plugin_name);
  481. return ret;
  482. }
  483. static void
  484. CTracer_disable_plugin(CTracer *self, PyObject * disposition)
  485. {
  486. PyObject * file_tracer = NULL;
  487. PyObject * plugin = NULL;
  488. PyObject * plugin_name = NULL;
  489. PyObject * msg = NULL;
  490. PyObject * ignored = NULL;
  491. PyErr_Print();
  492. file_tracer = PyObject_GetAttr(disposition, str_file_tracer);
  493. if (file_tracer == NULL) {
  494. goto error;
  495. }
  496. if (file_tracer == Py_None) {
  497. /* This shouldn't happen... */
  498. goto ok;
  499. }
  500. plugin = PyObject_GetAttr(file_tracer, str__coverage_plugin);
  501. if (plugin == NULL) {
  502. goto error;
  503. }
  504. plugin_name = PyObject_GetAttr(plugin, str__coverage_plugin_name);
  505. if (plugin_name == NULL) {
  506. goto error;
  507. }
  508. msg = MyText_FromFormat(
  509. "Disabling plugin '%s' due to previous exception",
  510. MyText_AsString(plugin_name)
  511. );
  512. if (msg == NULL) {
  513. goto error;
  514. }
  515. STATS( self->stats.pycalls++; )
  516. ignored = PyObject_CallFunctionObjArgs(self->warn, msg, NULL);
  517. if (ignored == NULL) {
  518. goto error;
  519. }
  520. /* Disable the plugin for future files, and stop tracing this file. */
  521. if (PyObject_SetAttr(plugin, str__coverage_enabled, Py_False) < 0) {
  522. goto error;
  523. }
  524. if (PyObject_SetAttr(disposition, str_trace, Py_False) < 0) {
  525. goto error;
  526. }
  527. goto ok;
  528. error:
  529. /* This function doesn't return a status, so if an error happens, print it,
  530. * but don't interrupt the flow. */
  531. /* PySys_WriteStderr is nicer, but is not in the public API. */
  532. fprintf(stderr, "Error occurred while disabling plugin:\n");
  533. PyErr_Print();
  534. ok:
  535. Py_XDECREF(file_tracer);
  536. Py_XDECREF(plugin);
  537. Py_XDECREF(plugin_name);
  538. Py_XDECREF(msg);
  539. Py_XDECREF(ignored);
  540. }
  541. static int
  542. CTracer_unpack_pair(CTracer *self, PyObject *pair, int *p_one, int *p_two)
  543. {
  544. int ret = RET_ERROR;
  545. int the_int;
  546. PyObject * pyint = NULL;
  547. int index;
  548. if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2) {
  549. PyErr_SetString(
  550. PyExc_TypeError,
  551. "line_number_range must return 2-tuple"
  552. );
  553. goto error;
  554. }
  555. for (index = 0; index < 2; index++) {
  556. pyint = PyTuple_GetItem(pair, index);
  557. if (pyint == NULL) {
  558. goto error;
  559. }
  560. if (pyint_as_int(pyint, &the_int) < 0) {
  561. goto error;
  562. }
  563. *(index == 0 ? p_one : p_two) = the_int;
  564. }
  565. ret = RET_OK;
  566. error:
  567. return ret;
  568. }
  569. static int
  570. CTracer_handle_line(CTracer *self, PyFrameObject *frame)
  571. {
  572. int ret = RET_ERROR;
  573. int ret2;
  574. STATS( self->stats.lines++; )
  575. if (self->pdata_stack->depth >= 0) {
  576. SHOWLOG(self->pdata_stack->depth, frame->f_lineno, frame->f_code->co_filename, "line");
  577. if (self->cur_entry.file_data) {
  578. int lineno_from = -1;
  579. int lineno_to = -1;
  580. /* We're tracing in this frame: record something. */
  581. if (self->cur_entry.file_tracer != Py_None) {
  582. PyObject * from_to = NULL;
  583. STATS( self->stats.pycalls++; )
  584. from_to = PyObject_CallMethodObjArgs(self->cur_entry.file_tracer, str_line_number_range, frame, NULL);
  585. if (from_to == NULL) {
  586. goto error;
  587. }
  588. ret2 = CTracer_unpack_pair(self, from_to, &lineno_from, &lineno_to);
  589. Py_DECREF(from_to);
  590. if (ret2 < 0) {
  591. CTracer_disable_plugin(self, self->cur_entry.disposition);
  592. goto ok;
  593. }
  594. }
  595. else {
  596. lineno_from = lineno_to = frame->f_lineno;
  597. }
  598. if (lineno_from != -1) {
  599. for (; lineno_from <= lineno_to; lineno_from++) {
  600. if (self->tracing_arcs) {
  601. /* Tracing arcs: key is (last_line,this_line). */
  602. if (CTracer_record_pair(self, self->cur_entry.last_line, lineno_from) < 0) {
  603. goto error;
  604. }
  605. }
  606. else {
  607. /* Tracing lines: key is simply this_line. */
  608. PyObject * this_line = MyInt_FromInt(lineno_from);
  609. if (this_line == NULL) {
  610. goto error;
  611. }
  612. ret2 = PyDict_SetItem(self->cur_entry.file_data, this_line, Py_None);
  613. Py_DECREF(this_line);
  614. if (ret2 < 0) {
  615. goto error;
  616. }
  617. }
  618. self->cur_entry.last_line = lineno_from;
  619. }
  620. }
  621. }
  622. }
  623. ok:
  624. ret = RET_OK;
  625. error:
  626. return ret;
  627. }
  628. static int
  629. CTracer_handle_return(CTracer *self, PyFrameObject *frame)
  630. {
  631. int ret = RET_ERROR;
  632. STATS( self->stats.returns++; )
  633. /* A near-copy of this code is above in the missing-return handler. */
  634. if (CTracer_set_pdata_stack(self) < 0) {
  635. goto error;
  636. }
  637. if (self->pdata_stack->depth >= 0) {
  638. if (self->tracing_arcs && self->cur_entry.file_data) {
  639. /* Need to distinguish between RETURN_VALUE and YIELD_VALUE. Read
  640. * the current bytecode to see what it is. In unusual circumstances
  641. * (Cython code), co_code can be the empty string, so range-check
  642. * f_lasti before reading the byte.
  643. */
  644. int bytecode = RETURN_VALUE;
  645. PyObject * pCode = frame->f_code->co_code;
  646. int lasti = frame->f_lasti;
  647. if (lasti < MyBytes_GET_SIZE(pCode)) {
  648. bytecode = MyBytes_AS_STRING(pCode)[lasti];
  649. }
  650. if (bytecode != YIELD_VALUE) {
  651. int first = frame->f_code->co_firstlineno;
  652. if (CTracer_record_pair(self, self->cur_entry.last_line, -first) < 0) {
  653. goto error;
  654. }
  655. }
  656. }
  657. /* If this frame started a context, then returning from it ends the context. */
  658. if (self->cur_entry.started_context) {
  659. PyObject * val;
  660. Py_DECREF(self->context);
  661. self->context = Py_None;
  662. Py_INCREF(self->context);
  663. STATS( self->stats.pycalls++; )
  664. val = PyObject_CallFunctionObjArgs(self->switch_context, self->context, NULL);
  665. if (val == NULL) {
  666. goto error;
  667. }
  668. Py_DECREF(val);
  669. }
  670. /* Pop the stack. */
  671. SHOWLOG(self->pdata_stack->depth, frame->f_lineno, frame->f_code->co_filename, "return");
  672. self->cur_entry = self->pdata_stack->stack[self->pdata_stack->depth];
  673. self->pdata_stack->depth--;
  674. }
  675. ret = RET_OK;
  676. error:
  677. return ret;
  678. }
  679. static int
  680. CTracer_handle_exception(CTracer *self, PyFrameObject *frame)
  681. {
  682. /* Some code (Python 2.3, and pyexpat anywhere) fires an exception event
  683. without a return event. To detect that, we'll keep a copy of the
  684. parent frame for an exception event. If the next event is in that
  685. frame, then we must have returned without a return event. We can
  686. synthesize the missing event then.
  687. Python itself fixed this problem in 2.4. Pyexpat still has the bug.
  688. I've reported the problem with pyexpat as http://bugs.python.org/issue6359 .
  689. If it gets fixed, this code should still work properly. Maybe some day
  690. the bug will be fixed everywhere coverage.py is supported, and we can
  691. remove this missing-return detection.
  692. More about this fix: http://nedbatchelder.com/blog/200907/a_nasty_little_bug.html
  693. */
  694. STATS( self->stats.exceptions++; )
  695. self->last_exc_back = frame->f_back;
  696. self->last_exc_firstlineno = frame->f_code->co_firstlineno;
  697. return RET_OK;
  698. }
  699. /*
  700. * The Trace Function
  701. */
  702. static int
  703. CTracer_trace(CTracer *self, PyFrameObject *frame, int what, PyObject *arg_unused)
  704. {
  705. int ret = RET_ERROR;
  706. #if WHAT_LOG || TRACE_LOG
  707. PyObject * ascii = NULL;
  708. #endif
  709. #if WHAT_LOG
  710. if (what <= (int)(sizeof(what_sym)/sizeof(const char *))) {
  711. ascii = MyText_AS_BYTES(frame->f_code->co_filename);
  712. printf("trace: %s @ %s %d\n", what_sym[what], MyBytes_AS_STRING(ascii), frame->f_lineno);
  713. Py_DECREF(ascii);
  714. }
  715. #endif
  716. #if TRACE_LOG
  717. ascii = MyText_AS_BYTES(frame->f_code->co_filename);
  718. if (strstr(MyBytes_AS_STRING(ascii), start_file) && frame->f_lineno == start_line) {
  719. logging = TRUE;
  720. }
  721. Py_DECREF(ascii);
  722. #endif
  723. /* See below for details on missing-return detection. */
  724. if (CTracer_check_missing_return(self, frame) < 0) {
  725. goto error;
  726. }
  727. switch (what) {
  728. case PyTrace_CALL:
  729. if (CTracer_handle_call(self, frame) < 0) {
  730. goto error;
  731. }
  732. break;
  733. case PyTrace_RETURN:
  734. if (CTracer_handle_return(self, frame) < 0) {
  735. goto error;
  736. }
  737. break;
  738. case PyTrace_LINE:
  739. if (CTracer_handle_line(self, frame) < 0) {
  740. goto error;
  741. }
  742. break;
  743. case PyTrace_EXCEPTION:
  744. if (CTracer_handle_exception(self, frame) < 0) {
  745. goto error;
  746. }
  747. break;
  748. default:
  749. STATS( self->stats.others++; )
  750. break;
  751. }
  752. ret = RET_OK;
  753. goto cleanup;
  754. error:
  755. STATS( self->stats.errors++; )
  756. cleanup:
  757. return ret;
  758. }
  759. /*
  760. * Python has two ways to set the trace function: sys.settrace(fn), which
  761. * takes a Python callable, and PyEval_SetTrace(func, obj), which takes
  762. * a C function and a Python object. The way these work together is that
  763. * sys.settrace(pyfn) calls PyEval_SetTrace(builtin_func, pyfn), using the
  764. * Python callable as the object in PyEval_SetTrace. So sys.gettrace()
  765. * simply returns the Python object used as the second argument to
  766. * PyEval_SetTrace. So sys.gettrace() will return our self parameter, which
  767. * means it must be callable to be used in sys.settrace().
  768. *
  769. * So we make ourself callable, equivalent to invoking our trace function.
  770. *
  771. * To help with the process of replaying stored frames, this function has an
  772. * optional keyword argument:
  773. *
  774. * def CTracer_call(frame, event, arg, lineno=0)
  775. *
  776. * If provided, the lineno argument is used as the line number, and the
  777. * frame's f_lineno member is ignored.
  778. */
  779. static PyObject *
  780. CTracer_call(CTracer *self, PyObject *args, PyObject *kwds)
  781. {
  782. PyFrameObject *frame;
  783. PyObject *what_str;
  784. PyObject *arg;
  785. int lineno = 0;
  786. int what;
  787. int orig_lineno;
  788. PyObject *ret = NULL;
  789. PyObject * ascii = NULL;
  790. static char *what_names[] = {
  791. "call", "exception", "line", "return",
  792. "c_call", "c_exception", "c_return",
  793. NULL
  794. };
  795. static char *kwlist[] = {"frame", "event", "arg", "lineno", NULL};
  796. if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O!O|i:Tracer_call", kwlist,
  797. &PyFrame_Type, &frame, &MyText_Type, &what_str, &arg, &lineno)) {
  798. goto done;
  799. }
  800. /* In Python, the what argument is a string, we need to find an int
  801. for the C function. */
  802. for (what = 0; what_names[what]; what++) {
  803. int should_break;
  804. ascii = MyText_AS_BYTES(what_str);
  805. should_break = !strcmp(MyBytes_AS_STRING(ascii), what_names[what]);
  806. Py_DECREF(ascii);
  807. if (should_break) {
  808. break;
  809. }
  810. }
  811. #if WHAT_LOG
  812. ascii = MyText_AS_BYTES(frame->f_code->co_filename);
  813. printf("pytrace: %s @ %s %d\n", what_sym[what], MyBytes_AS_STRING(ascii), frame->f_lineno);
  814. Py_DECREF(ascii);
  815. #endif
  816. /* Save off the frame's lineno, and use the forced one, if provided. */
  817. orig_lineno = frame->f_lineno;
  818. if (lineno > 0) {
  819. frame->f_lineno = lineno;
  820. }
  821. /* Invoke the C function, and return ourselves. */
  822. if (CTracer_trace(self, frame, what, arg) == RET_OK) {
  823. Py_INCREF(self);
  824. ret = (PyObject *)self;
  825. }
  826. /* Clean up. */
  827. frame->f_lineno = orig_lineno;
  828. /* For better speed, install ourselves the C way so that future calls go
  829. directly to CTracer_trace, without this intermediate function.
  830. Only do this if this is a CALL event, since new trace functions only
  831. take effect then. If we don't condition it on CALL, then we'll clobber
  832. the new trace function before it has a chance to get called. To
  833. understand why, there are three internal values to track: frame.f_trace,
  834. c_tracefunc, and c_traceobj. They are explained here:
  835. http://nedbatchelder.com/text/trace-function.html
  836. Without the conditional on PyTrace_CALL, this is what happens:
  837. def func(): # f_trace c_tracefunc c_traceobj
  838. # -------------- -------------- --------------
  839. # CTracer CTracer.trace CTracer
  840. sys.settrace(my_func)
  841. # CTracer trampoline my_func
  842. # Now Python calls trampoline(CTracer), which calls this function
  843. # which calls PyEval_SetTrace below, setting us as the tracer again:
  844. # CTracer CTracer.trace CTracer
  845. # and it's as if the settrace never happened.
  846. */
  847. if (what == PyTrace_CALL) {
  848. PyEval_SetTrace((Py_tracefunc)CTracer_trace, (PyObject*)self);
  849. }
  850. done:
  851. return ret;
  852. }
  853. static PyObject *
  854. CTracer_start(CTracer *self, PyObject *args_unused)
  855. {
  856. PyEval_SetTrace((Py_tracefunc)CTracer_trace, (PyObject*)self);
  857. self->started = TRUE;
  858. self->tracing_arcs = self->trace_arcs && PyObject_IsTrue(self->trace_arcs);
  859. /* start() returns a trace function usable with sys.settrace() */
  860. Py_INCREF(self);
  861. return (PyObject *)self;
  862. }
  863. static PyObject *
  864. CTracer_stop(CTracer *self, PyObject *args_unused)
  865. {
  866. if (self->started) {
  867. PyEval_SetTrace(NULL, NULL);
  868. self->started = FALSE;
  869. }
  870. Py_RETURN_NONE;
  871. }
  872. static PyObject *
  873. CTracer_get_stats(CTracer *self)
  874. {
  875. #if COLLECT_STATS
  876. return Py_BuildValue(
  877. "{sI,sI,sI,sI,sI,sI,sI,sI,si,sI,sI,sI}",
  878. "calls", self->stats.calls,
  879. "lines", self->stats.lines,
  880. "returns", self->stats.returns,
  881. "exceptions", self->stats.exceptions,
  882. "others", self->stats.others,
  883. "new_files", self->stats.new_files,
  884. "missed_returns", self->stats.missed_returns,
  885. "stack_reallocs", self->stats.stack_reallocs,
  886. "stack_alloc", self->pdata_stack->alloc,
  887. "errors", self->stats.errors,
  888. "pycalls", self->stats.pycalls,
  889. "start_context_calls", self->stats.start_context_calls
  890. );
  891. #else
  892. Py_RETURN_NONE;
  893. #endif /* COLLECT_STATS */
  894. }
  895. static PyMemberDef
  896. CTracer_members[] = {
  897. { "should_trace", T_OBJECT, offsetof(CTracer, should_trace), 0,
  898. PyDoc_STR("Function indicating whether to trace a file.") },
  899. { "check_include", T_OBJECT, offsetof(CTracer, check_include), 0,
  900. PyDoc_STR("Function indicating whether to include a file.") },
  901. { "warn", T_OBJECT, offsetof(CTracer, warn), 0,
  902. PyDoc_STR("Function for issuing warnings.") },
  903. { "concur_id_func", T_OBJECT, offsetof(CTracer, concur_id_func), 0,
  904. PyDoc_STR("Function for determining concurrency context") },
  905. { "data", T_OBJECT, offsetof(CTracer, data), 0,
  906. PyDoc_STR("The raw dictionary of trace data.") },
  907. { "file_tracers", T_OBJECT, offsetof(CTracer, file_tracers), 0,
  908. PyDoc_STR("Mapping from file name to plugin name.") },
  909. { "should_trace_cache", T_OBJECT, offsetof(CTracer, should_trace_cache), 0,
  910. PyDoc_STR("Dictionary caching should_trace results.") },
  911. { "trace_arcs", T_OBJECT, offsetof(CTracer, trace_arcs), 0,
  912. PyDoc_STR("Should we trace arcs, or just lines?") },
  913. { "should_start_context", T_OBJECT, offsetof(CTracer, should_start_context), 0,
  914. PyDoc_STR("Function for starting contexts.") },
  915. { "switch_context", T_OBJECT, offsetof(CTracer, switch_context), 0,
  916. PyDoc_STR("Function for switch to a new context.") },
  917. { NULL }
  918. };
  919. static PyMethodDef
  920. CTracer_methods[] = {
  921. { "start", (PyCFunction) CTracer_start, METH_VARARGS,
  922. PyDoc_STR("Start the tracer") },
  923. { "stop", (PyCFunction) CTracer_stop, METH_VARARGS,
  924. PyDoc_STR("Stop the tracer") },
  925. { "get_stats", (PyCFunction) CTracer_get_stats, METH_VARARGS,
  926. PyDoc_STR("Get statistics about the tracing") },
  927. { NULL }
  928. };
  929. PyTypeObject
  930. CTracerType = {
  931. MyType_HEAD_INIT
  932. "coverage.CTracer", /*tp_name*/
  933. sizeof(CTracer), /*tp_basicsize*/
  934. 0, /*tp_itemsize*/
  935. (destructor)CTracer_dealloc, /*tp_dealloc*/
  936. 0, /*tp_print*/
  937. 0, /*tp_getattr*/
  938. 0, /*tp_setattr*/
  939. 0, /*tp_compare*/
  940. 0, /*tp_repr*/
  941. 0, /*tp_as_number*/
  942. 0, /*tp_as_sequence*/
  943. 0, /*tp_as_mapping*/
  944. 0, /*tp_hash */
  945. (ternaryfunc)CTracer_call, /*tp_call*/
  946. 0, /*tp_str*/
  947. 0, /*tp_getattro*/
  948. 0, /*tp_setattro*/
  949. 0, /*tp_as_buffer*/
  950. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
  951. "CTracer objects", /* tp_doc */
  952. 0, /* tp_traverse */
  953. 0, /* tp_clear */
  954. 0, /* tp_richcompare */
  955. 0, /* tp_weaklistoffset */
  956. 0, /* tp_iter */
  957. 0, /* tp_iternext */
  958. CTracer_methods, /* tp_methods */
  959. CTracer_members, /* tp_members */
  960. 0, /* tp_getset */
  961. 0, /* tp_base */
  962. 0, /* tp_dict */
  963. 0, /* tp_descr_get */
  964. 0, /* tp_descr_set */
  965. 0, /* tp_dictoffset */
  966. (initproc)CTracer_init, /* tp_init */
  967. 0, /* tp_alloc */
  968. 0, /* tp_new */
  969. };