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 gdbpy_ref
<> 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
.get (), addr
);
188 _("The value of the '%s' attribute is not a pointer."),
195 /* Called by the Python interpreter to obtain string representation
196 of the UnwindInfo object. */
199 unwind_infopy_str (PyObject
*self
)
201 unwind_info_object
*unwind_info
= (unwind_info_object
*) self
;
204 stb
.puts ("Frame ID: ");
205 fprint_frame_id (&stb
, unwind_info
->frame_id
);
209 struct value_print_options opts
;
212 get_user_print_options (&opts
);
213 stb
.printf ("\nSaved registers: (");
214 for (i
= 0; VEC_iterate (saved_reg
, unwind_info
->saved_regs
, i
, reg
); i
++)
216 struct value
*value
= value_object_to_value (reg
->value
);
218 stb
.printf ("%s(%d, ", sep
, reg
->number
);
223 value_print (value
, &stb
, &opts
);
226 CATCH (except
, RETURN_MASK_ALL
)
228 GDB_PY_HANDLE_EXCEPTION (except
);
239 return PyString_FromString (stb
.c_str ());
242 /* Create UnwindInfo instance for given PendingFrame and frame ID.
243 Sets Python error and returns NULL on error. */
246 pyuw_create_unwind_info (PyObject
*pyo_pending_frame
,
247 struct frame_id frame_id
)
249 unwind_info_object
*unwind_info
250 = PyObject_New (unwind_info_object
, &unwind_info_object_type
);
252 if (((pending_frame_object
*) pyo_pending_frame
)->frame_info
== NULL
)
254 PyErr_SetString (PyExc_ValueError
,
255 "Attempting to use stale PendingFrame");
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
;
265 /* The implementation of
266 gdb.UnwindInfo.add_saved_register (REG, VALUE) -> None. */
269 unwind_infopy_add_saved_register (PyObject
*self
, PyObject
*args
)
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
;
278 if (pending_frame
->frame_info
== NULL
)
280 PyErr_SetString (PyExc_ValueError
,
281 "UnwindInfo instance refers to a stale PendingFrame");
284 if (!PyArg_UnpackTuple (args
, "previous_frame_register", 2, 2,
285 &pyo_reg_id
, &pyo_reg_value
))
287 if (!pyuw_parse_register_id (pending_frame
->gdbarch
, pyo_reg_id
, ®num
))
289 PyErr_SetString (PyExc_ValueError
, "Bad register");
296 if (pyo_reg_value
== NULL
297 || (value
= value_object_to_value (pyo_reg_value
)) == NULL
)
299 PyErr_SetString (PyExc_ValueError
, "Bad register value");
302 data_size
= register_size (pending_frame
->gdbarch
, regnum
);
303 if (data_size
!= TYPE_LENGTH (value_type (value
)))
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
);
318 for (i
= 0; VEC_iterate (saved_reg
, unwind_info
->saved_regs
, i
, reg
); i
++)
320 if (regnum
== reg
->number
)
322 Py_DECREF (reg
->value
);
328 reg
= VEC_safe_push (saved_reg
, unwind_info
->saved_regs
, NULL
);
329 reg
->number
= regnum
;
331 Py_INCREF (pyo_reg_value
);
332 reg
->value
= pyo_reg_value
;
337 /* UnwindInfo cleanup. */
340 unwind_infopy_dealloc (PyObject
*self
)
342 unwind_info_object
*unwind_info
= (unwind_info_object
*) self
;
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
);
353 /* Called by the Python interpreter to obtain string representation
354 of the PendingFrame object. */
357 pending_framepy_str (PyObject
*self
)
359 struct frame_info
*frame
= ((pending_frame_object
*) self
)->frame_info
;
360 const char *sp_str
= NULL
;
361 const char *pc_str
= NULL
;
364 return PyString_FromString ("Stale PendingFrame instance");
367 sp_str
= core_addr_to_string_nz (get_frame_sp (frame
));
368 pc_str
= core_addr_to_string_nz (get_frame_pc (frame
));
370 CATCH (except
, RETURN_MASK_ALL
)
372 GDB_PY_HANDLE_EXCEPTION (except
);
376 return PyString_FromFormat ("SP=%s,PC=%s", sp_str
, pc_str
);
379 /* Implementation of gdb.PendingFrame.read_register (self, reg) -> gdb.Value.
380 Returns the value of register REG as gdb.Value instance. */
383 pending_framepy_read_register (PyObject
*self
, PyObject
*args
)
385 pending_frame_object
*pending_frame
= (pending_frame_object
*) self
;
386 struct value
*val
= NULL
;
388 PyObject
*pyo_reg_id
;
390 if (pending_frame
->frame_info
== NULL
)
392 PyErr_SetString (PyExc_ValueError
,
393 "Attempting to read register from stale PendingFrame");
396 if (!PyArg_UnpackTuple (args
, "read_register", 1, 1, &pyo_reg_id
))
398 if (!pyuw_parse_register_id (pending_frame
->gdbarch
, pyo_reg_id
, ®num
))
400 PyErr_SetString (PyExc_ValueError
, "Bad register");
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
);
413 PyErr_Format (PyExc_ValueError
,
414 "Cannot read register %d from frame.",
417 CATCH (except
, RETURN_MASK_ALL
)
419 GDB_PY_HANDLE_EXCEPTION (except
);
423 return val
== NULL
? NULL
: value_to_value_object (val
);
427 PendingFrame.create_unwind_info (self, frameId) -> UnwindInfo. */
430 pending_framepy_create_unwind_info (PyObject
*self
, PyObject
*args
)
432 PyObject
*pyo_frame_id
;
437 if (!PyArg_ParseTuple (args
, "O:create_unwind_info", &pyo_frame_id
))
439 if (!pyuw_object_attribute_to_pointer (pyo_frame_id
, "sp", &sp
))
441 PyErr_SetString (PyExc_ValueError
,
442 _("frame_id should have 'sp' attribute."));
446 /* The logic of building frame_id depending on the attributes of
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)
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
));
460 return pyuw_create_unwind_info (self
,
461 frame_id_build_special (sp
, pc
, special
));
464 /* frame_unwind.this_id method. */
467 pyuw_this_id (struct frame_info
*this_frame
, void **cache_ptr
,
468 struct frame_id
*this_id
)
470 *this_id
= ((cached_frame_info
*) *cache_ptr
)->frame_id
;
473 fprintf_unfiltered (gdb_stdlog
, "%s: frame_id: ", __FUNCTION__
);
474 fprint_frame_id (gdb_stdlog
, *this_id
);
475 fprintf_unfiltered (gdb_stdlog
, "\n");
479 /* frame_unwind.prev_register. */
481 static struct value
*
482 pyuw_prev_register (struct frame_info
*this_frame
, void **cache_ptr
,
485 cached_frame_info
*cached_frame
= (cached_frame_info
*) *cache_ptr
;
486 struct reg_info
*reg_info
= cached_frame
->reg
;
487 struct reg_info
*reg_info_end
= reg_info
+ cached_frame
->reg_count
;
489 TRACE_PY_UNWIND (1, "%s (frame=%p,...,reg=%d)\n", __FUNCTION__
, this_frame
,
491 for (; reg_info
< reg_info_end
; ++reg_info
)
493 if (regnum
== reg_info
->number
)
494 return frame_unwind_got_bytes (this_frame
, regnum
, reg_info
->data
);
497 return frame_unwind_got_optimized (this_frame
, regnum
);
500 /* Frame sniffer dispatch. */
503 pyuw_sniffer (const struct frame_unwind
*self
, struct frame_info
*this_frame
,
506 struct gdbarch
*gdbarch
= (struct gdbarch
*) (self
->unwind_data
);
507 cached_frame_info
*cached_frame
;
509 gdbpy_enter
enter_py (gdbarch
, current_language
);
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
)));
515 /* Create PendingFrame instance to pass to sniffers. */
516 pending_frame_object
*pfo
= PyObject_New (pending_frame_object
,
517 &pending_frame_object_type
);
518 gdbpy_ref
<> pyo_pending_frame ((PyObject
*) pfo
);
519 if (pyo_pending_frame
== NULL
)
521 gdbpy_print_stack ();
524 pfo
->gdbarch
= gdbarch
;
525 scoped_restore invalidate_frame
= make_scoped_restore (&pfo
->frame_info
,
529 if (gdb_python_module
== NULL
530 || ! PyObject_HasAttrString (gdb_python_module
, "execute_unwinders"))
532 PyErr_SetString (PyExc_NameError
,
533 "Installation error: gdb.execute_unwinders function "
535 gdbpy_print_stack ();
538 gdbpy_ref
<> pyo_execute (PyObject_GetAttrString (gdb_python_module
,
539 "execute_unwinders"));
540 if (pyo_execute
== NULL
)
542 gdbpy_print_stack ();
546 gdbpy_ref
<> pyo_unwind_info
547 (PyObject_CallFunctionObjArgs (pyo_execute
.get (),
548 pyo_pending_frame
.get (), NULL
));
549 if (pyo_unwind_info
== NULL
)
551 gdbpy_print_stack ();
554 if (pyo_unwind_info
== Py_None
)
557 /* Received UnwindInfo, cache data. */
558 if (PyObject_IsInstance (pyo_unwind_info
.get (),
559 (PyObject
*) &unwind_info_object_type
) <= 0)
560 error (_("A Unwinder should return gdb.UnwindInfo instance."));
563 unwind_info_object
*unwind_info
=
564 (unwind_info_object
*) pyo_unwind_info
.get ();
565 int reg_count
= VEC_length (saved_reg
, unwind_info
->saved_regs
);
570 = ((cached_frame_info
*)
571 xmalloc (sizeof (*cached_frame
)
572 + reg_count
* sizeof (cached_frame
->reg
[0])));
573 cached_frame
->gdbarch
= gdbarch
;
574 cached_frame
->frame_id
= unwind_info
->frame_id
;
575 cached_frame
->reg_count
= reg_count
;
577 /* Populate registers array. */
578 for (i
= 0; VEC_iterate (saved_reg
, unwind_info
->saved_regs
, i
, reg
); i
++)
580 struct value
*value
= value_object_to_value (reg
->value
);
581 size_t data_size
= register_size (gdbarch
, reg
->number
);
583 cached_frame
->reg
[i
].number
= reg
->number
;
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
);
590 memcpy (cached_frame
->reg
[i
].data
, value_contents (value
), data_size
);
594 *cache_ptr
= cached_frame
;
598 /* Frame cache release shim. */
601 pyuw_dealloc_cache (struct frame_info
*this_frame
, void *cache
)
603 TRACE_PY_UNWIND (3, "%s: enter", __FUNCTION__
);
607 struct pyuw_gdbarch_data_type
609 /* Has the unwinder shim been prepended? */
610 int unwinder_registered
;
614 pyuw_gdbarch_data_init (struct gdbarch
*gdbarch
)
616 return GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct pyuw_gdbarch_data_type
);
619 /* New inferior architecture callback: register the Python unwinders
623 pyuw_on_new_gdbarch (struct gdbarch
*newarch
)
625 struct pyuw_gdbarch_data_type
*data
626 = (struct pyuw_gdbarch_data_type
*) gdbarch_data (newarch
,
629 if (!data
->unwinder_registered
)
631 struct frame_unwind
*unwinder
632 = GDBARCH_OBSTACK_ZALLOC (newarch
, struct frame_unwind
);
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
;
638 unwinder
->unwind_data
= (const struct frame_data
*) newarch
;
639 unwinder
->sniffer
= pyuw_sniffer
;
640 unwinder
->dealloc_cache
= pyuw_dealloc_cache
;
641 frame_unwind_prepend_unwinder (newarch
, unwinder
);
642 data
->unwinder_registered
= 1;
646 /* Initialize unwind machinery. */
649 gdbpy_initialize_unwind (void)
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."),
659 &setdebuglist
, &showdebuglist
);
661 = gdbarch_data_register_post_init (pyuw_gdbarch_data_init
);
662 observer_attach_architecture_changed (pyuw_on_new_gdbarch
);
664 if (PyType_Ready (&pending_frame_object_type
) < 0)
666 rc
= gdb_pymodule_addobject (gdb_module
, "PendingFrame",
667 (PyObject
*) &pending_frame_object_type
);
671 if (PyType_Ready (&unwind_info_object_type
) < 0)
673 return gdb_pymodule_addobject (gdb_module
, "UnwindInfo",
674 (PyObject
*) &unwind_info_object_type
);
677 static PyMethodDef pending_frame_object_methods
[] =
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"
687 {NULL
} /* Sentinel */
690 PyTypeObject pending_frame_object_type
=
692 PyVarObject_HEAD_INIT (NULL
, 0)
693 "gdb.PendingFrame", /* tp_name */
694 sizeof (pending_frame_object
), /* tp_basicsize */
702 0, /* tp_as_number */
703 0, /* tp_as_sequence */
704 0, /* tp_as_mapping */
707 pending_framepy_str
, /* tp_str */
710 0, /* tp_as_buffer */
711 Py_TPFLAGS_DEFAULT
, /* tp_flags */
712 "GDB PendingFrame object", /* tp_doc */
715 0, /* tp_richcompare */
716 0, /* tp_weaklistoffset */
719 pending_frame_object_methods
, /* tp_methods */
724 0, /* tp_descr_get */
725 0, /* tp_descr_set */
726 0, /* tp_dictoffset */
731 static PyMethodDef unwind_info_object_methods
[] =
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 */
740 PyTypeObject unwind_info_object_type
=
742 PyVarObject_HEAD_INIT (NULL
, 0)
743 "gdb.UnwindInfo", /* tp_name */
744 sizeof (unwind_info_object
), /* tp_basicsize */
746 unwind_infopy_dealloc
, /* tp_dealloc */
752 0, /* tp_as_number */
753 0, /* tp_as_sequence */
754 0, /* tp_as_mapping */
757 unwind_infopy_str
, /* tp_str */
760 0, /* tp_as_buffer */
761 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
762 "GDB UnwindInfo object", /* tp_doc */
765 0, /* tp_richcompare */
766 0, /* tp_weaklistoffset */
769 unwind_info_object_methods
, /* tp_methods */
774 0, /* tp_descr_get */
775 0, /* tp_descr_set */
776 0, /* tp_dictoffset */