]>
Commit | Line | Data |
---|---|---|
75c0bdf4 TW |
1 | /* Python interface to btrace instruction history. |
2 | ||
1d506c26 | 3 | Copyright 2016-2024 Free Software Foundation, Inc. |
75c0bdf4 TW |
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 | 26 | #include "py-record-btrace.h" |
4a4495d6 | 27 | #include "record-btrace.h" |
75c0bdf4 | 28 | #include "disasm.h" |
0d12e84c | 29 | #include "gdbarch.h" |
75c0bdf4 | 30 | |
75c0bdf4 TW |
31 | /* Python object for btrace record lists. */ |
32 | ||
f99b5177 | 33 | struct btpy_list_object { |
75c0bdf4 TW |
34 | PyObject_HEAD |
35 | ||
36 | /* The thread this list belongs to. */ | |
00431a78 | 37 | thread_info *thread; |
75c0bdf4 TW |
38 | |
39 | /* The first index being part of this list. */ | |
40 | Py_ssize_t first; | |
41 | ||
42 | /* The last index begin part of this list. */ | |
43 | Py_ssize_t last; | |
44 | ||
45 | /* Stride size. */ | |
46 | Py_ssize_t step; | |
47 | ||
0ed5da75 | 48 | /* Either &BTPY_CALL_TYPE or &RECPY_INSN_TYPE. */ |
75c0bdf4 | 49 | PyTypeObject* element_type; |
f99b5177 | 50 | }; |
75c0bdf4 | 51 | |
75c0bdf4 TW |
52 | /* Python type for btrace lists. */ |
53 | ||
54 | static PyTypeObject btpy_list_type = { | |
55 | PyVarObject_HEAD_INIT (NULL, 0) | |
56 | }; | |
57 | ||
0ed5da75 TW |
58 | /* Returns either a btrace_insn for the given Python gdb.RecordInstruction |
59 | object or sets an appropriate Python exception and returns NULL. */ | |
75c0bdf4 | 60 | |
0ed5da75 TW |
61 | static const btrace_insn * |
62 | btrace_insn_from_recpy_insn (const PyObject * const pyobject) | |
75c0bdf4 | 63 | { |
0ed5da75 TW |
64 | const btrace_insn *insn; |
65 | const recpy_element_object *obj; | |
66 | thread_info *tinfo; | |
67 | btrace_insn_iterator iter; | |
75c0bdf4 | 68 | |
0ed5da75 TW |
69 | if (Py_TYPE (pyobject) != &recpy_insn_type) |
70 | { | |
71 | PyErr_Format (gdbpy_gdb_error, _("Must be gdb.RecordInstruction")); | |
72 | return NULL; | |
73 | } | |
75c0bdf4 | 74 | |
0ed5da75 | 75 | obj = (const recpy_element_object *) pyobject; |
00431a78 | 76 | tinfo = obj->thread; |
75c0bdf4 | 77 | |
0ed5da75 TW |
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; | |
75c0bdf4 TW |
98 | } |
99 | ||
14f819c8 TW |
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; | |
00431a78 | 119 | tinfo = obj->thread; |
14f819c8 TW |
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 | ||
913aeadd | 143 | /* Looks at the recorded item with the number NUMBER and create a |
0ed5da75 | 144 | gdb.RecordInstruction or gdb.RecordGap object for it accordingly. */ |
75c0bdf4 TW |
145 | |
146 | static PyObject * | |
00431a78 | 147 | btpy_insn_or_gap_new (thread_info *tinfo, Py_ssize_t number) |
75c0bdf4 | 148 | { |
913aeadd TW |
149 | btrace_insn_iterator iter; |
150 | int err_code; | |
151 | ||
152 | btrace_find_insn_by_number (&iter, &tinfo->btrace, number); | |
153 | err_code = btrace_insn_get_error (&iter); | |
154 | ||
155 | if (err_code != 0) | |
156 | { | |
157 | const btrace_config *config; | |
158 | const char *err_string; | |
159 | ||
160 | config = btrace_conf (&tinfo->btrace); | |
161 | err_string = btrace_decode_error (config->format, err_code); | |
162 | ||
163 | return recpy_gap_new (err_code, err_string, number); | |
164 | } | |
165 | ||
00431a78 | 166 | return recpy_insn_new (tinfo, RECORD_METHOD_BTRACE, number); |
75c0bdf4 TW |
167 | } |
168 | ||
75c0bdf4 TW |
169 | /* Create a new gdb.BtraceList object. */ |
170 | ||
171 | static PyObject * | |
e9683acf FW |
172 | btpy_list_new (thread_info *thread, Py_ssize_t first, Py_ssize_t last, |
173 | Py_ssize_t step, PyTypeObject *element_type) | |
75c0bdf4 TW |
174 | { |
175 | btpy_list_object * const obj = PyObject_New (btpy_list_object, | |
176 | &btpy_list_type); | |
177 | ||
178 | if (obj == NULL) | |
179 | return NULL; | |
180 | ||
00431a78 | 181 | obj->thread = thread; |
75c0bdf4 TW |
182 | obj->first = first; |
183 | obj->last = last; | |
184 | obj->step = step; | |
185 | obj->element_type = element_type; | |
186 | ||
187 | return (PyObject *) obj; | |
188 | } | |
189 | ||
0ed5da75 TW |
190 | /* Implementation of RecordInstruction.sal [gdb.Symtab_and_line] for btrace. |
191 | Returns the SAL associated with this instruction. */ | |
75c0bdf4 | 192 | |
0ed5da75 TW |
193 | PyObject * |
194 | recpy_bt_insn_sal (PyObject *self, void *closure) | |
75c0bdf4 | 195 | { |
0ed5da75 | 196 | const btrace_insn * const insn = btrace_insn_from_recpy_insn (self); |
75c0bdf4 TW |
197 | PyObject *result = NULL; |
198 | ||
75c0bdf4 | 199 | if (insn == NULL) |
0ed5da75 | 200 | return NULL; |
75c0bdf4 | 201 | |
a70b8144 | 202 | try |
75c0bdf4 TW |
203 | { |
204 | result = symtab_and_line_to_sal_object (find_pc_line (insn->pc, 0)); | |
205 | } | |
230d2906 | 206 | catch (const gdb_exception &except) |
75c0bdf4 TW |
207 | { |
208 | GDB_PY_HANDLE_EXCEPTION (except); | |
209 | } | |
75c0bdf4 TW |
210 | |
211 | return result; | |
212 | } | |
213 | ||
0ed5da75 TW |
214 | /* Implementation of RecordInstruction.pc [int] for btrace. |
215 | Returns the instruction address. */ | |
75c0bdf4 | 216 | |
0ed5da75 TW |
217 | PyObject * |
218 | recpy_bt_insn_pc (PyObject *self, void *closure) | |
75c0bdf4 | 219 | { |
0ed5da75 | 220 | const btrace_insn * const insn = btrace_insn_from_recpy_insn (self); |
75c0bdf4 | 221 | |
75c0bdf4 | 222 | if (insn == NULL) |
0ed5da75 | 223 | return NULL; |
75c0bdf4 | 224 | |
d1cab987 | 225 | return gdb_py_object_from_ulongest (insn->pc).release (); |
75c0bdf4 TW |
226 | } |
227 | ||
0ed5da75 TW |
228 | /* Implementation of RecordInstruction.size [int] for btrace. |
229 | Returns the instruction size. */ | |
75c0bdf4 | 230 | |
0ed5da75 TW |
231 | PyObject * |
232 | recpy_bt_insn_size (PyObject *self, void *closure) | |
75c0bdf4 | 233 | { |
0ed5da75 | 234 | const btrace_insn * const insn = btrace_insn_from_recpy_insn (self); |
75c0bdf4 | 235 | |
75c0bdf4 | 236 | if (insn == NULL) |
0ed5da75 | 237 | return NULL; |
75c0bdf4 | 238 | |
47f0e2ff | 239 | return gdb_py_object_from_longest (insn->size).release (); |
75c0bdf4 TW |
240 | } |
241 | ||
0ed5da75 | 242 | /* Implementation of RecordInstruction.is_speculative [bool] for btrace. |
75c0bdf4 TW |
243 | Returns if this instruction was executed speculatively. */ |
244 | ||
0ed5da75 TW |
245 | PyObject * |
246 | recpy_bt_insn_is_speculative (PyObject *self, void *closure) | |
75c0bdf4 | 247 | { |
0ed5da75 | 248 | const btrace_insn * const insn = btrace_insn_from_recpy_insn (self); |
75c0bdf4 | 249 | |
75c0bdf4 | 250 | if (insn == NULL) |
0ed5da75 | 251 | return NULL; |
75c0bdf4 TW |
252 | |
253 | if (insn->flags & BTRACE_INSN_FLAG_SPECULATIVE) | |
254 | Py_RETURN_TRUE; | |
255 | else | |
256 | Py_RETURN_FALSE; | |
257 | } | |
258 | ||
0ed5da75 | 259 | /* Implementation of RecordInstruction.data [buffer] for btrace. |
75c0bdf4 TW |
260 | Returns raw instruction data. */ |
261 | ||
0ed5da75 TW |
262 | PyObject * |
263 | recpy_bt_insn_data (PyObject *self, void *closure) | |
75c0bdf4 | 264 | { |
0ed5da75 | 265 | const btrace_insn * const insn = btrace_insn_from_recpy_insn (self); |
075c55e0 | 266 | gdb::byte_vector buffer; |
75c0bdf4 TW |
267 | PyObject *object; |
268 | ||
75c0bdf4 | 269 | if (insn == NULL) |
0ed5da75 | 270 | return NULL; |
75c0bdf4 | 271 | |
a70b8144 | 272 | try |
75c0bdf4 | 273 | { |
075c55e0 TT |
274 | buffer.resize (insn->size); |
275 | read_memory (insn->pc, buffer.data (), insn->size); | |
75c0bdf4 | 276 | } |
230d2906 | 277 | catch (const gdb_exception &except) |
75c0bdf4 | 278 | { |
75c0bdf4 TW |
279 | GDB_PY_HANDLE_EXCEPTION (except); |
280 | } | |
75c0bdf4 | 281 | |
075c55e0 TT |
282 | object = PyBytes_FromStringAndSize ((const char *) buffer.data (), |
283 | insn->size); | |
75c0bdf4 TW |
284 | |
285 | if (object == NULL) | |
286 | return NULL; | |
287 | ||
288 | return PyMemoryView_FromObject (object); | |
289 | } | |
290 | ||
0ed5da75 TW |
291 | /* Implementation of RecordInstruction.decoded [str] for btrace. |
292 | Returns the instruction as human readable string. */ | |
75c0bdf4 | 293 | |
0ed5da75 TW |
294 | PyObject * |
295 | recpy_bt_insn_decoded (PyObject *self, void *closure) | |
75c0bdf4 | 296 | { |
0ed5da75 | 297 | const btrace_insn * const insn = btrace_insn_from_recpy_insn (self); |
75c0bdf4 TW |
298 | string_file strfile; |
299 | ||
75c0bdf4 | 300 | if (insn == NULL) |
0ed5da75 | 301 | return NULL; |
75c0bdf4 | 302 | |
a70b8144 | 303 | try |
75c0bdf4 | 304 | { |
99d9c3b9 | 305 | gdb_print_insn (current_inferior ()->arch (), insn->pc, &strfile, NULL); |
75c0bdf4 | 306 | } |
230d2906 | 307 | catch (const gdb_exception &except) |
75c0bdf4 TW |
308 | { |
309 | gdbpy_convert_exception (except); | |
310 | return NULL; | |
311 | } | |
75c0bdf4 | 312 | |
75c0bdf4 TW |
313 | return PyBytes_FromString (strfile.string ().c_str ()); |
314 | } | |
315 | ||
14f819c8 TW |
316 | /* Implementation of RecordFunctionSegment.level [int] for btrace. |
317 | Returns the call level. */ | |
75c0bdf4 | 318 | |
14f819c8 TW |
319 | PyObject * |
320 | recpy_bt_func_level (PyObject *self, void *closure) | |
75c0bdf4 | 321 | { |
14f819c8 TW |
322 | const btrace_function * const func = btrace_func_from_recpy_func (self); |
323 | thread_info *tinfo; | |
75c0bdf4 | 324 | |
75c0bdf4 | 325 | if (func == NULL) |
14f819c8 | 326 | return NULL; |
75c0bdf4 | 327 | |
00431a78 | 328 | tinfo = ((recpy_element_object *) self)->thread; |
47f0e2ff TT |
329 | return gdb_py_object_from_longest (tinfo->btrace.level |
330 | + func->level).release (); | |
75c0bdf4 TW |
331 | } |
332 | ||
14f819c8 TW |
333 | /* Implementation of RecordFunctionSegment.symbol [gdb.Symbol] for btrace. |
334 | Returns the symbol associated with this function call. */ | |
75c0bdf4 | 335 | |
14f819c8 TW |
336 | PyObject * |
337 | recpy_bt_func_symbol (PyObject *self, void *closure) | |
75c0bdf4 | 338 | { |
14f819c8 | 339 | const btrace_function * const func = btrace_func_from_recpy_func (self); |
75c0bdf4 | 340 | |
75c0bdf4 | 341 | if (func == NULL) |
14f819c8 | 342 | return NULL; |
75c0bdf4 TW |
343 | |
344 | if (func->sym == NULL) | |
345 | Py_RETURN_NONE; | |
346 | ||
347 | return symbol_to_symbol_object (func->sym); | |
348 | } | |
349 | ||
14f819c8 TW |
350 | /* Implementation of RecordFunctionSegment.instructions [list] for btrace. |
351 | Returns the list of instructions that belong to this function call. */ | |
75c0bdf4 | 352 | |
14f819c8 TW |
353 | PyObject * |
354 | recpy_bt_func_instructions (PyObject *self, void *closure) | |
75c0bdf4 | 355 | { |
14f819c8 | 356 | const btrace_function * const func = btrace_func_from_recpy_func (self); |
75c0bdf4 TW |
357 | unsigned int len; |
358 | ||
75c0bdf4 | 359 | if (func == NULL) |
14f819c8 | 360 | return NULL; |
75c0bdf4 | 361 | |
0860c437 | 362 | len = func->insn.size (); |
75c0bdf4 TW |
363 | |
364 | /* Gaps count as one instruction. */ | |
365 | if (len == 0) | |
366 | len = 1; | |
367 | ||
00431a78 | 368 | return btpy_list_new (((recpy_element_object *) self)->thread, |
14f819c8 TW |
369 | func->insn_offset, func->insn_offset + len, 1, |
370 | &recpy_insn_type); | |
75c0bdf4 TW |
371 | } |
372 | ||
14f819c8 TW |
373 | /* Implementation of RecordFunctionSegment.up [RecordFunctionSegment] for |
374 | btrace. Returns the caller / returnee of this function. */ | |
75c0bdf4 | 375 | |
14f819c8 TW |
376 | PyObject * |
377 | recpy_bt_func_up (PyObject *self, void *closure) | |
75c0bdf4 | 378 | { |
14f819c8 | 379 | const btrace_function * const func = btrace_func_from_recpy_func (self); |
75c0bdf4 | 380 | |
75c0bdf4 | 381 | if (func == NULL) |
14f819c8 | 382 | return NULL; |
75c0bdf4 | 383 | |
42bfe59e | 384 | if (func->up == 0) |
75c0bdf4 TW |
385 | Py_RETURN_NONE; |
386 | ||
00431a78 | 387 | return recpy_func_new (((recpy_element_object *) self)->thread, |
42bfe59e | 388 | RECORD_METHOD_BTRACE, func->up); |
75c0bdf4 TW |
389 | } |
390 | ||
14f819c8 TW |
391 | /* Implementation of RecordFunctionSegment.prev [RecordFunctionSegment] for |
392 | btrace. Returns a previous segment of this function. */ | |
75c0bdf4 | 393 | |
14f819c8 TW |
394 | PyObject * |
395 | recpy_bt_func_prev (PyObject *self, void *closure) | |
75c0bdf4 | 396 | { |
14f819c8 | 397 | const btrace_function * const func = btrace_func_from_recpy_func (self); |
75c0bdf4 | 398 | |
75c0bdf4 | 399 | if (func == NULL) |
14f819c8 | 400 | return NULL; |
75c0bdf4 | 401 | |
4aeb0dfc | 402 | if (func->prev == 0) |
75c0bdf4 TW |
403 | Py_RETURN_NONE; |
404 | ||
00431a78 | 405 | return recpy_func_new (((recpy_element_object *) self)->thread, |
4aeb0dfc | 406 | RECORD_METHOD_BTRACE, func->prev); |
75c0bdf4 TW |
407 | } |
408 | ||
14f819c8 TW |
409 | /* Implementation of RecordFunctionSegment.next [RecordFunctionSegment] for |
410 | btrace. Returns a following segment of this function. */ | |
75c0bdf4 | 411 | |
14f819c8 TW |
412 | PyObject * |
413 | recpy_bt_func_next (PyObject *self, void *closure) | |
75c0bdf4 | 414 | { |
14f819c8 | 415 | const btrace_function * const func = btrace_func_from_recpy_func (self); |
75c0bdf4 | 416 | |
75c0bdf4 | 417 | if (func == NULL) |
14f819c8 | 418 | return NULL; |
75c0bdf4 | 419 | |
4aeb0dfc | 420 | if (func->next == 0) |
75c0bdf4 TW |
421 | Py_RETURN_NONE; |
422 | ||
00431a78 | 423 | return recpy_func_new (((recpy_element_object *) self)->thread, |
4aeb0dfc | 424 | RECORD_METHOD_BTRACE, func->next); |
75c0bdf4 TW |
425 | } |
426 | ||
427 | /* Implementation of BtraceList.__len__ (self) -> int. */ | |
428 | ||
429 | static Py_ssize_t | |
430 | btpy_list_length (PyObject *self) | |
431 | { | |
432 | const btpy_list_object * const obj = (btpy_list_object *) self; | |
433 | const Py_ssize_t distance = obj->last - obj->first; | |
434 | const Py_ssize_t result = distance / obj->step; | |
435 | ||
436 | if ((distance % obj->step) == 0) | |
437 | return result; | |
438 | ||
439 | return result + 1; | |
440 | } | |
441 | ||
442 | /* Implementation of | |
443 | BtraceList.__getitem__ (self, key) -> BtraceInstruction and | |
444 | BtraceList.__getitem__ (self, key) -> BtraceFunctionCall. */ | |
445 | ||
446 | static PyObject * | |
447 | btpy_list_item (PyObject *self, Py_ssize_t index) | |
448 | { | |
449 | const btpy_list_object * const obj = (btpy_list_object *) self; | |
0ed5da75 | 450 | Py_ssize_t number; |
75c0bdf4 TW |
451 | |
452 | if (index < 0 || index >= btpy_list_length (self)) | |
453 | return PyErr_Format (PyExc_IndexError, _("Index out of range: %zd."), | |
454 | index); | |
455 | ||
0ed5da75 TW |
456 | number = obj->first + (obj->step * index); |
457 | ||
458 | if (obj->element_type == &recpy_insn_type) | |
00431a78 | 459 | return recpy_insn_new (obj->thread, RECORD_METHOD_BTRACE, number); |
0ed5da75 | 460 | else |
00431a78 | 461 | return recpy_func_new (obj->thread, RECORD_METHOD_BTRACE, number); |
75c0bdf4 TW |
462 | } |
463 | ||
464 | /* Implementation of BtraceList.__getitem__ (self, slice) -> BtraceList. */ | |
465 | ||
466 | static PyObject * | |
467 | btpy_list_slice (PyObject *self, PyObject *value) | |
468 | { | |
469 | const btpy_list_object * const obj = (btpy_list_object *) self; | |
470 | const Py_ssize_t length = btpy_list_length (self); | |
471 | Py_ssize_t start, stop, step, slicelength; | |
472 | ||
5aee4587 | 473 | if (PyLong_Check (value)) |
75c0bdf4 | 474 | { |
5aee4587 | 475 | Py_ssize_t index = PyLong_AsSsize_t (value); |
75c0bdf4 TW |
476 | |
477 | /* Emulate Python behavior for negative indices. */ | |
478 | if (index < 0) | |
479 | index += length; | |
480 | ||
481 | return btpy_list_item (self, index); | |
482 | } | |
483 | ||
484 | if (!PySlice_Check (value)) | |
485 | return PyErr_Format (PyExc_TypeError, _("Index must be int or slice.")); | |
486 | ||
edae3fd6 | 487 | if (0 != PySlice_GetIndicesEx (value, length, &start, &stop, |
75c0bdf4 TW |
488 | &step, &slicelength)) |
489 | return NULL; | |
490 | ||
00431a78 | 491 | return btpy_list_new (obj->thread, obj->first + obj->step * start, |
75c0bdf4 TW |
492 | obj->first + obj->step * stop, obj->step * step, |
493 | obj->element_type); | |
494 | } | |
495 | ||
496 | /* Helper function that returns the position of an element in a BtraceList | |
497 | or -1 if the element is not in the list. */ | |
498 | ||
499 | static LONGEST | |
500 | btpy_list_position (PyObject *self, PyObject *value) | |
501 | { | |
502 | const btpy_list_object * const list_obj = (btpy_list_object *) self; | |
0ed5da75 | 503 | const recpy_element_object * const obj = (const recpy_element_object *) value; |
75c0bdf4 TW |
504 | Py_ssize_t index = obj->number; |
505 | ||
506 | if (list_obj->element_type != Py_TYPE (value)) | |
507 | return -1; | |
508 | ||
00431a78 | 509 | if (list_obj->thread != obj->thread) |
75c0bdf4 TW |
510 | return -1; |
511 | ||
512 | if (index < list_obj->first || index > list_obj->last) | |
513 | return -1; | |
514 | ||
515 | index -= list_obj->first; | |
516 | ||
517 | if (index % list_obj->step != 0) | |
518 | return -1; | |
519 | ||
520 | return index / list_obj->step; | |
521 | } | |
522 | ||
523 | /* Implementation of "in" operator for BtraceLists. */ | |
524 | ||
525 | static int | |
526 | btpy_list_contains (PyObject *self, PyObject *value) | |
527 | { | |
528 | if (btpy_list_position (self, value) < 0) | |
529 | return 0; | |
530 | ||
531 | return 1; | |
532 | } | |
533 | ||
534 | /* Implementation of BtraceLists.index (self, value) -> int. */ | |
535 | ||
536 | static PyObject * | |
537 | btpy_list_index (PyObject *self, PyObject *value) | |
538 | { | |
539 | const LONGEST index = btpy_list_position (self, value); | |
540 | ||
541 | if (index < 0) | |
542 | return PyErr_Format (PyExc_ValueError, _("Not in list.")); | |
543 | ||
4bde49dc | 544 | return gdb_py_object_from_longest (index).release (); |
75c0bdf4 TW |
545 | } |
546 | ||
547 | /* Implementation of BtraceList.count (self, value) -> int. */ | |
548 | ||
549 | static PyObject * | |
550 | btpy_list_count (PyObject *self, PyObject *value) | |
551 | { | |
552 | /* We know that if an element is in the list, it is so exactly one time, | |
553 | enabling us to reuse the "is element of" check. */ | |
47f0e2ff TT |
554 | return gdb_py_object_from_longest (btpy_list_contains (self, |
555 | value)).release (); | |
75c0bdf4 TW |
556 | } |
557 | ||
558 | /* Python rich compare function to allow for equality and inequality checks | |
559 | in Python. */ | |
560 | ||
561 | static PyObject * | |
562 | btpy_list_richcompare (PyObject *self, PyObject *other, int op) | |
563 | { | |
564 | const btpy_list_object * const obj1 = (btpy_list_object *) self; | |
565 | const btpy_list_object * const obj2 = (btpy_list_object *) other; | |
566 | ||
567 | if (Py_TYPE (self) != Py_TYPE (other)) | |
568 | { | |
569 | Py_INCREF (Py_NotImplemented); | |
570 | return Py_NotImplemented; | |
571 | } | |
572 | ||
573 | switch (op) | |
574 | { | |
575 | case Py_EQ: | |
00431a78 | 576 | if (obj1->thread == obj2->thread |
75c0bdf4 TW |
577 | && obj1->element_type == obj2->element_type |
578 | && obj1->first == obj2->first | |
579 | && obj1->last == obj2->last | |
580 | && obj1->step == obj2->step) | |
581 | Py_RETURN_TRUE; | |
582 | else | |
583 | Py_RETURN_FALSE; | |
584 | ||
585 | case Py_NE: | |
00431a78 | 586 | if (obj1->thread != obj2->thread |
75c0bdf4 TW |
587 | || obj1->element_type != obj2->element_type |
588 | || obj1->first != obj2->first | |
589 | || obj1->last != obj2->last | |
590 | || obj1->step != obj2->step) | |
591 | Py_RETURN_TRUE; | |
592 | else | |
593 | Py_RETURN_FALSE; | |
594 | ||
595 | default: | |
596 | break; | |
597 | } | |
598 | ||
599 | Py_INCREF (Py_NotImplemented); | |
600 | return Py_NotImplemented; | |
601 | } | |
602 | ||
603 | /* Implementation of | |
604 | BtraceRecord.method [str]. */ | |
605 | ||
606 | PyObject * | |
607 | recpy_bt_method (PyObject *self, void *closure) | |
608 | { | |
5aee4587 | 609 | return PyUnicode_FromString ("btrace"); |
75c0bdf4 TW |
610 | } |
611 | ||
612 | /* Implementation of | |
613 | BtraceRecord.format [str]. */ | |
614 | ||
615 | PyObject * | |
616 | recpy_bt_format (PyObject *self, void *closure) | |
617 | { | |
ae20e79a | 618 | const recpy_record_object * const record = (recpy_record_object *) self; |
00431a78 | 619 | const struct thread_info * const tinfo = record->thread; |
75c0bdf4 TW |
620 | const struct btrace_config * config; |
621 | ||
622 | if (tinfo == NULL) | |
623 | Py_RETURN_NONE; | |
624 | ||
625 | config = btrace_conf (&tinfo->btrace); | |
626 | ||
627 | if (config == NULL) | |
628 | Py_RETURN_NONE; | |
629 | ||
5aee4587 | 630 | return PyUnicode_FromString (btrace_format_short_string (config->format)); |
75c0bdf4 TW |
631 | } |
632 | ||
633 | /* Implementation of | |
634 | BtraceRecord.replay_position [BtraceInstruction]. */ | |
635 | ||
636 | PyObject * | |
637 | recpy_bt_replay_position (PyObject *self, void *closure) | |
638 | { | |
ae20e79a | 639 | const recpy_record_object * const record = (recpy_record_object *) self; |
00431a78 | 640 | thread_info * tinfo = record->thread; |
75c0bdf4 TW |
641 | |
642 | if (tinfo == NULL) | |
643 | Py_RETURN_NONE; | |
644 | ||
645 | if (tinfo->btrace.replay == NULL) | |
646 | Py_RETURN_NONE; | |
647 | ||
913aeadd TW |
648 | return btpy_insn_or_gap_new (tinfo, |
649 | btrace_insn_number (tinfo->btrace.replay)); | |
75c0bdf4 TW |
650 | } |
651 | ||
652 | /* Implementation of | |
653 | BtraceRecord.begin [BtraceInstruction]. */ | |
654 | ||
655 | PyObject * | |
656 | recpy_bt_begin (PyObject *self, void *closure) | |
657 | { | |
ae20e79a | 658 | const recpy_record_object * const record = (recpy_record_object *) self; |
00431a78 | 659 | thread_info *const tinfo = record->thread; |
75c0bdf4 TW |
660 | struct btrace_insn_iterator iterator; |
661 | ||
662 | if (tinfo == NULL) | |
663 | Py_RETURN_NONE; | |
664 | ||
4a4495d6 | 665 | btrace_fetch (tinfo, record_btrace_get_cpu ()); |
75c0bdf4 TW |
666 | |
667 | if (btrace_is_empty (tinfo)) | |
668 | Py_RETURN_NONE; | |
669 | ||
670 | btrace_insn_begin (&iterator, &tinfo->btrace); | |
913aeadd | 671 | return btpy_insn_or_gap_new (tinfo, btrace_insn_number (&iterator)); |
75c0bdf4 TW |
672 | } |
673 | ||
674 | /* Implementation of | |
675 | BtraceRecord.end [BtraceInstruction]. */ | |
676 | ||
677 | PyObject * | |
678 | recpy_bt_end (PyObject *self, void *closure) | |
679 | { | |
ae20e79a | 680 | const recpy_record_object * const record = (recpy_record_object *) self; |
00431a78 | 681 | thread_info *const tinfo = record->thread; |
75c0bdf4 TW |
682 | struct btrace_insn_iterator iterator; |
683 | ||
684 | if (tinfo == NULL) | |
685 | Py_RETURN_NONE; | |
686 | ||
4a4495d6 | 687 | btrace_fetch (tinfo, record_btrace_get_cpu ()); |
75c0bdf4 TW |
688 | |
689 | if (btrace_is_empty (tinfo)) | |
690 | Py_RETURN_NONE; | |
691 | ||
692 | btrace_insn_end (&iterator, &tinfo->btrace); | |
913aeadd | 693 | return btpy_insn_or_gap_new (tinfo, btrace_insn_number (&iterator)); |
75c0bdf4 TW |
694 | } |
695 | ||
696 | /* Implementation of | |
697 | BtraceRecord.instruction_history [list]. */ | |
698 | ||
699 | PyObject * | |
700 | recpy_bt_instruction_history (PyObject *self, void *closure) | |
701 | { | |
ae20e79a | 702 | const recpy_record_object * const record = (recpy_record_object *) self; |
00431a78 | 703 | thread_info *const tinfo = record->thread; |
75c0bdf4 TW |
704 | struct btrace_insn_iterator iterator; |
705 | unsigned long first = 0; | |
706 | unsigned long last = 0; | |
707 | ||
708 | if (tinfo == NULL) | |
709 | Py_RETURN_NONE; | |
710 | ||
4a4495d6 | 711 | btrace_fetch (tinfo, record_btrace_get_cpu ()); |
75c0bdf4 TW |
712 | |
713 | if (btrace_is_empty (tinfo)) | |
714 | Py_RETURN_NONE; | |
715 | ||
716 | btrace_insn_begin (&iterator, &tinfo->btrace); | |
717 | first = btrace_insn_number (&iterator); | |
718 | ||
719 | btrace_insn_end (&iterator, &tinfo->btrace); | |
720 | last = btrace_insn_number (&iterator); | |
721 | ||
00431a78 | 722 | return btpy_list_new (tinfo, first, last, 1, &recpy_insn_type); |
75c0bdf4 TW |
723 | } |
724 | ||
725 | /* Implementation of | |
726 | BtraceRecord.function_call_history [list]. */ | |
727 | ||
728 | PyObject * | |
729 | recpy_bt_function_call_history (PyObject *self, void *closure) | |
730 | { | |
ae20e79a | 731 | const recpy_record_object * const record = (recpy_record_object *) self; |
00431a78 | 732 | thread_info *const tinfo = record->thread; |
8d0050ea TW |
733 | struct btrace_call_iterator iterator; |
734 | unsigned long first = 0; | |
735 | unsigned long last = 0; | |
75c0bdf4 | 736 | |
8d0050ea TW |
737 | if (tinfo == NULL) |
738 | Py_RETURN_NONE; | |
75c0bdf4 | 739 | |
4a4495d6 | 740 | btrace_fetch (tinfo, record_btrace_get_cpu ()); |
75c0bdf4 | 741 | |
8d0050ea TW |
742 | if (btrace_is_empty (tinfo)) |
743 | Py_RETURN_NONE; | |
75c0bdf4 | 744 | |
8d0050ea TW |
745 | btrace_call_begin (&iterator, &tinfo->btrace); |
746 | first = btrace_call_number (&iterator); | |
75c0bdf4 | 747 | |
8d0050ea TW |
748 | btrace_call_end (&iterator, &tinfo->btrace); |
749 | last = btrace_call_number (&iterator); | |
75c0bdf4 | 750 | |
00431a78 | 751 | return btpy_list_new (tinfo, first, last, 1, &recpy_func_type); |
75c0bdf4 TW |
752 | } |
753 | ||
754 | /* Implementation of BtraceRecord.goto (self, BtraceInstruction) -> None. */ | |
755 | ||
756 | PyObject * | |
757 | recpy_bt_goto (PyObject *self, PyObject *args) | |
758 | { | |
ae20e79a | 759 | const recpy_record_object * const record = (recpy_record_object *) self; |
00431a78 | 760 | thread_info *const tinfo = record->thread; |
0ed5da75 | 761 | const recpy_element_object *obj; |
96b1ad86 | 762 | PyObject *parse_obj; |
75c0bdf4 TW |
763 | |
764 | if (tinfo == NULL || btrace_is_empty (tinfo)) | |
765 | return PyErr_Format (gdbpy_gdb_error, _("Empty branch trace.")); | |
766 | ||
96b1ad86 | 767 | if (!PyArg_ParseTuple (args, "O", &parse_obj)) |
75c0bdf4 TW |
768 | return NULL; |
769 | ||
96b1ad86 | 770 | if (Py_TYPE (parse_obj) != &recpy_insn_type) |
75c0bdf4 | 771 | return PyErr_Format (PyExc_TypeError, _("Argument must be instruction.")); |
96b1ad86 | 772 | obj = (const recpy_element_object *) parse_obj; |
75c0bdf4 | 773 | |
a70b8144 | 774 | try |
75c0bdf4 TW |
775 | { |
776 | struct btrace_insn_iterator iter; | |
777 | ||
778 | btrace_insn_end (&iter, &tinfo->btrace); | |
779 | ||
780 | if (btrace_insn_number (&iter) == obj->number) | |
781 | target_goto_record_end (); | |
782 | else | |
783 | target_goto_record (obj->number); | |
784 | } | |
230d2906 | 785 | catch (const gdb_exception &except) |
75c0bdf4 TW |
786 | { |
787 | GDB_PY_HANDLE_EXCEPTION (except); | |
788 | } | |
75c0bdf4 TW |
789 | |
790 | Py_RETURN_NONE; | |
791 | } | |
792 | ||
75c0bdf4 TW |
793 | /* BtraceList methods. */ |
794 | ||
6bd434d6 | 795 | static PyMethodDef btpy_list_methods[] = |
75c0bdf4 | 796 | { |
40c94099 | 797 | { "count", btpy_list_count, METH_O, "count number of occurrences"}, |
75c0bdf4 TW |
798 | { "index", btpy_list_index, METH_O, "index of entry"}, |
799 | {NULL} | |
800 | }; | |
801 | ||
802 | /* BtraceList sequence methods. */ | |
803 | ||
804 | static PySequenceMethods btpy_list_sequence_methods = | |
805 | { | |
806 | NULL | |
807 | }; | |
808 | ||
809 | /* BtraceList mapping methods. Necessary for slicing. */ | |
810 | ||
811 | static PyMappingMethods btpy_list_mapping_methods = | |
812 | { | |
813 | NULL | |
814 | }; | |
815 | ||
816 | /* Sets up the btrace record API. */ | |
817 | ||
3965bff5 | 818 | static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION |
75c0bdf4 TW |
819 | gdbpy_initialize_btrace (void) |
820 | { | |
75c0bdf4 TW |
821 | btpy_list_type.tp_new = PyType_GenericNew; |
822 | btpy_list_type.tp_flags = Py_TPFLAGS_DEFAULT; | |
823 | btpy_list_type.tp_basicsize = sizeof (btpy_list_object); | |
824 | btpy_list_type.tp_name = "gdb.BtraceObjectList"; | |
825 | btpy_list_type.tp_doc = "GDB btrace list object"; | |
826 | btpy_list_type.tp_methods = btpy_list_methods; | |
827 | btpy_list_type.tp_as_sequence = &btpy_list_sequence_methods; | |
828 | btpy_list_type.tp_as_mapping = &btpy_list_mapping_methods; | |
829 | btpy_list_type.tp_richcompare = btpy_list_richcompare; | |
830 | ||
831 | btpy_list_sequence_methods.sq_item = btpy_list_item; | |
832 | btpy_list_sequence_methods.sq_length = btpy_list_length; | |
833 | btpy_list_sequence_methods.sq_contains = btpy_list_contains; | |
834 | ||
835 | btpy_list_mapping_methods.mp_subscript = btpy_list_slice; | |
836 | ||
14f819c8 | 837 | return PyType_Ready (&btpy_list_type); |
75c0bdf4 | 838 | } |
3965bff5 AB |
839 | |
840 | GDBPY_INITIALIZE_FILE (gdbpy_initialize_btrace); |