From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 8850 invoked by alias); 29 Apr 2008 15:59:02 -0000 Received: (qmail 8687 invoked by uid 22791); 29 Apr 2008 15:58:58 -0000 X-Spam-Check-By: sourceware.org Received: from igw2.br.ibm.com (HELO igw2.br.ibm.com) (32.104.18.25) by sourceware.org (qpsmtpd/0.31) with ESMTP; Tue, 29 Apr 2008 15:58:30 +0000 Received: from mailhub1.br.ibm.com (mailhub1 [9.18.232.109]) by igw2.br.ibm.com (Postfix) with ESMTP id ED18217F4D7 for ; Tue, 29 Apr 2008 12:48:27 -0300 (BRST) Received: from d24av01.br.ibm.com (d24av01.br.ibm.com [9.18.232.46]) by mailhub1.br.ibm.com (8.13.8/8.13.8/NCO v8.7) with ESMTP id m3TFwQJq3526790 for ; Tue, 29 Apr 2008 12:58:27 -0300 Received: from d24av01.br.ibm.com (loopback [127.0.0.1]) by d24av01.br.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id m3TFwFiE019018 for ; Tue, 29 Apr 2008 12:58:15 -0300 Received: from [9.18.238.95] (dyn531828.br.ibm.com [9.18.238.95]) by d24av01.br.ibm.com (8.12.11.20060308/8.12.11) with ESMTP id m3TFwFCr018715 for ; Tue, 29 Apr 2008 12:58:15 -0300 Message-Id: <20080429155304.641779989@br.ibm.com> References: <20080429155212.444237503@br.ibm.com> User-Agent: quilt/0.46-1 Date: Tue, 29 Apr 2008 15:59:00 -0000 From: Thiago Jung Bauermann To: gdb-patches@sourceware.org Subject: [RFC][patch 3/9] export hooks mechanism to Python Content-Disposition: inline; filename=python-hooks.diff Mime-Version: 1.0 X-Mailer: Evolution 2.12.3 Content-Transfer-Encoding: 7bit X-IsSubscribed: yes Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2008-04/txt/msg00660.txt.bz2 2008-04-29 Tom Tromey * 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 . */ + +#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