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