This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[RFC][patch 3/9] export hooks mechanism to Python


2008-04-29  Tom Tromey <tromey@redhat.com>

	* Makefile.in (SUBDIR_PYTHON_OBS): Add python-hooks.o.
	(SUBDIR_PYTHON_SRCS): Add python/hooks.c.
	(python-hooks.o): New target.
	* interps.c (interp_set): Don't call clear_interpreter_hooks.
	[Temporary hack].
	* python/hooks.c: New file.
	* python/python-internal.h (variable_to_python,
	gdbpy_get_hook_function, gdbpy_initialize_hooks): Declare.
	* python/python.c (demand_python): Call gdbpy_initialize_hooks.

Index: tromey.git/gdb/Makefile.in
===================================================================
--- tromey.git.orig/gdb/Makefile.in	2008-04-29 11:04:11.000000000 -0300
+++ tromey.git/gdb/Makefile.in	2008-04-29 11:05:09.000000000 -0300
@@ -262,9 +262,11 @@ SUBDIR_TUI_CFLAGS= \
 #
 SUBDIR_PYTHON_OBS = \
 	python.o \
+	python-hooks.o \
 	python-value.o
 SUBDIR_PYTHON_SRCS = \
 	python/python.c \
+	python/hooks.c \
 	python/value.c
 SUBDIR_PYTHON_DEPS =
 SUBDIR_PYTHON_LDFLAGS=
@@ -3413,6 +3415,10 @@ python.o: $(srcdir)/python/python.c $(de
 	$(command_h) $(libiberty_h) $(cli_decode_h) $(charset_h) $(top_h) \
 	$(exceptions_h) $(python_internal_h) $(version_h)
 	$(CC) -c $(INTERNAL_CFLAGS) $(PYTHON_CFLAGS) $(srcdir)/python/python.c
+python-hooks.o: $(srcdir)/python/hooks.c $(defs_h) $(cli_decode_h) \
+	$(charset_h) $(gdb_events_h) $(python_h) $(python_internal_h)
+	$(CC) -c $(INTERNAL_CFLAGS) $(PYTHON_CFLAGS) \
+	$(srcdir)/python/hooks.c -o python-hooks.o
 python-value.o: $(srcdir)/python/value.c $(defs_h) $(exceptions_h) \
 	$(python_internal_h) $(value_h)
 	$(CC) -c $(INTERNAL_CFLAGS) $(PYTHON_CFLAGS) \
Index: tromey.git/gdb/interps.c
===================================================================
--- tromey.git.orig/gdb/interps.c	2008-04-29 10:57:39.000000000 -0300
+++ tromey.git/gdb/interps.c	2008-04-29 11:05:09.000000000 -0300
@@ -191,7 +191,11 @@ interp_set (struct interp *interp, int t
     }
 
   /* Clear out any installed interpreter hooks/event handlers.  */
-  clear_interpreter_hooks ();
+  /* FIXME - we don't want python to be a normal interpreter, but we
+     do want to be able to hook in everywhere.  Not sure what to do
+     about this -- maybe allow events to be delivered to multiple
+     handlers.  */
+/*   clear_interpreter_hooks (); */
 
   if (interp->procs->resume_proc != NULL
       && (!interp->procs->resume_proc (interp->data)))
Index: tromey.git/gdb/python/hooks.c
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ tromey.git/gdb/python/hooks.c	2008-04-29 11:05:09.000000000 -0300
@@ -0,0 +1,120 @@
+/* Notifications from gdb to Python
+
+   Copyright (C) 2008 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 "cli/cli-decode.h"
+#include "charset.h"
+#include "gdb-events.h"
+#include "python.h"
+#include "python-internal.h"
+
+static struct gdb_events handlers;
+
+PyObject *
+gdbpy_get_hook_function (const char *name)
+{
+  PyObject *hooks;
+
+  if (! PyObject_HasAttrString (gdb_module, "hooks"))
+    return NULL;
+  hooks = PyObject_GetAttrString (gdb_module, "hooks");
+  if (! hooks)
+    return NULL;
+  /* The cast is because the Python function doesn't declare const argument.
+     This is a problem in Python version 2.4, but not in 2.5.  */
+  if (! PyObject_HasAttrString (hooks, (char *) name))
+    return NULL;
+  /* The cast is because the Python function doesn't declare const argument.
+     This is a problem in Python version 2.4, but not in 2.5.  */
+  return PyObject_GetAttrString (hooks, (char *) name);
+}
+
+static void
+gdbpy_set_hook (struct cmd_list_element *cmd)
+{
+  char *name;
+  PyObject *nameobj, *valobj, *func;
+
+  func = gdbpy_get_hook_function ("post_set");
+  if (! func)
+    return;
+
+  valobj = variable_to_python (cmd);
+  if (! valobj)
+    return;
+
+  name = concat (cmd->prefixname ? cmd->prefixname : "", " ", cmd->name, NULL);
+  nameobj = PyString_Decode (name, strlen (name), host_charset (),
+			     NULL /* FIXME */);
+  xfree (name);
+
+  PyObject_CallFunctionObjArgs (func, nameobj, valobj, NULL);
+  if (PyErr_Occurred ())
+    PyErr_Print ();
+}
+
+static void
+gdbpy_attach (void)
+{
+  PyObject *func;
+  func = gdbpy_get_hook_function ("post_attach");
+  if (func)
+    {
+      PyObject_CallFunctionObjArgs (func, NULL);
+      if (PyErr_Occurred ())
+	PyErr_Print ();
+    }
+}
+
+static void
+gdbpy_detach (void)
+{
+  PyObject *func;
+  func = gdbpy_get_hook_function ("post_detach");
+  if (func)
+    {
+      PyObject_CallFunctionObjArgs (func, NULL);
+      if (PyErr_Occurred ())
+	PyErr_Print ();
+    }
+}
+
+static void
+gdbpy_architecture_changed (void)
+{
+  PyObject *func;
+  func = gdbpy_get_hook_function ("post_architecture_change");
+  if (func)
+    {
+      PyObject_CallFunctionObjArgs (func, NULL);
+      if (PyErr_Occurred ())
+	PyErr_Print ();
+    }
+}
+
+void
+gdbpy_initialize_hooks (void)
+{
+  handlers.architecture_changed = gdbpy_architecture_changed;
+  deprecated_set_gdb_event_hooks (&handlers);
+
+  deprecated_set_hook = gdbpy_set_hook;
+  deprecated_attach_hook = gdbpy_attach;
+  deprecated_detach_hook = gdbpy_detach;
+}
Index: tromey.git/gdb/python/python-internal.h
===================================================================
--- tromey.git.orig/gdb/python/python-internal.h	2008-04-29 11:04:11.000000000 -0300
+++ tromey.git/gdb/python/python-internal.h	2008-04-29 11:05:09.000000000 -0300
@@ -32,12 +32,16 @@ extern PyTypeObject value_object_type;
 PyObject *gdbpy_make_value_from_int (PyObject *self, PyObject *args);
 PyObject *gdbpy_get_value_from_history (PyObject *self, PyObject *args);
 
+PyObject *variable_to_python (struct cmd_list_element *);
 PyObject *value_to_value_object (struct value *v);
 PyObject *gdb_owned_value_to_value_object (struct value *v);
 
 struct value *value_object_to_value (PyObject *self);
 
+PyObject *gdbpy_get_hook_function (const char *);
+
 void gdbpy_initialize_values (void);
+void gdbpy_initialize_hooks (void);
 
 /* Use this after a TRY_EXCEPT to throw the appropriate Python
    exception.  FIXME: throw different errors depending on
Index: tromey.git/gdb/python/python.c
===================================================================
--- tromey.git.orig/gdb/python/python.c	2008-04-29 11:04:11.000000000 -0300
+++ tromey.git/gdb/python/python.c	2008-04-29 11:05:09.000000000 -0300
@@ -64,6 +64,7 @@ demand_python ()
       PyModule_AddStringConstant (gdb_module, "HOST_CONFIG", host_name);
       PyModule_AddStringConstant (gdb_module, "TARGET_CONFIG", target_name);
 
+      gdbpy_initialize_hooks ();
       gdbpy_initialize_values ();
 
       PyRun_SimpleString ("import gdb");

-- 
-- 
[]'s
Thiago Jung Bauermann
Software Engineer
IBM Linux Technology Center


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]