This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
[PATCH v1 03/13] script language API for GDB: python.[ch] changes
- From: xdje42 at gmail dot com
- To: gdb-patches at sourceware dot org
- Date: Thu, 05 Dec 2013 21:52:18 -0800
- Subject: [PATCH v1 03/13] script language API for GDB: python.[ch] changes
- Authentication-results: sourceware.org; auth=none
This patch contains the changes to python-internal.h, python.c, python.h.
It defines the "ops" vectors for Python, defines script_language_python
as the top level interface to Python, and then has a bunch of renamings.
The functions that implement the script_language_ops "methods" are all
named ${lang}_${method_name}.
Functions that were exported to gdb now live in the ops vectors.
This patch also removes a lot of the stubs that had to be defined
when gdb was compiled --without-python.
2013-12-05 Doug Evans <xdje42@gmail.com>
* python/python-internal.h: #include "scripting.h".
(gdbpy_auto_load_enabled): Declare.
(gdbpy_apply_val_pretty_printer): Declare.
(gdbpy_apply_frame_filter): Declare.
(gdbpy_preserve_values): Declare.
(gdbpy_breakpoint_cond_says_stop): Declare.
(gdbpy_breakpoint_has_cond): Declare.
(void source_python_script_for_objfile): Delete.
* python/python.c: #include "scripting-priv.h".
(script_language_python): Moved here from py-auto-load.c.
Redefined to be of type script_language_defn.
(python_scripting_script_ops): New global.
(python_scripting_ops): New global.
(gdbpy_clear_quit_flag): Renamed from clear_quit_flag, made static.
New arg slang.
(gdbpy_set_quit_flag): Renamed from set_quit_flag, made static.
New arg slang.
(gdbpy_check_quit_flag): Renamed from check_quit_flag, made static.
New arg slang.
(gdbpy_eval_from_control_command): Renamed from
eval_python_from_control_command, made static. New arg slang.
(gdbpy_source_script) Renamed from source_python_script, made static.
New arg slang.
(gdbpy_source_objfile_script): Renamed from
source_python_script_for_objfile, made static. New arg slang.
(gdbpy_start_type_printers): Renamed from start_type_printers, made
static. New args slang, slang_printers. Change result type to "void".
(gdbpy_apply_type_printers): Renamed from apply_type_printers, made
static. New arg slang. Rename arg printers to slang_printers
and change type to script_type_printers *.
(gdbpy_free_type_printers): Renamed from free_type_printers, made
static. Replace argument arg with slang, slang_printers.
(!HAVE_PYTHON, eval_python_from_control_command): Delete.
(!HAVE_PYTHON, source_python_script): Delete.
(!HAVE_PYTHON, gdbpy_should_stop): Delete.
(!HAVE_PYTHON, gdbpy_breakpoint_has_py_cond): Delete.
(!HAVE_PYTHON, start_type_printers): Delete.
(!HAVE_PYTHON, apply_type_printers): Delete.
(!HAVE_PYTHON, free_type_printers): Delete.
(gdbpy_finish_initialization) Renamed from
finish_python_initialization, made static. New arg slang.
(gdbpy_initialized): New function.
(script_lang_python_initialized): New function.
* python/python.h: #include "scripting.h". Delete #include
"value.h", "mi/mi-cmds.h".
(script_language_python): Declare.
(GDBPY_AUTO_FILE_NAME): Delete.
(enum py_bt_status): Moved to scripting.h and renamed to
script_bt_status.
(enum frame_filter_flags): Moved to scripting.h.
(enum py_frame_args): Moved to scripting.h and renamed to
script_frame_args.
(finish_python_initialization): Delete.
(eval_python_from_control_command): Delete.
(source_python_script): Delete.
(apply_val_pretty_printer): Delete.
(apply_frame_filter): Delete.
(preserve_python_values): Delete.
(gdbpy_script_language_defn): Delete.
(gdbpy_should_stop, gdbpy_breakpoint_has_py_cond): Delete.
(start_type_printers, apply_type_printers, free_type_printers): Delete.
(script_lang_python_initialized): Declare.
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 125670e..e873e10 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -20,6 +20,8 @@
#ifndef GDB_PYTHON_INTERNAL_H
#define GDB_PYTHON_INTERNAL_H
+#include "scripting.h"
+
/* These WITH_* macros are defined by the CPython API checker that
comes with the Python plugin for GCC. See:
https://gcc-python-plugin.readthedocs.org/en/latest/cpychecker.html
@@ -279,7 +281,33 @@ typedef struct
extern struct cmd_list_element *set_python_list;
extern struct cmd_list_element *show_python_list;
-
+
+/* script_language_script_ops "methods". */
+
+extern int gdbpy_auto_load_enabled (const struct script_language_defn *);
+
+/* script_language_ops "methods". */
+
+extern int gdbpy_apply_val_pretty_printer
+ (const struct script_language_defn *,
+ struct type *type, const gdb_byte *valaddr,
+ int embedded_offset, CORE_ADDR address,
+ struct ui_file *stream, int recurse,
+ const struct value *val,
+ const struct value_print_options *options,
+ const struct language_defn *language);
+extern enum script_bt_status gdbpy_apply_frame_filter
+ (const struct script_language_defn *,
+ struct frame_info *frame, int flags, enum script_frame_args args_type,
+ struct ui_out *out, int frame_low, int frame_high);
+extern void gdbpy_preserve_values (const struct script_language_defn *,
+ struct objfile *objfile,
+ htab_t copied_types);
+extern enum scr_bp_stop gdbpy_breakpoint_cond_says_stop
+ (const struct script_language_defn *, struct breakpoint *);
+extern int gdbpy_breakpoint_has_cond (const struct script_language_defn *,
+ struct breakpoint *b);
+
PyObject *gdbpy_history (PyObject *self, PyObject *args);
PyObject *gdbpy_breakpoints (PyObject *, PyObject *);
PyObject *gdbpy_frame_stop_reason_string (PyObject *, PyObject *);
@@ -435,9 +463,6 @@ extern const struct language_defn *python_language;
void gdbpy_print_stack (void);
-void source_python_script_for_objfile (struct objfile *objfile, FILE *file,
- const char *filename);
-
PyObject *python_string_to_unicode (PyObject *obj);
char *unicode_to_target_string (PyObject *unicode_str);
char *python_string_to_target_string (PyObject *obj);
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 1873936..f689719 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -32,8 +32,8 @@
#include "serial.h"
#include "readline/tilde.h"
#include "python.h"
+#include "scripting-priv.h"
#include "cli/cli-utils.h"
-
#include <ctype.h>
/* Declared constants and enum for python stack printing. */
@@ -57,6 +57,34 @@ static const char *const python_excp_enums[] =
static const char *gdbpy_should_print_stack = python_excp_message;
#ifdef HAVE_PYTHON
+/* Forward decls, these are defined later. */
+static const struct script_language_script_ops python_scripting_script_ops;
+static const struct script_language_ops python_scripting_ops;
+#endif
+
+/* The main struct describing GDB's interface to the Python
+ scripting language. */
+const struct script_language_defn script_language_python =
+{
+ SCRIPT_LANG_PYTHON,
+ "python",
+ "Python",
+
+ ".py",
+ "-gdb.py",
+
+ python_control,
+
+#ifdef HAVE_PYTHON
+ &python_scripting_script_ops,
+ &python_scripting_ops
+#else
+ NULL,
+ NULL
+#endif
+};
+
+#ifdef HAVE_PYTHON
#include "libiberty.h"
#include "cli/cli-decode.h"
@@ -104,6 +132,59 @@ PyObject *gdbpy_gdb_error;
/* The `gdb.MemoryError' exception. */
PyObject *gdbpy_gdb_memory_error;
+static script_sourcer_func gdbpy_source_script;
+static objfile_script_sourcer_func gdbpy_source_objfile_script;
+static void gdbpy_finish_initialization (const struct script_language_defn *);
+static int gdbpy_initialized (const struct script_language_defn *);
+static void gdbpy_eval_from_control_command
+ (const struct script_language_defn *, struct command_line *cmd);
+static void gdbpy_start_type_printers (const struct script_language_defn *,
+ struct script_type_printers *);
+static char *gdbpy_apply_type_printers (const struct script_language_defn *,
+ const struct script_type_printers *,
+ struct type *);
+static void gdbpy_free_type_printers (const struct script_language_defn *,
+ struct script_type_printers *);
+static void gdbpy_clear_quit_flag (const struct script_language_defn *);
+static void gdbpy_set_quit_flag (const struct script_language_defn *);
+static int gdbpy_check_quit_flag (const struct script_language_defn *);
+
+/* The interface between gdb proper and loading of python scripts. */
+
+static const struct script_language_script_ops python_scripting_script_ops =
+{
+ gdbpy_source_script,
+ gdbpy_source_objfile_script,
+ gdbpy_auto_load_enabled
+};
+
+/* The interface between gdb proper and python scripting. */
+
+static const struct script_language_ops python_scripting_ops =
+{
+ gdbpy_finish_initialization,
+ gdbpy_initialized,
+
+ gdbpy_eval_from_control_command,
+
+ gdbpy_start_type_printers,
+ gdbpy_apply_type_printers,
+ gdbpy_free_type_printers,
+
+ gdbpy_apply_val_pretty_printer,
+
+ gdbpy_apply_frame_filter,
+
+ gdbpy_preserve_values,
+
+ gdbpy_breakpoint_has_cond,
+ gdbpy_breakpoint_cond_says_stop,
+
+ gdbpy_clear_quit_flag,
+ gdbpy_set_quit_flag,
+ gdbpy_check_quit_flag,
+};
+
/* Architecture and language to be used in callbacks from
the Python interpreter. */
struct gdbarch *python_gdbarch;
@@ -169,8 +250,8 @@ ensure_python_env (struct gdbarch *gdbarch,
/* Clear the quit flag. */
-void
-clear_quit_flag (void)
+static void
+gdbpy_clear_quit_flag (const struct script_language_defn *slang)
{
/* This clears the flag as a side effect. */
PyOS_InterruptOccurred ();
@@ -178,16 +259,16 @@ clear_quit_flag (void)
/* Set the quit flag. */
-void
-set_quit_flag (void)
+static void
+gdbpy_set_quit_flag (const struct script_language_defn *slang)
{
PyErr_SetInterrupt ();
}
/* Return true if the quit flag has been set, false otherwise. */
-int
-check_quit_flag (void)
+static int
+gdbpy_check_quit_flag (const struct script_language_defn *slang)
{
return PyOS_InterruptOccurred ();
}
@@ -343,8 +424,9 @@ compute_python_string (struct command_line *l)
/* Take a command line structure representing a 'python' command, and
evaluate its body using the Python interpreter. */
-void
-eval_python_from_control_command (struct command_line *cmd)
+static void
+gdbpy_eval_from_control_command (const struct script_language_defn *slang,
+ struct command_line *cmd)
{
int ret;
char *script;
@@ -765,12 +847,14 @@ gdbpy_find_pc_line (PyObject *self, PyObject *args)
}
/* Read a file as Python code.
+ This is the script_language_script_ops.script_sourcer "method".
FILE is the file to run. FILENAME is name of the file FILE.
This does not throw any errors. If an exception occurs python will print
the traceback and clear the error indicator. */
-void
-source_python_script (FILE *file, const char *filename)
+static void
+gdbpy_source_script (const struct script_language_defn *slang,
+ FILE *file, const char *filename)
{
struct cleanup *cleanup;
@@ -1158,16 +1242,18 @@ gdbpy_progspaces (PyObject *unused1, PyObject *unused2)
/* The "current" objfile. This is set when gdb detects that a new
objfile has been loaded. It is only set for the duration of a call to
- source_python_script_for_objfile; it is NULL at other times. */
+ gdbpy_source_objfile_script; it is NULL at other times. */
static struct objfile *gdbpy_current_objfile;
/* Set the current objfile to OBJFILE and then read FILE named FILENAME
as Python code. This does not throw any errors. If an exception
- occurs python will print the traceback and clear the error indicator. */
+ occurs python will print the traceback and clear the error indicator.
+ This is the script_language_script_ops.objfile_script_sourcer "method". */
-void
-source_python_script_for_objfile (struct objfile *objfile, FILE *file,
- const char *filename)
+static void
+gdbpy_source_objfile_script (const struct script_language_defn *slang,
+ struct objfile *objfile, FILE *file,
+ const char *filename)
{
struct cleanup *cleanups;
@@ -1225,18 +1311,20 @@ gdbpy_objfiles (PyObject *unused1, PyObject *unused2)
return list;
}
-/* Compute the list of active type printers and return it. The result
- of this function can be passed to apply_type_printers, and should
- be freed by free_type_printers. */
+/* Compute the list of active python type printers and store them in
+ SLANG_PRINTERS->py_type_printers. The product of this function is used by
+ gdbpy_apply_type_printers, and freed by gdbpy_free_type_printers.
+ This is the script_language_ops.start_type_printers "method". */
-void *
-start_type_printers (void)
+static void
+gdbpy_start_type_printers (const struct script_language_defn *slang,
+ struct script_type_printers *slang_printers)
{
struct cleanup *cleanups;
- PyObject *type_module, *func = NULL, *result_obj = NULL;
+ PyObject *type_module, *func = NULL, *printers_obj = NULL;
if (!gdb_python_initialized)
- return NULL;
+ return;
cleanups = ensure_python_env (get_current_arch (), current_language);
@@ -1254,32 +1342,32 @@ start_type_printers (void)
goto done;
}
- result_obj = PyObject_CallFunctionObjArgs (func, (char *) NULL);
- if (result_obj == NULL)
+ printers_obj = PyObject_CallFunctionObjArgs (func, (char *) NULL);
+ if (printers_obj == NULL)
gdbpy_print_stack ();
+ else
+ slang_printers->py_type_printers = printers_obj;
done:
Py_XDECREF (type_module);
Py_XDECREF (func);
do_cleanups (cleanups);
- return result_obj;
}
/* If TYPE is recognized by some type printer, return a newly
allocated string holding the type's replacement name. The caller
is responsible for freeing the string. Otherwise, return NULL.
+ This is the script_language_ops.apply_type_printers "method". */
- This function has a bit of a funny name, since it actually applies
- recognizers, but this seemed clearer given the start_type_printers
- and free_type_printers functions. */
-
-char *
-apply_type_printers (void *printers, struct type *type)
+static char *
+gdbpy_apply_type_printers (const struct script_language_defn *slang,
+ const struct script_type_printers *slang_printers,
+ struct type *type)
{
struct cleanup *cleanups;
PyObject *type_obj, *type_module = NULL, *func = NULL;
PyObject *result_obj = NULL;
- PyObject *printers_obj = printers;
+ PyObject *printers_obj = slang_printers->py_type_printers;
char *result = NULL;
if (printers_obj == NULL)
@@ -1335,13 +1423,15 @@ apply_type_printers (void *printers, struct type *type)
return result;
}
-/* Free the result of start_type_printers. */
+/* Free the result of start_type_printers.
+ This is the script_language_ops.free_type_printers "method". */
-void
-free_type_printers (void *arg)
+static void
+gdbpy_free_type_printers (const struct script_language_defn *slang,
+ struct script_type_printers *slang_printers)
{
struct cleanup *cleanups;
- PyObject *printers = arg;
+ PyObject *printers = slang_printers->py_type_printers;
if (printers == NULL)
return;
@@ -1381,61 +1471,6 @@ python_command (char *arg, int from_tty)
python_interactive_command (arg, from_tty);
}
-void
-eval_python_from_control_command (struct command_line *cmd)
-{
- error (_("Python scripting is not supported in this copy of GDB."));
-}
-
-void
-source_python_script (FILE *file, const char *filename)
-{
- throw_error (UNSUPPORTED_ERROR,
- _("Python scripting is not supported in this copy of GDB."));
-}
-
-int
-gdbpy_should_stop (struct gdbpy_breakpoint_object *bp_obj)
-{
- internal_error (__FILE__, __LINE__,
- _("gdbpy_should_stop called when Python scripting is " \
- "not supported."));
-}
-
-int
-gdbpy_breakpoint_has_py_cond (struct gdbpy_breakpoint_object *bp_obj)
-{
- internal_error (__FILE__, __LINE__,
- _("gdbpy_breakpoint_has_py_cond called when Python " \
- "scripting is not supported."));
-}
-
-void *
-start_type_printers (void)
-{
- return NULL;
-}
-
-char *
-apply_type_printers (void *ignore, struct type *type)
-{
- return NULL;
-}
-
-void
-free_type_printers (void *arg)
-{
-}
-
-enum py_bt_status
-apply_frame_filter (struct frame_info *frame, int flags,
- enum py_frame_args args_type,
- struct ui_out *out, int frame_low,
- int frame_high)
-{
- return PY_BT_NO_FILTERS;
-}
-
#endif /* HAVE_PYTHON */
@@ -1733,10 +1768,11 @@ message == an error message without a stack will be printed."),
/* Perform the remaining python initializations.
These must be done after GDB is at least mostly initialized.
E.g., The "info pretty-printer" command needs the "info" prefix
- command installed. */
+ command installed.
+ This is the script_language_ops.finish_initialization "method". */
-void
-finish_python_initialization (void)
+static void
+gdbpy_finish_initialization (const struct script_language_defn *slang)
{
PyObject *m;
char *gdb_pythondir;
@@ -1815,8 +1851,29 @@ finish_python_initialization (void)
do_cleanups (cleanup);
}
+/* Return non-zero if Python has successfully initialized.
+ This is the script_languages_ops.initialized "method". */
+
+static int
+gdbpy_initialized (const struct script_language_defn *slang)
+{
+ return gdb_python_initialized;
+}
+
#endif /* HAVE_PYTHON */
+/* Some code (e.g., MI) wants to know if a particular scripting language
+ successfully initialized.
+ Return non-zero if Python scripting successfully initialized. */
+
+int
+script_lang_python_initialized (void)
+{
+ if (script_language_python.ops != NULL)
+ return script_language_python.ops->initialized (&script_language_python);
+ return 0;
+}
+
#ifdef HAVE_PYTHON
diff --git a/gdb/python/python.h b/gdb/python/python.h
index c07b2aa..da5fe76 100644
--- a/gdb/python/python.h
+++ b/gdb/python/python.h
@@ -20,105 +20,11 @@
#ifndef GDB_PYTHON_H
#define GDB_PYTHON_H
-#include "value.h"
-#include "mi/mi-cmds.h"
+#include "scripting.h"
-struct gdbpy_breakpoint_object;
+extern const struct script_language_defn script_language_python;
-/* The suffix of per-objfile scripts to auto-load.
- E.g. When the program loads libfoo.so, look for libfoo-gdb.py. */
-#define GDBPY_AUTO_FILE_NAME "-gdb.py"
-
-/* Python frame-filter status return values. */
-enum py_bt_status
- {
- /* Return when an error has occurred in processing frame filters,
- or when printing the stack. */
- PY_BT_ERROR = -1,
-
- /* Return from internal routines to indicate that the function
- succeeded. */
- PY_BT_OK = 1,
-
- /* Return when the frame filter process is complete, and all
- operations have succeeded. */
- PY_BT_COMPLETED = 2,
-
- /* Return when the frame filter process is complete, but there
- were no filter registered and enabled to process. */
- PY_BT_NO_FILTERS = 3
- };
-
-/* Flags to pass to apply_frame_filter. */
-
-enum frame_filter_flags
- {
- /* Set this flag if frame level is to be printed. */
- PRINT_LEVEL = 1,
-
- /* Set this flag if frame information is to be printed. */
- PRINT_FRAME_INFO = 2,
-
- /* Set this flag if frame arguments are to be printed. */
- PRINT_ARGS = 4,
-
- /* Set this flag if frame locals are to be printed. */
- PRINT_LOCALS = 8,
- };
-
-/* A choice of the different frame argument printing strategies that
- can occur in different cases of frame filter instantiation. */
-typedef enum py_frame_args
-{
- /* Print no values for arguments when invoked from the MI. */
- NO_VALUES = PRINT_NO_VALUES,
-
- MI_PRINT_ALL_VALUES = PRINT_ALL_VALUES,
-
- /* Print only simple values (what MI defines as "simple") for
- arguments when invoked from the MI. */
- MI_PRINT_SIMPLE_VALUES = PRINT_SIMPLE_VALUES,
-
-
- /* Print only scalar values for arguments when invoked from the
- CLI. */
- CLI_SCALAR_VALUES,
-
- /* Print all values for arguments when invoked from the
- CLI. */
- CLI_ALL_VALUES
-} py_frame_args;
-
-extern void finish_python_initialization (void);
-
-void eval_python_from_control_command (struct command_line *);
-
-void source_python_script (FILE *file, const char *filename);
-
-int apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
- int embedded_offset, CORE_ADDR address,
- struct ui_file *stream, int recurse,
- const struct value *val,
- const struct value_print_options *options,
- const struct language_defn *language);
-
-enum py_bt_status apply_frame_filter (struct frame_info *frame, int flags,
- enum py_frame_args args_type,
- struct ui_out *out, int frame_low,
- int frame_high);
-
-void preserve_python_values (struct objfile *objfile, htab_t copied_types);
-
-const struct script_language *gdbpy_script_language_defn (void);
-
-int gdbpy_should_stop (struct gdbpy_breakpoint_object *bp_obj);
-
-int gdbpy_breakpoint_has_py_cond (struct gdbpy_breakpoint_object *bp_obj);
-
-void *start_type_printers (void);
-
-char *apply_type_printers (void *, struct type *type);
-
-void free_type_printers (void *arg);
+/* Return non-zero if Python scripting successfully initialized. */
+extern int script_lang_python_initialized (void);
#endif /* GDB_PYTHON_H */