]>
Commit | Line | Data |
---|---|---|
1 | /* Python interface to btrace instruction history. | |
2 | ||
3 | Copyright 2016-2025 Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "gdbcore.h" | |
21 | #include "cli/cli-cmds.h" | |
22 | #include "gdbthread.h" | |
23 | #include "btrace.h" | |
24 | #include "py-record.h" | |
25 | #include "py-record-btrace.h" | |
26 | #include "record-btrace.h" | |
27 | #include "disasm.h" | |
28 | #include "gdbarch.h" | |
29 | ||
30 | /* Python object for btrace record lists. */ | |
31 | ||
32 | struct btpy_list_object { | |
33 | PyObject_HEAD | |
34 | ||
35 | /* The thread this list belongs to. */ | |
36 | thread_info *thread; | |
37 | ||
38 | /* The first index being part of this list. */ | |
39 | Py_ssize_t first; | |
40 | ||
41 | /* The last index begin part of this list. */ | |
42 | Py_ssize_t last; | |
43 | ||
44 | /* Stride size. */ | |
45 | Py_ssize_t step; | |
46 | ||
47 | /* Either &recpy_func_type, &recpy_insn_type, &recpy_aux_type or | |
48 | &recpy_gap_type. */ | |
49 | PyTypeObject* element_type; | |
50 | }; | |
51 | ||
52 | /* Python type for btrace lists. */ | |
53 | ||
54 | static PyTypeObject btpy_list_type = { | |
55 | PyVarObject_HEAD_INIT (NULL, 0) | |
56 | }; | |
57 | ||
58 | /* Returns either a btrace_insn for the given Python gdb.RecordInstruction | |
59 | object or sets an appropriate Python exception and returns NULL. */ | |
60 | ||
61 | static const btrace_insn * | |
62 | btrace_insn_from_recpy_insn (const PyObject * const pyobject) | |
63 | { | |
64 | const btrace_insn *insn; | |
65 | const recpy_element_object *obj; | |
66 | thread_info *tinfo; | |
67 | btrace_insn_iterator iter; | |
68 | ||
69 | if (Py_TYPE (pyobject) != &recpy_insn_type) | |
70 | { | |
71 | PyErr_Format (gdbpy_gdb_error, _("Must be gdb.RecordInstruction")); | |
72 | return NULL; | |
73 | } | |
74 | ||
75 | obj = (const recpy_element_object *) pyobject; | |
76 | tinfo = obj->thread; | |
77 | ||
78 | if (tinfo == NULL || btrace_is_empty (tinfo)) | |
79 | { | |
80 | PyErr_Format (gdbpy_gdb_error, _("No such instruction.")); | |
81 | return NULL; | |
82 | } | |
83 | ||
84 | if (btrace_find_insn_by_number (&iter, &tinfo->btrace, obj->number) == 0) | |
85 | { | |
86 | PyErr_Format (gdbpy_gdb_error, _("No such instruction.")); | |
87 | return NULL; | |
88 | } | |
89 | ||
90 | insn = btrace_insn_get (&iter); | |
91 | if (insn == NULL) | |
92 | { | |
93 | PyErr_Format (gdbpy_gdb_error, _("Not a valid instruction.")); | |
94 | return NULL; | |
95 | } | |
96 | ||
97 | return insn; | |
98 | } | |
99 | ||
100 | /* Returns either a btrace_function for the given Python | |
101 | gdb.RecordFunctionSegment object or sets an appropriate Python exception and | |
102 | returns NULL. */ | |
103 | ||
104 | static const btrace_function * | |
105 | btrace_func_from_recpy_func (const PyObject * const pyobject) | |
106 | { | |
107 | const btrace_function *func; | |
108 | const recpy_element_object *obj; | |
109 | thread_info *tinfo; | |
110 | btrace_call_iterator iter; | |
111 | ||
112 | if (Py_TYPE (pyobject) != &recpy_func_type) | |
113 | { | |
114 | PyErr_Format (gdbpy_gdb_error, _("Must be gdb.RecordFunctionSegment")); | |
115 | return NULL; | |
116 | } | |
117 | ||
118 | obj = (const recpy_element_object *) pyobject; | |
119 | tinfo = obj->thread; | |
120 | ||
121 | if (tinfo == NULL || btrace_is_empty (tinfo)) | |
122 | { | |
123 | PyErr_Format (gdbpy_gdb_error, _("No such function segment.")); | |
124 | return NULL; | |
125 | } | |
126 | ||
127 | if (btrace_find_call_by_number (&iter, &tinfo->btrace, obj->number) == 0) | |
128 | { | |
129 | PyErr_Format (gdbpy_gdb_error, _("No such function segment.")); | |
130 | return NULL; | |
131 | } | |
132 | ||
133 | func = btrace_call_get (&iter); | |
134 | if (func == NULL) | |
135 | { | |
136 | PyErr_Format (gdbpy_gdb_error, _("Not a valid function segment.")); | |
137 | return NULL; | |
138 | } | |
139 | ||
140 | return func; | |
141 | } | |
142 | ||
143 | /* Looks at the recorded item with the number NUMBER and create a | |
144 | gdb.RecordInstruction, gdb.RecordGap or gdb.RecordAuxiliary object | |
145 | for it accordingly. */ | |
146 | ||
147 | static PyObject * | |
148 | btpy_item_new (thread_info *tinfo, Py_ssize_t number) | |
149 | { | |
150 | btrace_insn_iterator iter; | |
151 | int err_code; | |
152 | ||
153 | if (btrace_find_insn_by_number (&iter, &tinfo->btrace, number) == 0) | |
154 | { | |
155 | PyErr_Format (gdbpy_gdb_error, _("No such instruction.")); | |
156 | return nullptr; | |
157 | } | |
158 | ||
159 | err_code = btrace_insn_get_error (&iter); | |
160 | ||
161 | if (err_code != 0) | |
162 | { | |
163 | const btrace_config *config; | |
164 | const char *err_string; | |
165 | ||
166 | config = btrace_conf (&tinfo->btrace); | |
167 | err_string = btrace_decode_error (config->format, err_code); | |
168 | ||
169 | return recpy_gap_new (err_code, err_string, number); | |
170 | } | |
171 | ||
172 | const struct btrace_insn *insn = btrace_insn_get (&iter); | |
173 | gdb_assert (insn != nullptr); | |
174 | ||
175 | if (insn->iclass == BTRACE_INSN_AUX) | |
176 | return recpy_aux_new (tinfo, RECORD_METHOD_BTRACE, number); | |
177 | ||
178 | return recpy_insn_new (tinfo, RECORD_METHOD_BTRACE, number); | |
179 | } | |
180 | ||
181 | /* Create a new gdb.BtraceList object. */ | |
182 | ||
183 | static PyObject * | |
184 | btpy_list_new (thread_info *thread, Py_ssize_t first, Py_ssize_t last, | |
185 | Py_ssize_t step, PyTypeObject *element_type) | |
186 | { | |
187 | btpy_list_object * const obj = PyObject_New (btpy_list_object, | |
188 | &btpy_list_type); | |
189 | ||
190 | if (obj == NULL) | |
191 | return NULL; | |
192 | ||
193 | obj->thread = thread; | |
194 | obj->first = first; | |
195 | obj->last = last; | |
196 | obj->step = step; | |
197 | obj->element_type = element_type; | |
198 | ||
199 | return (PyObject *) obj; | |
200 | } | |
201 | ||
202 | /* Implementation of RecordInstruction.sal [gdb.Symtab_and_line] for btrace. | |
203 | Returns the SAL associated with this instruction. */ | |
204 | ||
205 | PyObject * | |
206 | recpy_bt_insn_sal (PyObject *self, void *closure) | |
207 | { | |
208 | const btrace_insn * const insn = btrace_insn_from_recpy_insn (self); | |
209 | PyObject *result = NULL; | |
210 | ||
211 | if (insn == NULL) | |
212 | return NULL; | |
213 | ||
214 | try | |
215 | { | |
216 | result = symtab_and_line_to_sal_object (find_pc_line (insn->pc, 0)); | |
217 | } | |
218 | catch (const gdb_exception &except) | |
219 | { | |
220 | return gdbpy_handle_gdb_exception (nullptr, except); | |
221 | } | |
222 | ||
223 | return result; | |
224 | } | |
225 | ||
226 | /* Implementation of RecordInstruction.pc [int] for btrace. | |
227 | Returns the instruction address. */ | |
228 | ||
229 | PyObject * | |
230 | recpy_bt_insn_pc (PyObject *self, void *closure) | |
231 | { | |
232 | const btrace_insn * const insn = btrace_insn_from_recpy_insn (self); | |
233 | ||
234 | if (insn == NULL) | |
235 | return NULL; | |
236 | ||
237 | return gdb_py_object_from_ulongest (insn->pc).release (); | |
238 | } | |
239 | ||
240 | /* Implementation of RecordInstruction.size [int] for btrace. | |
241 | Returns the instruction size. */ | |
242 | ||
243 | PyObject * | |
244 | recpy_bt_insn_size (PyObject *self, void *closure) | |
245 | { | |
246 | const btrace_insn * const insn = btrace_insn_from_recpy_insn (self); | |
247 | ||
248 | if (insn == NULL) | |
249 | return NULL; | |
250 | ||
251 | return gdb_py_object_from_longest (insn->size).release (); | |
252 | } | |
253 | ||
254 | /* Implementation of RecordInstruction.is_speculative [bool] for btrace. | |
255 | Returns if this instruction was executed speculatively. */ | |
256 | ||
257 | PyObject * | |
258 | recpy_bt_insn_is_speculative (PyObject *self, void *closure) | |
259 | { | |
260 | const btrace_insn * const insn = btrace_insn_from_recpy_insn (self); | |
261 | ||
262 | if (insn == NULL) | |
263 | return NULL; | |
264 | ||
265 | if (insn->flags & BTRACE_INSN_FLAG_SPECULATIVE) | |
266 | Py_RETURN_TRUE; | |
267 | else | |
268 | Py_RETURN_FALSE; | |
269 | } | |
270 | ||
271 | /* Implementation of RecordInstruction.data [buffer] for btrace. | |
272 | Returns raw instruction data. */ | |
273 | ||
274 | PyObject * | |
275 | recpy_bt_insn_data (PyObject *self, void *closure) | |
276 | { | |
277 | const btrace_insn * const insn = btrace_insn_from_recpy_insn (self); | |
278 | gdb::byte_vector buffer; | |
279 | PyObject *object; | |
280 | ||
281 | if (insn == NULL) | |
282 | return NULL; | |
283 | ||
284 | try | |
285 | { | |
286 | buffer.resize (insn->size); | |
287 | read_memory (insn->pc, buffer.data (), insn->size); | |
288 | } | |
289 | catch (const gdb_exception &except) | |
290 | { | |
291 | return gdbpy_handle_gdb_exception (nullptr, except); | |
292 | } | |
293 | ||
294 | object = PyBytes_FromStringAndSize ((const char *) buffer.data (), | |
295 | insn->size); | |
296 | ||
297 | if (object == NULL) | |
298 | return NULL; | |
299 | ||
300 | return PyMemoryView_FromObject (object); | |
301 | } | |
302 | ||
303 | /* Implementation of RecordInstruction.decoded [str] for btrace. | |
304 | Returns the instruction as human readable string. */ | |
305 | ||
306 | PyObject * | |
307 | recpy_bt_insn_decoded (PyObject *self, void *closure) | |
308 | { | |
309 | const btrace_insn * const insn = btrace_insn_from_recpy_insn (self); | |
310 | string_file strfile; | |
311 | ||
312 | if (insn == NULL) | |
313 | return NULL; | |
314 | ||
315 | try | |
316 | { | |
317 | gdb_print_insn (current_inferior ()->arch (), insn->pc, &strfile, NULL); | |
318 | } | |
319 | catch (const gdb_exception &except) | |
320 | { | |
321 | return gdbpy_handle_gdb_exception (nullptr, except); | |
322 | } | |
323 | ||
324 | return PyBytes_FromString (strfile.string ().c_str ()); | |
325 | } | |
326 | ||
327 | /* Implementation of RecordFunctionSegment.level [int] for btrace. | |
328 | Returns the call level. */ | |
329 | ||
330 | PyObject * | |
331 | recpy_bt_func_level (PyObject *self, void *closure) | |
332 | { | |
333 | const btrace_function * const func = btrace_func_from_recpy_func (self); | |
334 | thread_info *tinfo; | |
335 | ||
336 | if (func == NULL) | |
337 | return NULL; | |
338 | ||
339 | tinfo = ((recpy_element_object *) self)->thread; | |
340 | return gdb_py_object_from_longest (tinfo->btrace.level | |
341 | + func->level).release (); | |
342 | } | |
343 | ||
344 | /* Implementation of RecordFunctionSegment.symbol [gdb.Symbol] for btrace. | |
345 | Returns the symbol associated with this function call. */ | |
346 | ||
347 | PyObject * | |
348 | recpy_bt_func_symbol (PyObject *self, void *closure) | |
349 | { | |
350 | const btrace_function * const func = btrace_func_from_recpy_func (self); | |
351 | ||
352 | if (func == NULL) | |
353 | return NULL; | |
354 | ||
355 | if (func->sym == NULL) | |
356 | Py_RETURN_NONE; | |
357 | ||
358 | return symbol_to_symbol_object (func->sym); | |
359 | } | |
360 | ||
361 | /* Implementation of RecordFunctionSegment.instructions [list] for btrace. | |
362 | Returns the list of instructions that belong to this function call. */ | |
363 | ||
364 | PyObject * | |
365 | recpy_bt_func_instructions (PyObject *self, void *closure) | |
366 | { | |
367 | const btrace_function * const func = btrace_func_from_recpy_func (self); | |
368 | unsigned int len; | |
369 | ||
370 | if (func == NULL) | |
371 | return NULL; | |
372 | ||
373 | len = func->insn.size (); | |
374 | ||
375 | /* Gaps count as one instruction. */ | |
376 | if (len == 0) | |
377 | len = 1; | |
378 | ||
379 | return btpy_list_new (((recpy_element_object *) self)->thread, | |
380 | func->insn_offset, func->insn_offset + len, 1, | |
381 | &recpy_insn_type); | |
382 | } | |
383 | ||
384 | /* Implementation of RecordFunctionSegment.up [RecordFunctionSegment] for | |
385 | btrace. Returns the caller / returnee of this function. */ | |
386 | ||
387 | PyObject * | |
388 | recpy_bt_func_up (PyObject *self, void *closure) | |
389 | { | |
390 | const btrace_function * const func = btrace_func_from_recpy_func (self); | |
391 | ||
392 | if (func == NULL) | |
393 | return NULL; | |
394 | ||
395 | if (func->up == 0) | |
396 | Py_RETURN_NONE; | |
397 | ||
398 | return recpy_func_new (((recpy_element_object *) self)->thread, | |
399 | RECORD_METHOD_BTRACE, func->up); | |
400 | } | |
401 | ||
402 | /* Implementation of RecordFunctionSegment.prev [RecordFunctionSegment] for | |
403 | btrace. Returns a previous segment of this function. */ | |
404 | ||
405 | PyObject * | |
406 | recpy_bt_func_prev (PyObject *self, void *closure) | |
407 | { | |
408 | const btrace_function * const func = btrace_func_from_recpy_func (self); | |
409 | ||
410 | if (func == NULL) | |
411 | return NULL; | |
412 | ||
413 | if (func->prev == 0) | |
414 | Py_RETURN_NONE; | |
415 | ||
416 | return recpy_func_new (((recpy_element_object *) self)->thread, | |
417 | RECORD_METHOD_BTRACE, func->prev); | |
418 | } | |
419 | ||
420 | /* Implementation of RecordFunctionSegment.next [RecordFunctionSegment] for | |
421 | btrace. Returns a following segment of this function. */ | |
422 | ||
423 | PyObject * | |
424 | recpy_bt_func_next (PyObject *self, void *closure) | |
425 | { | |
426 | const btrace_function * const func = btrace_func_from_recpy_func (self); | |
427 | ||
428 | if (func == NULL) | |
429 | return NULL; | |
430 | ||
431 | if (func->next == 0) | |
432 | Py_RETURN_NONE; | |
433 | ||
434 | return recpy_func_new (((recpy_element_object *) self)->thread, | |
435 | RECORD_METHOD_BTRACE, func->next); | |
436 | } | |
437 | ||
438 | /* Implementation of Auxiliary.data [str] for btrace. */ | |
439 | ||
440 | PyObject * | |
441 | recpy_bt_aux_data (PyObject *self, void *closure) | |
442 | { | |
443 | const btrace_insn *insn; | |
444 | const recpy_element_object *obj; | |
445 | thread_info *tinfo; | |
446 | btrace_insn_iterator iter; | |
447 | ||
448 | if (Py_TYPE (self) != &recpy_aux_type) | |
449 | { | |
450 | PyErr_Format (gdbpy_gdb_error, _("Must be a gdb.Auxiliary.")); | |
451 | return nullptr; | |
452 | } | |
453 | ||
454 | obj = (const recpy_element_object *) self; | |
455 | tinfo = obj->thread; | |
456 | ||
457 | if (tinfo == nullptr || btrace_is_empty (tinfo)) | |
458 | { | |
459 | PyErr_Format (gdbpy_gdb_error, _("No such auxiliary object.")); | |
460 | return nullptr; | |
461 | } | |
462 | ||
463 | if (btrace_find_insn_by_number (&iter, &tinfo->btrace, obj->number) == 0) | |
464 | { | |
465 | PyErr_Format (gdbpy_gdb_error, _("No such auxiliary object.")); | |
466 | return nullptr; | |
467 | } | |
468 | ||
469 | insn = btrace_insn_get (&iter); | |
470 | if (insn == nullptr || insn->iclass != BTRACE_INSN_AUX) | |
471 | { | |
472 | PyErr_Format (gdbpy_gdb_error, _("Not a valid auxiliary object.")); | |
473 | return nullptr; | |
474 | } | |
475 | ||
476 | return PyUnicode_FromString | |
477 | (iter.btinfo->aux_data.at (insn->aux_data_index).c_str ()); | |
478 | } | |
479 | ||
480 | /* Implementation of BtraceList.__len__ (self) -> int. */ | |
481 | ||
482 | static Py_ssize_t | |
483 | btpy_list_length (PyObject *self) | |
484 | { | |
485 | const btpy_list_object * const obj = (btpy_list_object *) self; | |
486 | const Py_ssize_t distance = obj->last - obj->first; | |
487 | const Py_ssize_t result = distance / obj->step; | |
488 | ||
489 | if ((distance % obj->step) == 0) | |
490 | return result; | |
491 | ||
492 | return result + 1; | |
493 | } | |
494 | ||
495 | /* Implementation of | |
496 | BtraceList.__getitem__ (self, key) -> BtraceInstruction, | |
497 | BtraceList.__getitem__ (self, key) -> BtraceFunctionCall, | |
498 | BtraceList.__getitem__ (self, key) -> BtraceAuxiliary. */ | |
499 | ||
500 | static PyObject * | |
501 | btpy_list_item (PyObject *self, Py_ssize_t index) | |
502 | { | |
503 | const btpy_list_object * const obj = (btpy_list_object *) self; | |
504 | Py_ssize_t number; | |
505 | ||
506 | if (index < 0 || index >= btpy_list_length (self)) | |
507 | return PyErr_Format (PyExc_IndexError, _("Index out of range: %zd."), | |
508 | index); | |
509 | ||
510 | number = obj->first + (obj->step * index); | |
511 | ||
512 | if (obj->element_type == &recpy_func_type) | |
513 | return recpy_func_new (obj->thread, RECORD_METHOD_BTRACE, number); | |
514 | else if (obj->element_type == &recpy_insn_type | |
515 | || obj->element_type == &recpy_aux_type) | |
516 | return btpy_item_new (obj->thread, number); | |
517 | else | |
518 | return PyErr_Format (gdbpy_gdb_error, _("Not a valid BtraceList object.")); | |
519 | } | |
520 | ||
521 | /* Implementation of BtraceList.__getitem__ (self, slice) -> BtraceList. */ | |
522 | ||
523 | static PyObject * | |
524 | btpy_list_slice (PyObject *self, PyObject *value) | |
525 | { | |
526 | const btpy_list_object * const obj = (btpy_list_object *) self; | |
527 | const Py_ssize_t length = btpy_list_length (self); | |
528 | Py_ssize_t start, stop, step, slicelength; | |
529 | ||
530 | if (PyLong_Check (value)) | |
531 | { | |
532 | Py_ssize_t index = PyLong_AsSsize_t (value); | |
533 | ||
534 | /* Emulate Python behavior for negative indices. */ | |
535 | if (index < 0) | |
536 | index += length; | |
537 | ||
538 | return btpy_list_item (self, index); | |
539 | } | |
540 | ||
541 | if (!PySlice_Check (value)) | |
542 | return PyErr_Format (PyExc_TypeError, _("Index must be int or slice.")); | |
543 | ||
544 | if (0 != PySlice_GetIndicesEx (value, length, &start, &stop, | |
545 | &step, &slicelength)) | |
546 | return NULL; | |
547 | ||
548 | return btpy_list_new (obj->thread, obj->first + obj->step * start, | |
549 | obj->first + obj->step * stop, obj->step * step, | |
550 | obj->element_type); | |
551 | } | |
552 | ||
553 | /* Helper function that returns the position of an element in a BtraceList | |
554 | or -1 if the element is not in the list. */ | |
555 | ||
556 | static LONGEST | |
557 | btpy_list_position (PyObject *self, PyObject *value) | |
558 | { | |
559 | const btpy_list_object * const list_obj = (btpy_list_object *) self; | |
560 | const recpy_element_object * const obj = (const recpy_element_object *) value; | |
561 | Py_ssize_t index = obj->number; | |
562 | ||
563 | if (list_obj->element_type != Py_TYPE (value)) | |
564 | return -1; | |
565 | ||
566 | if (list_obj->thread != obj->thread) | |
567 | return -1; | |
568 | ||
569 | if (index < list_obj->first || index > list_obj->last) | |
570 | return -1; | |
571 | ||
572 | index -= list_obj->first; | |
573 | ||
574 | if (index % list_obj->step != 0) | |
575 | return -1; | |
576 | ||
577 | return index / list_obj->step; | |
578 | } | |
579 | ||
580 | /* Implementation of "in" operator for BtraceLists. */ | |
581 | ||
582 | static int | |
583 | btpy_list_contains (PyObject *self, PyObject *value) | |
584 | { | |
585 | if (btpy_list_position (self, value) < 0) | |
586 | return 0; | |
587 | ||
588 | return 1; | |
589 | } | |
590 | ||
591 | /* Implementation of BtraceLists.index (self, value) -> int. */ | |
592 | ||
593 | static PyObject * | |
594 | btpy_list_index (PyObject *self, PyObject *value) | |
595 | { | |
596 | const LONGEST index = btpy_list_position (self, value); | |
597 | ||
598 | if (index < 0) | |
599 | return PyErr_Format (PyExc_ValueError, _("Not in list.")); | |
600 | ||
601 | return gdb_py_object_from_longest (index).release (); | |
602 | } | |
603 | ||
604 | /* Implementation of BtraceList.count (self, value) -> int. */ | |
605 | ||
606 | static PyObject * | |
607 | btpy_list_count (PyObject *self, PyObject *value) | |
608 | { | |
609 | /* We know that if an element is in the list, it is so exactly one time, | |
610 | enabling us to reuse the "is element of" check. */ | |
611 | return gdb_py_object_from_longest (btpy_list_contains (self, | |
612 | value)).release (); | |
613 | } | |
614 | ||
615 | /* Python rich compare function to allow for equality and inequality checks | |
616 | in Python. */ | |
617 | ||
618 | static PyObject * | |
619 | btpy_list_richcompare (PyObject *self, PyObject *other, int op) | |
620 | { | |
621 | const btpy_list_object * const obj1 = (btpy_list_object *) self; | |
622 | const btpy_list_object * const obj2 = (btpy_list_object *) other; | |
623 | ||
624 | if (Py_TYPE (self) != Py_TYPE (other)) | |
625 | { | |
626 | Py_INCREF (Py_NotImplemented); | |
627 | return Py_NotImplemented; | |
628 | } | |
629 | ||
630 | switch (op) | |
631 | { | |
632 | case Py_EQ: | |
633 | if (obj1->thread == obj2->thread | |
634 | && obj1->element_type == obj2->element_type | |
635 | && obj1->first == obj2->first | |
636 | && obj1->last == obj2->last | |
637 | && obj1->step == obj2->step) | |
638 | Py_RETURN_TRUE; | |
639 | else | |
640 | Py_RETURN_FALSE; | |
641 | ||
642 | case Py_NE: | |
643 | if (obj1->thread != obj2->thread | |
644 | || obj1->element_type != obj2->element_type | |
645 | || obj1->first != obj2->first | |
646 | || obj1->last != obj2->last | |
647 | || obj1->step != obj2->step) | |
648 | Py_RETURN_TRUE; | |
649 | else | |
650 | Py_RETURN_FALSE; | |
651 | ||
652 | default: | |
653 | break; | |
654 | } | |
655 | ||
656 | Py_INCREF (Py_NotImplemented); | |
657 | return Py_NotImplemented; | |
658 | } | |
659 | ||
660 | /* Implementation of | |
661 | BtraceRecord.method [str]. */ | |
662 | ||
663 | PyObject * | |
664 | recpy_bt_method (PyObject *self, void *closure) | |
665 | { | |
666 | return PyUnicode_FromString ("btrace"); | |
667 | } | |
668 | ||
669 | /* Implementation of | |
670 | BtraceRecord.format [str]. */ | |
671 | ||
672 | PyObject * | |
673 | recpy_bt_format (PyObject *self, void *closure) | |
674 | { | |
675 | const recpy_record_object * const record = (recpy_record_object *) self; | |
676 | const struct thread_info * const tinfo = record->thread; | |
677 | const struct btrace_config * config; | |
678 | ||
679 | if (tinfo == NULL) | |
680 | Py_RETURN_NONE; | |
681 | ||
682 | config = btrace_conf (&tinfo->btrace); | |
683 | ||
684 | if (config == NULL) | |
685 | Py_RETURN_NONE; | |
686 | ||
687 | return PyUnicode_FromString (btrace_format_short_string (config->format)); | |
688 | } | |
689 | ||
690 | /* Implementation of | |
691 | BtraceRecord.replay_position [BtraceInstruction]. */ | |
692 | ||
693 | PyObject * | |
694 | recpy_bt_replay_position (PyObject *self, void *closure) | |
695 | { | |
696 | const recpy_record_object * const record = (recpy_record_object *) self; | |
697 | thread_info * tinfo = record->thread; | |
698 | ||
699 | if (tinfo == NULL) | |
700 | Py_RETURN_NONE; | |
701 | ||
702 | if (tinfo->btrace.replay == NULL) | |
703 | Py_RETURN_NONE; | |
704 | ||
705 | return btpy_item_new (tinfo, btrace_insn_number (tinfo->btrace.replay)); | |
706 | } | |
707 | ||
708 | /* Implementation of | |
709 | BtraceRecord.begin [BtraceInstruction]. */ | |
710 | ||
711 | PyObject * | |
712 | recpy_bt_begin (PyObject *self, void *closure) | |
713 | { | |
714 | const recpy_record_object * const record = (recpy_record_object *) self; | |
715 | thread_info *const tinfo = record->thread; | |
716 | struct btrace_insn_iterator iterator; | |
717 | ||
718 | if (tinfo == NULL) | |
719 | Py_RETURN_NONE; | |
720 | ||
721 | btrace_fetch (tinfo, record_btrace_get_cpu ()); | |
722 | ||
723 | if (btrace_is_empty (tinfo)) | |
724 | Py_RETURN_NONE; | |
725 | ||
726 | btrace_insn_begin (&iterator, &tinfo->btrace); | |
727 | return btpy_item_new (tinfo, btrace_insn_number (&iterator)); | |
728 | } | |
729 | ||
730 | /* Implementation of | |
731 | BtraceRecord.end [BtraceInstruction]. */ | |
732 | ||
733 | PyObject * | |
734 | recpy_bt_end (PyObject *self, void *closure) | |
735 | { | |
736 | const recpy_record_object * const record = (recpy_record_object *) self; | |
737 | thread_info *const tinfo = record->thread; | |
738 | struct btrace_insn_iterator iterator; | |
739 | ||
740 | if (tinfo == NULL) | |
741 | Py_RETURN_NONE; | |
742 | ||
743 | btrace_fetch (tinfo, record_btrace_get_cpu ()); | |
744 | ||
745 | if (btrace_is_empty (tinfo)) | |
746 | Py_RETURN_NONE; | |
747 | ||
748 | btrace_insn_end (&iterator, &tinfo->btrace); | |
749 | return btpy_item_new (tinfo, btrace_insn_number (&iterator)); | |
750 | } | |
751 | ||
752 | /* Implementation of | |
753 | BtraceRecord.instruction_history [list]. */ | |
754 | ||
755 | PyObject * | |
756 | recpy_bt_instruction_history (PyObject *self, void *closure) | |
757 | { | |
758 | const recpy_record_object * const record = (recpy_record_object *) self; | |
759 | thread_info *const tinfo = record->thread; | |
760 | struct btrace_insn_iterator iterator; | |
761 | unsigned long first = 0; | |
762 | unsigned long last = 0; | |
763 | ||
764 | if (tinfo == NULL) | |
765 | Py_RETURN_NONE; | |
766 | ||
767 | btrace_fetch (tinfo, record_btrace_get_cpu ()); | |
768 | ||
769 | if (btrace_is_empty (tinfo)) | |
770 | Py_RETURN_NONE; | |
771 | ||
772 | btrace_insn_begin (&iterator, &tinfo->btrace); | |
773 | first = btrace_insn_number (&iterator); | |
774 | ||
775 | btrace_insn_end (&iterator, &tinfo->btrace); | |
776 | last = btrace_insn_number (&iterator); | |
777 | ||
778 | return btpy_list_new (tinfo, first, last, 1, &recpy_insn_type); | |
779 | } | |
780 | ||
781 | /* Implementation of | |
782 | BtraceRecord.function_call_history [list]. */ | |
783 | ||
784 | PyObject * | |
785 | recpy_bt_function_call_history (PyObject *self, void *closure) | |
786 | { | |
787 | const recpy_record_object * const record = (recpy_record_object *) self; | |
788 | thread_info *const tinfo = record->thread; | |
789 | struct btrace_call_iterator iterator; | |
790 | unsigned long first = 0; | |
791 | unsigned long last = 0; | |
792 | ||
793 | if (tinfo == NULL) | |
794 | Py_RETURN_NONE; | |
795 | ||
796 | btrace_fetch (tinfo, record_btrace_get_cpu ()); | |
797 | ||
798 | if (btrace_is_empty (tinfo)) | |
799 | Py_RETURN_NONE; | |
800 | ||
801 | btrace_call_begin (&iterator, &tinfo->btrace); | |
802 | first = btrace_call_number (&iterator); | |
803 | ||
804 | btrace_call_end (&iterator, &tinfo->btrace); | |
805 | last = btrace_call_number (&iterator); | |
806 | ||
807 | return btpy_list_new (tinfo, first, last, 1, &recpy_func_type); | |
808 | } | |
809 | ||
810 | /* Helper function that calls PTW_FILTER with PAYLOAD and IP as arguments. | |
811 | Returns the string that will be printed, if there is a filter to call. */ | |
812 | static std::optional<std::string> | |
813 | recpy_call_filter (const uint64_t payload, std::optional<uint64_t> ip, | |
814 | const void *ptw_filter) | |
815 | { | |
816 | std::optional<std::string> result; | |
817 | ||
818 | gdb_assert (ptw_filter != nullptr); | |
819 | if ((PyObject *) ptw_filter == Py_None) | |
820 | return result; | |
821 | ||
822 | gdbpy_enter enter_py; | |
823 | ||
824 | gdbpy_ref<> py_payload = gdb_py_object_from_ulongest (payload); | |
825 | ||
826 | gdbpy_ref<> py_ip; | |
827 | if (!ip.has_value ()) | |
828 | py_ip = gdbpy_ref<>::new_reference (Py_None); | |
829 | else | |
830 | py_ip = gdb_py_object_from_ulongest (*ip); | |
831 | ||
832 | gdbpy_ref<> py_result (PyObject_CallFunctionObjArgs ((PyObject *) ptw_filter, | |
833 | py_payload.get (), | |
834 | py_ip.get (), | |
835 | nullptr)); | |
836 | ||
837 | if (py_result == nullptr) | |
838 | { | |
839 | gdbpy_print_stack (); | |
840 | gdbpy_error (_("Couldn't call the ptwrite filter.")); | |
841 | } | |
842 | ||
843 | /* Py_None is valid and results in no output. */ | |
844 | if (py_result == Py_None) | |
845 | { | |
846 | result = ""; | |
847 | return result; | |
848 | } | |
849 | ||
850 | gdb::unique_xmalloc_ptr<char> user_string | |
851 | = gdbpy_obj_to_string (py_result.get ()); | |
852 | ||
853 | if (user_string == nullptr) | |
854 | { | |
855 | gdbpy_print_stack (); | |
856 | gdbpy_error (_("The ptwrite filter didn't return a string.")); | |
857 | } | |
858 | else | |
859 | result = user_string.get (); | |
860 | ||
861 | return result; | |
862 | } | |
863 | ||
864 | /* Helper function returning the current ptwrite filter. */ | |
865 | ||
866 | static PyObject * | |
867 | get_ptwrite_filter () | |
868 | { | |
869 | gdbpy_ref<> module (PyImport_ImportModule ("gdb.ptwrite")); | |
870 | ||
871 | if (PyErr_Occurred ()) | |
872 | { | |
873 | gdbpy_print_stack (); | |
874 | gdbpy_error (_("Couldn't import gdb.ptwrite.")); | |
875 | } | |
876 | ||
877 | /* We need to keep the reference count. */ | |
878 | gdbpy_ref<> ptw_filter (gdbpy_call_method (module.get (), "get_filter")); | |
879 | ||
880 | if (PyErr_Occurred ()) | |
881 | { | |
882 | gdbpy_print_stack (); | |
883 | gdbpy_error (_("Couldn't get the ptwrite filter.")); | |
884 | } | |
885 | ||
886 | return ptw_filter.get(); | |
887 | } | |
888 | ||
889 | /* Used for registering any python ptwrite filter to the current thread. A | |
890 | pointer to this function is stored in the python extension interface. */ | |
891 | ||
892 | void | |
893 | gdbpy_load_ptwrite_filter (const struct extension_language_defn *extlang, | |
894 | struct btrace_thread_info *btinfo) | |
895 | { | |
896 | gdb_assert (btinfo != nullptr); | |
897 | ||
898 | gdbpy_enter enter_py; | |
899 | ||
900 | btinfo->ptw_context = get_ptwrite_filter (); | |
901 | ||
902 | #if defined (HAVE_STRUCT_PT_EVENT_VARIANT_PTWRITE) | |
903 | if (!btinfo->target->conf.pt.ptwrite && btinfo->ptw_context != Py_None) | |
904 | warning (_("The target doesn't support decoding ptwrite events.")); | |
905 | #else | |
906 | if (btinfo->ptw_context != Py_None) | |
907 | warning (_("Libipt doesn't support decoding ptwrite events.")); | |
908 | #endif /* defined (HAVE_STRUCT_PT_EVENT_VARIANT_PTWRITE) */ | |
909 | ||
910 | btinfo->ptw_callback_fun = &recpy_call_filter; | |
911 | } | |
912 | ||
913 | /* Implementation of BtraceRecord.goto (self, BtraceInstruction) -> None. */ | |
914 | ||
915 | PyObject * | |
916 | recpy_bt_goto (PyObject *self, PyObject *args) | |
917 | { | |
918 | const recpy_record_object * const record = (recpy_record_object *) self; | |
919 | thread_info *const tinfo = record->thread; | |
920 | const recpy_element_object *obj; | |
921 | PyObject *parse_obj; | |
922 | ||
923 | if (tinfo == NULL || btrace_is_empty (tinfo)) | |
924 | return PyErr_Format (gdbpy_gdb_error, _("Empty branch trace.")); | |
925 | ||
926 | if (!PyArg_ParseTuple (args, "O", &parse_obj)) | |
927 | return NULL; | |
928 | ||
929 | if (Py_TYPE (parse_obj) != &recpy_insn_type) | |
930 | return PyErr_Format (PyExc_TypeError, _("Argument must be instruction.")); | |
931 | obj = (const recpy_element_object *) parse_obj; | |
932 | ||
933 | try | |
934 | { | |
935 | struct btrace_insn_iterator iter; | |
936 | ||
937 | btrace_insn_end (&iter, &tinfo->btrace); | |
938 | ||
939 | if (btrace_insn_number (&iter) == obj->number) | |
940 | target_goto_record_end (); | |
941 | else | |
942 | target_goto_record (obj->number); | |
943 | } | |
944 | catch (const gdb_exception &except) | |
945 | { | |
946 | return gdbpy_handle_gdb_exception (nullptr, except); | |
947 | } | |
948 | ||
949 | Py_RETURN_NONE; | |
950 | } | |
951 | ||
952 | /* Implementation of BtraceRecord.clear (self) -> None. */ | |
953 | ||
954 | PyObject * | |
955 | recpy_bt_clear (PyObject *self, PyObject *args) | |
956 | { | |
957 | const recpy_record_object * const record = (recpy_record_object *) self; | |
958 | thread_info *const tinfo = record->thread; | |
959 | ||
960 | btrace_clear (tinfo); | |
961 | ||
962 | Py_RETURN_NONE; | |
963 | } | |
964 | ||
965 | /* BtraceList methods. */ | |
966 | ||
967 | static PyMethodDef btpy_list_methods[] = | |
968 | { | |
969 | { "count", btpy_list_count, METH_O, "count number of occurrences"}, | |
970 | { "index", btpy_list_index, METH_O, "index of entry"}, | |
971 | {NULL} | |
972 | }; | |
973 | ||
974 | /* BtraceList sequence methods. */ | |
975 | ||
976 | static PySequenceMethods btpy_list_sequence_methods = | |
977 | { | |
978 | NULL | |
979 | }; | |
980 | ||
981 | /* BtraceList mapping methods. Necessary for slicing. */ | |
982 | ||
983 | static PyMappingMethods btpy_list_mapping_methods = | |
984 | { | |
985 | NULL | |
986 | }; | |
987 | ||
988 | /* Sets up the btrace record API. */ | |
989 | ||
990 | static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION | |
991 | gdbpy_initialize_btrace (void) | |
992 | { | |
993 | btpy_list_type.tp_new = PyType_GenericNew; | |
994 | btpy_list_type.tp_flags = Py_TPFLAGS_DEFAULT; | |
995 | btpy_list_type.tp_basicsize = sizeof (btpy_list_object); | |
996 | btpy_list_type.tp_name = "gdb.BtraceObjectList"; | |
997 | btpy_list_type.tp_doc = "GDB btrace list object"; | |
998 | btpy_list_type.tp_methods = btpy_list_methods; | |
999 | btpy_list_type.tp_as_sequence = &btpy_list_sequence_methods; | |
1000 | btpy_list_type.tp_as_mapping = &btpy_list_mapping_methods; | |
1001 | btpy_list_type.tp_richcompare = btpy_list_richcompare; | |
1002 | ||
1003 | btpy_list_sequence_methods.sq_item = btpy_list_item; | |
1004 | btpy_list_sequence_methods.sq_length = btpy_list_length; | |
1005 | btpy_list_sequence_methods.sq_contains = btpy_list_contains; | |
1006 | ||
1007 | btpy_list_mapping_methods.mp_subscript = btpy_list_slice; | |
1008 | ||
1009 | return gdbpy_type_ready (&btpy_list_type); | |
1010 | } | |
1011 | ||
1012 | GDBPY_INITIALIZE_FILE (gdbpy_initialize_btrace); |