Commit | Line | Data |
---|---|---|
d11916aa SS |
1 | /* Python frame unwinder interface. |
2 | ||
b811d2c2 | 3 | Copyright (C) 2015-2020 Free Software Foundation, Inc. |
d11916aa SS |
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 "arch-utils.h" | |
22 | #include "frame-unwind.h" | |
23 | #include "gdb_obstack.h" | |
24 | #include "gdbcmd.h" | |
25 | #include "language.h" | |
76727919 | 26 | #include "observable.h" |
d11916aa SS |
27 | #include "python-internal.h" |
28 | #include "regcache.h" | |
29 | #include "valprint.h" | |
d11916aa SS |
30 | |
31 | #define TRACE_PY_UNWIND(level, args...) if (pyuw_debug >= level) \ | |
32 | { fprintf_unfiltered (gdb_stdlog, args); } | |
33 | ||
34 | typedef struct | |
35 | { | |
36 | PyObject_HEAD | |
37 | ||
38 | /* Frame we are unwinding. */ | |
39 | struct frame_info *frame_info; | |
40 | ||
41 | /* Its architecture, passed by the sniffer caller. */ | |
42 | struct gdbarch *gdbarch; | |
43 | } pending_frame_object; | |
44 | ||
45 | /* Saved registers array item. */ | |
46 | ||
0c6aef22 | 47 | struct saved_reg |
d11916aa | 48 | { |
0c6aef22 TT |
49 | saved_reg (int n, gdbpy_ref<> &&v) |
50 | : number (n), | |
51 | value (std::move (v)) | |
52 | { | |
53 | } | |
54 | ||
d11916aa | 55 | int number; |
0c6aef22 TT |
56 | gdbpy_ref<> value; |
57 | }; | |
d11916aa SS |
58 | |
59 | /* The data we keep for the PyUnwindInfo: pending_frame, saved registers | |
60 | and frame ID. */ | |
61 | ||
62 | typedef struct | |
63 | { | |
64 | PyObject_HEAD | |
65 | ||
66 | /* gdb.PendingFrame for the frame we are unwinding. */ | |
67 | PyObject *pending_frame; | |
68 | ||
69 | /* Its ID. */ | |
70 | struct frame_id frame_id; | |
71 | ||
72 | /* Saved registers array. */ | |
0c6aef22 | 73 | std::vector<saved_reg> *saved_regs; |
d11916aa SS |
74 | } unwind_info_object; |
75 | ||
76 | /* The data we keep for a frame we can unwind: frame ID and an array of | |
77 | (register_number, register_value) pairs. */ | |
78 | ||
79 | typedef struct | |
80 | { | |
81 | /* Frame ID. */ | |
82 | struct frame_id frame_id; | |
83 | ||
84 | /* GDB Architecture. */ | |
85 | struct gdbarch *gdbarch; | |
86 | ||
87 | /* Length of the `reg' array below. */ | |
88 | int reg_count; | |
89 | ||
4fa847d7 | 90 | cached_reg_t reg[]; |
d11916aa SS |
91 | } cached_frame_info; |
92 | ||
13fa0398 | 93 | extern PyTypeObject pending_frame_object_type |
d11916aa SS |
94 | CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pending_frame_object"); |
95 | ||
13fa0398 | 96 | extern PyTypeObject unwind_info_object_type |
d11916aa SS |
97 | CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("unwind_info_object"); |
98 | ||
99 | static unsigned int pyuw_debug = 0; | |
100 | ||
101 | static struct gdbarch_data *pyuw_gdbarch_data; | |
102 | ||
d11916aa SS |
103 | /* Convert gdb.Value instance to inferior's pointer. Return 1 on success, |
104 | 0 on failure. */ | |
105 | ||
106 | static int | |
107 | pyuw_value_obj_to_pointer (PyObject *pyo_value, CORE_ADDR *addr) | |
108 | { | |
109 | int rc = 0; | |
110 | struct value *value; | |
111 | ||
a70b8144 | 112 | try |
d11916aa SS |
113 | { |
114 | if ((value = value_object_to_value (pyo_value)) != NULL) | |
115 | { | |
116 | *addr = unpack_pointer (value_type (value), | |
117 | value_contents (value)); | |
118 | rc = 1; | |
119 | } | |
120 | } | |
230d2906 | 121 | catch (const gdb_exception &except) |
d11916aa SS |
122 | { |
123 | gdbpy_convert_exception (except); | |
124 | } | |
d11916aa SS |
125 | return rc; |
126 | } | |
127 | ||
128 | /* Get attribute from an object and convert it to the inferior's | |
129 | pointer value. Return 1 if attribute exists and its value can be | |
130 | converted. Otherwise, if attribute does not exist or its value is | |
131 | None, return 0. In all other cases set Python error and return | |
132 | 0. */ | |
133 | ||
134 | static int | |
135 | pyuw_object_attribute_to_pointer (PyObject *pyo, const char *attr_name, | |
136 | CORE_ADDR *addr) | |
137 | { | |
138 | int rc = 0; | |
139 | ||
140 | if (PyObject_HasAttrString (pyo, attr_name)) | |
141 | { | |
7780f186 | 142 | gdbpy_ref<> pyo_value (PyObject_GetAttrString (pyo, attr_name)); |
d11916aa SS |
143 | |
144 | if (pyo_value != NULL && pyo_value != Py_None) | |
145 | { | |
4586d543 | 146 | rc = pyuw_value_obj_to_pointer (pyo_value.get (), addr); |
d11916aa SS |
147 | if (!rc) |
148 | PyErr_Format ( | |
149 | PyExc_ValueError, | |
150 | _("The value of the '%s' attribute is not a pointer."), | |
151 | attr_name); | |
152 | } | |
d11916aa SS |
153 | } |
154 | return rc; | |
155 | } | |
156 | ||
157 | /* Called by the Python interpreter to obtain string representation | |
158 | of the UnwindInfo object. */ | |
159 | ||
160 | static PyObject * | |
161 | unwind_infopy_str (PyObject *self) | |
162 | { | |
d11916aa | 163 | unwind_info_object *unwind_info = (unwind_info_object *) self; |
d7e74731 | 164 | string_file stb; |
d11916aa | 165 | |
d7e74731 PA |
166 | stb.puts ("Frame ID: "); |
167 | fprint_frame_id (&stb, unwind_info->frame_id); | |
d11916aa | 168 | { |
a121b7c1 | 169 | const char *sep = ""; |
d11916aa | 170 | struct value_print_options opts; |
d11916aa SS |
171 | |
172 | get_user_print_options (&opts); | |
d7e74731 | 173 | stb.printf ("\nSaved registers: ("); |
0c6aef22 | 174 | for (const saved_reg ® : *unwind_info->saved_regs) |
d11916aa | 175 | { |
0c6aef22 | 176 | struct value *value = value_object_to_value (reg.value.get ()); |
d11916aa | 177 | |
0c6aef22 | 178 | stb.printf ("%s(%d, ", sep, reg.number); |
d11916aa SS |
179 | if (value != NULL) |
180 | { | |
a70b8144 | 181 | try |
d11916aa | 182 | { |
d7e74731 PA |
183 | value_print (value, &stb, &opts); |
184 | stb.puts (")"); | |
d11916aa | 185 | } |
230d2906 | 186 | catch (const gdb_exception &except) |
d11916aa SS |
187 | { |
188 | GDB_PY_HANDLE_EXCEPTION (except); | |
189 | } | |
d11916aa SS |
190 | } |
191 | else | |
d7e74731 | 192 | stb.puts ("<BAD>)"); |
d11916aa SS |
193 | sep = ", "; |
194 | } | |
d7e74731 | 195 | stb.puts (")"); |
d11916aa | 196 | } |
d11916aa | 197 | |
d7e74731 | 198 | return PyString_FromString (stb.c_str ()); |
d11916aa SS |
199 | } |
200 | ||
201 | /* Create UnwindInfo instance for given PendingFrame and frame ID. | |
202 | Sets Python error and returns NULL on error. */ | |
203 | ||
204 | static PyObject * | |
205 | pyuw_create_unwind_info (PyObject *pyo_pending_frame, | |
206 | struct frame_id frame_id) | |
207 | { | |
208 | unwind_info_object *unwind_info | |
209 | = PyObject_New (unwind_info_object, &unwind_info_object_type); | |
210 | ||
211 | if (((pending_frame_object *) pyo_pending_frame)->frame_info == NULL) | |
212 | { | |
213 | PyErr_SetString (PyExc_ValueError, | |
214 | "Attempting to use stale PendingFrame"); | |
215 | return NULL; | |
216 | } | |
217 | unwind_info->frame_id = frame_id; | |
218 | Py_INCREF (pyo_pending_frame); | |
219 | unwind_info->pending_frame = pyo_pending_frame; | |
0c6aef22 | 220 | unwind_info->saved_regs = new std::vector<saved_reg>; |
d11916aa SS |
221 | return (PyObject *) unwind_info; |
222 | } | |
223 | ||
224 | /* The implementation of | |
225 | gdb.UnwindInfo.add_saved_register (REG, VALUE) -> None. */ | |
226 | ||
227 | static PyObject * | |
228 | unwind_infopy_add_saved_register (PyObject *self, PyObject *args) | |
229 | { | |
230 | unwind_info_object *unwind_info = (unwind_info_object *) self; | |
231 | pending_frame_object *pending_frame | |
232 | = (pending_frame_object *) (unwind_info->pending_frame); | |
233 | PyObject *pyo_reg_id; | |
234 | PyObject *pyo_reg_value; | |
235 | int regnum; | |
236 | ||
237 | if (pending_frame->frame_info == NULL) | |
238 | { | |
239 | PyErr_SetString (PyExc_ValueError, | |
240 | "UnwindInfo instance refers to a stale PendingFrame"); | |
241 | return NULL; | |
242 | } | |
243 | if (!PyArg_UnpackTuple (args, "previous_frame_register", 2, 2, | |
244 | &pyo_reg_id, &pyo_reg_value)) | |
245 | return NULL; | |
43d5901d | 246 | if (!gdbpy_parse_register_id (pending_frame->gdbarch, pyo_reg_id, ®num)) |
d11916aa SS |
247 | { |
248 | PyErr_SetString (PyExc_ValueError, "Bad register"); | |
249 | return NULL; | |
250 | } | |
251 | { | |
252 | struct value *value; | |
253 | size_t data_size; | |
254 | ||
255 | if (pyo_reg_value == NULL | |
256 | || (value = value_object_to_value (pyo_reg_value)) == NULL) | |
257 | { | |
258 | PyErr_SetString (PyExc_ValueError, "Bad register value"); | |
259 | return NULL; | |
260 | } | |
261 | data_size = register_size (pending_frame->gdbarch, regnum); | |
262 | if (data_size != TYPE_LENGTH (value_type (value))) | |
263 | { | |
264 | PyErr_Format ( | |
265 | PyExc_ValueError, | |
266 | "The value of the register returned by the Python " | |
267 | "sniffer has unexpected size: %u instead of %u.", | |
268 | (unsigned) TYPE_LENGTH (value_type (value)), | |
269 | (unsigned) data_size); | |
270 | return NULL; | |
271 | } | |
272 | } | |
273 | { | |
0c6aef22 TT |
274 | gdbpy_ref<> new_value = gdbpy_ref<>::new_reference (pyo_reg_value); |
275 | bool found = false; | |
276 | for (saved_reg ® : *unwind_info->saved_regs) | |
d11916aa | 277 | { |
0c6aef22 | 278 | if (regnum == reg.number) |
d11916aa | 279 | { |
0c6aef22 TT |
280 | found = true; |
281 | reg.value = std::move (new_value); | |
d11916aa SS |
282 | break; |
283 | } | |
284 | } | |
0c6aef22 TT |
285 | if (!found) |
286 | unwind_info->saved_regs->emplace_back (regnum, std::move (new_value)); | |
d11916aa SS |
287 | } |
288 | Py_RETURN_NONE; | |
289 | } | |
290 | ||
291 | /* UnwindInfo cleanup. */ | |
292 | ||
293 | static void | |
294 | unwind_infopy_dealloc (PyObject *self) | |
295 | { | |
296 | unwind_info_object *unwind_info = (unwind_info_object *) self; | |
d11916aa SS |
297 | |
298 | Py_XDECREF (unwind_info->pending_frame); | |
0c6aef22 | 299 | delete unwind_info->saved_regs; |
d11916aa SS |
300 | Py_TYPE (self)->tp_free (self); |
301 | } | |
302 | ||
303 | /* Called by the Python interpreter to obtain string representation | |
304 | of the PendingFrame object. */ | |
305 | ||
306 | static PyObject * | |
307 | pending_framepy_str (PyObject *self) | |
308 | { | |
309 | struct frame_info *frame = ((pending_frame_object *) self)->frame_info; | |
310 | const char *sp_str = NULL; | |
311 | const char *pc_str = NULL; | |
312 | ||
313 | if (frame == NULL) | |
314 | return PyString_FromString ("Stale PendingFrame instance"); | |
a70b8144 | 315 | try |
d11916aa SS |
316 | { |
317 | sp_str = core_addr_to_string_nz (get_frame_sp (frame)); | |
318 | pc_str = core_addr_to_string_nz (get_frame_pc (frame)); | |
319 | } | |
230d2906 | 320 | catch (const gdb_exception &except) |
d11916aa SS |
321 | { |
322 | GDB_PY_HANDLE_EXCEPTION (except); | |
323 | } | |
d11916aa SS |
324 | |
325 | return PyString_FromFormat ("SP=%s,PC=%s", sp_str, pc_str); | |
326 | } | |
327 | ||
328 | /* Implementation of gdb.PendingFrame.read_register (self, reg) -> gdb.Value. | |
329 | Returns the value of register REG as gdb.Value instance. */ | |
330 | ||
331 | static PyObject * | |
332 | pending_framepy_read_register (PyObject *self, PyObject *args) | |
333 | { | |
334 | pending_frame_object *pending_frame = (pending_frame_object *) self; | |
335 | struct value *val = NULL; | |
336 | int regnum; | |
337 | PyObject *pyo_reg_id; | |
338 | ||
339 | if (pending_frame->frame_info == NULL) | |
340 | { | |
341 | PyErr_SetString (PyExc_ValueError, | |
342 | "Attempting to read register from stale PendingFrame"); | |
343 | return NULL; | |
344 | } | |
345 | if (!PyArg_UnpackTuple (args, "read_register", 1, 1, &pyo_reg_id)) | |
346 | return NULL; | |
43d5901d | 347 | if (!gdbpy_parse_register_id (pending_frame->gdbarch, pyo_reg_id, ®num)) |
d11916aa SS |
348 | { |
349 | PyErr_SetString (PyExc_ValueError, "Bad register"); | |
350 | return NULL; | |
351 | } | |
352 | ||
a70b8144 | 353 | try |
d11916aa | 354 | { |
33cc7d36 KB |
355 | /* Fetch the value associated with a register, whether it's |
356 | a real register or a so called "user" register, like "pc", | |
357 | which maps to a real register. In the past, | |
358 | get_frame_register_value() was used here, which did not | |
359 | handle the user register case. */ | |
360 | val = value_of_register (regnum, pending_frame->frame_info); | |
d11916aa SS |
361 | if (val == NULL) |
362 | PyErr_Format (PyExc_ValueError, | |
363 | "Cannot read register %d from frame.", | |
364 | regnum); | |
365 | } | |
230d2906 | 366 | catch (const gdb_exception &except) |
d11916aa SS |
367 | { |
368 | GDB_PY_HANDLE_EXCEPTION (except); | |
369 | } | |
d11916aa SS |
370 | |
371 | return val == NULL ? NULL : value_to_value_object (val); | |
372 | } | |
373 | ||
374 | /* Implementation of | |
375 | PendingFrame.create_unwind_info (self, frameId) -> UnwindInfo. */ | |
376 | ||
377 | static PyObject * | |
378 | pending_framepy_create_unwind_info (PyObject *self, PyObject *args) | |
379 | { | |
380 | PyObject *pyo_frame_id; | |
381 | CORE_ADDR sp; | |
382 | CORE_ADDR pc; | |
383 | CORE_ADDR special; | |
384 | ||
385 | if (!PyArg_ParseTuple (args, "O:create_unwind_info", &pyo_frame_id)) | |
386 | return NULL; | |
387 | if (!pyuw_object_attribute_to_pointer (pyo_frame_id, "sp", &sp)) | |
388 | { | |
389 | PyErr_SetString (PyExc_ValueError, | |
390 | _("frame_id should have 'sp' attribute.")); | |
391 | return NULL; | |
392 | } | |
393 | ||
394 | /* The logic of building frame_id depending on the attributes of | |
395 | the frame_id object: | |
396 | Has Has Has Function to call | |
397 | 'sp'? 'pc'? 'special'? | |
398 | ------|------|--------------|------------------------- | |
399 | Y N * frame_id_build_wild (sp) | |
400 | Y Y N frame_id_build (sp, pc) | |
401 | Y Y Y frame_id_build_special (sp, pc, special) | |
402 | */ | |
403 | if (!pyuw_object_attribute_to_pointer (pyo_frame_id, "pc", &pc)) | |
404 | return pyuw_create_unwind_info (self, frame_id_build_wild (sp)); | |
405 | if (!pyuw_object_attribute_to_pointer (pyo_frame_id, "special", &special)) | |
406 | return pyuw_create_unwind_info (self, frame_id_build (sp, pc)); | |
407 | else | |
408 | return pyuw_create_unwind_info (self, | |
409 | frame_id_build_special (sp, pc, special)); | |
410 | } | |
411 | ||
87dbc774 AB |
412 | /* Implementation of PendingFrame.architecture (self) -> gdb.Architecture. */ |
413 | ||
414 | static PyObject * | |
415 | pending_framepy_architecture (PyObject *self, PyObject *args) | |
416 | { | |
417 | pending_frame_object *pending_frame = (pending_frame_object *) self; | |
418 | ||
419 | if (pending_frame->frame_info == NULL) | |
420 | { | |
421 | PyErr_SetString (PyExc_ValueError, | |
422 | "Attempting to read register from stale PendingFrame"); | |
423 | return NULL; | |
424 | } | |
425 | return gdbarch_to_arch_object (pending_frame->gdbarch); | |
426 | } | |
427 | ||
d11916aa SS |
428 | /* frame_unwind.this_id method. */ |
429 | ||
430 | static void | |
431 | pyuw_this_id (struct frame_info *this_frame, void **cache_ptr, | |
432 | struct frame_id *this_id) | |
433 | { | |
434 | *this_id = ((cached_frame_info *) *cache_ptr)->frame_id; | |
435 | if (pyuw_debug >= 1) | |
436 | { | |
437 | fprintf_unfiltered (gdb_stdlog, "%s: frame_id: ", __FUNCTION__); | |
438 | fprint_frame_id (gdb_stdlog, *this_id); | |
439 | fprintf_unfiltered (gdb_stdlog, "\n"); | |
440 | } | |
441 | } | |
442 | ||
443 | /* frame_unwind.prev_register. */ | |
444 | ||
445 | static struct value * | |
446 | pyuw_prev_register (struct frame_info *this_frame, void **cache_ptr, | |
447 | int regnum) | |
448 | { | |
19ba03f4 | 449 | cached_frame_info *cached_frame = (cached_frame_info *) *cache_ptr; |
4fa847d7 AH |
450 | cached_reg_t *reg_info = cached_frame->reg; |
451 | cached_reg_t *reg_info_end = reg_info + cached_frame->reg_count; | |
d11916aa SS |
452 | |
453 | TRACE_PY_UNWIND (1, "%s (frame=%p,...,reg=%d)\n", __FUNCTION__, this_frame, | |
454 | regnum); | |
455 | for (; reg_info < reg_info_end; ++reg_info) | |
456 | { | |
4fa847d7 | 457 | if (regnum == reg_info->num) |
d11916aa SS |
458 | return frame_unwind_got_bytes (this_frame, regnum, reg_info->data); |
459 | } | |
460 | ||
461 | return frame_unwind_got_optimized (this_frame, regnum); | |
462 | } | |
463 | ||
464 | /* Frame sniffer dispatch. */ | |
465 | ||
466 | static int | |
467 | pyuw_sniffer (const struct frame_unwind *self, struct frame_info *this_frame, | |
468 | void **cache_ptr) | |
469 | { | |
470 | struct gdbarch *gdbarch = (struct gdbarch *) (self->unwind_data); | |
d11916aa SS |
471 | cached_frame_info *cached_frame; |
472 | ||
c0171de6 TT |
473 | gdbpy_enter enter_py (gdbarch, current_language); |
474 | ||
d11916aa SS |
475 | TRACE_PY_UNWIND (3, "%s (SP=%s, PC=%s)\n", __FUNCTION__, |
476 | paddress (gdbarch, get_frame_sp (this_frame)), | |
477 | paddress (gdbarch, get_frame_pc (this_frame))); | |
478 | ||
479 | /* Create PendingFrame instance to pass to sniffers. */ | |
c0171de6 TT |
480 | pending_frame_object *pfo = PyObject_New (pending_frame_object, |
481 | &pending_frame_object_type); | |
7780f186 | 482 | gdbpy_ref<> pyo_pending_frame ((PyObject *) pfo); |
d11916aa | 483 | if (pyo_pending_frame == NULL) |
c0171de6 TT |
484 | { |
485 | gdbpy_print_stack (); | |
486 | return 0; | |
487 | } | |
488 | pfo->gdbarch = gdbarch; | |
489 | scoped_restore invalidate_frame = make_scoped_restore (&pfo->frame_info, | |
490 | this_frame); | |
d11916aa SS |
491 | |
492 | /* Run unwinders. */ | |
493 | if (gdb_python_module == NULL | |
08235187 | 494 | || ! PyObject_HasAttrString (gdb_python_module, "_execute_unwinders")) |
d11916aa SS |
495 | { |
496 | PyErr_SetString (PyExc_NameError, | |
08235187 | 497 | "Installation error: gdb._execute_unwinders function " |
d11916aa | 498 | "is missing"); |
c0171de6 TT |
499 | gdbpy_print_stack (); |
500 | return 0; | |
d11916aa | 501 | } |
7780f186 | 502 | gdbpy_ref<> pyo_execute (PyObject_GetAttrString (gdb_python_module, |
08235187 | 503 | "_execute_unwinders")); |
d11916aa | 504 | if (pyo_execute == NULL) |
c0171de6 TT |
505 | { |
506 | gdbpy_print_stack (); | |
507 | return 0; | |
508 | } | |
509 | ||
7780f186 | 510 | gdbpy_ref<> pyo_unwind_info |
c0171de6 TT |
511 | (PyObject_CallFunctionObjArgs (pyo_execute.get (), |
512 | pyo_pending_frame.get (), NULL)); | |
d11916aa | 513 | if (pyo_unwind_info == NULL) |
c0171de6 | 514 | { |
9ccabccd PA |
515 | /* If the unwinder is cancelled due to a Ctrl-C, then propagate |
516 | the Ctrl-C as a GDB exception instead of swallowing it. */ | |
6ef2312a | 517 | gdbpy_print_stack_or_quit (); |
c0171de6 TT |
518 | return 0; |
519 | } | |
d11916aa | 520 | if (pyo_unwind_info == Py_None) |
c0171de6 | 521 | return 0; |
d11916aa SS |
522 | |
523 | /* Received UnwindInfo, cache data. */ | |
c0171de6 | 524 | if (PyObject_IsInstance (pyo_unwind_info.get (), |
d11916aa SS |
525 | (PyObject *) &unwind_info_object_type) <= 0) |
526 | error (_("A Unwinder should return gdb.UnwindInfo instance.")); | |
527 | ||
528 | { | |
c0171de6 TT |
529 | unwind_info_object *unwind_info = |
530 | (unwind_info_object *) pyo_unwind_info.get (); | |
0c6aef22 | 531 | int reg_count = unwind_info->saved_regs->size (); |
d11916aa | 532 | |
16892a03 AH |
533 | cached_frame |
534 | = ((cached_frame_info *) | |
535 | xmalloc (sizeof (*cached_frame) | |
536 | + reg_count * sizeof (cached_frame->reg[0]))); | |
d11916aa SS |
537 | cached_frame->gdbarch = gdbarch; |
538 | cached_frame->frame_id = unwind_info->frame_id; | |
539 | cached_frame->reg_count = reg_count; | |
540 | ||
541 | /* Populate registers array. */ | |
0c6aef22 | 542 | for (int i = 0; i < unwind_info->saved_regs->size (); ++i) |
d11916aa | 543 | { |
0c6aef22 TT |
544 | saved_reg *reg = &(*unwind_info->saved_regs)[i]; |
545 | ||
546 | struct value *value = value_object_to_value (reg->value.get ()); | |
d11916aa SS |
547 | size_t data_size = register_size (gdbarch, reg->number); |
548 | ||
4fa847d7 | 549 | cached_frame->reg[i].num = reg->number; |
d11916aa SS |
550 | |
551 | /* `value' validation was done before, just assert. */ | |
552 | gdb_assert (value != NULL); | |
553 | gdb_assert (data_size == TYPE_LENGTH (value_type (value))); | |
d11916aa | 554 | |
4fa847d7 | 555 | cached_frame->reg[i].data = (gdb_byte *) xmalloc (data_size); |
d11916aa SS |
556 | memcpy (cached_frame->reg[i].data, value_contents (value), data_size); |
557 | } | |
558 | } | |
559 | ||
560 | *cache_ptr = cached_frame; | |
d11916aa | 561 | return 1; |
d11916aa SS |
562 | } |
563 | ||
564 | /* Frame cache release shim. */ | |
565 | ||
566 | static void | |
567 | pyuw_dealloc_cache (struct frame_info *this_frame, void *cache) | |
568 | { | |
569 | TRACE_PY_UNWIND (3, "%s: enter", __FUNCTION__); | |
4fa847d7 AH |
570 | cached_frame_info *cached_frame = (cached_frame_info *) cache; |
571 | ||
8455d262 | 572 | for (int i = 0; i < cached_frame->reg_count; i++) |
4fa847d7 AH |
573 | xfree (cached_frame->reg[i].data); |
574 | ||
d11916aa SS |
575 | xfree (cache); |
576 | } | |
577 | ||
578 | struct pyuw_gdbarch_data_type | |
579 | { | |
580 | /* Has the unwinder shim been prepended? */ | |
581 | int unwinder_registered; | |
582 | }; | |
583 | ||
584 | static void * | |
585 | pyuw_gdbarch_data_init (struct gdbarch *gdbarch) | |
586 | { | |
587 | return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct pyuw_gdbarch_data_type); | |
588 | } | |
589 | ||
590 | /* New inferior architecture callback: register the Python unwinders | |
591 | intermediary. */ | |
592 | ||
593 | static void | |
594 | pyuw_on_new_gdbarch (struct gdbarch *newarch) | |
595 | { | |
19ba03f4 SM |
596 | struct pyuw_gdbarch_data_type *data |
597 | = (struct pyuw_gdbarch_data_type *) gdbarch_data (newarch, | |
598 | pyuw_gdbarch_data); | |
d11916aa SS |
599 | |
600 | if (!data->unwinder_registered) | |
601 | { | |
602 | struct frame_unwind *unwinder | |
603 | = GDBARCH_OBSTACK_ZALLOC (newarch, struct frame_unwind); | |
604 | ||
605 | unwinder->type = NORMAL_FRAME; | |
606 | unwinder->stop_reason = default_frame_unwind_stop_reason; | |
607 | unwinder->this_id = pyuw_this_id; | |
608 | unwinder->prev_register = pyuw_prev_register; | |
19ba03f4 | 609 | unwinder->unwind_data = (const struct frame_data *) newarch; |
d11916aa SS |
610 | unwinder->sniffer = pyuw_sniffer; |
611 | unwinder->dealloc_cache = pyuw_dealloc_cache; | |
612 | frame_unwind_prepend_unwinder (newarch, unwinder); | |
613 | data->unwinder_registered = 1; | |
614 | } | |
615 | } | |
616 | ||
617 | /* Initialize unwind machinery. */ | |
618 | ||
619 | int | |
620 | gdbpy_initialize_unwind (void) | |
621 | { | |
622 | int rc; | |
623 | add_setshow_zuinteger_cmd | |
624 | ("py-unwind", class_maintenance, &pyuw_debug, | |
625 | _("Set Python unwinder debugging."), | |
626 | _("Show Python unwinder debugging."), | |
627 | _("When non-zero, Python unwinder debugging is enabled."), | |
628 | NULL, | |
629 | NULL, | |
630 | &setdebuglist, &showdebuglist); | |
631 | pyuw_gdbarch_data | |
632 | = gdbarch_data_register_post_init (pyuw_gdbarch_data_init); | |
76727919 | 633 | gdb::observers::architecture_changed.attach (pyuw_on_new_gdbarch); |
d11916aa SS |
634 | |
635 | if (PyType_Ready (&pending_frame_object_type) < 0) | |
636 | return -1; | |
637 | rc = gdb_pymodule_addobject (gdb_module, "PendingFrame", | |
638 | (PyObject *) &pending_frame_object_type); | |
639 | if (rc) | |
640 | return rc; | |
641 | ||
642 | if (PyType_Ready (&unwind_info_object_type) < 0) | |
643 | return -1; | |
644 | return gdb_pymodule_addobject (gdb_module, "UnwindInfo", | |
645 | (PyObject *) &unwind_info_object_type); | |
646 | } | |
647 | ||
648 | static PyMethodDef pending_frame_object_methods[] = | |
649 | { | |
650 | { "read_register", pending_framepy_read_register, METH_VARARGS, | |
651 | "read_register (REG) -> gdb.Value\n" | |
652 | "Return the value of the REG in the frame." }, | |
653 | { "create_unwind_info", | |
654 | pending_framepy_create_unwind_info, METH_VARARGS, | |
655 | "create_unwind_info (FRAME_ID) -> gdb.UnwindInfo\n" | |
656 | "Construct UnwindInfo for this PendingFrame, using FRAME_ID\n" | |
657 | "to identify it." }, | |
87dbc774 AB |
658 | { "architecture", |
659 | pending_framepy_architecture, METH_NOARGS, | |
660 | "architecture () -> gdb.Architecture\n" | |
661 | "The architecture for this PendingFrame." }, | |
d11916aa SS |
662 | {NULL} /* Sentinel */ |
663 | }; | |
664 | ||
13fa0398 | 665 | PyTypeObject pending_frame_object_type = |
d11916aa SS |
666 | { |
667 | PyVarObject_HEAD_INIT (NULL, 0) | |
668 | "gdb.PendingFrame", /* tp_name */ | |
669 | sizeof (pending_frame_object), /* tp_basicsize */ | |
670 | 0, /* tp_itemsize */ | |
671 | 0, /* tp_dealloc */ | |
672 | 0, /* tp_print */ | |
673 | 0, /* tp_getattr */ | |
674 | 0, /* tp_setattr */ | |
675 | 0, /* tp_compare */ | |
676 | 0, /* tp_repr */ | |
677 | 0, /* tp_as_number */ | |
678 | 0, /* tp_as_sequence */ | |
679 | 0, /* tp_as_mapping */ | |
680 | 0, /* tp_hash */ | |
681 | 0, /* tp_call */ | |
682 | pending_framepy_str, /* tp_str */ | |
683 | 0, /* tp_getattro */ | |
684 | 0, /* tp_setattro */ | |
685 | 0, /* tp_as_buffer */ | |
686 | Py_TPFLAGS_DEFAULT, /* tp_flags */ | |
687 | "GDB PendingFrame object", /* tp_doc */ | |
688 | 0, /* tp_traverse */ | |
689 | 0, /* tp_clear */ | |
690 | 0, /* tp_richcompare */ | |
691 | 0, /* tp_weaklistoffset */ | |
692 | 0, /* tp_iter */ | |
693 | 0, /* tp_iternext */ | |
694 | pending_frame_object_methods, /* tp_methods */ | |
695 | 0, /* tp_members */ | |
696 | 0, /* tp_getset */ | |
697 | 0, /* tp_base */ | |
698 | 0, /* tp_dict */ | |
699 | 0, /* tp_descr_get */ | |
700 | 0, /* tp_descr_set */ | |
701 | 0, /* tp_dictoffset */ | |
702 | 0, /* tp_init */ | |
703 | 0, /* tp_alloc */ | |
704 | }; | |
705 | ||
706 | static PyMethodDef unwind_info_object_methods[] = | |
707 | { | |
708 | { "add_saved_register", | |
709 | unwind_infopy_add_saved_register, METH_VARARGS, | |
710 | "add_saved_register (REG, VALUE) -> None\n" | |
711 | "Set the value of the REG in the previous frame to VALUE." }, | |
712 | { NULL } /* Sentinel */ | |
713 | }; | |
714 | ||
13fa0398 | 715 | PyTypeObject unwind_info_object_type = |
d11916aa SS |
716 | { |
717 | PyVarObject_HEAD_INIT (NULL, 0) | |
718 | "gdb.UnwindInfo", /* tp_name */ | |
719 | sizeof (unwind_info_object), /* tp_basicsize */ | |
720 | 0, /* tp_itemsize */ | |
721 | unwind_infopy_dealloc, /* tp_dealloc */ | |
722 | 0, /* tp_print */ | |
723 | 0, /* tp_getattr */ | |
724 | 0, /* tp_setattr */ | |
725 | 0, /* tp_compare */ | |
726 | 0, /* tp_repr */ | |
727 | 0, /* tp_as_number */ | |
728 | 0, /* tp_as_sequence */ | |
729 | 0, /* tp_as_mapping */ | |
730 | 0, /* tp_hash */ | |
731 | 0, /* tp_call */ | |
732 | unwind_infopy_str, /* tp_str */ | |
733 | 0, /* tp_getattro */ | |
734 | 0, /* tp_setattro */ | |
735 | 0, /* tp_as_buffer */ | |
736 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ | |
737 | "GDB UnwindInfo object", /* tp_doc */ | |
738 | 0, /* tp_traverse */ | |
739 | 0, /* tp_clear */ | |
740 | 0, /* tp_richcompare */ | |
741 | 0, /* tp_weaklistoffset */ | |
742 | 0, /* tp_iter */ | |
743 | 0, /* tp_iternext */ | |
744 | unwind_info_object_methods, /* tp_methods */ | |
745 | 0, /* tp_members */ | |
746 | 0, /* tp_getset */ | |
747 | 0, /* tp_base */ | |
748 | 0, /* tp_dict */ | |
749 | 0, /* tp_descr_get */ | |
750 | 0, /* tp_descr_set */ | |
751 | 0, /* tp_dictoffset */ | |
752 | 0, /* tp_init */ | |
753 | 0, /* tp_alloc */ | |
754 | }; |