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