(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.
+2009-07-02 Ulrich Weigand <uweigand@de.ibm.com>
+
+ * 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.
+
2009-07-02 Ulrich Weigand <uweigand@de.ibm.com>
* arch-utils.c (selected_byte_order): Return target_byte_order_user.
error (_("Expression of type other than \"Function returning ...\" used as function"));
}
if (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_INTERNAL_FUNCTION)
- return call_internal_function (argvec[0], nargs, argvec + 1);
+ return call_internal_function (exp->gdbarch, exp->language_defn,
+ argvec[0], nargs, argvec + 1);
return call_function_by_hand (argvec[0], nargs, argvec + 1);
/* pai: FIXME save value from call_function_by_hand, then adjust pc by adjust_fn_pc if +ve */
#include "defs.h"
+#include "arch-utils.h"
#include "value.h"
#include "exceptions.h"
#include "python-internal.h"
#include "gdbcmd.h"
#include "cli/cli-decode.h"
#include "completer.h"
+#include "language.h"
/* Struct representing built-in completion types. */
struct cmdpy_completer
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;
xfree (self->doc);
xfree (self->prefixname);
- PyGILState_Release (state);
+ do_cleanups (cleanup);
}
/* Called by gdb to invoke the command. */
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."));
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."));
#include "cli/cli-decode.h"
#include "completer.h"
#include "expression.h"
+#include "language.h"
static PyTypeObject fnpy_object_type;
/* 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);
#endif
struct value;
+struct language_defn;
extern PyObject *gdb_module;
extern PyTypeObject value_object_type;
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. */
#include "python-internal.h"
#include "charset.h"
#include "objfiles.h"
+#include "language.h"
typedef struct
{
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
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)
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;
}
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)
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);
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.
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
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);
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#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 <ctype.h>
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
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);
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;
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;
input = fopen (filename, "r");
debugfile = filename;
- cleanups = make_cleanup (xfree, filename);
+ make_cleanup (xfree, filename);
make_cleanup (xfree, realname);
if (!input && debug_file_directory)
do_cleanups (cleanups);
gdbpy_current_objfile = NULL;
-
- PyGILState_Release (state);
}
/* Return the current Objfile, or None if there isn't one. */
}
struct value *
-call_internal_function (struct value *func, int argc, struct value **argv)
+call_internal_function (struct gdbarch *gdbarch,
+ const struct language_defn *language,
+ struct value *func, int argc, struct value **argv)
{
struct internal_function *ifn;
int result;
result = get_internalvar_function (VALUE_INTERNALVAR (func), &ifn);
gdb_assert (result);
- return (*ifn->handler) (ifn->cookie, argc, argv);
+ return (*ifn->handler) (gdbarch, language, ifn->cookie, argc, argv);
}
/* The 'function' command. This does nothing -- it is just a
/* User function handler. */
-typedef struct value *(*internal_function_fn) (void *cookie,
+typedef struct value *(*internal_function_fn) (struct gdbarch *gdbarch,
+ const struct language_defn *language,
+ void *cookie,
int argc,
struct value **argv);
internal_function_fn handler,
void *cookie);
-struct value *call_internal_function (struct value *function,
+struct value *call_internal_function (struct gdbarch *gdbarch,
+ const struct language_defn *language,
+ struct value *function,
int argc, struct value **argv);
char *value_internal_function_name (struct value *);
static char *value_get_print_value (struct value *value,
enum varobj_display_formats format,
- PyObject *value_formatter);
+ struct varobj *var);
static int varobj_value_is_changeable_p (struct varobj *var);
return (var->root->rootvar == var);
}
+#ifdef HAVE_PYTHON
+/* Helper function to install a Python environment suitable for
+ use during operations on VAR. */
+struct cleanup *
+varobj_ensure_python_env (struct varobj *var)
+{
+ return ensure_python_env (var->root->exp->gdbarch,
+ var->root->exp->language_defn);
+}
+#endif
+
/* Creates a varobj (not its children) */
/* Return the full FRAME which corresponds to the given CORE_ADDR
&& var->value && !value_lazy (var->value))
{
xfree (var->print_value);
- var->print_value = value_get_print_value (var->value, var->format,
- var->pretty_printer);
+ var->print_value = value_get_print_value (var->value, var->format, var);
}
return var->format;
char *result = NULL;
#if HAVE_PYTHON
- PyGILState_STATE state = PyGILState_Ensure ();
+ struct cleanup *back_to = varobj_ensure_python_env (var);
+
if (var->pretty_printer)
result = gdbpy_get_display_hint (var->pretty_printer);
- PyGILState_Release (state);
+
+ do_cleanups (back_to);
#endif
return result;
int i;
int children_changed = 0;
PyObject *printer = var->pretty_printer;
- PyGILState_STATE state;
- state = PyGILState_Ensure ();
- back_to = make_cleanup_py_restore_gil (&state);
+ back_to = varobj_ensure_python_env (var);
*cchanged = 0;
if (!PyObject_HasAttr (printer, gdbpy_children_cst))
lazy -- if it is, the code above has decided that the value
should not be fetched. */
if (value && !value_lazy (value))
- print_value = value_get_print_value (value, var->format,
- var->pretty_printer);
+ print_value = value_get_print_value (value, var->format, var);
/* If the type is changeable, compare the old and the new values.
If this is the initial assignment, we don't have any old value
{
#if HAVE_PYTHON
struct cleanup *cleanup;
- PyGILState_STATE state;
PyObject *pretty_printer = NULL;
- state = PyGILState_Ensure ();
- cleanup = make_cleanup_py_restore_gil (&state);
+ cleanup = varobj_ensure_python_env (var);
if (var->value)
{
#if HAVE_PYTHON
PyObject *mainmod, *globals, *pretty_printer, *constructor;
struct cleanup *back_to, *value;
- PyGILState_STATE state;
-
- state = PyGILState_Ensure ();
- back_to = make_cleanup_py_restore_gil (&state);
+ back_to = varobj_ensure_python_env (var);
mainmod = PyImport_AddModule ("__main__");
globals = PyModule_GetDict (mainmod);
static void
free_variable (struct varobj *var)
{
+#if HAVE_PYTHON
+ if (var->pretty_printer)
+ {
+ struct cleanup *cleanup = varobj_ensure_python_env (var);
+ Py_DECREF (var->pretty_printer);
+ do_cleanups (cleanup);
+ }
+#endif
+
value_free (var->value);
/* Free the expression if this is a root variable. */
xfree (var->root);
}
-#if HAVE_PYTHON
- {
- PyGILState_STATE state = PyGILState_Ensure ();
- Py_XDECREF (var->pretty_printer);
- PyGILState_Release (state);
- }
-#endif
-
xfree (var->name);
xfree (var->obj_name);
xfree (var->print_value);
static char *
value_get_print_value (struct value *value, enum varobj_display_formats format,
- PyObject *value_formatter)
+ struct varobj *var)
{
long dummy;
struct ui_file *stb;
#if HAVE_PYTHON
{
- PyGILState_STATE state = PyGILState_Ensure ();
+ struct cleanup *back_to = varobj_ensure_python_env (var);
+ PyObject *value_formatter = var->pretty_printer;
+
if (value_formatter && PyObject_HasAttr (value_formatter,
gdbpy_to_string_cst))
{
&replacement);
if (thevalue && !string_print)
{
- PyGILState_Release (state);
+ do_cleanups (back_to);
return thevalue;
}
if (replacement)
value = replacement;
}
- PyGILState_Release (state);
+ do_cleanups (back_to);
}
#endif
if (format == var->format)
return xstrdup (var->print_value);
else
- return value_get_print_value (var->value, format,
- var->pretty_printer);
+ return value_get_print_value (var->value, format, var);
}
}
}