1 /* Python frame unwinder interface.
3 Copyright (C) 2015-2017 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"
33 #define TRACE_PY_UNWIND(level, args...) if (pyuw_debug >= level) \
34 { fprintf_unfiltered (gdb_stdlog, args); }
40 /* Frame we are unwinding. */
41 struct frame_info
*frame_info
;
43 /* Its architecture, passed by the sniffer caller. */
44 struct gdbarch
*gdbarch
;
45 } pending_frame_object
;
47 /* Saved registers array item. */
54 DEF_VEC_O (saved_reg
);
56 /* The data we keep for the PyUnwindInfo: pending_frame, saved registers
63 /* gdb.PendingFrame for the frame we are unwinding. */
64 PyObject
*pending_frame
;
67 struct frame_id frame_id
;
69 /* Saved registers array. */
70 VEC (saved_reg
) *saved_regs
;
73 /* The data we keep for a frame we can unwind: frame ID and an array of
74 (register_number, register_value) pairs. */
78 /* Register number. */
81 /* Register data bytes pointer. */
82 gdb_byte data
[MAX_REGISTER_SIZE
];
88 struct frame_id frame_id
;
90 /* GDB Architecture. */
91 struct gdbarch
*gdbarch
;
93 /* Length of the `reg' array below. */
96 struct reg_info reg
[];
99 extern PyTypeObject pending_frame_object_type
100 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pending_frame_object");
102 extern PyTypeObject unwind_info_object_type
103 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("unwind_info_object");
105 static unsigned int pyuw_debug
= 0;
107 static struct gdbarch_data
*pyuw_gdbarch_data
;
109 /* Parses register id, which can be either a number or a name.
110 Returns 1 on success, 0 otherwise. */
113 pyuw_parse_register_id (struct gdbarch
*gdbarch
, PyObject
*pyo_reg_id
,
116 if (pyo_reg_id
== NULL
)
118 if (gdbpy_is_string (pyo_reg_id
))
120 gdb::unique_xmalloc_ptr
<char> reg_name (gdbpy_obj_to_string (pyo_reg_id
));
122 if (reg_name
== NULL
)
124 *reg_num
= user_reg_map_name_to_regnum (gdbarch
, reg_name
.get (),
125 strlen (reg_name
.get ()));
126 return *reg_num
>= 0;
128 else if (PyInt_Check (pyo_reg_id
))
131 if (gdb_py_int_as_long (pyo_reg_id
, &value
) && (int) value
== value
)
133 *reg_num
= (int) value
;
134 return user_reg_map_regnum_to_name (gdbarch
, *reg_num
) != NULL
;
140 /* Convert gdb.Value instance to inferior's pointer. Return 1 on success,
144 pyuw_value_obj_to_pointer (PyObject
*pyo_value
, CORE_ADDR
*addr
)
151 if ((value
= value_object_to_value (pyo_value
)) != NULL
)
153 *addr
= unpack_pointer (value_type (value
),
154 value_contents (value
));
158 CATCH (except
, RETURN_MASK_ALL
)
160 gdbpy_convert_exception (except
);
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
173 pyuw_object_attribute_to_pointer (PyObject
*pyo
, const char *attr_name
,
178 if (PyObject_HasAttrString (pyo
, attr_name
))
180 PyObject
*pyo_value
= PyObject_GetAttrString (pyo
, attr_name
);
182 if (pyo_value
!= NULL
&& pyo_value
!= Py_None
)
184 rc
= pyuw_value_obj_to_pointer (pyo_value
, addr
);
188 _("The value of the '%s' attribute is not a pointer."),
191 Py_XDECREF (pyo_value
);
196 /* Called by the Python interpreter to obtain string representation
197 of the UnwindInfo object. */
200 unwind_infopy_str (PyObject
*self
)
202 struct ui_file
*strfile
= mem_fileopen ();
203 unwind_info_object
*unwind_info
= (unwind_info_object
*) self
;
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: (");
216 for (i
= 0; VEC_iterate (saved_reg
, unwind_info
->saved_regs
, i
, reg
); i
++)
218 struct value
*value
= value_object_to_value (reg
->value
);
220 fprintf_unfiltered (strfile
, "%s(%d, ", sep
, reg
->number
);
225 value_print (value
, strfile
, &opts
);
226 fprintf_unfiltered (strfile
, ")");
228 CATCH (except
, RETURN_MASK_ALL
)
230 GDB_PY_HANDLE_EXCEPTION (except
);
235 fprintf_unfiltered (strfile
, "<BAD>)");
238 fprintf_unfiltered (strfile
, ")");
241 std::string s
= ui_file_as_string (strfile
);
242 result
= PyString_FromString (s
.c_str ());
243 ui_file_delete (strfile
);
247 /* Create UnwindInfo instance for given PendingFrame and frame ID.
248 Sets Python error and returns NULL on error. */
251 pyuw_create_unwind_info (PyObject
*pyo_pending_frame
,
252 struct frame_id frame_id
)
254 unwind_info_object
*unwind_info
255 = PyObject_New (unwind_info_object
, &unwind_info_object_type
);
257 if (((pending_frame_object
*) pyo_pending_frame
)->frame_info
== NULL
)
259 PyErr_SetString (PyExc_ValueError
,
260 "Attempting to use stale PendingFrame");
263 unwind_info
->frame_id
= frame_id
;
264 Py_INCREF (pyo_pending_frame
);
265 unwind_info
->pending_frame
= pyo_pending_frame
;
266 unwind_info
->saved_regs
= VEC_alloc (saved_reg
, 4);
267 return (PyObject
*) unwind_info
;
270 /* The implementation of
271 gdb.UnwindInfo.add_saved_register (REG, VALUE) -> None. */
274 unwind_infopy_add_saved_register (PyObject
*self
, PyObject
*args
)
276 unwind_info_object
*unwind_info
= (unwind_info_object
*) self
;
277 pending_frame_object
*pending_frame
278 = (pending_frame_object
*) (unwind_info
->pending_frame
);
279 PyObject
*pyo_reg_id
;
280 PyObject
*pyo_reg_value
;
283 if (pending_frame
->frame_info
== NULL
)
285 PyErr_SetString (PyExc_ValueError
,
286 "UnwindInfo instance refers to a stale PendingFrame");
289 if (!PyArg_UnpackTuple (args
, "previous_frame_register", 2, 2,
290 &pyo_reg_id
, &pyo_reg_value
))
292 if (!pyuw_parse_register_id (pending_frame
->gdbarch
, pyo_reg_id
, ®num
))
294 PyErr_SetString (PyExc_ValueError
, "Bad register");
301 if (pyo_reg_value
== NULL
302 || (value
= value_object_to_value (pyo_reg_value
)) == NULL
)
304 PyErr_SetString (PyExc_ValueError
, "Bad register value");
307 data_size
= register_size (pending_frame
->gdbarch
, regnum
);
308 if (data_size
!= TYPE_LENGTH (value_type (value
)))
312 "The value of the register returned by the Python "
313 "sniffer has unexpected size: %u instead of %u.",
314 (unsigned) TYPE_LENGTH (value_type (value
)),
315 (unsigned) data_size
);
323 for (i
= 0; VEC_iterate (saved_reg
, unwind_info
->saved_regs
, i
, reg
); i
++)
325 if (regnum
== reg
->number
)
327 Py_DECREF (reg
->value
);
333 reg
= VEC_safe_push (saved_reg
, unwind_info
->saved_regs
, NULL
);
334 reg
->number
= regnum
;
336 Py_INCREF (pyo_reg_value
);
337 reg
->value
= pyo_reg_value
;
342 /* UnwindInfo cleanup. */
345 unwind_infopy_dealloc (PyObject
*self
)
347 unwind_info_object
*unwind_info
= (unwind_info_object
*) self
;
351 Py_XDECREF (unwind_info
->pending_frame
);
352 for (i
= 0; VEC_iterate (saved_reg
, unwind_info
->saved_regs
, i
, reg
); i
++)
353 Py_DECREF (reg
->value
);
354 VEC_free (saved_reg
, unwind_info
->saved_regs
);
355 Py_TYPE (self
)->tp_free (self
);
358 /* Called by the Python interpreter to obtain string representation
359 of the PendingFrame object. */
362 pending_framepy_str (PyObject
*self
)
364 struct frame_info
*frame
= ((pending_frame_object
*) self
)->frame_info
;
365 const char *sp_str
= NULL
;
366 const char *pc_str
= NULL
;
369 return PyString_FromString ("Stale PendingFrame instance");
372 sp_str
= core_addr_to_string_nz (get_frame_sp (frame
));
373 pc_str
= core_addr_to_string_nz (get_frame_pc (frame
));
375 CATCH (except
, RETURN_MASK_ALL
)
377 GDB_PY_HANDLE_EXCEPTION (except
);
381 return PyString_FromFormat ("SP=%s,PC=%s", sp_str
, pc_str
);
384 /* Implementation of gdb.PendingFrame.read_register (self, reg) -> gdb.Value.
385 Returns the value of register REG as gdb.Value instance. */
388 pending_framepy_read_register (PyObject
*self
, PyObject
*args
)
390 pending_frame_object
*pending_frame
= (pending_frame_object
*) self
;
391 struct value
*val
= NULL
;
393 PyObject
*pyo_reg_id
;
395 if (pending_frame
->frame_info
== NULL
)
397 PyErr_SetString (PyExc_ValueError
,
398 "Attempting to read register from stale PendingFrame");
401 if (!PyArg_UnpackTuple (args
, "read_register", 1, 1, &pyo_reg_id
))
403 if (!pyuw_parse_register_id (pending_frame
->gdbarch
, pyo_reg_id
, ®num
))
405 PyErr_SetString (PyExc_ValueError
, "Bad register");
411 /* Fetch the value associated with a register, whether it's
412 a real register or a so called "user" register, like "pc",
413 which maps to a real register. In the past,
414 get_frame_register_value() was used here, which did not
415 handle the user register case. */
416 val
= value_of_register (regnum
, pending_frame
->frame_info
);
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 /* frame_unwind.this_id method. */
472 pyuw_this_id (struct frame_info
*this_frame
, void **cache_ptr
,
473 struct frame_id
*this_id
)
475 *this_id
= ((cached_frame_info
*) *cache_ptr
)->frame_id
;
478 fprintf_unfiltered (gdb_stdlog
, "%s: frame_id: ", __FUNCTION__
);
479 fprint_frame_id (gdb_stdlog
, *this_id
);
480 fprintf_unfiltered (gdb_stdlog
, "\n");
484 /* frame_unwind.prev_register. */
486 static struct value
*
487 pyuw_prev_register (struct frame_info
*this_frame
, void **cache_ptr
,
490 cached_frame_info
*cached_frame
= (cached_frame_info
*) *cache_ptr
;
491 struct reg_info
*reg_info
= cached_frame
->reg
;
492 struct reg_info
*reg_info_end
= reg_info
+ cached_frame
->reg_count
;
494 TRACE_PY_UNWIND (1, "%s (frame=%p,...,reg=%d)\n", __FUNCTION__
, this_frame
,
496 for (; reg_info
< reg_info_end
; ++reg_info
)
498 if (regnum
== reg_info
->number
)
499 return frame_unwind_got_bytes (this_frame
, regnum
, reg_info
->data
);
502 return frame_unwind_got_optimized (this_frame
, regnum
);
505 /* Frame sniffer dispatch. */
508 pyuw_sniffer (const struct frame_unwind
*self
, struct frame_info
*this_frame
,
511 struct gdbarch
*gdbarch
= (struct gdbarch
*) (self
->unwind_data
);
512 cached_frame_info
*cached_frame
;
514 gdbpy_enter
enter_py (gdbarch
, current_language
);
516 TRACE_PY_UNWIND (3, "%s (SP=%s, PC=%s)\n", __FUNCTION__
,
517 paddress (gdbarch
, get_frame_sp (this_frame
)),
518 paddress (gdbarch
, get_frame_pc (this_frame
)));
520 /* Create PendingFrame instance to pass to sniffers. */
521 pending_frame_object
*pfo
= PyObject_New (pending_frame_object
,
522 &pending_frame_object_type
);
523 gdbpy_ref
pyo_pending_frame ((PyObject
*) pfo
);
524 if (pyo_pending_frame
== NULL
)
526 gdbpy_print_stack ();
529 pfo
->gdbarch
= gdbarch
;
530 scoped_restore invalidate_frame
= make_scoped_restore (&pfo
->frame_info
,
534 if (gdb_python_module
== NULL
535 || ! PyObject_HasAttrString (gdb_python_module
, "execute_unwinders"))
537 PyErr_SetString (PyExc_NameError
,
538 "Installation error: gdb.execute_unwinders function "
540 gdbpy_print_stack ();
543 gdbpy_ref
pyo_execute (PyObject_GetAttrString (gdb_python_module
,
544 "execute_unwinders"));
545 if (pyo_execute
== NULL
)
547 gdbpy_print_stack ();
551 gdbpy_ref pyo_unwind_info
552 (PyObject_CallFunctionObjArgs (pyo_execute
.get (),
553 pyo_pending_frame
.get (), NULL
));
554 if (pyo_unwind_info
== NULL
)
556 gdbpy_print_stack ();
559 if (pyo_unwind_info
== Py_None
)
562 /* Received UnwindInfo, cache data. */
563 if (PyObject_IsInstance (pyo_unwind_info
.get (),
564 (PyObject
*) &unwind_info_object_type
) <= 0)
565 error (_("A Unwinder should return gdb.UnwindInfo instance."));
568 unwind_info_object
*unwind_info
=
569 (unwind_info_object
*) pyo_unwind_info
.get ();
570 int reg_count
= VEC_length (saved_reg
, unwind_info
->saved_regs
);
575 = ((cached_frame_info
*)
576 xmalloc (sizeof (*cached_frame
)
577 + reg_count
* sizeof (cached_frame
->reg
[0])));
578 cached_frame
->gdbarch
= gdbarch
;
579 cached_frame
->frame_id
= unwind_info
->frame_id
;
580 cached_frame
->reg_count
= reg_count
;
582 /* Populate registers array. */
583 for (i
= 0; VEC_iterate (saved_reg
, unwind_info
->saved_regs
, i
, reg
); i
++)
585 struct value
*value
= value_object_to_value (reg
->value
);
586 size_t data_size
= register_size (gdbarch
, reg
->number
);
588 cached_frame
->reg
[i
].number
= reg
->number
;
590 /* `value' validation was done before, just assert. */
591 gdb_assert (value
!= NULL
);
592 gdb_assert (data_size
== TYPE_LENGTH (value_type (value
)));
593 gdb_assert (data_size
<= MAX_REGISTER_SIZE
);
595 memcpy (cached_frame
->reg
[i
].data
, value_contents (value
), data_size
);
599 *cache_ptr
= cached_frame
;
603 /* Frame cache release shim. */
606 pyuw_dealloc_cache (struct frame_info
*this_frame
, void *cache
)
608 TRACE_PY_UNWIND (3, "%s: enter", __FUNCTION__
);
612 struct pyuw_gdbarch_data_type
614 /* Has the unwinder shim been prepended? */
615 int unwinder_registered
;
619 pyuw_gdbarch_data_init (struct gdbarch
*gdbarch
)
621 return GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct pyuw_gdbarch_data_type
);
624 /* New inferior architecture callback: register the Python unwinders
628 pyuw_on_new_gdbarch (struct gdbarch
*newarch
)
630 struct pyuw_gdbarch_data_type
*data
631 = (struct pyuw_gdbarch_data_type
*) gdbarch_data (newarch
,
634 if (!data
->unwinder_registered
)
636 struct frame_unwind
*unwinder
637 = GDBARCH_OBSTACK_ZALLOC (newarch
, struct frame_unwind
);
639 unwinder
->type
= NORMAL_FRAME
;
640 unwinder
->stop_reason
= default_frame_unwind_stop_reason
;
641 unwinder
->this_id
= pyuw_this_id
;
642 unwinder
->prev_register
= pyuw_prev_register
;
643 unwinder
->unwind_data
= (const struct frame_data
*) newarch
;
644 unwinder
->sniffer
= pyuw_sniffer
;
645 unwinder
->dealloc_cache
= pyuw_dealloc_cache
;
646 frame_unwind_prepend_unwinder (newarch
, unwinder
);
647 data
->unwinder_registered
= 1;
651 /* Initialize unwind machinery. */
654 gdbpy_initialize_unwind (void)
657 add_setshow_zuinteger_cmd
658 ("py-unwind", class_maintenance
, &pyuw_debug
,
659 _("Set Python unwinder debugging."),
660 _("Show Python unwinder debugging."),
661 _("When non-zero, Python unwinder debugging is enabled."),
664 &setdebuglist
, &showdebuglist
);
666 = gdbarch_data_register_post_init (pyuw_gdbarch_data_init
);
667 observer_attach_architecture_changed (pyuw_on_new_gdbarch
);
669 if (PyType_Ready (&pending_frame_object_type
) < 0)
671 rc
= gdb_pymodule_addobject (gdb_module
, "PendingFrame",
672 (PyObject
*) &pending_frame_object_type
);
676 if (PyType_Ready (&unwind_info_object_type
) < 0)
678 return gdb_pymodule_addobject (gdb_module
, "UnwindInfo",
679 (PyObject
*) &unwind_info_object_type
);
682 static PyMethodDef pending_frame_object_methods
[] =
684 { "read_register", pending_framepy_read_register
, METH_VARARGS
,
685 "read_register (REG) -> gdb.Value\n"
686 "Return the value of the REG in the frame." },
687 { "create_unwind_info",
688 pending_framepy_create_unwind_info
, METH_VARARGS
,
689 "create_unwind_info (FRAME_ID) -> gdb.UnwindInfo\n"
690 "Construct UnwindInfo for this PendingFrame, using FRAME_ID\n"
692 {NULL
} /* Sentinel */
695 PyTypeObject pending_frame_object_type
=
697 PyVarObject_HEAD_INIT (NULL
, 0)
698 "gdb.PendingFrame", /* tp_name */
699 sizeof (pending_frame_object
), /* tp_basicsize */
707 0, /* tp_as_number */
708 0, /* tp_as_sequence */
709 0, /* tp_as_mapping */
712 pending_framepy_str
, /* tp_str */
715 0, /* tp_as_buffer */
716 Py_TPFLAGS_DEFAULT
, /* tp_flags */
717 "GDB PendingFrame object", /* tp_doc */
720 0, /* tp_richcompare */
721 0, /* tp_weaklistoffset */
724 pending_frame_object_methods
, /* tp_methods */
729 0, /* tp_descr_get */
730 0, /* tp_descr_set */
731 0, /* tp_dictoffset */
736 static PyMethodDef unwind_info_object_methods
[] =
738 { "add_saved_register",
739 unwind_infopy_add_saved_register
, METH_VARARGS
,
740 "add_saved_register (REG, VALUE) -> None\n"
741 "Set the value of the REG in the previous frame to VALUE." },
742 { NULL
} /* Sentinel */
745 PyTypeObject unwind_info_object_type
=
747 PyVarObject_HEAD_INIT (NULL
, 0)
748 "gdb.UnwindInfo", /* tp_name */
749 sizeof (unwind_info_object
), /* tp_basicsize */
751 unwind_infopy_dealloc
, /* tp_dealloc */
757 0, /* tp_as_number */
758 0, /* tp_as_sequence */
759 0, /* tp_as_mapping */
762 unwind_infopy_str
, /* tp_str */
765 0, /* tp_as_buffer */
766 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
767 "GDB UnwindInfo object", /* tp_doc */
770 0, /* tp_richcompare */
771 0, /* tp_weaklistoffset */
774 unwind_info_object_methods
, /* tp_methods */
779 0, /* tp_descr_get */
780 0, /* tp_descr_set */
781 0, /* tp_dictoffset */