|
| 1 | +#include "c_trace_callbacks.h" |
| 2 | + |
| 3 | +#define CYTHON_MODULE "line_profiler._line_profiler" |
| 4 | +#define DISABLE_CALLBACK "disable_line_events" |
| 5 | +#define RAISE_IN_CALL(func_name, xc, const_msg) \ |
| 6 | + PyErr_SetString(xc, \ |
| 7 | + "in `" CYTHON_MODULE "." func_name "()`: " \ |
| 8 | + const_msg) |
| 9 | + |
| 10 | +TraceCallback *alloc_callback() |
| 11 | +{ |
| 12 | + /* Heap-allocate a new `TraceCallback`. */ |
| 13 | + TraceCallback *callback = (TraceCallback*)malloc(sizeof(TraceCallback)); |
| 14 | + if (callback == NULL) RAISE_IN_CALL( |
| 15 | + // If we're here we have bigger fish to fry... but be nice and |
| 16 | + // raise an error explicitly anyway |
| 17 | + "alloc_callback", |
| 18 | + PyExc_MemoryError, |
| 19 | + "failed to allocate memory for storing the existing " |
| 20 | + "`sys` trace callback" |
| 21 | + ); |
| 22 | + return callback; |
| 23 | +} |
| 24 | + |
| 25 | +void free_callback(TraceCallback *callback) |
| 26 | +{ |
| 27 | + /* Free a heap-allocated `TraceCallback`. */ |
| 28 | + if (callback != NULL) free(callback); |
| 29 | + return; |
| 30 | +} |
| 31 | + |
| 32 | +void fetch_callback(TraceCallback *callback) |
| 33 | +{ |
| 34 | + /* Store the members `.c_tracefunc` and `.c_traceobj` of the |
| 35 | + * current thread on `callback`. |
| 36 | + */ |
| 37 | + // Shouldn't happen, but just to be safe |
| 38 | + if (callback == NULL) return; |
| 39 | + // No need to `Py_DECREF()` the thread callback, since it isn't a |
| 40 | + // `PyObject` |
| 41 | + PyThreadState *thread_state = PyThreadState_Get(); |
| 42 | + callback->c_tracefunc = thread_state->c_tracefunc; |
| 43 | + callback->c_traceobj = thread_state->c_traceobj; |
| 44 | + // No need for NULL check with `Py_XINCREF()` |
| 45 | + Py_XINCREF(callback->c_traceobj); |
| 46 | + return; |
| 47 | +} |
| 48 | + |
| 49 | +void nullify_callback(TraceCallback *callback) |
| 50 | +{ |
| 51 | + // No need for NULL check with `Py_XDECREF()` |
| 52 | + Py_XDECREF(callback->c_traceobj); |
| 53 | + callback->c_tracefunc = NULL; |
| 54 | + callback->c_traceobj = NULL; |
| 55 | + return; |
| 56 | +} |
| 57 | + |
| 58 | +void restore_callback(TraceCallback *callback) |
| 59 | +{ |
| 60 | + /* Use `PyEval_SetTrace()` to set the trace callback on the current |
| 61 | + * thread to be consistent with the `callback`, then nullify the |
| 62 | + * pointers on `callback`. |
| 63 | + */ |
| 64 | + // Shouldn't happen, but just to be safe |
| 65 | + if (callback == NULL) return; |
| 66 | + PyEval_SetTrace(callback->c_tracefunc, callback->c_traceobj); |
| 67 | + nullify_callback(callback); |
| 68 | + return; |
| 69 | +} |
| 70 | + |
| 71 | +inline int is_null_callback(TraceCallback *callback) |
| 72 | +{ |
| 73 | + return ( |
| 74 | + callback == NULL |
| 75 | + || callback->c_tracefunc == NULL |
| 76 | + || callback->c_traceobj == NULL |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +int call_callback( |
| 81 | + TraceCallback *callback, |
| 82 | + PyFrameObject *py_frame, |
| 83 | + int what, |
| 84 | + PyObject *arg |
| 85 | +) |
| 86 | +{ |
| 87 | + /* Call the cached trace callback `callback` where appropriate, and |
| 88 | + * in a "safe" way so that: |
| 89 | + * - If it alters the `sys` trace callback, or |
| 90 | + * - If it sets `.f_trace_lines` to false, |
| 91 | + * said alterations are reverted so as not to hinder profiling. |
| 92 | + * |
| 93 | + * Returns: |
| 94 | + * - 0 if `callback` is `NULL` or has nullified members; |
| 95 | + * - -1 if an error occurs (e.g. when the disabling of line |
| 96 | + * events for the frame-local trace function failed); |
| 97 | + * - The result of calling said callback otherwise. |
| 98 | + * |
| 99 | + * Side effects: |
| 100 | + * - If the callback unsets the `sys` callback, the `sys` |
| 101 | + * callback is preserved but `callback` itself is nullified. |
| 102 | + * This is to comply with what Python usually does: if the |
| 103 | + * trace callback errors out, `sys.settrace(None)` is called. |
| 104 | + * - If a frame-local callback sets the `.f_trace_lines` to |
| 105 | + * false, `.f_trace_lines` is reverted but `.f_trace` is |
| 106 | + * wrapped so that it no loger sees line events. |
| 107 | + * |
| 108 | + * Notes: |
| 109 | + * It is tempting to assume said current callback value to be |
| 110 | + * `{ python_trace_callback, <profiler> }`, but remember that |
| 111 | + * our callback may very well be called via another callback, |
| 112 | + * much like how we call the cached callback via |
| 113 | + * `python_trace_callback()`. |
| 114 | + */ |
| 115 | + TraceCallback before, after; |
| 116 | + PyObject *mod = NULL, *dle = NULL, *f_trace = NULL; |
| 117 | + char f_trace_lines; |
| 118 | + int result; |
| 119 | + |
| 120 | + if (is_null_callback(callback)) return 0; |
| 121 | + |
| 122 | + f_trace_lines = py_frame->f_trace_lines; |
| 123 | + fetch_callback(&before); |
| 124 | + result = (callback->c_tracefunc)( |
| 125 | + callback->c_traceobj, py_frame, what, arg |
| 126 | + ); |
| 127 | + |
| 128 | + // Check if the callback has unset itself; if so, nullify `callback` |
| 129 | + fetch_callback(&after); |
| 130 | + if (is_null_callback(&after)) nullify_callback(callback); |
| 131 | + nullify_callback(&after); |
| 132 | + restore_callback(&before); |
| 133 | + |
| 134 | + // Check if a callback has disabled future line events for the |
| 135 | + // frame, and if so, revert the change while withholding future line |
| 136 | + // events from the callback |
| 137 | + if ( |
| 138 | + !(py_frame->f_trace_lines) |
| 139 | + && f_trace_lines != py_frame->f_trace_lines |
| 140 | + ) |
| 141 | + { |
| 142 | + py_frame->f_trace_lines = f_trace_lines; |
| 143 | + if (py_frame->f_trace != NULL && py_frame->f_trace != Py_None) |
| 144 | + { |
| 145 | + // FIXME: can we get more performance by stashing a somewhat |
| 146 | + // permanent reference to |
| 147 | + // `line_profiler._line_profiler.disable_line_events()` |
| 148 | + // somewhere? |
| 149 | + mod = PyImport_AddModuleRef(CYTHON_MODULE); |
| 150 | + if (mod == NULL) |
| 151 | + { |
| 152 | + RAISE_IN_CALL( |
| 153 | + "call_callback", |
| 154 | + PyExc_ImportError, |
| 155 | + "cannot import `" CYTHON_MODULE "`" |
| 156 | + ); |
| 157 | + result = -1; |
| 158 | + goto cleanup; |
| 159 | + } |
| 160 | + dle = PyObject_GetAttrString(mod, DISABLE_CALLBACK); |
| 161 | + if (dle == NULL) |
| 162 | + { |
| 163 | + RAISE_IN_CALL( |
| 164 | + "call_callback", |
| 165 | + PyExc_AttributeError, |
| 166 | + "`line_profiler._line_profiler` has no " |
| 167 | + "attribute `" DISABLE_CALLBACK "`" |
| 168 | + ); |
| 169 | + result = -1; |
| 170 | + goto cleanup; |
| 171 | + } |
| 172 | + // Note: DON'T `Py_[X]DECREF()` the pointer! Nothing else is |
| 173 | + // holding a reference to it. |
| 174 | + f_trace = PyObject_CallFunctionObjArgs( |
| 175 | + dle, py_frame->f_trace, NULL |
| 176 | + ); |
| 177 | + if (f_trace == NULL) |
| 178 | + { |
| 179 | + // No need to raise another exception, it's already |
| 180 | + // raised in the call |
| 181 | + result = -1; |
| 182 | + goto cleanup; |
| 183 | + } |
| 184 | + py_frame->f_trace = f_trace; |
| 185 | + } |
| 186 | + } |
| 187 | +cleanup: |
| 188 | + Py_XDECREF(mod); |
| 189 | + Py_XDECREF(dle); |
| 190 | + return result; |
| 191 | +} |
0 commit comments