gdb/python: make more use of RegisterDescriptors

This commit unifies all of the Python register lookup code (used by
Frame.read_register, PendingFrame.read_register, and
gdb.UnwindInfo.add_saved_register), and adds support for using a
gdb.RegisterDescriptor for register lookup.

Currently the register unwind code (PendingFrame and UnwindInfo) allow
registers to be looked up either by name, or by GDB's internal
number.  I suspect the number was added for performance reasons, when
unwinding we don't want to repeatedly map from name to number for
every unwind.  However, this kind-of sucks, it means Python scripts
could include GDB's internal register numbers, and if we ever change
this numbering in the future users scripts will break in unexpected
ways.

Meanwhile, the Frame.read_register method only supports accessing
registers using a string, the register name.

This commit unifies all of the register to register-number lookup code
in our Python bindings, and adds a third choice into the mix, the use
of gdb.RegisterDescriptor.

The register descriptors can be looked up by name, but once looked up,
they contain GDB's register number, and so provide all of the
performance benefits of using a register number directly.  However, as
they are looked up by name we are no longer tightly binding the Python
API to GDB's internal numbering scheme.

As we may already have scripts in the wild that are using the register
numbers directly I have kept support for this in the API, but I have
listed this method last in the manual, and I have tried to stress that
this is NOT a good method to use and that users should use either a
string or register descriptor approach.

After this commit all existing Python code should function as before,
but users now have new options for how to identify registers.

gdb/ChangeLog:

	* python/py-frame.c: Remove 'user-regs.h' include.
	(frapy_read_register): Rewrite to make use of
	gdbpy_parse_register_id.
	* python/py-registers.c (gdbpy_parse_register_id): New function,
	moved here from python/py-unwind.c.  Updated the return type, and
	also accepts register descriptor objects.
	* python/py-unwind.c: Remove 'user-regs.h' include.
	(pyuw_parse_register_id): Moved to python/py-registers.c.
	(unwind_infopy_add_saved_register): Update to use
	gdbpy_parse_register_id.
	(pending_framepy_read_register): Likewise.
	* python/python-internal.h (gdbpy_parse_register_id): Declare.

gdb/testsuite/ChangeLog:

	* gdb.python/py-unwind.py: Update to make use of a register
	descriptor.

gdb/doc/ChangeLog:

	* python.texi (Unwinding Frames in Python): Update descriptions
	for PendingFrame.read_register and
	gdb.UnwindInfo.add_saved_register.
	(Frames In Python): Update description of Frame.read_register.
This commit is contained in:
Andrew Burgess 2020-07-22 12:13:11 +01:00
parent 14fa8fb307
commit 43d5901ded
9 changed files with 153 additions and 57 deletions

View file

@ -27,7 +27,6 @@
#include "python-internal.h"
#include "symfile.h"
#include "objfiles.h"
#include "user-regs.h"
typedef struct {
PyObject_HEAD
@ -242,12 +241,11 @@ frapy_pc (PyObject *self, PyObject *args)
static PyObject *
frapy_read_register (PyObject *self, PyObject *args)
{
const char *regnum_str;
PyObject *pyo_reg_id;
struct value *val = NULL;
if (!PyArg_ParseTuple (args, "s", &regnum_str))
if (!PyArg_UnpackTuple (args, "read_register", 1, 1, &pyo_reg_id))
return NULL;
try
{
struct frame_info *frame;
@ -255,14 +253,18 @@ frapy_read_register (PyObject *self, PyObject *args)
FRAPY_REQUIRE_VALID (self, frame);
regnum = user_reg_map_name_to_regnum (get_frame_arch (frame),
regnum_str,
strlen (regnum_str));
if (regnum >= 0)
val = value_of_register (regnum, frame);
if (!gdbpy_parse_register_id (get_frame_arch (frame), pyo_reg_id,
&regnum))
{
PyErr_SetString (PyExc_ValueError, "Bad register");
return NULL;
}
gdb_assert (regnum >= 0);
val = value_of_register (regnum, frame);
if (val == NULL)
PyErr_SetString (PyExc_ValueError, _("Unknown register."));
PyErr_SetString (PyExc_ValueError, _("Can't read register."));
}
catch (const gdb_exception &except)
{