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.
This commit is contained in:
parent
a5d3f94c27
commit
3965bff5b9
35 changed files with 254 additions and 158 deletions
|
@ -488,77 +488,120 @@ struct gdbarch *arch_object_to_gdbarch (PyObject *obj);
|
|||
|
||||
extern struct program_space *progspace_object_to_program_space (PyObject *obj);
|
||||
|
||||
void gdbpy_initialize_gdb_readline (void);
|
||||
int gdbpy_initialize_auto_load (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_values (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_frames (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_instruction (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_btrace (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_record (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_symtabs (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_commands (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_symbols (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_symtabs (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_blocks (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_types (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_functions (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_pspace (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_objfile (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_breakpoints (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_breakpoint_locations ()
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_finishbreakpoints (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_lazy_string (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_linetable (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_parameters (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_thread (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_inferior (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_eventregistry (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_event (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_arch (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_registers ()
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_xmethods (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_unwind (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_tui ()
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
void gdbpy_finalize_tui ();
|
||||
int gdbpy_initialize_membuf ()
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_connection ()
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
int gdbpy_initialize_micommands (void)
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
void gdbpy_finalize_micommands ();
|
||||
int gdbpy_initialize_disasm ()
|
||||
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
|
||||
/* A class for managing the initialization, and finalization functions
|
||||
from all Python files (e.g. gdb/python/py-*.c).
|
||||
|
||||
Within any Python file, create an instance of this class, passing in
|
||||
the initialization function, and, optionally, the finalization
|
||||
function.
|
||||
|
||||
These functions are added to a single global list of functions, which
|
||||
can then be called from do_start_initialization and finalize_python
|
||||
(see python.c) to initialize all the Python files within GDB. */
|
||||
|
||||
class gdbpy_initialize_file
|
||||
{
|
||||
/* The type of a function that can be called just after GDB has setup the
|
||||
Python interpreter. This function will setup any additional Python
|
||||
state required by a particular subsystem. Return 0 if the setup was
|
||||
successful, or return -1 if setup failed, in which case a Python
|
||||
exception should have been raised. */
|
||||
|
||||
using gdbpy_initialize_file_ftype = int (*) (void);
|
||||
|
||||
/* The type of a function that can be called just before GDB shuts down
|
||||
the Python interpreter. This function can cleanup an Python state
|
||||
that is cached within GDB, for example, if GDB is holding any
|
||||
references to Python objects, these should be released before the
|
||||
Python interpreter is shut down.
|
||||
|
||||
There is no error return in this case. This function is only called
|
||||
when GDB is already shutting down. The function should make a best
|
||||
effort to clean up, and then return. */
|
||||
|
||||
using gdbpy_finalize_file_ftype = void (*) (void);
|
||||
|
||||
/* The type for an initialization and finalization function pair. */
|
||||
|
||||
using callback_pair_t = std::pair<gdbpy_initialize_file_ftype,
|
||||
gdbpy_finalize_file_ftype>;
|
||||
|
||||
/* Return the vector of callbacks. The vector is defined as a static
|
||||
variable within this function so that it will be initialized the first
|
||||
time this function is called. This is important, as this function is
|
||||
called as part of the global object initialization process; if the
|
||||
vector was a static variable within this class then we could not
|
||||
guarantee that it had been initialized before it was used. */
|
||||
|
||||
static std::vector<callback_pair_t> &
|
||||
callbacks ()
|
||||
{
|
||||
static std::vector<callback_pair_t> list;
|
||||
return list;
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
/* Register the initialization (INIT) and finalization (FINI) functions
|
||||
for a Python file. See the comments on the function types above for
|
||||
when these functions will be called.
|
||||
|
||||
Either of these functions can be nullptr, in which case no function
|
||||
will be called.
|
||||
|
||||
The FINI argument is optional, and defaults to nullptr (no function to
|
||||
call). */
|
||||
|
||||
gdbpy_initialize_file (gdbpy_initialize_file_ftype init,
|
||||
gdbpy_finalize_file_ftype fini = nullptr)
|
||||
{
|
||||
callbacks ().emplace_back (init, fini);
|
||||
}
|
||||
|
||||
/* Run all the Python file initialize functions and return true. If any
|
||||
of the initialize functions fails then this function returns false.
|
||||
In the case of failure it is undefined how many of the initialize
|
||||
functions will have been called. */
|
||||
|
||||
static bool
|
||||
initialize_all ()
|
||||
{
|
||||
/* The initialize_all function should only be called once. The
|
||||
following check reverses the global list, which will effect this
|
||||
initialize_all call, as well as the later finalize_all call.
|
||||
|
||||
The environment variable checked here is the same as the one checked
|
||||
in the generated init.c file. */
|
||||
if (getenv ("GDB_REVERSE_INIT_FUNCTIONS") != nullptr)
|
||||
std::reverse (callbacks ().begin (), callbacks ().end ());
|
||||
|
||||
for (const auto &p : gdbpy_initialize_file::callbacks ())
|
||||
{
|
||||
if (p.first != nullptr && p.first () < 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Run all the Python file finalize functions. */
|
||||
|
||||
static void
|
||||
finalize_all ()
|
||||
{
|
||||
for (const auto &p : gdbpy_initialize_file::callbacks ())
|
||||
{
|
||||
if (p.second != nullptr)
|
||||
p.second ();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/* Macro to simplify registering the initialization and finalization
|
||||
functions for a Python file. */
|
||||
|
||||
#define GDBPY_INITIALIZE_FILE(INIT, ...) \
|
||||
static gdbpy_initialize_file \
|
||||
CONCAT(gdbpy_initialize_file_obj_, __LINE__) (INIT, ##__VA_ARGS__)
|
||||
|
||||
PyMODINIT_FUNC gdbpy_events_mod_func ();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue