From d452c4bcefbbe7b2305a5ce26469e06d940a690c Mon Sep 17 00:00:00 2001 From: Ulrich Weigand Date: Thu, 2 Jul 2009 17:04:23 +0000 Subject: * python/python-internal.h (struct language_defn): Declare. (python_gdbarch, python_language): Likewise. (ensure_python_env): Add prototype. (make_cleanup_py_restore_gil): Remove prototype. * python/python.c: Include "arch-utils.h", "value.h" and "language.h". (python_gdbarch, python_language): New global variables. (struct python_env): New data type. (ensure_python_env, restore_python_env): New functions. (eval_python_from_control_command): Call ensure_python_env to install current architecture and language. (python_command, gdbpy_new_objfile): Likewise. * python/python-cmd.c: Include "arch-utils.h" and "language.h". (cmdpy_destroyer, cmdpy_function, cmdpy_completer): Call ensure_python_env. * python/python-type.c (clean_up_objfile_types): Likewise. * python/python-objfile.c: Include "language.h". (clean_up_objfile): Call ensure_python_env. * python/python-prettyprint.c (apply_val_pretty_printer): Likewise. (apply_varobj_pretty_printer): Do not call PyGILState_Ensure. * varobj.c (varobj_ensure_python_env): New helper function. (varobj_get_display_hint, update_dynamic_varobj_children, install_default_visualizer, varobj_set_visualizer, free_variable, value_get_print_value): Call it. (value_get_print_value): Add varobj argument instead of pretty printer argument. Update all callers. * python/python-utils.c (py_gil_restore, make_cleanup_py_restore_gil): Remove. * value.h (internal_function_fn): Add GDBARCH and LANGUAGE argument. (call_internal_function): Likewise. * value.c (call_internal_function): Likewise. Pass to handler. * eval.c (evaluate_subexp_standard): Update call. * python/python-function.c: Include "language.h". (fnpy_call): Add GDBARCH and LANGAUAGE arguments and call make_cleanup_python_env. * python/python-value.c (builtin_type_pyint, builtin_type_pyfloat, builtin_type_pylong, builtin_type_pybool, builtin_type_pychar, valpy_str): Use python_gdbarch and python_language instead of current_gdbarch and current_language. * python/python-type.c (typy_lookup_typename): Likewise. --- gdb/python/python-cmd.c | 16 +++++----- gdb/python/python-function.c | 8 ++--- gdb/python/python-internal.h | 8 ++++- gdb/python/python-objfile.c | 7 +++-- gdb/python/python-prettyprint.c | 6 +--- gdb/python/python-type.c | 6 ++-- gdb/python/python-utils.c | 17 ----------- gdb/python/python-value.c | 12 ++++---- gdb/python/python.c | 65 +++++++++++++++++++++++++++++++++-------- 9 files changed, 84 insertions(+), 61 deletions(-) (limited to 'gdb/python') diff --git a/gdb/python/python-cmd.c b/gdb/python/python-cmd.c index 8f59a22..528aca6 100644 --- a/gdb/python/python-cmd.c +++ b/gdb/python/python-cmd.c @@ -19,6 +19,7 @@ #include "defs.h" +#include "arch-utils.h" #include "value.h" #include "exceptions.h" #include "python-internal.h" @@ -26,6 +27,7 @@ #include "gdbcmd.h" #include "cli/cli-decode.h" #include "completer.h" +#include "language.h" /* Struct representing built-in completion types. */ struct cmdpy_completer @@ -90,9 +92,9 @@ static void cmdpy_destroyer (struct cmd_list_element *self, void *context) { cmdpy_object *cmd; - PyGILState_STATE state; + struct cleanup *cleanup; - state = PyGILState_Ensure (); + cleanup = ensure_python_env (get_current_arch (), current_language); /* Release our hold on the command object. */ cmd = (cmdpy_object *) context; @@ -105,7 +107,7 @@ cmdpy_destroyer (struct cmd_list_element *self, void *context) xfree (self->doc); xfree (self->prefixname); - PyGILState_Release (state); + do_cleanups (cleanup); } /* Called by gdb to invoke the command. */ @@ -115,10 +117,8 @@ cmdpy_function (struct cmd_list_element *command, char *args, int from_tty) cmdpy_object *obj = (cmdpy_object *) get_cmd_context (command); PyObject *argobj, *ttyobj, *result; struct cleanup *cleanup; - PyGILState_STATE state; - state = PyGILState_Ensure (); - cleanup = make_cleanup_py_restore_gil (&state); + cleanup = ensure_python_env (get_current_arch (), current_language); if (! obj) error (_("Invalid invocation of Python command object.")); @@ -182,10 +182,8 @@ cmdpy_completer (struct cmd_list_element *command, char *text, char *word) PyObject *textobj, *wordobj, *resultobj = NULL; char **result = NULL; struct cleanup *cleanup; - PyGILState_STATE state; - state = PyGILState_Ensure (); - cleanup = make_cleanup_py_restore_gil (&state); + cleanup = ensure_python_env (get_current_arch (), current_language); if (! obj) error (_("Invalid invocation of Python command object.")); diff --git a/gdb/python/python-function.c b/gdb/python/python-function.c index 4a85a33..8a5abaf 100644 --- a/gdb/python/python-function.c +++ b/gdb/python/python-function.c @@ -27,6 +27,7 @@ #include "cli/cli-decode.h" #include "completer.h" #include "expression.h" +#include "language.h" static PyTypeObject fnpy_object_type; @@ -53,16 +54,15 @@ convert_values_to_python (int argc, struct value **argv) /* Call a Python function object's invoke method. */ static struct value * -fnpy_call (void *cookie, int argc, struct value **argv) +fnpy_call (struct gdbarch *gdbarch, const struct language_defn *language, + void *cookie, int argc, struct value **argv) { int i; struct value *value = NULL; PyObject *result, *callable, *args; struct cleanup *cleanup; - PyGILState_STATE state; - state = PyGILState_Ensure (); - cleanup = make_cleanup_py_restore_gil (&state); + cleanup = ensure_python_env (gdbarch, language); args = convert_values_to_python (argc, argv); diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 35d3870..39d37e1 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -62,6 +62,7 @@ typedef int Py_ssize_t; #endif struct value; +struct language_defn; extern PyObject *gdb_module; extern PyTypeObject value_object_type; @@ -89,7 +90,12 @@ void gdbpy_initialize_functions (void); void gdbpy_initialize_objfile (void); struct cleanup *make_cleanup_py_decref (PyObject *py); -struct cleanup *make_cleanup_py_restore_gil (PyGILState_STATE *state); + +struct cleanup *ensure_python_env (struct gdbarch *gdbarch, + const struct language_defn *language); + +extern struct gdbarch *python_gdbarch; +extern const struct language_defn *python_language; /* Use this after a TRY_EXCEPT to throw the appropriate Python exception. */ diff --git a/gdb/python/python-objfile.c b/gdb/python/python-objfile.c index b70a006..a483d33 100644 --- a/gdb/python/python-objfile.c +++ b/gdb/python/python-objfile.c @@ -21,6 +21,7 @@ #include "python-internal.h" #include "charset.h" #include "objfiles.h" +#include "language.h" typedef struct { @@ -119,13 +120,13 @@ objfpy_set_printers (PyObject *o, PyObject *value, void *ignore) static void clean_up_objfile (struct objfile *objfile, void *datum) { - PyGILState_STATE state; + struct cleanup *cleanup; objfile_object *object = datum; - state = PyGILState_Ensure (); + cleanup = ensure_python_env (get_objfile_arch (objfile), current_language); object->objfile = NULL; Py_DECREF ((PyObject *) object); - PyGILState_Release (state); + do_cleanups (cleanup); } /* Return a borrowed reference to the Python object of type Objfile diff --git a/gdb/python/python-prettyprint.c b/gdb/python/python-prettyprint.c index 09ef261..57e517a 100644 --- a/gdb/python/python-prettyprint.c +++ b/gdb/python/python-prettyprint.c @@ -474,10 +474,8 @@ apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr, char *hint = NULL; struct cleanup *cleanups; int result = 0; - PyGILState_STATE state; - state = PyGILState_Ensure (); - cleanups = make_cleanup_py_restore_gil (&state); + cleanups = ensure_python_env (gdbarch, language); /* Instantiate the printer. */ if (valaddr) @@ -526,13 +524,11 @@ apply_varobj_pretty_printer (PyObject *printer_obj, struct value **replacement) { char *result; - PyGILState_STATE state = PyGILState_Ensure (); *replacement = NULL; result = pretty_print_one_value (printer_obj, replacement); if (result == NULL); gdbpy_print_stack (); - PyGILState_Release (state); return result; } diff --git a/gdb/python/python-type.c b/gdb/python/python-type.c index 0874a99..e185112 100644 --- a/gdb/python/python-type.c +++ b/gdb/python/python-type.c @@ -374,7 +374,7 @@ typy_lookup_typename (char *type_name) else if (!strncmp (type_name, "enum ", 5)) type = lookup_enum (type_name + 5, NULL); else - type = lookup_typename (current_language, current_gdbarch, + type = lookup_typename (python_language, python_gdbarch, type_name, NULL, 0); } if (except.reason < 0) @@ -532,12 +532,10 @@ clean_up_objfile_types (struct objfile *objfile, void *datum) type_object *obj = datum; htab_t copied_types; struct cleanup *cleanup; - PyGILState_STATE state; /* This prevents another thread from freeing the objects we're operating on. */ - state = PyGILState_Ensure (); - cleanup = make_cleanup_py_restore_gil (&state); + cleanup = ensure_python_env (get_objfile_arch (objfile), current_language); copied_types = create_copied_types_hash (objfile); diff --git a/gdb/python/python-utils.c b/gdb/python/python-utils.c index ddac2f5..9f6fe07 100644 --- a/gdb/python/python-utils.c +++ b/gdb/python/python-utils.c @@ -46,23 +46,6 @@ make_cleanup_py_decref (PyObject *py) return make_cleanup (py_decref, (void *) py); } -/* A cleanup function to restore the thread state. */ - -static void -py_gil_restore (void *p) -{ - PyGILState_STATE *state = p; - PyGILState_Release (*state); -} - -/* Return a new cleanup which will restore the Python GIL state. */ - -struct cleanup * -make_cleanup_py_restore_gil (PyGILState_STATE *state) -{ - return make_cleanup (py_gil_restore, state); -} - /* Converts a Python 8-bit string to a unicode string object. Assumes the 8-bit string is in the host charset. If an error occurs during conversion, returns NULL with a python exception set. diff --git a/gdb/python/python-value.c b/gdb/python/python-value.c index ebc15dc..d4353f7 100644 --- a/gdb/python/python-value.c +++ b/gdb/python/python-value.c @@ -44,19 +44,19 @@ struct value *values_in_python = NULL; GDB (which uses target arithmetic). */ /* Python's integer type corresponds to C's long type. */ -#define builtin_type_pyint builtin_type (current_gdbarch)->builtin_long +#define builtin_type_pyint builtin_type (python_gdbarch)->builtin_long /* Python's float type corresponds to C's double type. */ -#define builtin_type_pyfloat builtin_type (current_gdbarch)->builtin_double +#define builtin_type_pyfloat builtin_type (python_gdbarch)->builtin_double /* Python's long type corresponds to C's long long type. */ -#define builtin_type_pylong builtin_type (current_gdbarch)->builtin_long_long +#define builtin_type_pylong builtin_type (python_gdbarch)->builtin_long_long #define builtin_type_pybool \ - language_bool_type (current_language, current_gdbarch) + language_bool_type (python_language, python_gdbarch) #define builtin_type_pychar \ - language_string_char_type (current_language, current_gdbarch) + language_string_char_type (python_language, python_gdbarch) typedef struct { PyObject_HEAD @@ -333,7 +333,7 @@ valpy_str (PyObject *self) TRY_CATCH (except, RETURN_MASK_ALL) { common_val_print (((value_object *) self)->value, stb, 0, - &opts, current_language); + &opts, python_language); s = ui_file_xstrdup (stb, &dummy); } GDB_PY_HANDLE_EXCEPTION (except); diff --git a/gdb/python/python.c b/gdb/python/python.c index 3ad69d4..254bd28 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -18,12 +18,15 @@ along with this program. If not, see . */ #include "defs.h" +#include "arch-utils.h" #include "command.h" #include "ui-out.h" #include "cli/cli-script.h" #include "gdbcmd.h" #include "objfiles.h" #include "observer.h" +#include "value.h" +#include "language.h" #include @@ -58,6 +61,52 @@ PyObject *gdbpy_children_cst; PyObject *gdbpy_display_hint_cst; PyObject *gdbpy_doc_cst; + +/* Architecture and language to be used in callbacks from + the Python interpreter. */ +struct gdbarch *python_gdbarch; +const struct language_defn *python_language; + +/* Restore global language and architecture and Python GIL state + when leaving the Python interpreter. */ + +struct python_env +{ + PyGILState_STATE state; + struct gdbarch *gdbarch; + const struct language_defn *language; +}; + +static void +restore_python_env (void *p) +{ + struct python_env *env = (struct python_env *)p; + PyGILState_Release (env->state); + python_gdbarch = env->gdbarch; + python_language = env->language; + xfree (env); +} + +/* Called before entering the Python interpreter to install the + current language and architecture to be used for Python values. */ + +struct cleanup * +ensure_python_env (struct gdbarch *gdbarch, + const struct language_defn *language) +{ + struct python_env *env = xmalloc (sizeof *env); + + env->state = PyGILState_Ensure (); + env->gdbarch = python_gdbarch; + env->language = python_language; + + python_gdbarch = gdbarch; + python_language = language; + + return make_cleanup (restore_python_env, env); +} + + /* Given a command_line, return a command string suitable for passing to Python. Lines in the string are separated by newlines. The return value is allocated using xmalloc and the caller is @@ -96,13 +145,11 @@ eval_python_from_control_command (struct command_line *cmd) int ret; char *script; struct cleanup *cleanup; - PyGILState_STATE state; if (cmd->body_count != 1) error (_("Invalid \"python\" block structure.")); - state = PyGILState_Ensure (); - cleanup = make_cleanup_py_restore_gil (&state); + cleanup = ensure_python_env (get_current_arch (), current_language); script = compute_python_string (cmd->body_list[0]); ret = PyRun_SimpleString (script); @@ -122,10 +169,7 @@ static void python_command (char *arg, int from_tty) { struct cleanup *cleanup; - PyGILState_STATE state; - - state = PyGILState_Ensure (); - cleanup = make_cleanup_py_restore_gil (&state); + cleanup = ensure_python_env (get_current_arch (), current_language); while (arg && *arg && isspace (*arg)) ++arg; @@ -330,13 +374,12 @@ gdbpy_new_objfile (struct objfile *objfile) char *filename, *debugfile; int len; FILE *input; - PyGILState_STATE state; struct cleanup *cleanups; if (!gdbpy_auto_load || !objfile || !objfile->name) return; - state = PyGILState_Ensure (); + cleanups = ensure_python_env (get_objfile_arch (objfile), current_language); gdbpy_current_objfile = objfile; @@ -349,7 +392,7 @@ gdbpy_new_objfile (struct objfile *objfile) input = fopen (filename, "r"); debugfile = filename; - cleanups = make_cleanup (xfree, filename); + make_cleanup (xfree, filename); make_cleanup (xfree, realname); if (!input && debug_file_directory) @@ -391,8 +434,6 @@ gdbpy_new_objfile (struct objfile *objfile) do_cleanups (cleanups); gdbpy_current_objfile = NULL; - - PyGILState_Release (state); } /* Return the current Objfile, or None if there isn't one. */ -- cgit v1.1