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