Debian Codegraph @

Modules/_testcapi/monitoring.c

raw
1 #include "parts.h"
2 #include "util.h"
3
4 #include "monitoring.h"
5
6 #define Py_BUILD_CORE
7 #include "internal/pycore_instruments.h"
8
9 typedef struct {
10 PyObject_HEAD
11 PyMonitoringState *monitoring_states;
12 uint64_t version;
13 int num_events;
14 /* Other fields */
15 } PyCodeLikeObject;
16
17 #define PyCodeLikeObject_CAST(op) ((PyCodeLikeObject *)(op))
18
19 static PyObject *
20 CodeLike_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
21 {
22 int num_events;
23 if (!PyArg_ParseTuple(args, "i", &num_events)) {
24 return NULL;
25 }
26 PyMonitoringState *states = (PyMonitoringState *)PyMem_Calloc(
27 num_events, sizeof(PyMonitoringState));
28 if (states == NULL) {
29 return NULL;
30 }
31 PyCodeLikeObject *self = (PyCodeLikeObject *) type->tp_alloc(type, 0);
32 if (self != NULL) {
33 self->version = 0;
34 self->monitoring_states = states;
35 self->num_events = num_events;
36 }
37 else {
38 PyMem_Free(states);
39 }
40 return (PyObject *) self;
41 }
42
43 static void
44 CodeLike_dealloc(PyObject *op)
45 {
46 PyCodeLikeObject *self = PyCodeLikeObject_CAST(op);
47 if (self->monitoring_states) {
48 PyMem_Free(self->monitoring_states);
49 }
50 Py_TYPE(self)->tp_free((PyObject *) self);
51 }
52
53 static PyObject *
54 CodeLike_str(PyObject *op)
55 {
56 PyCodeLikeObject *self = PyCodeLikeObject_CAST(op);
57 PyObject *res = NULL;
58 PyObject *sep = NULL;
59 PyObject *parts = NULL;
60 if (self->monitoring_states) {
61 parts = PyList_New(0);
62 if (parts == NULL) {
63 goto end;
64 }
65
66 PyObject *heading = PyUnicode_FromString("PyCodeLikeObject");
67 if (heading == NULL) {
68 goto end;
69 }
70 int err = PyList_Append(parts, heading);
71 Py_DECREF(heading);
72 if (err < 0) {
73 goto end;
74 }
75
76 for (int i = 0; i < self->num_events; i++) {
77 PyObject *part = PyUnicode_FromFormat(" %d", self->monitoring_states[i].active);
78 if (part == NULL) {
79 goto end;
80 }
81 int err = PyList_Append(parts, part);
82 Py_XDECREF(part);
83 if (err < 0) {
84 goto end;
85 }
86 }
87 sep = PyUnicode_FromString(": ");
88 if (sep == NULL) {
89 goto end;
90 }
91 res = PyUnicode_Join(sep, parts);
92 }
93 end:
94 Py_XDECREF(sep);
95 Py_XDECREF(parts);
96 return res;
97 }
98
99 static PyTypeObject PyCodeLike_Type = {
100 .ob_base = PyVarObject_HEAD_INIT(NULL, 0)
101 .tp_name = "monitoring.CodeLike",
102 .tp_doc = PyDoc_STR("CodeLike objects"),
103 .tp_basicsize = sizeof(PyCodeLikeObject),
104 .tp_itemsize = 0,
105 .tp_flags = Py_TPFLAGS_DEFAULT,
106 .tp_new = CodeLike_new,
107 .tp_dealloc = CodeLike_dealloc,
108 .tp_str = CodeLike_str,
109 };
110
111 #define RAISE_UNLESS_CODELIKE(v) if (!Py_IS_TYPE((v), &PyCodeLike_Type)) { \
112 PyErr_Format(PyExc_TypeError, "expected a code-like, got %s", Py_TYPE(v)->tp_name); \
113 return NULL; \
114 }
115
116 /*******************************************************************/
117
118 static PyMonitoringState *
119 setup_fire(PyObject *codelike, int offset, PyObject *exc)
120 {
121 RAISE_UNLESS_CODELIKE(codelike);
122 PyCodeLikeObject *cl = ((PyCodeLikeObject *)codelike);
123 assert(offset >= 0 && offset < cl->num_events);
124 PyMonitoringState *state = &cl->monitoring_states[offset];
125
126 if (exc != NULL) {
127 PyErr_SetRaisedException(Py_NewRef(exc));
128 }
129 return state;
130 }
131
132 static int
133 teardown_fire(int res, PyMonitoringState *state, PyObject *exception)
134 {
135 if (res == -1) {
136 return -1;
137 }
138 if (exception) {
139 assert(PyErr_Occurred());
140 assert(((PyObject*)Py_TYPE(exception)) == PyErr_Occurred());
141 }
142
143 else {
144 assert(!PyErr_Occurred());
145 }
146 PyErr_Clear();
147 return state->active;
148 }
149
150 static PyObject *
151 fire_event_py_start(PyObject *self, PyObject *args)
152 {
153 PyObject *codelike;
154 int offset;
155 if (!PyArg_ParseTuple(args, "Oi", &codelike, &offset)) {
156 return NULL;
157 }
158 PyObject *exception = NULL;
159 PyMonitoringState *state = setup_fire(codelike, offset, exception);
160 if (state == NULL) {
161 return NULL;
162 }
163 int res = PyMonitoring_FirePyStartEvent(state, codelike, offset);
164 RETURN_INT(teardown_fire(res, state, exception));
165 }
166
167 static PyObject *
168 fire_event_py_resume(PyObject *self, PyObject *args)
169 {
170 PyObject *codelike;
171 int offset;
172 if (!PyArg_ParseTuple(args, "Oi", &codelike, &offset)) {
173 return NULL;
174 }
175 PyObject *exception = NULL;
176 PyMonitoringState *state = setup_fire(codelike, offset, exception);
177 if (state == NULL) {
178 return NULL;
179 }
180 int res = PyMonitoring_FirePyResumeEvent(state, codelike, offset);
181 RETURN_INT(teardown_fire(res, state, exception));
182 }
183
184 static PyObject *
185 fire_event_py_return(PyObject *self, PyObject *args)
186 {
187 PyObject *codelike;
188 int offset;
189 PyObject *retval;
190 if (!PyArg_ParseTuple(args, "OiO", &codelike, &offset, &retval)) {
191 return NULL;
192 }
193 PyObject *exception = NULL;
194 PyMonitoringState *state = setup_fire(codelike, offset, exception);
195 if (state == NULL) {
196 return NULL;
197 }
198 int res = PyMonitoring_FirePyReturnEvent(state, codelike, offset, retval);
199 RETURN_INT(teardown_fire(res, state, exception));
200 }
201
202 static PyObject *
203 fire_event_c_return(PyObject *self, PyObject *args)
204 {
205 PyObject *codelike;
206 int offset;
207 PyObject *retval;
208 if (!PyArg_ParseTuple(args, "OiO", &codelike, &offset, &retval)) {
209 return NULL;
210 }
211 PyObject *exception = NULL;
212 PyMonitoringState *state = setup_fire(codelike, offset, exception);
213 if (state == NULL) {
214 return NULL;
215 }
216 int res = PyMonitoring_FireCReturnEvent(state, codelike, offset, retval);
217 RETURN_INT(teardown_fire(res, state, exception));
218 }
219
220 static PyObject *
221 fire_event_py_yield(PyObject *self, PyObject *args)
222 {
223 PyObject *codelike;
224 int offset;
225 PyObject *retval;
226 if (!PyArg_ParseTuple(args, "OiO", &codelike, &offset, &retval)) {
227 return NULL;
228 }
229 PyObject *exception = NULL;
230 PyMonitoringState *state = setup_fire(codelike, offset, exception);
231 if (state == NULL) {
232 return NULL;
233 }
234 int res = PyMonitoring_FirePyYieldEvent(state, codelike, offset, retval);
235 RETURN_INT(teardown_fire(res, state, exception));
236 }
237
238 static PyObject *
239 fire_event_call(PyObject *self, PyObject *args)
240 {
241 PyObject *codelike;
242 int offset;
243 PyObject *callable, *arg0;
244 if (!PyArg_ParseTuple(args, "OiOO", &codelike, &offset, &callable, &arg0)) {
245 return NULL;
246 }
247 PyObject *exception = NULL;
248 PyMonitoringState *state = setup_fire(codelike, offset, exception);
249 if (state == NULL) {
250 return NULL;
251 }
252 int res = PyMonitoring_FireCallEvent(state, codelike, offset, callable, arg0);
253 RETURN_INT(teardown_fire(res, state, exception));
254 }
255
256 static PyObject *
257 fire_event_line(PyObject *self, PyObject *args)
258 {
259 PyObject *codelike;
260 int offset, lineno;
261 if (!PyArg_ParseTuple(args, "Oii", &codelike, &offset, &lineno)) {
262 return NULL;
263 }
264 PyObject *exception = NULL;
265 PyMonitoringState *state = setup_fire(codelike, offset, exception);
266 if (state == NULL) {
267 return NULL;
268 }
269 int res = PyMonitoring_FireLineEvent(state, codelike, offset, lineno);
270 RETURN_INT(teardown_fire(res, state, exception));
271 }
272
273 static PyObject *
274 fire_event_jump(PyObject *self, PyObject *args)
275 {
276 PyObject *codelike;
277 int offset;
278 PyObject *target_offset;
279 if (!PyArg_ParseTuple(args, "OiO", &codelike, &offset, &target_offset)) {
280 return NULL;
281 }
282 PyObject *exception = NULL;
283 PyMonitoringState *state = setup_fire(codelike, offset, exception);
284 if (state == NULL) {
285 return NULL;
286 }
287 int res = PyMonitoring_FireJumpEvent(state, codelike, offset, target_offset);
288 RETURN_INT(teardown_fire(res, state, exception));
289 }
290
291 static PyObject *
292 fire_event_branch_right(PyObject *self, PyObject *args)
293 {
294 PyObject *codelike;
295 int offset;
296 PyObject *target_offset;
297 if (!PyArg_ParseTuple(args, "OiO", &codelike, &offset, &target_offset)) {
298 return NULL;
299 }
300 PyObject *exception = NULL;
301 PyMonitoringState *state = setup_fire(codelike, offset, exception);
302 if (state == NULL) {
303 return NULL;
304 }
305 int res = PyMonitoring_FireBranchRightEvent(state, codelike, offset, target_offset);
306 RETURN_INT(teardown_fire(res, state, exception));
307 }
308
309 static PyObject *
310 fire_event_branch_left(PyObject *self, PyObject *args)
311 {
312 PyObject *codelike;
313 int offset;
314 PyObject *target_offset;
315 if (!PyArg_ParseTuple(args, "OiO", &codelike, &offset, &target_offset)) {
316 return NULL;
317 }
318 PyObject *exception = NULL;
319 PyMonitoringState *state = setup_fire(codelike, offset, exception);
320 if (state == NULL) {
321 return NULL;
322 }
323 int res = PyMonitoring_FireBranchLeftEvent(state, codelike, offset, target_offset);
324 RETURN_INT(teardown_fire(res, state, exception));
325 }
326
327 static PyObject *
328 fire_event_py_throw(PyObject *self, PyObject *args)
329 {
330 PyObject *codelike;
331 int offset;
332 PyObject *exception;
333 if (!PyArg_ParseTuple(args, "OiO", &codelike, &offset, &exception)) {
334 return NULL;
335 }
336 NULLABLE(exception);
337 PyMonitoringState *state = setup_fire(codelike, offset, exception);
338 if (state == NULL) {
339 return NULL;
340 }
341 int res = PyMonitoring_FirePyThrowEvent(state, codelike, offset);
342 RETURN_INT(teardown_fire(res, state, exception));
343 }
344
345 static PyObject *
346 fire_event_raise(PyObject *self, PyObject *args)
347 {
348 PyObject *codelike;
349 int offset;
350 PyObject *exception;
351 if (!PyArg_ParseTuple(args, "OiO", &codelike, &offset, &exception)) {
352 return NULL;
353 }
354 NULLABLE(exception);
355 PyMonitoringState *state = setup_fire(codelike, offset, exception);
356 if (state == NULL) {
357 return NULL;
358 }
359 int res = PyMonitoring_FireRaiseEvent(state, codelike, offset);
360 RETURN_INT(teardown_fire(res, state, exception));
361 }
362
363 static PyObject *
364 fire_event_c_raise(PyObject *self, PyObject *args)
365 {
366 PyObject *codelike;
367 int offset;
368 PyObject *exception;
369 if (!PyArg_ParseTuple(args, "OiO", &codelike, &offset, &exception)) {
370 return NULL;
371 }
372 NULLABLE(exception);
373 PyMonitoringState *state = setup_fire(codelike, offset, exception);
374 if (state == NULL) {
375 return NULL;
376 }
377 int res = PyMonitoring_FireCRaiseEvent(state, codelike, offset);
378 RETURN_INT(teardown_fire(res, state, exception));
379 }
380
381 static PyObject *
382 fire_event_reraise(PyObject *self, PyObject *args)
383 {
384 PyObject *codelike;
385 int offset;
386 PyObject *exception;
387 if (!PyArg_ParseTuple(args, "OiO", &codelike, &offset, &exception)) {
388 return NULL;
389 }
390 NULLABLE(exception);
391 PyMonitoringState *state = setup_fire(codelike, offset, exception);
392 if (state == NULL) {
393 return NULL;
394 }
395 int res = PyMonitoring_FireReraiseEvent(state, codelike, offset);
396 RETURN_INT(teardown_fire(res, state, exception));
397 }
398
399 static PyObject *
400 fire_event_exception_handled(PyObject *self, PyObject *args)
401 {
402 PyObject *codelike;
403 int offset;
404 PyObject *exception;
405 if (!PyArg_ParseTuple(args, "OiO", &codelike, &offset, &exception)) {
406 return NULL;
407 }
408 NULLABLE(exception);
409 PyMonitoringState *state = setup_fire(codelike, offset, exception);
410 if (state == NULL) {
411 return NULL;
412 }
413 int res = PyMonitoring_FireExceptionHandledEvent(state, codelike, offset);
414 RETURN_INT(teardown_fire(res, state, exception));
415 }
416
417 static PyObject *
418 fire_event_py_unwind(PyObject *self, PyObject *args)
419 {
420 PyObject *codelike;
421 int offset;
422 PyObject *exception;
423 if (!PyArg_ParseTuple(args, "OiO", &codelike, &offset, &exception)) {
424 return NULL;
425 }
426 NULLABLE(exception);
427 PyMonitoringState *state = setup_fire(codelike, offset, exception);
428 if (state == NULL) {
429 return NULL;
430 }
431 int res = PyMonitoring_FirePyUnwindEvent(state, codelike, offset);
432 RETURN_INT(teardown_fire(res, state, exception));
433 }
434
435 static PyObject *
436 fire_event_stop_iteration(PyObject *self, PyObject *args)
437 {
438 PyObject *codelike;
439 int offset;
440 PyObject *value;
441 if (!PyArg_ParseTuple(args, "OiO", &codelike, &offset, &value)) {
442 return NULL;
443 }
444 NULLABLE(value);
445 PyObject *exception = NULL;
446 PyMonitoringState *state = setup_fire(codelike, offset, exception);
447 if (state == NULL) {
448 return NULL;
449 }
450 int res = PyMonitoring_FireStopIterationEvent(state, codelike, offset, value);
451 RETURN_INT(teardown_fire(res, state, exception));
452 }
453
454 /*******************************************************************/
455
456 static PyObject *
457 enter_scope(PyObject *self, PyObject *args)
458 {
459 PyObject *codelike;
460 int event1, event2=0;
461 Py_ssize_t num_events = PyTuple_Size(args) - 1;
462 if (num_events == 1) {
463 if (!PyArg_ParseTuple(args, "Oi", &codelike, &event1)) {
464 return NULL;
465 }
466 }
467 else {
468 assert(num_events == 2);
469 if (!PyArg_ParseTuple(args, "Oii", &codelike, &event1, &event2)) {
470 return NULL;
471 }
472 }
473 RAISE_UNLESS_CODELIKE(codelike);
474 PyCodeLikeObject *cl = (PyCodeLikeObject *) codelike;
475
476 uint8_t events[] = { event1, event2 };
477
478 PyMonitoring_EnterScope(cl->monitoring_states,
479 &cl->version,
480 events,
481 num_events);
482
483 Py_RETURN_NONE;
484 }
485
486 static PyObject *
487 exit_scope(PyObject *self, PyObject *args)
488 {
489 PyMonitoring_ExitScope();
490 Py_RETURN_NONE;
491 }
492
493 static PyMethodDef TestMethods[] = {
494 {"fire_event_py_start", fire_event_py_start, METH_VARARGS},
495 {"fire_event_py_resume", fire_event_py_resume, METH_VARARGS},
496 {"fire_event_py_return", fire_event_py_return, METH_VARARGS},
497 {"fire_event_c_return", fire_event_c_return, METH_VARARGS},
498 {"fire_event_py_yield", fire_event_py_yield, METH_VARARGS},
499 {"fire_event_call", fire_event_call, METH_VARARGS},
500 {"fire_event_line", fire_event_line, METH_VARARGS},
501 {"fire_event_jump", fire_event_jump, METH_VARARGS},
502 {"fire_event_branch_left", fire_event_branch_left, METH_VARARGS},
503 {"fire_event_branch_right", fire_event_branch_right, METH_VARARGS},
504 {"fire_event_py_throw", fire_event_py_throw, METH_VARARGS},
505 {"fire_event_raise", fire_event_raise, METH_VARARGS},
506 {"fire_event_c_raise", fire_event_c_raise, METH_VARARGS},
507 {"fire_event_reraise", fire_event_reraise, METH_VARARGS},
508 {"fire_event_exception_handled", fire_event_exception_handled, METH_VARARGS},
509 {"fire_event_py_unwind", fire_event_py_unwind, METH_VARARGS},
510 {"fire_event_stop_iteration", fire_event_stop_iteration, METH_VARARGS},
511 {"monitoring_enter_scope", enter_scope, METH_VARARGS},
512 {"monitoring_exit_scope", exit_scope, METH_VARARGS},
513 {NULL},
514 };
515
516 int
517 _PyTestCapi_Init_Monitoring(PyObject *m)
518 {
519 if (PyType_Ready(&PyCodeLike_Type) < 0) {
520 return -1;
521 }
522 if (PyModule_AddObjectRef(m, "CodeLike", (PyObject *) &PyCodeLike_Type) < 0) {
523 Py_DECREF(m);
524 return -1;
525 }
526 if (PyModule_AddFunctions(m, TestMethods) < 0) {
527 return -1;
528 }
529 return 0;
530 }