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