1 /* Python frame unwinder interface.
3 Copyright (C) 2015-2016 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. */
77 /* Register number. */
80 /* Register data bytes pointer. */
81 gdb_byte data
[MAX_REGISTER_SIZE
];
87 struct frame_id frame_id
;
89 /* GDB Architecture. */
90 struct gdbarch
*gdbarch
;
92 /* Length of the `reg' array below. */
95 struct reg_info reg
[];
98 extern PyTypeObject pending_frame_object_type
99 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pending_frame_object");
101 extern PyTypeObject unwind_info_object_type
102 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("unwind_info_object");
104 static unsigned int pyuw_debug
= 0;
106 static struct gdbarch_data
*pyuw_gdbarch_data
;
108 /* Parses register id, which can be either a number or a name.
109 Returns 1 on success, 0 otherwise. */
112 pyuw_parse_register_id (struct gdbarch
*gdbarch
, PyObject
*pyo_reg_id
,
115 if (pyo_reg_id
== NULL
)
117 if (gdbpy_is_string (pyo_reg_id
))
119 const char *reg_name
= gdbpy_obj_to_string (pyo_reg_id
);
121 if (reg_name
== NULL
)
123 *reg_num
= user_reg_map_name_to_regnum (gdbarch
, reg_name
,
125 return *reg_num
>= 0;
127 else if (PyInt_Check (pyo_reg_id
))
130 if (gdb_py_int_as_long (pyo_reg_id
, &value
) && (int) value
== value
)
132 *reg_num
= (int) value
;
133 return user_reg_map_regnum_to_name (gdbarch
, *reg_num
) != NULL
;
139 /* Convert gdb.Value instance to inferior's pointer. Return 1 on success,
143 pyuw_value_obj_to_pointer (PyObject
*pyo_value
, CORE_ADDR
*addr
)
150 if ((value
= value_object_to_value (pyo_value
)) != NULL
)
152 *addr
= unpack_pointer (value_type (value
),
153 value_contents (value
));
157 CATCH (except
, RETURN_MASK_ALL
)
159 gdbpy_convert_exception (except
);
165 /* Get attribute from an object and convert it to the inferior's
166 pointer value. Return 1 if attribute exists and its value can be
167 converted. Otherwise, if attribute does not exist or its value is
168 None, return 0. In all other cases set Python error and return
172 pyuw_object_attribute_to_pointer (PyObject
*pyo
, const char *attr_name
,
177 if (PyObject_HasAttrString (pyo
, attr_name
))
179 PyObject
*pyo_value
= PyObject_GetAttrString (pyo
, attr_name
);
181 if (pyo_value
!= NULL
&& pyo_value
!= Py_None
)
183 rc
= pyuw_value_obj_to_pointer (pyo_value
, addr
);
187 _("The value of the '%s' attribute is not a pointer."),
190 Py_XDECREF (pyo_value
);
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: (");
216 i
< VEC_iterate (saved_reg
, unwind_info
->saved_regs
, i
, reg
);
219 struct value
*value
= value_object_to_value (reg
->value
);
221 fprintf_unfiltered (strfile
, "%s(%d, ", sep
, reg
->number
);
226 value_print (value
, strfile
, &opts
);
227 fprintf_unfiltered (strfile
, ")");
229 CATCH (except
, RETURN_MASK_ALL
)
231 GDB_PY_HANDLE_EXCEPTION (except
);
236 fprintf_unfiltered (strfile
, "<BAD>)");
239 fprintf_unfiltered (strfile
, ")");
242 char *s
= ui_file_xstrdup (strfile
, NULL
);
244 result
= PyString_FromString (s
);
247 ui_file_delete (strfile
);
251 /* Create UnwindInfo instance for given PendingFrame and frame ID.
252 Sets Python error and returns NULL on error. */
255 pyuw_create_unwind_info (PyObject
*pyo_pending_frame
,
256 struct frame_id frame_id
)
258 unwind_info_object
*unwind_info
259 = PyObject_New (unwind_info_object
, &unwind_info_object_type
);
261 if (((pending_frame_object
*) pyo_pending_frame
)->frame_info
== NULL
)
263 PyErr_SetString (PyExc_ValueError
,
264 "Attempting to use stale PendingFrame");
267 unwind_info
->frame_id
= frame_id
;
268 Py_INCREF (pyo_pending_frame
);
269 unwind_info
->pending_frame
= pyo_pending_frame
;
270 unwind_info
->saved_regs
= VEC_alloc (saved_reg
, 4);
271 return (PyObject
*) unwind_info
;
274 /* The implementation of
275 gdb.UnwindInfo.add_saved_register (REG, VALUE) -> None. */
278 unwind_infopy_add_saved_register (PyObject
*self
, PyObject
*args
)
280 unwind_info_object
*unwind_info
= (unwind_info_object
*) self
;
281 pending_frame_object
*pending_frame
282 = (pending_frame_object
*) (unwind_info
->pending_frame
);
283 PyObject
*pyo_reg_id
;
284 PyObject
*pyo_reg_value
;
287 if (pending_frame
->frame_info
== NULL
)
289 PyErr_SetString (PyExc_ValueError
,
290 "UnwindInfo instance refers to a stale PendingFrame");
293 if (!PyArg_UnpackTuple (args
, "previous_frame_register", 2, 2,
294 &pyo_reg_id
, &pyo_reg_value
))
296 if (!pyuw_parse_register_id (pending_frame
->gdbarch
, pyo_reg_id
, ®num
))
298 PyErr_SetString (PyExc_ValueError
, "Bad register");
305 if (pyo_reg_value
== NULL
306 || (value
= value_object_to_value (pyo_reg_value
)) == NULL
)
308 PyErr_SetString (PyExc_ValueError
, "Bad register value");
311 data_size
= register_size (pending_frame
->gdbarch
, regnum
);
312 if (data_size
!= TYPE_LENGTH (value_type (value
)))
316 "The value of the register returned by the Python "
317 "sniffer has unexpected size: %u instead of %u.",
318 (unsigned) TYPE_LENGTH (value_type (value
)),
319 (unsigned) data_size
);
327 for (i
= 0; VEC_iterate (saved_reg
, unwind_info
->saved_regs
, i
, reg
); i
++)
329 if (regnum
== reg
->number
)
331 Py_DECREF (reg
->value
);
337 reg
= VEC_safe_push (saved_reg
, unwind_info
->saved_regs
, NULL
);
338 reg
->number
= regnum
;
340 Py_INCREF (pyo_reg_value
);
341 reg
->value
= pyo_reg_value
;
346 /* UnwindInfo cleanup. */
349 unwind_infopy_dealloc (PyObject
*self
)
351 unwind_info_object
*unwind_info
= (unwind_info_object
*) self
;
355 Py_XDECREF (unwind_info
->pending_frame
);
356 for (i
= 0; VEC_iterate (saved_reg
, unwind_info
->saved_regs
, i
, reg
); i
++)
357 Py_DECREF (reg
->value
);
358 VEC_free (saved_reg
, unwind_info
->saved_regs
);
359 Py_TYPE (self
)->tp_free (self
);
362 /* Called by the Python interpreter to obtain string representation
363 of the PendingFrame object. */
366 pending_framepy_str (PyObject
*self
)
368 struct frame_info
*frame
= ((pending_frame_object
*) self
)->frame_info
;
369 const char *sp_str
= NULL
;
370 const char *pc_str
= NULL
;
373 return PyString_FromString ("Stale PendingFrame instance");
376 sp_str
= core_addr_to_string_nz (get_frame_sp (frame
));
377 pc_str
= core_addr_to_string_nz (get_frame_pc (frame
));
379 CATCH (except
, RETURN_MASK_ALL
)
381 GDB_PY_HANDLE_EXCEPTION (except
);
385 return PyString_FromFormat ("SP=%s,PC=%s", sp_str
, pc_str
);
388 /* Implementation of gdb.PendingFrame.read_register (self, reg) -> gdb.Value.
389 Returns the value of register REG as gdb.Value instance. */
392 pending_framepy_read_register (PyObject
*self
, PyObject
*args
)
394 pending_frame_object
*pending_frame
= (pending_frame_object
*) self
;
395 struct value
*val
= NULL
;
397 PyObject
*pyo_reg_id
;
399 if (pending_frame
->frame_info
== NULL
)
401 PyErr_SetString (PyExc_ValueError
,
402 "Attempting to read register from stale PendingFrame");
405 if (!PyArg_UnpackTuple (args
, "read_register", 1, 1, &pyo_reg_id
))
407 if (!pyuw_parse_register_id (pending_frame
->gdbarch
, pyo_reg_id
, ®num
))
409 PyErr_SetString (PyExc_ValueError
, "Bad register");
415 val
= get_frame_register_value (pending_frame
->frame_info
, regnum
);
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 /* Invalidate PendingFrame instance. */
471 pending_frame_invalidate (void *pyo_pending_frame
)
473 if (pyo_pending_frame
!= NULL
)
474 ((pending_frame_object
*) pyo_pending_frame
)->frame_info
= NULL
;
477 /* frame_unwind.this_id method. */
480 pyuw_this_id (struct frame_info
*this_frame
, void **cache_ptr
,
481 struct frame_id
*this_id
)
483 *this_id
= ((cached_frame_info
*) *cache_ptr
)->frame_id
;
486 fprintf_unfiltered (gdb_stdlog
, "%s: frame_id: ", __FUNCTION__
);
487 fprint_frame_id (gdb_stdlog
, *this_id
);
488 fprintf_unfiltered (gdb_stdlog
, "\n");
492 /* frame_unwind.prev_register. */
494 static struct value
*
495 pyuw_prev_register (struct frame_info
*this_frame
, void **cache_ptr
,
498 cached_frame_info
*cached_frame
= (cached_frame_info
*) *cache_ptr
;
499 struct reg_info
*reg_info
= cached_frame
->reg
;
500 struct reg_info
*reg_info_end
= reg_info
+ cached_frame
->reg_count
;
502 TRACE_PY_UNWIND (1, "%s (frame=%p,...,reg=%d)\n", __FUNCTION__
, this_frame
,
504 for (; reg_info
< reg_info_end
; ++reg_info
)
506 if (regnum
== reg_info
->number
)
507 return frame_unwind_got_bytes (this_frame
, regnum
, reg_info
->data
);
510 return frame_unwind_got_optimized (this_frame
, regnum
);
513 /* Frame sniffer dispatch. */
516 pyuw_sniffer (const struct frame_unwind
*self
, struct frame_info
*this_frame
,
519 struct gdbarch
*gdbarch
= (struct gdbarch
*) (self
->unwind_data
);
520 struct cleanup
*cleanups
= ensure_python_env (gdbarch
, current_language
);
521 PyObject
*pyo_execute
;
522 PyObject
*pyo_pending_frame
;
523 PyObject
*pyo_unwind_info
;
524 cached_frame_info
*cached_frame
;
526 TRACE_PY_UNWIND (3, "%s (SP=%s, PC=%s)\n", __FUNCTION__
,
527 paddress (gdbarch
, get_frame_sp (this_frame
)),
528 paddress (gdbarch
, get_frame_pc (this_frame
)));
530 /* Create PendingFrame instance to pass to sniffers. */
531 pyo_pending_frame
= (PyObject
*) PyObject_New (pending_frame_object
,
532 &pending_frame_object_type
);
533 if (pyo_pending_frame
== NULL
)
535 ((pending_frame_object
*) pyo_pending_frame
)->gdbarch
= gdbarch
;
536 ((pending_frame_object
*) pyo_pending_frame
)->frame_info
= this_frame
;
537 make_cleanup_py_decref (pyo_pending_frame
);
538 make_cleanup (pending_frame_invalidate
, (void *) pyo_pending_frame
);
541 if (gdb_python_module
== NULL
542 || ! PyObject_HasAttrString (gdb_python_module
, "execute_unwinders"))
544 PyErr_SetString (PyExc_NameError
,
545 "Installation error: gdb.execute_unwinders function "
549 pyo_execute
= PyObject_GetAttrString (gdb_python_module
, "execute_unwinders");
550 if (pyo_execute
== NULL
)
552 make_cleanup_py_decref (pyo_execute
);
554 = PyObject_CallFunctionObjArgs (pyo_execute
, pyo_pending_frame
, NULL
);
555 if (pyo_unwind_info
== NULL
)
557 make_cleanup_py_decref (pyo_unwind_info
);
558 if (pyo_unwind_info
== Py_None
)
561 /* Received UnwindInfo, cache data. */
562 if (PyObject_IsInstance (pyo_unwind_info
,
563 (PyObject
*) &unwind_info_object_type
) <= 0)
564 error (_("A Unwinder should return gdb.UnwindInfo instance."));
567 unwind_info_object
*unwind_info
= (unwind_info_object
*) pyo_unwind_info
;
568 int reg_count
= VEC_length (saved_reg
, unwind_info
->saved_regs
);
573 = ((cached_frame_info
*)
574 xmalloc (sizeof (*cached_frame
)
575 + reg_count
* sizeof (cached_frame
->reg
[0])));
576 cached_frame
->gdbarch
= gdbarch
;
577 cached_frame
->frame_id
= unwind_info
->frame_id
;
578 cached_frame
->reg_count
= reg_count
;
580 /* Populate registers array. */
581 for (i
= 0; VEC_iterate (saved_reg
, unwind_info
->saved_regs
, i
, reg
); i
++)
583 struct value
*value
= value_object_to_value (reg
->value
);
584 size_t data_size
= register_size (gdbarch
, reg
->number
);
586 cached_frame
->reg
[i
].number
= reg
->number
;
588 /* `value' validation was done before, just assert. */
589 gdb_assert (value
!= NULL
);
590 gdb_assert (data_size
== TYPE_LENGTH (value_type (value
)));
591 gdb_assert (data_size
<= MAX_REGISTER_SIZE
);
593 memcpy (cached_frame
->reg
[i
].data
, value_contents (value
), data_size
);
597 *cache_ptr
= cached_frame
;
598 do_cleanups (cleanups
);
602 gdbpy_print_stack ();
605 do_cleanups (cleanups
);
609 /* Frame cache release shim. */
612 pyuw_dealloc_cache (struct frame_info
*this_frame
, void *cache
)
614 TRACE_PY_UNWIND (3, "%s: enter", __FUNCTION__
);
618 struct pyuw_gdbarch_data_type
620 /* Has the unwinder shim been prepended? */
621 int unwinder_registered
;
625 pyuw_gdbarch_data_init (struct gdbarch
*gdbarch
)
627 return GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct pyuw_gdbarch_data_type
);
630 /* New inferior architecture callback: register the Python unwinders
634 pyuw_on_new_gdbarch (struct gdbarch
*newarch
)
636 struct pyuw_gdbarch_data_type
*data
637 = (struct pyuw_gdbarch_data_type
*) gdbarch_data (newarch
,
640 if (!data
->unwinder_registered
)
642 struct frame_unwind
*unwinder
643 = GDBARCH_OBSTACK_ZALLOC (newarch
, struct frame_unwind
);
645 unwinder
->type
= NORMAL_FRAME
;
646 unwinder
->stop_reason
= default_frame_unwind_stop_reason
;
647 unwinder
->this_id
= pyuw_this_id
;
648 unwinder
->prev_register
= pyuw_prev_register
;
649 unwinder
->unwind_data
= (const struct frame_data
*) newarch
;
650 unwinder
->sniffer
= pyuw_sniffer
;
651 unwinder
->dealloc_cache
= pyuw_dealloc_cache
;
652 frame_unwind_prepend_unwinder (newarch
, unwinder
);
653 data
->unwinder_registered
= 1;
657 /* Initialize unwind machinery. */
660 gdbpy_initialize_unwind (void)
663 add_setshow_zuinteger_cmd
664 ("py-unwind", class_maintenance
, &pyuw_debug
,
665 _("Set Python unwinder debugging."),
666 _("Show Python unwinder debugging."),
667 _("When non-zero, Python unwinder debugging is enabled."),
670 &setdebuglist
, &showdebuglist
);
672 = gdbarch_data_register_post_init (pyuw_gdbarch_data_init
);
673 observer_attach_architecture_changed (pyuw_on_new_gdbarch
);
675 if (PyType_Ready (&pending_frame_object_type
) < 0)
677 rc
= gdb_pymodule_addobject (gdb_module
, "PendingFrame",
678 (PyObject
*) &pending_frame_object_type
);
682 if (PyType_Ready (&unwind_info_object_type
) < 0)
684 return gdb_pymodule_addobject (gdb_module
, "UnwindInfo",
685 (PyObject
*) &unwind_info_object_type
);
688 static PyMethodDef pending_frame_object_methods
[] =
690 { "read_register", pending_framepy_read_register
, METH_VARARGS
,
691 "read_register (REG) -> gdb.Value\n"
692 "Return the value of the REG in the frame." },
693 { "create_unwind_info",
694 pending_framepy_create_unwind_info
, METH_VARARGS
,
695 "create_unwind_info (FRAME_ID) -> gdb.UnwindInfo\n"
696 "Construct UnwindInfo for this PendingFrame, using FRAME_ID\n"
698 {NULL
} /* Sentinel */
701 PyTypeObject pending_frame_object_type
=
703 PyVarObject_HEAD_INIT (NULL
, 0)
704 "gdb.PendingFrame", /* tp_name */
705 sizeof (pending_frame_object
), /* tp_basicsize */
713 0, /* tp_as_number */
714 0, /* tp_as_sequence */
715 0, /* tp_as_mapping */
718 pending_framepy_str
, /* tp_str */
721 0, /* tp_as_buffer */
722 Py_TPFLAGS_DEFAULT
, /* tp_flags */
723 "GDB PendingFrame object", /* tp_doc */
726 0, /* tp_richcompare */
727 0, /* tp_weaklistoffset */
730 pending_frame_object_methods
, /* tp_methods */
735 0, /* tp_descr_get */
736 0, /* tp_descr_set */
737 0, /* tp_dictoffset */
742 static PyMethodDef unwind_info_object_methods
[] =
744 { "add_saved_register",
745 unwind_infopy_add_saved_register
, METH_VARARGS
,
746 "add_saved_register (REG, VALUE) -> None\n"
747 "Set the value of the REG in the previous frame to VALUE." },
748 { NULL
} /* Sentinel */
751 PyTypeObject unwind_info_object_type
=
753 PyVarObject_HEAD_INIT (NULL
, 0)
754 "gdb.UnwindInfo", /* tp_name */
755 sizeof (unwind_info_object
), /* tp_basicsize */
757 unwind_infopy_dealloc
, /* tp_dealloc */
763 0, /* tp_as_number */
764 0, /* tp_as_sequence */
765 0, /* tp_as_mapping */
768 unwind_infopy_str
, /* tp_str */
771 0, /* tp_as_buffer */
772 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
773 "GDB UnwindInfo object", /* tp_doc */
776 0, /* tp_richcompare */
777 0, /* tp_weaklistoffset */
780 unwind_info_object_methods
, /* tp_methods */
785 0, /* tp_descr_get */
786 0, /* tp_descr_set */
787 0, /* tp_dictoffset */