1 /* Python frame unwinder interface.
3 Copyright (C) 2015 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "arch-utils.h"
22 #include "frame-unwind.h"
23 #include "gdb_obstack.h"
27 #include "python-internal.h"
30 #include "user-regs.h"
32 #define TRACE_PY_UNWIND(level, args...) if (pyuw_debug >= level) \
33 { fprintf_unfiltered (gdb_stdlog, args); }
39 /* Frame we are unwinding. */
40 struct frame_info
*frame_info
;
42 /* Its architecture, passed by the sniffer caller. */
43 struct gdbarch
*gdbarch
;
44 } pending_frame_object
;
46 /* Saved registers array item. */
53 DEF_VEC_O (saved_reg
);
55 /* The data we keep for the PyUnwindInfo: pending_frame, saved registers
62 /* gdb.PendingFrame for the frame we are unwinding. */
63 PyObject
*pending_frame
;
66 struct frame_id frame_id
;
68 /* Saved registers array. */
69 VEC (saved_reg
) *saved_regs
;
72 /* The data we keep for a frame we can unwind: frame ID and an array of
73 (register_number, register_value) pairs. */
78 struct frame_id frame_id
;
80 /* GDB Architecture. */
81 struct gdbarch
*gdbarch
;
83 /* Length of the `reg' array below. */
88 /* Register number. */
91 /* Register data bytes pointer. */
92 gdb_byte data
[MAX_REGISTER_SIZE
];
96 static PyTypeObject pending_frame_object_type
97 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pending_frame_object");
99 static PyTypeObject unwind_info_object_type
100 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("unwind_info_object");
102 static unsigned int pyuw_debug
= 0;
104 static struct gdbarch_data
*pyuw_gdbarch_data
;
106 /* Parses register id, which can be either a number or a name.
107 Returns 1 on success, 0 otherwise. */
110 pyuw_parse_register_id (struct gdbarch
*gdbarch
, PyObject
*pyo_reg_id
,
113 if (pyo_reg_id
== NULL
)
115 if (gdbpy_is_string (pyo_reg_id
))
117 const char *reg_name
= gdbpy_obj_to_string (pyo_reg_id
);
119 if (reg_name
== NULL
)
121 *reg_num
= user_reg_map_name_to_regnum (gdbarch
, reg_name
,
123 return *reg_num
>= 0;
125 else if (PyInt_Check (pyo_reg_id
))
128 if (gdb_py_int_as_long (pyo_reg_id
, &value
) && (int) value
== value
)
130 *reg_num
= (int) value
;
131 return user_reg_map_regnum_to_name (gdbarch
, *reg_num
) != NULL
;
137 /* Convert gdb.Value instance to inferior's pointer. Return 1 on success,
141 pyuw_value_obj_to_pointer (PyObject
*pyo_value
, CORE_ADDR
*addr
)
148 if ((value
= value_object_to_value (pyo_value
)) != NULL
)
150 *addr
= unpack_pointer (value_type (value
),
151 value_contents (value
));
155 CATCH (except
, RETURN_MASK_ALL
)
157 gdbpy_convert_exception (except
);
163 /* Get attribute from an object and convert it to the inferior's
164 pointer value. Return 1 if attribute exists and its value can be
165 converted. Otherwise, if attribute does not exist or its value is
166 None, return 0. In all other cases set Python error and return
170 pyuw_object_attribute_to_pointer (PyObject
*pyo
, const char *attr_name
,
175 if (PyObject_HasAttrString (pyo
, attr_name
))
177 PyObject
*pyo_value
= PyObject_GetAttrString (pyo
, attr_name
);
180 if (pyo_value
!= NULL
&& pyo_value
!= Py_None
)
182 rc
= pyuw_value_obj_to_pointer (pyo_value
, addr
);
186 _("The value of the '%s' attribute is not a pointer."),
189 Py_XDECREF (pyo_value
);
194 /* Called by the Python interpreter to obtain string representation
195 of the UnwindInfo object. */
198 unwind_infopy_str (PyObject
*self
)
200 struct ui_file
*strfile
= mem_fileopen ();
201 unwind_info_object
*unwind_info
= (unwind_info_object
*) self
;
202 pending_frame_object
*pending_frame
203 = (pending_frame_object
*) (unwind_info
->pending_frame
);
206 fprintf_unfiltered (strfile
, "Frame ID: ");
207 fprint_frame_id (strfile
, unwind_info
->frame_id
);
211 struct value_print_options opts
;
214 get_user_print_options (&opts
);
215 fprintf_unfiltered (strfile
, "\nSaved registers: (");
217 i
< VEC_iterate (saved_reg
, unwind_info
->saved_regs
, i
, reg
);
220 struct value
*value
= value_object_to_value (reg
->value
);
222 fprintf_unfiltered (strfile
, "%s(%d, ", sep
, reg
->number
);
227 value_print (value
, strfile
, &opts
);
228 fprintf_unfiltered (strfile
, ")");
230 CATCH (except
, RETURN_MASK_ALL
)
232 GDB_PY_HANDLE_EXCEPTION (except
);
237 fprintf_unfiltered (strfile
, "<BAD>)");
240 fprintf_unfiltered (strfile
, ")");
243 char *s
= ui_file_xstrdup (strfile
, NULL
);
245 result
= PyString_FromString (s
);
248 ui_file_delete (strfile
);
252 /* Create UnwindInfo instance for given PendingFrame and frame ID.
253 Sets Python error and returns NULL on error. */
256 pyuw_create_unwind_info (PyObject
*pyo_pending_frame
,
257 struct frame_id frame_id
)
259 unwind_info_object
*unwind_info
260 = PyObject_New (unwind_info_object
, &unwind_info_object_type
);
262 if (((pending_frame_object
*) pyo_pending_frame
)->frame_info
== NULL
)
264 PyErr_SetString (PyExc_ValueError
,
265 "Attempting to use stale PendingFrame");
268 unwind_info
->frame_id
= frame_id
;
269 Py_INCREF (pyo_pending_frame
);
270 unwind_info
->pending_frame
= pyo_pending_frame
;
271 unwind_info
->saved_regs
= VEC_alloc (saved_reg
, 4);
272 return (PyObject
*) unwind_info
;
275 /* The implementation of
276 gdb.UnwindInfo.add_saved_register (REG, VALUE) -> None. */
279 unwind_infopy_add_saved_register (PyObject
*self
, PyObject
*args
)
281 unwind_info_object
*unwind_info
= (unwind_info_object
*) self
;
282 pending_frame_object
*pending_frame
283 = (pending_frame_object
*) (unwind_info
->pending_frame
);
284 PyObject
*pyo_reg_id
;
285 PyObject
*pyo_reg_value
;
288 if (pending_frame
->frame_info
== NULL
)
290 PyErr_SetString (PyExc_ValueError
,
291 "UnwindInfo instance refers to a stale PendingFrame");
294 if (!PyArg_UnpackTuple (args
, "previous_frame_register", 2, 2,
295 &pyo_reg_id
, &pyo_reg_value
))
297 if (!pyuw_parse_register_id (pending_frame
->gdbarch
, pyo_reg_id
, ®num
))
299 PyErr_SetString (PyExc_ValueError
, "Bad register");
306 if (pyo_reg_value
== NULL
307 || (value
= value_object_to_value (pyo_reg_value
)) == NULL
)
309 PyErr_SetString (PyExc_ValueError
, "Bad register value");
312 data_size
= register_size (pending_frame
->gdbarch
, regnum
);
313 if (data_size
!= TYPE_LENGTH (value_type (value
)))
317 "The value of the register returned by the Python "
318 "sniffer has unexpected size: %u instead of %u.",
319 (unsigned) TYPE_LENGTH (value_type (value
)),
320 (unsigned) data_size
);
328 for (i
= 0; VEC_iterate (saved_reg
, unwind_info
->saved_regs
, i
, reg
); i
++)
330 if (regnum
== reg
->number
)
332 Py_DECREF (reg
->value
);
338 reg
= VEC_safe_push (saved_reg
, unwind_info
->saved_regs
, NULL
);
339 reg
->number
= regnum
;
341 Py_INCREF (pyo_reg_value
);
342 reg
->value
= pyo_reg_value
;
347 /* UnwindInfo cleanup. */
350 unwind_infopy_dealloc (PyObject
*self
)
352 unwind_info_object
*unwind_info
= (unwind_info_object
*) self
;
356 Py_XDECREF (unwind_info
->pending_frame
);
357 for (i
= 0; VEC_iterate (saved_reg
, unwind_info
->saved_regs
, i
, reg
); i
++)
358 Py_DECREF (reg
->value
);
359 VEC_free (saved_reg
, unwind_info
->saved_regs
);
360 Py_TYPE (self
)->tp_free (self
);
363 /* Called by the Python interpreter to obtain string representation
364 of the PendingFrame object. */
367 pending_framepy_str (PyObject
*self
)
369 struct frame_info
*frame
= ((pending_frame_object
*) self
)->frame_info
;
370 const char *sp_str
= NULL
;
371 const char *pc_str
= NULL
;
374 return PyString_FromString ("Stale PendingFrame instance");
377 sp_str
= core_addr_to_string_nz (get_frame_sp (frame
));
378 pc_str
= core_addr_to_string_nz (get_frame_pc (frame
));
380 CATCH (except
, RETURN_MASK_ALL
)
382 GDB_PY_HANDLE_EXCEPTION (except
);
386 return PyString_FromFormat ("SP=%s,PC=%s", sp_str
, pc_str
);
389 /* Implementation of gdb.PendingFrame.read_register (self, reg) -> gdb.Value.
390 Returns the value of register REG as gdb.Value instance. */
393 pending_framepy_read_register (PyObject
*self
, PyObject
*args
)
395 pending_frame_object
*pending_frame
= (pending_frame_object
*) self
;
396 struct value
*val
= NULL
;
398 PyObject
*pyo_reg_id
;
400 if (pending_frame
->frame_info
== NULL
)
402 PyErr_SetString (PyExc_ValueError
,
403 "Attempting to read register from stale PendingFrame");
406 if (!PyArg_UnpackTuple (args
, "read_register", 1, 1, &pyo_reg_id
))
408 if (!pyuw_parse_register_id (pending_frame
->gdbarch
, pyo_reg_id
, ®num
))
410 PyErr_SetString (PyExc_ValueError
, "Bad register");
416 val
= get_frame_register_value (pending_frame
->frame_info
, regnum
);
418 PyErr_Format (PyExc_ValueError
,
419 "Cannot read register %d from frame.",
422 CATCH (except
, RETURN_MASK_ALL
)
424 GDB_PY_HANDLE_EXCEPTION (except
);
428 return val
== NULL
? NULL
: value_to_value_object (val
);
432 PendingFrame.create_unwind_info (self, frameId) -> UnwindInfo. */
435 pending_framepy_create_unwind_info (PyObject
*self
, PyObject
*args
)
437 PyObject
*pyo_frame_id
;
442 if (!PyArg_ParseTuple (args
, "O:create_unwind_info", &pyo_frame_id
))
444 if (!pyuw_object_attribute_to_pointer (pyo_frame_id
, "sp", &sp
))
446 PyErr_SetString (PyExc_ValueError
,
447 _("frame_id should have 'sp' attribute."));
451 /* The logic of building frame_id depending on the attributes of
453 Has Has Has Function to call
454 'sp'? 'pc'? 'special'?
455 ------|------|--------------|-------------------------
456 Y N * frame_id_build_wild (sp)
457 Y Y N frame_id_build (sp, pc)
458 Y Y Y frame_id_build_special (sp, pc, special)
460 if (!pyuw_object_attribute_to_pointer (pyo_frame_id
, "pc", &pc
))
461 return pyuw_create_unwind_info (self
, frame_id_build_wild (sp
));
462 if (!pyuw_object_attribute_to_pointer (pyo_frame_id
, "special", &special
))
463 return pyuw_create_unwind_info (self
, frame_id_build (sp
, pc
));
465 return pyuw_create_unwind_info (self
,
466 frame_id_build_special (sp
, pc
, special
));
469 /* Invalidate PendingFrame instance. */
472 pending_frame_invalidate (void *pyo_pending_frame
)
474 if (pyo_pending_frame
!= NULL
)
475 ((pending_frame_object
*) pyo_pending_frame
)->frame_info
= NULL
;
478 /* frame_unwind.this_id method. */
481 pyuw_this_id (struct frame_info
*this_frame
, void **cache_ptr
,
482 struct frame_id
*this_id
)
484 *this_id
= ((cached_frame_info
*) *cache_ptr
)->frame_id
;
487 fprintf_unfiltered (gdb_stdlog
, "%s: frame_id: ", __FUNCTION__
);
488 fprint_frame_id (gdb_stdlog
, *this_id
);
489 fprintf_unfiltered (gdb_stdlog
, "\n");
493 /* frame_unwind.prev_register. */
495 static struct value
*
496 pyuw_prev_register (struct frame_info
*this_frame
, void **cache_ptr
,
499 cached_frame_info
*cached_frame
= *cache_ptr
;
500 struct reg_info
*reg_info
= cached_frame
->reg
;
501 struct reg_info
*reg_info_end
= reg_info
+ cached_frame
->reg_count
;
503 TRACE_PY_UNWIND (1, "%s (frame=%p,...,reg=%d)\n", __FUNCTION__
, this_frame
,
505 for (; reg_info
< reg_info_end
; ++reg_info
)
507 if (regnum
== reg_info
->number
)
508 return frame_unwind_got_bytes (this_frame
, regnum
, reg_info
->data
);
511 return frame_unwind_got_optimized (this_frame
, regnum
);
514 /* Frame sniffer dispatch. */
517 pyuw_sniffer (const struct frame_unwind
*self
, struct frame_info
*this_frame
,
520 struct gdbarch
*gdbarch
= (struct gdbarch
*) (self
->unwind_data
);
521 struct cleanup
*cleanups
= ensure_python_env (gdbarch
, current_language
);
522 PyObject
*pyo_execute
;
523 PyObject
*pyo_pending_frame
;
524 PyObject
*pyo_unwind_info
;
525 cached_frame_info
*cached_frame
;
527 TRACE_PY_UNWIND (3, "%s (SP=%s, PC=%s)\n", __FUNCTION__
,
528 paddress (gdbarch
, get_frame_sp (this_frame
)),
529 paddress (gdbarch
, get_frame_pc (this_frame
)));
531 /* Create PendingFrame instance to pass to sniffers. */
532 pyo_pending_frame
= (PyObject
*) PyObject_New (pending_frame_object
,
533 &pending_frame_object_type
);
534 if (pyo_pending_frame
== NULL
)
536 ((pending_frame_object
*) pyo_pending_frame
)->gdbarch
= gdbarch
;
537 ((pending_frame_object
*) pyo_pending_frame
)->frame_info
= this_frame
;
538 make_cleanup (pending_frame_invalidate
, (void *) pyo_pending_frame
);
539 make_cleanup_py_decref (pyo_pending_frame
);
542 if (gdb_python_module
== NULL
543 || ! PyObject_HasAttrString (gdb_python_module
, "execute_unwinders"))
545 PyErr_SetString (PyExc_NameError
,
546 "Installation error: gdb.execute_unwinders function "
550 pyo_execute
= PyObject_GetAttrString (gdb_python_module
, "execute_unwinders");
551 if (pyo_execute
== NULL
)
553 make_cleanup_py_decref (pyo_execute
);
555 = PyObject_CallFunctionObjArgs (pyo_execute
, pyo_pending_frame
, NULL
);
556 if (pyo_unwind_info
== NULL
)
558 make_cleanup_py_decref (pyo_unwind_info
);
559 if (pyo_unwind_info
== Py_None
)
562 /* Received UnwindInfo, cache data. */
563 if (PyObject_IsInstance (pyo_unwind_info
,
564 (PyObject
*) &unwind_info_object_type
) <= 0)
565 error (_("A Unwinder should return gdb.UnwindInfo instance."));
568 unwind_info_object
*unwind_info
= (unwind_info_object
*) pyo_unwind_info
;
569 int reg_count
= VEC_length (saved_reg
, unwind_info
->saved_regs
);
573 cached_frame
= xmalloc (sizeof (*cached_frame
) +
574 reg_count
* sizeof (cached_frame
->reg
[0]));
575 cached_frame
->gdbarch
= gdbarch
;
576 cached_frame
->frame_id
= unwind_info
->frame_id
;
577 cached_frame
->reg_count
= reg_count
;
579 /* Populate registers array. */
580 for (i
= 0; VEC_iterate (saved_reg
, unwind_info
->saved_regs
, i
, reg
); i
++)
582 struct value
*value
= value_object_to_value (reg
->value
);
583 size_t data_size
= register_size (gdbarch
, reg
->number
);
585 cached_frame
->reg
[i
].number
= reg
->number
;
587 /* `value' validation was done before, just assert. */
588 gdb_assert (value
!= NULL
);
589 gdb_assert (data_size
== TYPE_LENGTH (value_type (value
)));
590 gdb_assert (data_size
<= MAX_REGISTER_SIZE
);
592 memcpy (cached_frame
->reg
[i
].data
, value_contents (value
), data_size
);
596 *cache_ptr
= cached_frame
;
597 do_cleanups (cleanups
);
601 gdbpy_print_stack ();
604 do_cleanups (cleanups
);
608 /* Frame cache release shim. */
611 pyuw_dealloc_cache (struct frame_info
*this_frame
, void *cache
)
613 TRACE_PY_UNWIND (3, "%s: enter", __FUNCTION__
);
617 struct pyuw_gdbarch_data_type
619 /* Has the unwinder shim been prepended? */
620 int unwinder_registered
;
624 pyuw_gdbarch_data_init (struct gdbarch
*gdbarch
)
626 return GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct pyuw_gdbarch_data_type
);
629 /* New inferior architecture callback: register the Python unwinders
633 pyuw_on_new_gdbarch (struct gdbarch
*newarch
)
635 struct pyuw_gdbarch_data_type
*data
=
636 gdbarch_data (newarch
, pyuw_gdbarch_data
);
638 if (!data
->unwinder_registered
)
640 struct frame_unwind
*unwinder
641 = GDBARCH_OBSTACK_ZALLOC (newarch
, struct frame_unwind
);
643 unwinder
->type
= NORMAL_FRAME
;
644 unwinder
->stop_reason
= default_frame_unwind_stop_reason
;
645 unwinder
->this_id
= pyuw_this_id
;
646 unwinder
->prev_register
= pyuw_prev_register
;
647 unwinder
->unwind_data
= (void *) newarch
;
648 unwinder
->sniffer
= pyuw_sniffer
;
649 unwinder
->dealloc_cache
= pyuw_dealloc_cache
;
650 frame_unwind_prepend_unwinder (newarch
, unwinder
);
651 data
->unwinder_registered
= 1;
655 /* Initialize unwind machinery. */
658 gdbpy_initialize_unwind (void)
661 add_setshow_zuinteger_cmd
662 ("py-unwind", class_maintenance
, &pyuw_debug
,
663 _("Set Python unwinder debugging."),
664 _("Show Python unwinder debugging."),
665 _("When non-zero, Python unwinder debugging is enabled."),
668 &setdebuglist
, &showdebuglist
);
670 = gdbarch_data_register_post_init (pyuw_gdbarch_data_init
);
671 observer_attach_architecture_changed (pyuw_on_new_gdbarch
);
673 if (PyType_Ready (&pending_frame_object_type
) < 0)
675 rc
= gdb_pymodule_addobject (gdb_module
, "PendingFrame",
676 (PyObject
*) &pending_frame_object_type
);
680 if (PyType_Ready (&unwind_info_object_type
) < 0)
682 return gdb_pymodule_addobject (gdb_module
, "UnwindInfo",
683 (PyObject
*) &unwind_info_object_type
);
686 static PyMethodDef pending_frame_object_methods
[] =
688 { "read_register", pending_framepy_read_register
, METH_VARARGS
,
689 "read_register (REG) -> gdb.Value\n"
690 "Return the value of the REG in the frame." },
691 { "create_unwind_info",
692 pending_framepy_create_unwind_info
, METH_VARARGS
,
693 "create_unwind_info (FRAME_ID) -> gdb.UnwindInfo\n"
694 "Construct UnwindInfo for this PendingFrame, using FRAME_ID\n"
696 {NULL
} /* Sentinel */
699 static PyTypeObject pending_frame_object_type
=
701 PyVarObject_HEAD_INIT (NULL
, 0)
702 "gdb.PendingFrame", /* tp_name */
703 sizeof (pending_frame_object
), /* tp_basicsize */
711 0, /* tp_as_number */
712 0, /* tp_as_sequence */
713 0, /* tp_as_mapping */
716 pending_framepy_str
, /* tp_str */
719 0, /* tp_as_buffer */
720 Py_TPFLAGS_DEFAULT
, /* tp_flags */
721 "GDB PendingFrame object", /* tp_doc */
724 0, /* tp_richcompare */
725 0, /* tp_weaklistoffset */
728 pending_frame_object_methods
, /* tp_methods */
733 0, /* tp_descr_get */
734 0, /* tp_descr_set */
735 0, /* tp_dictoffset */
740 static PyMethodDef unwind_info_object_methods
[] =
742 { "add_saved_register",
743 unwind_infopy_add_saved_register
, METH_VARARGS
,
744 "add_saved_register (REG, VALUE) -> None\n"
745 "Set the value of the REG in the previous frame to VALUE." },
746 { NULL
} /* Sentinel */
749 static PyTypeObject unwind_info_object_type
=
751 PyVarObject_HEAD_INIT (NULL
, 0)
752 "gdb.UnwindInfo", /* tp_name */
753 sizeof (unwind_info_object
), /* tp_basicsize */
755 unwind_infopy_dealloc
, /* tp_dealloc */
761 0, /* tp_as_number */
762 0, /* tp_as_sequence */
763 0, /* tp_as_mapping */
766 unwind_infopy_str
, /* tp_str */
769 0, /* tp_as_buffer */
770 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
771 "GDB UnwindInfo object", /* tp_doc */
774 0, /* tp_richcompare */
775 0, /* tp_weaklistoffset */
778 unwind_info_object_methods
, /* tp_methods */
783 0, /* tp_descr_get */
784 0, /* tp_descr_set */
785 0, /* tp_dictoffset */