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 struct ui_file
*strfile
= mem_fileopen ();
202 unwind_info_object
*unwind_info
= (unwind_info_object
*) self
;
205 fprintf_unfiltered (strfile
, "Frame ID: ");
206 fprint_frame_id (strfile
, unwind_info
->frame_id
);
210 struct value_print_options opts
;
213 get_user_print_options (&opts
);
214 fprintf_unfiltered (strfile
, "\nSaved registers: (");
215 for (i
= 0; VEC_iterate (saved_reg
, unwind_info
->saved_regs
, i
, reg
); i
++)
217 struct value
*value
= value_object_to_value (reg
->value
);
219 fprintf_unfiltered (strfile
, "%s(%d, ", sep
, reg
->number
);
224 value_print (value
, strfile
, &opts
);
225 fprintf_unfiltered (strfile
, ")");
227 CATCH (except
, RETURN_MASK_ALL
)
229 GDB_PY_HANDLE_EXCEPTION (except
);
234 fprintf_unfiltered (strfile
, "<BAD>)");
237 fprintf_unfiltered (strfile
, ")");
240 std::string s
= ui_file_as_string (strfile
);
241 result
= PyString_FromString (s
.c_str ());
242 ui_file_delete (strfile
);
246 /* Create UnwindInfo instance for given PendingFrame and frame ID.
247 Sets Python error and returns NULL on error. */
250 pyuw_create_unwind_info (PyObject
*pyo_pending_frame
,
251 struct frame_id frame_id
)
253 unwind_info_object
*unwind_info
254 = PyObject_New (unwind_info_object
, &unwind_info_object_type
);
256 if (((pending_frame_object
*) pyo_pending_frame
)->frame_info
== NULL
)
258 PyErr_SetString (PyExc_ValueError
,
259 "Attempting to use stale PendingFrame");
262 unwind_info
->frame_id
= frame_id
;
263 Py_INCREF (pyo_pending_frame
);
264 unwind_info
->pending_frame
= pyo_pending_frame
;
265 unwind_info
->saved_regs
= VEC_alloc (saved_reg
, 4);
266 return (PyObject
*) unwind_info
;
269 /* The implementation of
270 gdb.UnwindInfo.add_saved_register (REG, VALUE) -> None. */
273 unwind_infopy_add_saved_register (PyObject
*self
, PyObject
*args
)
275 unwind_info_object
*unwind_info
= (unwind_info_object
*) self
;
276 pending_frame_object
*pending_frame
277 = (pending_frame_object
*) (unwind_info
->pending_frame
);
278 PyObject
*pyo_reg_id
;
279 PyObject
*pyo_reg_value
;
282 if (pending_frame
->frame_info
== NULL
)
284 PyErr_SetString (PyExc_ValueError
,
285 "UnwindInfo instance refers to a stale PendingFrame");
288 if (!PyArg_UnpackTuple (args
, "previous_frame_register", 2, 2,
289 &pyo_reg_id
, &pyo_reg_value
))
291 if (!pyuw_parse_register_id (pending_frame
->gdbarch
, pyo_reg_id
, ®num
))
293 PyErr_SetString (PyExc_ValueError
, "Bad register");
300 if (pyo_reg_value
== NULL
301 || (value
= value_object_to_value (pyo_reg_value
)) == NULL
)
303 PyErr_SetString (PyExc_ValueError
, "Bad register value");
306 data_size
= register_size (pending_frame
->gdbarch
, regnum
);
307 if (data_size
!= TYPE_LENGTH (value_type (value
)))
311 "The value of the register returned by the Python "
312 "sniffer has unexpected size: %u instead of %u.",
313 (unsigned) TYPE_LENGTH (value_type (value
)),
314 (unsigned) data_size
);
322 for (i
= 0; VEC_iterate (saved_reg
, unwind_info
->saved_regs
, i
, reg
); i
++)
324 if (regnum
== reg
->number
)
326 Py_DECREF (reg
->value
);
332 reg
= VEC_safe_push (saved_reg
, unwind_info
->saved_regs
, NULL
);
333 reg
->number
= regnum
;
335 Py_INCREF (pyo_reg_value
);
336 reg
->value
= pyo_reg_value
;
341 /* UnwindInfo cleanup. */
344 unwind_infopy_dealloc (PyObject
*self
)
346 unwind_info_object
*unwind_info
= (unwind_info_object
*) self
;
350 Py_XDECREF (unwind_info
->pending_frame
);
351 for (i
= 0; VEC_iterate (saved_reg
, unwind_info
->saved_regs
, i
, reg
); i
++)
352 Py_DECREF (reg
->value
);
353 VEC_free (saved_reg
, unwind_info
->saved_regs
);
354 Py_TYPE (self
)->tp_free (self
);
357 /* Called by the Python interpreter to obtain string representation
358 of the PendingFrame object. */
361 pending_framepy_str (PyObject
*self
)
363 struct frame_info
*frame
= ((pending_frame_object
*) self
)->frame_info
;
364 const char *sp_str
= NULL
;
365 const char *pc_str
= NULL
;
368 return PyString_FromString ("Stale PendingFrame instance");
371 sp_str
= core_addr_to_string_nz (get_frame_sp (frame
));
372 pc_str
= core_addr_to_string_nz (get_frame_pc (frame
));
374 CATCH (except
, RETURN_MASK_ALL
)
376 GDB_PY_HANDLE_EXCEPTION (except
);
380 return PyString_FromFormat ("SP=%s,PC=%s", sp_str
, pc_str
);
383 /* Implementation of gdb.PendingFrame.read_register (self, reg) -> gdb.Value.
384 Returns the value of register REG as gdb.Value instance. */
387 pending_framepy_read_register (PyObject
*self
, PyObject
*args
)
389 pending_frame_object
*pending_frame
= (pending_frame_object
*) self
;
390 struct value
*val
= NULL
;
392 PyObject
*pyo_reg_id
;
394 if (pending_frame
->frame_info
== NULL
)
396 PyErr_SetString (PyExc_ValueError
,
397 "Attempting to read register from stale PendingFrame");
400 if (!PyArg_UnpackTuple (args
, "read_register", 1, 1, &pyo_reg_id
))
402 if (!pyuw_parse_register_id (pending_frame
->gdbarch
, pyo_reg_id
, ®num
))
404 PyErr_SetString (PyExc_ValueError
, "Bad register");
410 /* Fetch the value associated with a register, whether it's
411 a real register or a so called "user" register, like "pc",
412 which maps to a real register. In the past,
413 get_frame_register_value() was used here, which did not
414 handle the user register case. */
415 val
= value_of_register (regnum
, pending_frame
->frame_info
);
417 PyErr_Format (PyExc_ValueError
,
418 "Cannot read register %d from frame.",
421 CATCH (except
, RETURN_MASK_ALL
)
423 GDB_PY_HANDLE_EXCEPTION (except
);
427 return val
== NULL
? NULL
: value_to_value_object (val
);
431 PendingFrame.create_unwind_info (self, frameId) -> UnwindInfo. */
434 pending_framepy_create_unwind_info (PyObject
*self
, PyObject
*args
)
436 PyObject
*pyo_frame_id
;
441 if (!PyArg_ParseTuple (args
, "O:create_unwind_info", &pyo_frame_id
))
443 if (!pyuw_object_attribute_to_pointer (pyo_frame_id
, "sp", &sp
))
445 PyErr_SetString (PyExc_ValueError
,
446 _("frame_id should have 'sp' attribute."));
450 /* The logic of building frame_id depending on the attributes of
452 Has Has Has Function to call
453 'sp'? 'pc'? 'special'?
454 ------|------|--------------|-------------------------
455 Y N * frame_id_build_wild (sp)
456 Y Y N frame_id_build (sp, pc)
457 Y Y Y frame_id_build_special (sp, pc, special)
459 if (!pyuw_object_attribute_to_pointer (pyo_frame_id
, "pc", &pc
))
460 return pyuw_create_unwind_info (self
, frame_id_build_wild (sp
));
461 if (!pyuw_object_attribute_to_pointer (pyo_frame_id
, "special", &special
))
462 return pyuw_create_unwind_info (self
, frame_id_build (sp
, pc
));
464 return pyuw_create_unwind_info (self
,
465 frame_id_build_special (sp
, pc
, special
));
468 /* frame_unwind.this_id method. */
471 pyuw_this_id (struct frame_info
*this_frame
, void **cache_ptr
,
472 struct frame_id
*this_id
)
474 *this_id
= ((cached_frame_info
*) *cache_ptr
)->frame_id
;
477 fprintf_unfiltered (gdb_stdlog
, "%s: frame_id: ", __FUNCTION__
);
478 fprint_frame_id (gdb_stdlog
, *this_id
);
479 fprintf_unfiltered (gdb_stdlog
, "\n");
483 /* frame_unwind.prev_register. */
485 static struct value
*
486 pyuw_prev_register (struct frame_info
*this_frame
, void **cache_ptr
,
489 cached_frame_info
*cached_frame
= (cached_frame_info
*) *cache_ptr
;
490 struct reg_info
*reg_info
= cached_frame
->reg
;
491 struct reg_info
*reg_info_end
= reg_info
+ cached_frame
->reg_count
;
493 TRACE_PY_UNWIND (1, "%s (frame=%p,...,reg=%d)\n", __FUNCTION__
, this_frame
,
495 for (; reg_info
< reg_info_end
; ++reg_info
)
497 if (regnum
== reg_info
->number
)
498 return frame_unwind_got_bytes (this_frame
, regnum
, reg_info
->data
);
501 return frame_unwind_got_optimized (this_frame
, regnum
);
504 /* Frame sniffer dispatch. */
507 pyuw_sniffer (const struct frame_unwind
*self
, struct frame_info
*this_frame
,
510 struct gdbarch
*gdbarch
= (struct gdbarch
*) (self
->unwind_data
);
511 cached_frame_info
*cached_frame
;
513 gdbpy_enter
enter_py (gdbarch
, current_language
);
515 TRACE_PY_UNWIND (3, "%s (SP=%s, PC=%s)\n", __FUNCTION__
,
516 paddress (gdbarch
, get_frame_sp (this_frame
)),
517 paddress (gdbarch
, get_frame_pc (this_frame
)));
519 /* Create PendingFrame instance to pass to sniffers. */
520 pending_frame_object
*pfo
= PyObject_New (pending_frame_object
,
521 &pending_frame_object_type
);
522 gdbpy_ref
pyo_pending_frame ((PyObject
*) pfo
);
523 if (pyo_pending_frame
== NULL
)
525 gdbpy_print_stack ();
528 pfo
->gdbarch
= gdbarch
;
529 scoped_restore invalidate_frame
= make_scoped_restore (&pfo
->frame_info
,
533 if (gdb_python_module
== NULL
534 || ! PyObject_HasAttrString (gdb_python_module
, "execute_unwinders"))
536 PyErr_SetString (PyExc_NameError
,
537 "Installation error: gdb.execute_unwinders function "
539 gdbpy_print_stack ();
542 gdbpy_ref
pyo_execute (PyObject_GetAttrString (gdb_python_module
,
543 "execute_unwinders"));
544 if (pyo_execute
== NULL
)
546 gdbpy_print_stack ();
550 gdbpy_ref pyo_unwind_info
551 (PyObject_CallFunctionObjArgs (pyo_execute
.get (),
552 pyo_pending_frame
.get (), NULL
));
553 if (pyo_unwind_info
== NULL
)
555 gdbpy_print_stack ();
558 if (pyo_unwind_info
== Py_None
)
561 /* Received UnwindInfo, cache data. */
562 if (PyObject_IsInstance (pyo_unwind_info
.get (),
563 (PyObject
*) &unwind_info_object_type
) <= 0)
564 error (_("A Unwinder should return gdb.UnwindInfo instance."));
567 unwind_info_object
*unwind_info
=
568 (unwind_info_object
*) pyo_unwind_info
.get ();
569 int reg_count
= VEC_length (saved_reg
, unwind_info
->saved_regs
);
574 = ((cached_frame_info
*)
575 xmalloc (sizeof (*cached_frame
)
576 + reg_count
* sizeof (cached_frame
->reg
[0])));
577 cached_frame
->gdbarch
= gdbarch
;
578 cached_frame
->frame_id
= unwind_info
->frame_id
;
579 cached_frame
->reg_count
= reg_count
;
581 /* Populate registers array. */
582 for (i
= 0; VEC_iterate (saved_reg
, unwind_info
->saved_regs
, i
, reg
); i
++)
584 struct value
*value
= value_object_to_value (reg
->value
);
585 size_t data_size
= register_size (gdbarch
, reg
->number
);
587 cached_frame
->reg
[i
].number
= reg
->number
;
589 /* `value' validation was done before, just assert. */
590 gdb_assert (value
!= NULL
);
591 gdb_assert (data_size
== TYPE_LENGTH (value_type (value
)));
592 gdb_assert (data_size
<= MAX_REGISTER_SIZE
);
594 memcpy (cached_frame
->reg
[i
].data
, value_contents (value
), data_size
);
598 *cache_ptr
= cached_frame
;
602 /* Frame cache release shim. */
605 pyuw_dealloc_cache (struct frame_info
*this_frame
, void *cache
)
607 TRACE_PY_UNWIND (3, "%s: enter", __FUNCTION__
);
611 struct pyuw_gdbarch_data_type
613 /* Has the unwinder shim been prepended? */
614 int unwinder_registered
;
618 pyuw_gdbarch_data_init (struct gdbarch
*gdbarch
)
620 return GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct pyuw_gdbarch_data_type
);
623 /* New inferior architecture callback: register the Python unwinders
627 pyuw_on_new_gdbarch (struct gdbarch
*newarch
)
629 struct pyuw_gdbarch_data_type
*data
630 = (struct pyuw_gdbarch_data_type
*) gdbarch_data (newarch
,
633 if (!data
->unwinder_registered
)
635 struct frame_unwind
*unwinder
636 = GDBARCH_OBSTACK_ZALLOC (newarch
, struct frame_unwind
);
638 unwinder
->type
= NORMAL_FRAME
;
639 unwinder
->stop_reason
= default_frame_unwind_stop_reason
;
640 unwinder
->this_id
= pyuw_this_id
;
641 unwinder
->prev_register
= pyuw_prev_register
;
642 unwinder
->unwind_data
= (const struct frame_data
*) newarch
;
643 unwinder
->sniffer
= pyuw_sniffer
;
644 unwinder
->dealloc_cache
= pyuw_dealloc_cache
;
645 frame_unwind_prepend_unwinder (newarch
, unwinder
);
646 data
->unwinder_registered
= 1;
650 /* Initialize unwind machinery. */
653 gdbpy_initialize_unwind (void)
656 add_setshow_zuinteger_cmd
657 ("py-unwind", class_maintenance
, &pyuw_debug
,
658 _("Set Python unwinder debugging."),
659 _("Show Python unwinder debugging."),
660 _("When non-zero, Python unwinder debugging is enabled."),
663 &setdebuglist
, &showdebuglist
);
665 = gdbarch_data_register_post_init (pyuw_gdbarch_data_init
);
666 observer_attach_architecture_changed (pyuw_on_new_gdbarch
);
668 if (PyType_Ready (&pending_frame_object_type
) < 0)
670 rc
= gdb_pymodule_addobject (gdb_module
, "PendingFrame",
671 (PyObject
*) &pending_frame_object_type
);
675 if (PyType_Ready (&unwind_info_object_type
) < 0)
677 return gdb_pymodule_addobject (gdb_module
, "UnwindInfo",
678 (PyObject
*) &unwind_info_object_type
);
681 static PyMethodDef pending_frame_object_methods
[] =
683 { "read_register", pending_framepy_read_register
, METH_VARARGS
,
684 "read_register (REG) -> gdb.Value\n"
685 "Return the value of the REG in the frame." },
686 { "create_unwind_info",
687 pending_framepy_create_unwind_info
, METH_VARARGS
,
688 "create_unwind_info (FRAME_ID) -> gdb.UnwindInfo\n"
689 "Construct UnwindInfo for this PendingFrame, using FRAME_ID\n"
691 {NULL
} /* Sentinel */
694 PyTypeObject pending_frame_object_type
=
696 PyVarObject_HEAD_INIT (NULL
, 0)
697 "gdb.PendingFrame", /* tp_name */
698 sizeof (pending_frame_object
), /* tp_basicsize */
706 0, /* tp_as_number */
707 0, /* tp_as_sequence */
708 0, /* tp_as_mapping */
711 pending_framepy_str
, /* tp_str */
714 0, /* tp_as_buffer */
715 Py_TPFLAGS_DEFAULT
, /* tp_flags */
716 "GDB PendingFrame object", /* tp_doc */
719 0, /* tp_richcompare */
720 0, /* tp_weaklistoffset */
723 pending_frame_object_methods
, /* tp_methods */
728 0, /* tp_descr_get */
729 0, /* tp_descr_set */
730 0, /* tp_dictoffset */
735 static PyMethodDef unwind_info_object_methods
[] =
737 { "add_saved_register",
738 unwind_infopy_add_saved_register
, METH_VARARGS
,
739 "add_saved_register (REG, VALUE) -> None\n"
740 "Set the value of the REG in the previous frame to VALUE." },
741 { NULL
} /* Sentinel */
744 PyTypeObject unwind_info_object_type
=
746 PyVarObject_HEAD_INIT (NULL
, 0)
747 "gdb.UnwindInfo", /* tp_name */
748 sizeof (unwind_info_object
), /* tp_basicsize */
750 unwind_infopy_dealloc
, /* tp_dealloc */
756 0, /* tp_as_number */
757 0, /* tp_as_sequence */
758 0, /* tp_as_mapping */
761 unwind_infopy_str
, /* tp_str */
764 0, /* tp_as_buffer */
765 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
766 "GDB UnwindInfo object", /* tp_doc */
769 0, /* tp_richcompare */
770 0, /* tp_weaklistoffset */
773 unwind_info_object_methods
, /* tp_methods */
778 0, /* tp_descr_get */
779 0, /* tp_descr_set */
780 0, /* tp_dictoffset */