gdb/python: add gdb.Frame.__repr__() method

Add a gdb.Frame.__repr__() method.  Before this patch we would see
output like this:

  (gdb) pi
  >>> gdb.selected_frame()
  <gdb.Frame object at 0x7fa8cc2df270>

After this patch, we now see:

  (gdb) pi
  >>> gdb.selected_frame()
  <gdb.Frame level=0 frame-id={stack=0x7ffff7da0ed0,code=0x000000000040115d,!special}>

More verbose, but, I hope, more useful.

If the gdb.Frame becomes invalid, then we will see:

  (gdb) pi
  >>> invalid_frame_variable
  <gdb.Frame (invalid)>

which is inline with how other invalid objects are displayed.

Approved-By: Tom Tromey <tom@tromey.com>
This commit is contained in:
Andrew Burgess
2024-01-04 10:57:40 +00:00
parent 1925bba80e
commit d6defe8761
2 changed files with 26 additions and 1 deletions

View File

@@ -84,6 +84,23 @@ frapy_str (PyObject *self)
return PyUnicode_FromString (fid.to_string ().c_str ());
}
/* Implement repr() for gdb.Frame. */
static PyObject *
frapy_repr (PyObject *self)
{
frame_object *frame_obj = (frame_object *) self;
frame_info_ptr f_info = frame_find_by_id (frame_obj->frame_id);
if (f_info == nullptr)
return gdb_py_invalid_object_repr (self);
const frame_id &fid = frame_obj->frame_id;
return PyUnicode_FromFormat ("<%s level=%d frame-id=%s>",
Py_TYPE (self)->tp_name,
frame_relative_level (f_info),
fid.to_string ().c_str ());
}
/* Implementation of gdb.Frame.is_valid (self) -> Boolean.
Returns True if the frame corresponding to the frame_id of this
object still exists in the inferior. */
@@ -840,7 +857,7 @@ PyTypeObject frame_object_type = {
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
frapy_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */