binutils-gdb/gdb/python/py-function.c
Andrew Burgess 3965bff5b9 gdb/python: add mechanism to manage Python initialization functions
Currently, when we add a new python sub-system to GDB,
e.g. py-inferior.c, we end up having to create a new function like
gdbpy_initialize_inferior, which then has to be called from the
function do_start_initialization in python.c.

In some cases (py-micmd.c and py-tui.c), we have two functions
gdbpy_initialize_*, and gdbpy_finalize_*, with the second being called
from finalize_python which is also in python.c.

This commit proposes a mechanism to manage these initialization and
finalization calls, this means that adding a new Python subsystem will
no longer require changes to python.c or python-internal.h, instead,
the initialization and finalization functions will be registered
directly from the sub-system file, e.g. py-inferior.c, or py-micmd.c.

The initialization and finalization functions are managed through a
new class gdbpy_initialize_file in python-internal.h.  This class
contains a single global vector of all the initialization and
finalization functions.

In each Python sub-system we create a new gdbpy_initialize_file
object, the object constructor takes care of registering the two
callback functions.

Now from python.c we can call static functions on the
gdbpy_initialize_file class which take care of walking the callback
list and invoking each callback in turn.

To slightly simplify the Python sub-system files I added a new macro
GDBPY_INITIALIZE_FILE, which hides the need to create an object.  We
can now just do this:

  GDBPY_INITIALIZE_FILE (gdbpy_initialize_registers);

One possible problem with this change is that there is now no
guaranteed ordering of how the various sub-systems are initialized (or
finalized).  To try and avoid dependencies creeping in I have added a
use of the environment variable GDB_REVERSE_INIT_FUNCTIONS, this is
the same environment variable used in the generated init.c file.

Just like with init.c, when this environment variable is set we
reverse the list of Python initialization (and finalization)
functions.  As there is already a test that starts GDB with the
environment variable set then this should offer some level of
protection against dependencies creeping in - though for full
protection I guess we'd need to run all gdb.python/*.exp tests with
the variable set.

I have tested this patch with the environment variable set, and saw no
regressions, so I think we are fine right now.

One other change of note was for gdbpy_initialize_gdb_readline, this
function previously returned void.  In order to make this function
have the correct signature I've updated its return type to int, and we
now return 0 to indicate success.

All of the other initialize (and finalize) functions have been made
static within their respective sub-system files.

There should be no user visible changes after this commit.
2023-05-05 18:24:42 +01:00

192 lines
5.1 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* Convenience functions implemented in Python.
Copyright (C) 2008-2023 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
#include "value.h"
#include "python-internal.h"
#include "charset.h"
#include "gdbcmd.h"
#include "cli/cli-decode.h"
#include "completer.h"
#include "expression.h"
#include "language.h"
extern PyTypeObject fnpy_object_type
CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("PyObject");
/* Return a reference to a tuple ARGC elements long. Each element of the
tuple is a PyObject converted from the corresponding element of ARGV. */
static gdbpy_ref<>
convert_values_to_python (int argc, struct value **argv)
{
int i;
gdbpy_ref<> result (PyTuple_New (argc));
if (result == NULL)
return NULL;
for (i = 0; i < argc; ++i)
{
gdbpy_ref<> elt (value_to_value_object (argv[i]));
if (elt == NULL)
return NULL;
PyTuple_SetItem (result.get (), i, elt.release ());
}
return result;
}
/* Call a Python function object's invoke method. */
static struct value *
fnpy_call (struct gdbarch *gdbarch, const struct language_defn *language,
void *cookie, int argc, struct value **argv)
{
/* The gdbpy_enter object needs to be placed first, so that it's the last to
be destroyed. */
gdbpy_enter enter_py (gdbarch, language);
struct value *value;
gdbpy_ref<> result;
gdbpy_ref<> args = convert_values_to_python (argc, argv);
/* convert_values_to_python can return NULL on error. If we
encounter this, do not call the function, but allow the Python ->
error code conversion below to deal with the Python exception.
Note, that this is different if the function simply does not
have arguments. */
if (args != NULL)
{
gdbpy_ref<> callable (PyObject_GetAttrString ((PyObject *) cookie,
"invoke"));
if (callable == NULL)
error (_("No method named 'invoke' in object."));
result.reset (PyObject_Call (callable.get (), args.get (), NULL));
}
if (result == NULL)
gdbpy_handle_exception ();
value = convert_value_from_python (result.get ());
if (value == NULL)
{
gdbpy_print_stack ();
error (_("Error while executing Python code."));
}
return value;
}
/* Initializer for a Function object. It takes one argument, the name
of the function. */
static int
fnpy_init (PyObject *self, PyObject *args, PyObject *kwds)
{
const char *name;
gdb::unique_xmalloc_ptr<char> docstring;
if (! PyArg_ParseTuple (args, "s", &name))
return -1;
gdbpy_ref<> self_ref = gdbpy_ref<>::new_reference (self);
if (PyObject_HasAttrString (self, "__doc__"))
{
gdbpy_ref<> ds_obj (PyObject_GetAttrString (self, "__doc__"));
if (ds_obj != NULL)
{
if (gdbpy_is_string (ds_obj.get ()))
{
docstring = python_string_to_host_string (ds_obj.get ());
if (docstring == NULL)
return -1;
}
}
}
if (! docstring)
docstring.reset (xstrdup (_("This function is not documented.")));
add_internal_function (make_unique_xstrdup (name), std::move (docstring),
fnpy_call, self_ref.release ());
return 0;
}
/* Initialize internal function support. */
static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
gdbpy_initialize_functions (void)
{
fnpy_object_type.tp_new = PyType_GenericNew;
if (PyType_Ready (&fnpy_object_type) < 0)
return -1;
return gdb_pymodule_addobject (gdb_module, "Function",
(PyObject *) &fnpy_object_type);
}
GDBPY_INITIALIZE_FILE (gdbpy_initialize_functions);
PyTypeObject fnpy_object_type =
{
PyVarObject_HEAD_INIT (NULL, 0)
"gdb.Function", /*tp_name*/
sizeof (PyObject), /*tp_basicsize*/
0, /*tp_itemsize*/
0, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
"GDB function object", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
fnpy_init, /* tp_init */
0, /* tp_alloc */
};