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

Thiago Jung Bauermann
2008-06-15T22:31:41Z

[-- Attachment #1: Type: text/plain, Size: 323 bytes --]

Zitat von Tom Tromey <tromey@redhat.com>:

>>>>>> "Daniel" == Daniel Jacobowitz <drow@false.org> writes:
>
> Daniel> I won't look at this one, as I believe Tom's rewritten it to use
> Daniel> observers instead.
>
> Yeah, that's right.
>

Here's the new version.
--
[]'s
Thiago Jung Bauermann
IBM Linux Technology Center

[-- Attachment #2: python-hooks.diff --]
[-- Type: text/x-patch, Size: 6803 bytes --]

gdb/
2008-06-15 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.
* breakpoint.c (mention): Notify observers of created
breakpoint.
(delete_breakpoint): Notify observers of deleted
breakpoint.
* python/hooks.c: New file.
* python/python-internal.h (gdbpy_get_hook_function,
gdbpy_initialize_hooks): Declare.
* python/python.c (_initialize_python): Call gdbpy_initialize_hooks.

doc/
2008-06-15 Tom Tromey <tromey@redhat.com>

* observer.texi (Notifications): Document breakpoint_created
and breakpoint_deleted.

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index b15fa61..45ecfbd 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -257,9 +257,11 @@ SUBDIR_TUI_CFLAGS= \
#
# python sub directory definitons
#
-SUBDIR_PYTHON_OBS =
+SUBDIR_PYTHON_OBS = \
+ python-hooks.o
SUBDIR_PYTHON_SRCS = \
python/python.c \
+ python/hooks.c \
python/value.c
SUBDIR_PYTHON_DEPS =
SUBDIR_PYTHON_LDFLAGS=
@@ -3413,6 +3415,11 @@ python.o: $(srcdir)/python/python.c $(defs_h) $(python_h) \
$(exceptions_h) $(python_internal_h) $(version_h) $(cli_script_h) \
$(ui_out_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) \
+ $(observer_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) \
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 24ef7bf..4c2b1eb 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -4913,6 +4913,7 @@ mention (struct breakpoint *b)
if (deprecated_create_breakpoint_hook)
deprecated_create_breakpoint_hook (b);
breakpoint_create_event (b->number);
+ observer_notify_breakpoint_created (b->number);

if (b->ops != NULL && b->ops->print_mention != NULL)
b->ops->print_mention (b);
@@ -7141,6 +7142,7 @@ delete_breakpoint (struct breakpoint *bpt)
if (deprecated_delete_breakpoint_hook)
deprecated_delete_breakpoint_hook (bpt);
breakpoint_delete_event (bpt->number);
+ observer_notify_breakpoint_deleted (bpt->number);

if (breakpoint_chain == bpt)
breakpoint_chain = bpt->next;
diff --git a/gdb/doc/observer.texi b/gdb/doc/observer.texi
index 5bcc76c..b6aefab 100644
--- a/gdb/doc/observer.texi
+++ b/gdb/doc/observer.texi
@@ -2,7 +2,7 @@

@c This file is part of the GDB manual.
@c
-@c Copyright (C) 2003, 2004, 2005, 2006
+@c Copyright (C) 2003, 2004, 2005, 2006, 2008
@c Free Software Foundation, Inc.
@c
@c See the file gdbint.texinfo for copying conditions.
@@ -133,3 +133,13 @@ previously loaded symbol table data has now been invalidated.
The thread specified by @var{t} has been created.
@end deftypefun

+@deftypefun void breakpoint_created (int @var{bpnum})
+A new breakpoint has been created. The argument @var{bpnum} is the
+number of the newly-created breakpoint.
+@end deftypefun
+
+@deftypefun void breakpoint_deleted (int @var{bpnum})
+A breakpoint has been destroyed. The argument @var{bpnum} is the
+number of the newly-destroyed breakpoint.
+@end deftypefun
+
diff --git a/gdb/python/hooks.c b/gdb/python/hooks.c
new file mode 100644
index 0000000..21f3a28
--- /dev/null
+++ b/gdb/python/hooks.c
@@ -0,0 +1,62 @@
+/* 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"
+#include "observer.h"
+
+PyObject *
+gdbpy_get_hook_function (const char *name)
+{
+ PyObject *hooks;
+ PyObject *result;
+
+ 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))
+ {
+ Py_DECREF (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. */
+ result = PyObject_GetAttrString (hooks, (char *) name);
+ Py_DECREF (hooks);
+ return result;
+}
+
+void
+gdbpy_initialize_hooks (void)
+{
+ /* If you need to hook into any sort of state change in gdb, do so
+ by adding a new observer to observer.texi, attach to the new
+ observer here, and notify the new observer at the appropriate
+ place. */
+ observer_attach_breakpoint_created (gdbpy_breakpoint_created);
+ observer_attach_breakpoint_deleted (gdbpy_breakpoint_deleted);
+}
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 0abdfde..1bb2422 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -56,7 +56,10 @@ 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. */
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 5b8b975..2e8db56 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -282,6 +282,7 @@ the end of the command."));
PyModule_AddStringConstant (gdb_module, "HOST_CONFIG", (char*) host_name);
PyModule_AddStringConstant (gdb_module, "TARGET_CONFIG", (char*) target_name);

+ gdbpy_initialize_hooks ();
gdbpy_initialize_values ();

PyRun_SimpleString ("import gdb");