* [RFC - Python] New ObjFile event [not found] <AANLkTimybJtpUzy13FuLxQQMJfeWQuf3-cgqGmu23Bq5@mail.gmail.com> @ 2011-03-28 8:49 ` Kevin Pouget 2011-03-28 8:51 ` Kevin Pouget ` (3 more replies) 0 siblings, 4 replies; 26+ messages in thread From: Kevin Pouget @ 2011-03-28 8:49 UTC (permalink / raw) To: gdb-patches [-- Attachment #1: Type: text/plain, Size: 1393 bytes --] Hello, following this discussion at http://sourceware.org/ml/gdb/2011-03/msg00136.html, I would like to suggest a patch for the Python event system. Based on the ''exited", "cont", ... events, this patch allows a python script to register an interest to the loading of new object files in the debuggee. GDB observer "observer_attach_new_objfile" is used to trigger the Python callback. This patch completes the auto-loading feature (http://sourceware.org/gdb/current/onlinedocs/gdb/objfile_002dgdb_002epy-file.html#objfile_002dgdb_002epy-file), and, likewise, allows Python's "gdb.current_objfile ()" to return the current object file. Here is an example of it utilization: > def obj_handler (event): > if gdb.current_objfile () is not None: > print "-->",gdb.current_objfile ().filename > else: > print "---------------->Cleanup" > > gdb.events.newobjfile.connect (obj_handler) There is still one thing I'm not happy with in the code, it's how to 'properly' access "gdbpy_current_objfile" ? I commented out "static" and declared it "extern" when I needed it, but that's certainly not the best way: gdb/python/python.c /*static*/ struct objfile *gdbpy_current_objfile; gdb/python/py-inferior.c extern struct objfile *gdbpy_current_objfile; please let me know what you thing about it Cordially, Kevin [-- Attachment #2: gdb-newobjfile.diff --] [-- Type: application/octet-stream, Size: 6065 bytes --] ? gdb/python/py-newobjfileevent.c Index: gdb/Makefile.in =================================================================== RCS file: /cvs/src/src/gdb/Makefile.in,v retrieving revision 1.1163 diff -u -r1.1163 Makefile.in --- gdb/Makefile.in 9 Mar 2011 06:10:37 -0000 1.1163 +++ gdb/Makefile.in 28 Mar 2011 07:43:36 -0000 @@ -296,7 +296,8 @@ py-threadevent.o \ py-type.o \ py-utils.o \ - py-value.o + py-value.o \ + py-newobjfileevent.o SUBDIR_PYTHON_SRCS = \ python/python.c \ @@ -326,7 +327,8 @@ python/py-threadevent.c \ python/py-type.c \ python/py-utils.c \ - python/py-value.c + python/py-value.c \ + python/py-newobjfileevent.c SUBDIR_PYTHON_DEPS = SUBDIR_PYTHON_LDFLAGS= SUBDIR_PYTHON_CFLAGS= @@ -2110,6 +2112,11 @@ $(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-value.c $(POSTCOMPILE) +py-newobjfileevent.o: $(srcdir)/python/py-newobjfileevent.c + $(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-newobjfileevent.c + $(POSTCOMPILE) + + # # Dependency tracking. Most of this is conditional on GNU Make being # found by configure; if GNU Make is not found, we fall back to a Index: gdb/python/py-event.h =================================================================== RCS file: /cvs/src/src/gdb/python/py-event.h,v retrieving revision 1.1 diff -u -r1.1 py-event.h --- gdb/python/py-event.h 5 Feb 2011 05:27:23 -0000 1.1 +++ gdb/python/py-event.h 28 Mar 2011 07:43:36 -0000 @@ -105,6 +105,7 @@ extern int emit_continue_event (ptid_t ptid); extern int emit_exited_event (LONGEST exit_code); +extern int emit_new_objfile_event (struct objfile *objfile); extern int evpy_emit_event (PyObject *event, eventregistry_object *registry); Index: gdb/python/py-events.h =================================================================== RCS file: /cvs/src/src/gdb/python/py-events.h,v retrieving revision 1.1 diff -u -r1.1 py-events.h --- gdb/python/py-events.h 5 Feb 2011 05:27:23 -0000 1.1 +++ gdb/python/py-events.h 28 Mar 2011 07:43:36 -0000 @@ -45,6 +45,7 @@ eventregistry_object *stop; eventregistry_object *cont; eventregistry_object *exited; + eventregistry_object *newobjfile; PyObject *module; Index: gdb/python/py-evts.c =================================================================== RCS file: /cvs/src/src/gdb/python/py-evts.c,v retrieving revision 1.2 diff -u -r1.2 py-evts.c --- gdb/python/py-evts.c 14 Mar 2011 15:43:51 -0000 1.2 +++ gdb/python/py-evts.c 28 Mar 2011 07:43:36 -0000 @@ -57,6 +57,9 @@ if (add_new_registry (&gdb_py_events.exited, "exited") < 0) goto fail; + + if (add_new_registry (&gdb_py_events.newobjfile, "newobjfile") < 0) + goto fail; Py_INCREF (gdb_py_events.module); if (PyModule_AddObject (gdb_module, Index: gdb/python/py-inferior.c =================================================================== RCS file: /cvs/src/src/gdb/python/py-inferior.c,v retrieving revision 1.7 diff -u -r1.7 py-inferior.c --- gdb/python/py-inferior.c 17 Mar 2011 09:36:16 -0000 1.7 +++ gdb/python/py-inferior.c 28 Mar 2011 07:43:36 -0000 @@ -22,6 +22,7 @@ #include "gdbcore.h" #include "gdbthread.h" #include "inferior.h" +#include "objfiles.h" #include "observer.h" #include "python-internal.h" #include "arch-utils.h" @@ -129,6 +130,27 @@ do_cleanups (cleanup); } +extern struct objfile *gdbpy_current_objfile; +static void +python_new_objfile (struct objfile *objfile) +{ + struct cleanup *cleanup; + + /* Will be NULL when clearing the symtab. */ + if (objfile) + cleanup = ensure_python_env (get_objfile_arch (objfile), current_language); + else + cleanup = ensure_python_env (get_current_arch (), current_language); + + gdbpy_current_objfile = objfile; + + if (emit_new_objfile_event (objfile) < 0) + gdbpy_print_stack (); + + do_cleanups (cleanup); + gdbpy_current_objfile = NULL; +} + /* Return a borrowed reference to the Python object of type Inferior representing INFERIOR. If the object has already been created, return it, otherwise, create it. Return NULL on failure. */ @@ -669,7 +691,8 @@ observer_attach_normal_stop (python_on_normal_stop); observer_attach_target_resumed (python_on_resume); observer_attach_inferior_exit (python_inferior_exit); - + observer_attach_new_objfile (python_new_objfile); + if (PyType_Ready (&membuf_object_type) < 0) return; Index: gdb/python/python-internal.h =================================================================== RCS file: /cvs/src/src/gdb/python/python-internal.h,v retrieving revision 1.44 diff -u -r1.44 python-internal.h --- gdb/python/python-internal.h 28 Feb 2011 19:38:34 -0000 1.44 +++ gdb/python/python-internal.h 28 Mar 2011 07:43:36 -0000 @@ -207,6 +207,7 @@ void gdbpy_initialize_continue_event (void); void gdbpy_initialize_exited_event (void); void gdbpy_initialize_thread_event (void); +void gdbpy_initialize_new_objfile_event (void); struct cleanup *make_cleanup_py_decref (PyObject *py); Index: gdb/python/python.c =================================================================== RCS file: /cvs/src/src/gdb/python/python.c,v retrieving revision 1.63 diff -u -r1.63 python.c --- gdb/python/python.c 18 Mar 2011 08:44:47 -0000 1.63 +++ gdb/python/python.c 28 Mar 2011 07:43:36 -0000 @@ -811,8 +811,9 @@ /* The "current" objfile. This is set when gdb detects that a new objfile has been loaded. It is only set for the duration of a call to - source_python_script_for_objfile; it is NULL at other times. */ -static struct objfile *gdbpy_current_objfile; + source_python_script_for_objfile and new_objfile callbacks; it is NULL at + other times. */ +/*static*/ struct objfile *gdbpy_current_objfile; /* Set the current objfile to OBJFILE and then read STREAM,FILE as Python code. */ @@ -1074,6 +1075,7 @@ gdbpy_initialize_continue_event (); gdbpy_initialize_exited_event (); gdbpy_initialize_thread_event (); + gdbpy_initialize_new_objfile_event() ; PyRun_SimpleString ("import gdb"); PyRun_SimpleString ("gdb.pretty_printers = []"); ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [RFC - Python] New ObjFile event 2011-03-28 8:49 ` [RFC - Python] New ObjFile event Kevin Pouget @ 2011-03-28 8:51 ` Kevin Pouget 2011-03-28 9:00 ` Phil Muldoon 2011-03-28 8:54 ` Phil Muldoon ` (2 subsequent siblings) 3 siblings, 1 reply; 26+ messages in thread From: Kevin Pouget @ 2011-03-28 8:51 UTC (permalink / raw) To: gdb-patches sorry, gdb/python/py-newobjfileevent.c has not been included in the patch, here is its content: ----- /* Python interface to new object file loading events. Copyright (C) 2009, 2010, 2011 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 "py-event.h" static PyTypeObject new_objfile_event_object_type; PyObject * create_new_objfile_event_object (void) { return create_event_object (&new_objfile_event_object_type); } /* Callback function which notifies observers when a new objfile event occurs. This function will create a new Python new_objfile event object. Return -1 if emit fails. */ int emit_new_objfile_event (struct objfile *objfile) { PyObject *event; if (evregpy_no_listeners_p (gdb_py_events.newobjfile)) return 0; event = create_new_objfile_event_object (); if (event) return evpy_emit_event (event, gdb_py_events.newobjfile); return -1; } GDBPY_NEW_EVENT_TYPE (new_objfile, "gdb.NewObjFileEvent", "NewObjFileEvent", "GDB new object file event object", event_object_type, static); --- On Mon, Mar 28, 2011 at 4:06 AM, Kevin Pouget <kevin.pouget@gmail.com> wrote: > Hello, > > following this discussion at > http://sourceware.org/ml/gdb/2011-03/msg00136.html, I would like to > suggest a patch for the Python event system. Based on the ''exited", > "cont", ... events, this patch allows a python script to register an > interest to the loading of new object files in the debuggee. GDB > observer "observer_attach_new_objfile" is used to trigger the Python > callback. > > This patch completes the auto-loading feature > (http://sourceware.org/gdb/current/onlinedocs/gdb/objfile_002dgdb_002epy-file.html#objfile_002dgdb_002epy-file), > and, likewise, allows Python's "gdb.current_objfile ()" to return the current > object file. > > Here is an example of it utilization: > >> def obj_handler (event): >> if gdb.current_objfile () is not None: >> print "-->",gdb.current_objfile ().filename >> else: >> print "---------------->Cleanup" >> >> gdb.events.newobjfile.connect (obj_handler) > > > There is still one thing I'm not happy with in the code, it's how to > 'properly' access "gdbpy_current_objfile" ? I commented out "static" > and declared it "extern" when I needed it, but that's certainly not > the best way: > > gdb/python/python.c > /*static*/ struct objfile *gdbpy_current_objfile; > gdb/python/py-inferior.c > extern struct objfile *gdbpy_current_objfile; > > > please let me know what you thing about it > > Cordially, > > Kevin > ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [RFC - Python] New ObjFile event 2011-03-28 8:51 ` Kevin Pouget @ 2011-03-28 9:00 ` Phil Muldoon 0 siblings, 0 replies; 26+ messages in thread From: Phil Muldoon @ 2011-03-28 9:00 UTC (permalink / raw) To: Kevin Pouget; +Cc: gdb-patches Kevin Pouget <kevin.pouget@gmail.com> writes: > sorry, gdb/python/py-newobjfileevent.c has not been included in the > patch, here is its content: > > ----- > /* Python interface to new object file loading events. > > Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc. Just 2011 here. > emit_new_objfile_event (struct objfile *objfile) > { > PyObject *event; > > if (evregpy_no_listeners_p (gdb_py_events.newobjfile)) > return 0; > > event = create_new_objfile_event_object (); > if (event) > return evpy_emit_event (event, gdb_py_events.newobjfile); > return -1; > } Aha, ignore the comment from the previous patch. Cheers Phil ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [RFC - Python] New ObjFile event 2011-03-28 8:49 ` [RFC - Python] New ObjFile event Kevin Pouget 2011-03-28 8:51 ` Kevin Pouget @ 2011-03-28 8:54 ` Phil Muldoon 2011-03-28 9:03 ` Phil Muldoon 2011-03-28 20:28 ` Tom Tromey 3 siblings, 0 replies; 26+ messages in thread From: Phil Muldoon @ 2011-03-28 8:54 UTC (permalink / raw) To: Kevin Pouget; +Cc: gdb-patches Kevin Pouget <kevin.pouget@gmail.com> writes: > Hello, > > following this discussion at > http://sourceware.org/ml/gdb/2011-03/msg00136.html, I would like to > suggest a patch for the Python event system. Based on the ''exited", > "cont", ... events, this patch allows a python script to register an > interest to the loading of new object files in the debuggee. GDB > observer "observer_attach_new_objfile" is used to trigger the Python > callback. Just a few nits. I am not a maintainer, so please don't take this as any form of blessing ;) > + py-value.o \ > + py-newobjfileevent.o > > SUBDIR_PYTHON_SRCS = \ > python/python.c \ > @@ -326,7 +327,8 @@ > python/py-threadevent.c \ > python/py-type.c \ > python/py-utils.c \ > - python/py-value.c > + python/py-value.c \ > + python/py-newobjfileevent.c I'm not sure on the status of 8.3 file naming conventions, whether they matter anymore. > +extern struct objfile *gdbpy_current_objfile; > +static void > +python_new_objfile (struct objfile *objfile) > +{ > + struct cleanup *cleanup; > + > + /* Will be NULL when clearing the symtab. */ > + if (objfile) > + cleanup = ensure_python_env (get_objfile_arch (objfile), current_language); > + else > + cleanup = ensure_python_env (get_current_arch (), current_language); > + > + gdbpy_current_objfile = objfile; > + > + if (emit_new_objfile_event (objfile) < 0) > + gdbpy_print_stack (); The emit_new_objfile_event function seems to be missing? > -static struct objfile *gdbpy_current_objfile; > + source_python_script_for_objfile and new_objfile callbacks; it is NULL at > + other times. */ > +/*static*/ struct objfile *gdbpy_current_objfile; I realize this is something you are unsure of, but if you decide to emit 'static' can you remove the comment too. > /* Set the current objfile to OBJFILE and then read STREAM,FILE as > Python code. */ > @@ -1074,6 +1075,7 @@ > gdbpy_initialize_continue_event (); > gdbpy_initialize_exited_event (); > gdbpy_initialize_thread_event (); > + gdbpy_initialize_new_objfile_event() ; Needs a space after event and before the bracket. Cheers Phil ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [RFC - Python] New ObjFile event 2011-03-28 8:49 ` [RFC - Python] New ObjFile event Kevin Pouget 2011-03-28 8:51 ` Kevin Pouget 2011-03-28 8:54 ` Phil Muldoon @ 2011-03-28 9:03 ` Phil Muldoon 2011-03-28 10:02 ` Kevin Pouget 2011-03-28 20:28 ` Tom Tromey 3 siblings, 1 reply; 26+ messages in thread From: Phil Muldoon @ 2011-03-28 9:03 UTC (permalink / raw) To: Kevin Pouget; +Cc: gdb-patches Kevin Pouget <kevin.pouget@gmail.com> writes: > Hello, > > following this discussion at > http://sourceware.org/ml/gdb/2011-03/msg00136.html, I would like to > suggest a patch for the Python event system. Based on the ''exited", > "cont", ... events, this patch allows a python script to register an > interest to the loading of new object files in the debuggee. GDB > observer "observer_attach_new_objfile" is used to trigger the Python > callback. > > This patch completes the auto-loading feature Oops forgot to mention, this patch is missing tests, documentation and a ChangeLog entry. I realize this was/is an RFC, but the tests help "prove" the patch for maintainers. Looking good, though! Cheers Phil ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [RFC - Python] New ObjFile event 2011-03-28 9:03 ` Phil Muldoon @ 2011-03-28 10:02 ` Kevin Pouget 0 siblings, 0 replies; 26+ messages in thread From: Kevin Pouget @ 2011-03-28 10:02 UTC (permalink / raw) To: pmuldoon; +Cc: gdb-patches > I'm not sure on the status of 8.3 file naming conventions, whether they > matter anymore. I would say no, because python/py-threadevent.c already exists :) >> +/*static*/ struct objfile *gdbpy_current_objfile; > I realize this is something you are unsure of, but if you decide to emit > 'static' can you remove the comment too. sure, I let the 'static' in comments to highlight that I don't really want to do, this will be cleanup as soon as I get a better solution > + gdbpy_initialize_new_objfile_event() ; > Needs a space after event and before the bracket. done >> Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc. > Just 2011 here. done > Oops forgot to mention, this patch is missing tests, documentation and a > ChangeLog entry. I realize this was/is an RFC, but the tests help > "prove" the patch for maintainers. Looking good, though! I didn't know about it, I'll prepare it today Thanks for your advises, Kevin On Mon, Mar 28, 2011 at 4:54 AM, Phil Muldoon <pmuldoon@redhat.com> wrote: > Kevin Pouget <kevin.pouget@gmail.com> writes: > >> Hello, >> >> following this discussion at >> http://sourceware.org/ml/gdb/2011-03/msg00136.html, I would like to >> suggest a patch for the Python event system. Based on the ''exited", >> "cont", ... events, this patch allows a python script to register an >> interest to the loading of new object files in the debuggee. GDB >> observer "observer_attach_new_objfile" is used to trigger the Python >> callback. >> >> This patch completes the auto-loading feature > > Oops forgot to mention, this patch is missing tests, documentation and a > ChangeLog entry. I realize this was/is an RFC, but the tests help > "prove" the patch for maintainers. Looking good, though! > > Cheers > > Phil > ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [RFC - Python] New ObjFile event 2011-03-28 8:49 ` [RFC - Python] New ObjFile event Kevin Pouget ` (2 preceding siblings ...) 2011-03-28 9:03 ` Phil Muldoon @ 2011-03-28 20:28 ` Tom Tromey 2011-03-29 11:11 ` Kevin Pouget 3 siblings, 1 reply; 26+ messages in thread From: Tom Tromey @ 2011-03-28 20:28 UTC (permalink / raw) To: Kevin Pouget; +Cc: gdb-patches >>>>> "Kevin" == Kevin Pouget <kevin.pouget@gmail.com> writes: Kevin> following this discussion at Kevin> http://sourceware.org/ml/gdb/2011-03/msg00136.html, I would like to Kevin> suggest a patch for the Python event system. Based on the ''exited", Kevin> "cont", ... events, this patch allows a python script to register an Kevin> interest to the loading of new object files in the debuggee. GDB Kevin> observer "observer_attach_new_objfile" is used to trigger the Python Kevin> callback. It seems like a good idea. Do you have copyright assignment paperwork in place? If not, let me know and I can get you started. This is a requirement before we can accept a non-trivial patch. Kevin> and, likewise, allows Python's "gdb.current_objfile ()" to return Kevin> the current object file. I'm ambivalent about this part. It seems to me that the objfile should be an attribute of the event. Also setting it globally is ok, if you really want that, but it isn't a necessity. Kevin> There is still one thing I'm not happy with in the code, it's how to Kevin> 'properly' access "gdbpy_current_objfile" ? I commented out "static" Kevin> and declared it "extern" when I needed it, but that's certainly not Kevin> the best way: Dropping the `static' is ok. GDB is already full of this kind of thing. Kevin> please let me know what you thing about it Looks pretty good! Here are some notes on the patch itself. In addition to these, this patch needs a ChangeLog entry, a documentation update, and a test case. Kevin> +extern struct objfile *gdbpy_current_objfile; Should be in python-internal.h. Kevin> +static void Kevin> +python_new_objfile (struct objfile *objfile) New functions need an introductory comment. Kevin> + /* Will be NULL when clearing the symtab. */ Kevin> + if (objfile) Kevin> + cleanup = ensure_python_env (get_objfile_arch (objfile), current_language); Is this comment stale? It doesn't seem relevant. When can objfile==NULL? Kevin> - Kevin> + observer_attach_new_objfile (python_new_objfile); Kevin> + Gratuitous whitespace change. Kevin> int Kevin> emit_new_objfile_event (struct objfile *objfile) Kevin> { Kevin> PyObject *event; [...] Kevin> event = create_new_objfile_event_object (); Kevin> if (event) Kevin> return evpy_emit_event (event, gdb_py_events.newobjfile); Kevin> return -1; I think this should attach the objfile to the event object. Tom ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [RFC - Python] New ObjFile event 2011-03-28 20:28 ` Tom Tromey @ 2011-03-29 11:11 ` Kevin Pouget 2011-03-30 11:32 ` Kevin Pouget 0 siblings, 1 reply; 26+ messages in thread From: Kevin Pouget @ 2011-03-29 11:11 UTC (permalink / raw) To: Tom Tromey; +Cc: gdb-patches Hello, > Do you have copyright assignment paperwork in place? If not, let me > know and I can get you started. This is a requirement before we can > accept a non-trivial patch. No, not yet, what I am supposed to ? > Kevin> and, likewise, allows Python's "gdb.current_objfile ()" to return > Kevin> the current object file. > > I'm ambivalent about this part. > > It seems to me that the objfile should be an attribute of the event. > Also setting it globally is ok, if you really want that, but it isn't a > necessity. I was ambivalent about this point too; I chose "gdb.current_objfile ()" because autoloading (http://sourceware.org/gdb/current/onlinedocs/gdb/Auto_002dloading.html) already does it this way, and the two features are pretty similar. I felt that it would have been strange to access "gdb.current_objfile ()" in one case, and sth like "event.new_objfile" in the other case let me know if you agree with this perspective; it wouldn't be a big deal to flip to the other solution otherwise > Kevin> There is still one thing I'm not happy with in the code, it's how to > Kevin> 'properly' access "gdbpy_current_objfile" ? I commented out "static" > Kevin> and declared it "extern" when I needed it, but that's certainly not > Kevin> the best way: > > Dropping the `static' is ok. GDB is already full of this kind of thing. > > Kevin> +extern struct objfile *gdbpy_current_objfile; > > Should be in python-internal.h. fixed > Kevin> +static void > Kevin> +python_new_objfile (struct objfile *objfile) > > New functions need an introductory comment. > > Kevin> + /* Will be NULL when clearing the symtab. */ > Kevin> + if (objfile) > Kevin> + cleanup = ensure_python_env (get_objfile_arch (objfile), current_language); > > Is this comment stale? It doesn't seem relevant. When can objfile==NULL? I've been _once_ notified with NULL objfiles, coming from >> gdb/symfile.c:clear_symtab_users "observer_notify_new_objfile (NULL);" that's why I added the null-checking, but I didn't investigate what's behind this 'clear_symtab_users' and assumed 'observer_notify_new_objfile (NULL)' justifies the verification by itself is it okay for you? > Kevin> - > Kevin> + observer_attach_new_objfile (python_new_objfile); > Kevin> + > > Gratuitous whitespace change. removed > Kevin> int > Kevin> emit_new_objfile_event (struct objfile *objfile) > Kevin> { > Kevin> PyObject *event; > [...] > Kevin> event = create_new_objfile_event_object (); > Kevin> if (event) > Kevin> return evpy_emit_event (event, gdb_py_events.newobjfile); > Kevin> return -1; > > I think this should attach the objfile to the event object. that's discussed above, I'm waiting for your point of view * I added to the patch a few lines of documentation and updated the python event test-case. The exec is now linked with a shared-library automatically created, so that we know that the new objfile observers have to be notified. I'm not 100% confident about the test-case protocol, please don't hesitate to tell me if it's not resilient enough * I don't know how to add gdb/python/py-newobjfileevent.c and gdb/testsuite/gdb.python/py-events-shlib.c to the CVS diff, I'll move to git today so it should be fixed for the next patchs I send ; they are at the bottom of the mail, separated with '----' * I don't know if there is an 'automatic' way to update the ChangeLog (based on CVS/GIT), or if I should do it manually ? Cheers, Kevin ---- ? gdb/python/py-newobjfileevent.c ? gdb/testsuite/gdb.python/py-events-shlib.c Index: gdb/Makefile.in =================================================================== RCS file: /cvs/src/src/gdb/Makefile.in,v retrieving revision 1.1163 diff -u -r1.1163 Makefile.in --- gdb/Makefile.in 9 Mar 2011 06:10:37 -0000 1.1163 +++ gdb/Makefile.in 29 Mar 2011 08:08:41 -0000 @@ -296,7 +296,8 @@ py-threadevent.o \ py-type.o \ py-utils.o \ - py-value.o + py-value.o \ + py-newobjfileevent.o SUBDIR_PYTHON_SRCS = \ python/python.c \ @@ -326,7 +327,8 @@ python/py-threadevent.c \ python/py-type.c \ python/py-utils.c \ - python/py-value.c + python/py-value.c \ + python/py-newobjfileevent.c SUBDIR_PYTHON_DEPS = SUBDIR_PYTHON_LDFLAGS= SUBDIR_PYTHON_CFLAGS= @@ -2110,6 +2112,11 @@ $(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-value.c $(POSTCOMPILE) +py-newobjfileevent.o: $(srcdir)/python/py-newobjfileevent.c + $(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-newobjfileevent.c + $(POSTCOMPILE) + + # # Dependency tracking. Most of this is conditional on GNU Make being # found by configure; if GNU Make is not found, we fall back to a Index: gdb/doc/gdb.texinfo =================================================================== RCS file: /cvs/src/src/gdb/doc/gdb.texinfo,v retrieving revision 1.814 diff -u -r1.814 gdb.texinfo --- gdb/doc/gdb.texinfo 18 Mar 2011 08:44:46 -0000 1.814 +++ gdb/doc/gdb.texinfo 29 Mar 2011 08:08:45 -0000 @@ -22054,6 +22054,12 @@ This event indicates that the inferior or one of its threads has received as signal. @code{gdb.SignalEvent} has the following attributes: +@item events.newobjfile +Emits @code{gdb.NewObjFileEvent} which indicates that a new object-file has +been loaded in the inferior. + +During the callback, ``current objfile'' will be set to the new object file. + @table @code @defivar SignalEvent stop_signal A string representing the signal received by the inferior. A list of possible @@ -22696,10 +22702,10 @@ @findex gdb.current_objfile @defun current_objfile -When auto-loading a Python script (@pxref{Auto-loading}), @value{GDBN} -sets the ``current objfile'' to the corresponding objfile. This -function returns the current objfile. If there is no current objfile, -this function returns @code{None}. +When auto-loading a Python script (@pxref{Auto-loading}) and during new +object-file callbacks, @value{GDBN} sets the ``current objfile'' to the +corresponding objfile. This function returns the current objfile. If +there is no current objfile, this function returns @code{None}. @end defun @findex gdb.objfiles Index: gdb/python/py-event.h =================================================================== RCS file: /cvs/src/src/gdb/python/py-event.h,v retrieving revision 1.1 diff -u -r1.1 py-event.h --- gdb/python/py-event.h 5 Feb 2011 05:27:23 -0000 1.1 +++ gdb/python/py-event.h 29 Mar 2011 08:08:46 -0000 @@ -105,6 +105,7 @@ extern int emit_continue_event (ptid_t ptid); extern int emit_exited_event (LONGEST exit_code); +extern int emit_new_objfile_event (struct objfile *objfile); extern int evpy_emit_event (PyObject *event, eventregistry_object *registry); Index: gdb/python/py-events.h =================================================================== RCS file: /cvs/src/src/gdb/python/py-events.h,v retrieving revision 1.1 diff -u -r1.1 py-events.h --- gdb/python/py-events.h 5 Feb 2011 05:27:23 -0000 1.1 +++ gdb/python/py-events.h 29 Mar 2011 08:08:46 -0000 @@ -45,6 +45,7 @@ eventregistry_object *stop; eventregistry_object *cont; eventregistry_object *exited; + eventregistry_object *newobjfile; PyObject *module; Index: gdb/python/py-evts.c =================================================================== RCS file: /cvs/src/src/gdb/python/py-evts.c,v retrieving revision 1.2 diff -u -r1.2 py-evts.c --- gdb/python/py-evts.c 14 Mar 2011 15:43:51 -0000 1.2 +++ gdb/python/py-evts.c 29 Mar 2011 08:08:46 -0000 @@ -57,6 +57,9 @@ if (add_new_registry (&gdb_py_events.exited, "exited") < 0) goto fail; + + if (add_new_registry (&gdb_py_events.newobjfile, "newobjfile") < 0) + goto fail; Py_INCREF (gdb_py_events.module); if (PyModule_AddObject (gdb_module, Index: gdb/python/py-inferior.c =================================================================== RCS file: /cvs/src/src/gdb/python/py-inferior.c,v retrieving revision 1.7 diff -u -r1.7 py-inferior.c --- gdb/python/py-inferior.c 17 Mar 2011 09:36:16 -0000 1.7 +++ gdb/python/py-inferior.c 29 Mar 2011 08:08:46 -0000 @@ -22,6 +22,7 @@ #include "gdbcore.h" #include "gdbthread.h" #include "inferior.h" +#include "objfiles.h" #include "observer.h" #include "python-internal.h" #include "arch-utils.h" @@ -129,6 +130,31 @@ do_cleanups (cleanup); } +/* Callback used to notify Python listeners about new objfiles loaded in the + inferior. Python global variable 'gdb.current_objfile ()' will be set + during the notifications. + */ + +static void +python_new_objfile (struct objfile *objfile) +{ + struct cleanup *cleanup; + + /* Will be NULL when clearing the symtab. */ + if (objfile) + cleanup = ensure_python_env (get_objfile_arch (objfile), current_language); + else + cleanup = ensure_python_env (get_current_arch (), current_language); + + gdbpy_current_objfile = objfile; + + if (emit_new_objfile_event (objfile) < 0) + gdbpy_print_stack (); + + do_cleanups (cleanup); + gdbpy_current_objfile = NULL; +} + /* Return a borrowed reference to the Python object of type Inferior representing INFERIOR. If the object has already been created, return it, otherwise, create it. Return NULL on failure. */ @@ -669,6 +695,7 @@ observer_attach_normal_stop (python_on_normal_stop); observer_attach_target_resumed (python_on_resume); observer_attach_inferior_exit (python_inferior_exit); + observer_attach_new_objfile (python_new_objfile); if (PyType_Ready (&membuf_object_type) < 0) return; Index: gdb/python/python-internal.h =================================================================== RCS file: /cvs/src/src/gdb/python/python-internal.h,v retrieving revision 1.44 diff -u -r1.44 python-internal.h --- gdb/python/python-internal.h 28 Feb 2011 19:38:34 -0000 1.44 +++ gdb/python/python-internal.h 29 Mar 2011 08:08:46 -0000 @@ -207,6 +207,7 @@ void gdbpy_initialize_continue_event (void); void gdbpy_initialize_exited_event (void); void gdbpy_initialize_thread_event (void); +void gdbpy_initialize_new_objfile_event (void); struct cleanup *make_cleanup_py_decref (PyObject *py); @@ -215,6 +216,7 @@ extern struct gdbarch *python_gdbarch; extern const struct language_defn *python_language; +extern struct objfile *gdbpy_current_objfile; /* Use this after a TRY_EXCEPT to throw the appropriate Python exception. */ Index: gdb/python/python.c =================================================================== RCS file: /cvs/src/src/gdb/python/python.c,v retrieving revision 1.63 diff -u -r1.63 python.c --- gdb/python/python.c 18 Mar 2011 08:44:47 -0000 1.63 +++ gdb/python/python.c 29 Mar 2011 08:08:46 -0000 @@ -811,8 +811,9 @@ /* The "current" objfile. This is set when gdb detects that a new objfile has been loaded. It is only set for the duration of a call to - source_python_script_for_objfile; it is NULL at other times. */ -static struct objfile *gdbpy_current_objfile; + source_python_script_for_objfile and new_objfile callbacks; it is NULL at + other times. */ +struct objfile *gdbpy_current_objfile; /* Set the current objfile to OBJFILE and then read STREAM,FILE as Python code. */ @@ -1074,6 +1075,7 @@ gdbpy_initialize_continue_event (); gdbpy_initialize_exited_event (); gdbpy_initialize_thread_event (); + gdbpy_initialize_new_objfile_event () ; PyRun_SimpleString ("import gdb"); PyRun_SimpleString ("gdb.pretty_printers = []"); Index: gdb/testsuite/gdb.python/py-events.c =================================================================== RCS file: /cvs/src/src/gdb/testsuite/gdb.python/py-events.c,v retrieving revision 1.1 diff -u -r1.1 py-events.c --- gdb/testsuite/gdb.python/py-events.c 5 Feb 2011 05:27:23 -0000 1.1 +++ gdb/testsuite/gdb.python/py-events.c 29 Mar 2011 08:08:46 -0000 @@ -16,6 +16,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ +extern void do_nothing (void) ; + int second(){ return 12; } @@ -25,5 +27,6 @@ } int main (){ + do_nothing() ; return first(); } Index: gdb/testsuite/gdb.python/py-events.exp =================================================================== RCS file: /cvs/src/src/gdb/testsuite/gdb.python/py-events.exp,v retrieving revision 1.1 diff -u -r1.1 py-events.exp --- gdb/testsuite/gdb.python/py-events.exp 5 Feb 2011 05:27:23 -0000 1.1 +++ gdb/testsuite/gdb.python/py-events.exp 29 Mar 2011 08:08:46 -0000 @@ -24,23 +24,45 @@ load_lib gdb-python.exp +set libfile "py-events-shlib" +set libsrc $srcdir/$subdir/$libfile.c +set lib_sl $objdir/$subdir/$libfile.so +set lib_opts debug + set testfile "py-events" set srcfile ${testfile}.c set binfile ${objdir}/${subdir}/${testfile} +set exec_opts [list debug shlib=$lib_sl] set pyfile ${srcdir}/${subdir}/${testfile}.py -if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } { +if [get_compiler_info ${binfile}] { + return -1 +} + +if { [gdb_compile_shlib $libsrc $lib_sl $lib_opts] != "" + || [gdb_compile $srcdir/$subdir/$srcfile $binfile executable $exec_opts] != ""} { + untested "Could not compile either $libsrc or $srcdir/$subdir/$srcfile." return -1 } +# Start with a fresh gdb. + +gdb_exit +gdb_start + if { [skip_python_tests] } { continue } +gdb_reinitialize_dir $srcdir/$subdir +gdb_load ${binfile} + gdb_test_no_output "python execfile ('${pyfile}')" "" -if ![runto_main ] then { - fail "Can't run to main" - return -1 -} +gdb_test "Test_Newobj_Events" "New ObjectFile Event tester registered." + +gdb_breakpoint "main" {temporary} + +gdb_test "run" ".*event type: new_objfile.* +.*new objfile name.*" gdb_test "Test_Events" "Event testers registered." Index: gdb/testsuite/gdb.python/py-events.py =================================================================== RCS file: /cvs/src/src/gdb/testsuite/gdb.python/py-events.py,v retrieving revision 1.1 diff -u -r1.1 py-events.py --- gdb/testsuite/gdb.python/py-events.py 5 Feb 2011 05:27:23 -0000 1.1 +++ gdb/testsuite/gdb.python/py-events.py 29 Mar 2011 08:08:46 -0000 @@ -48,6 +48,14 @@ if ( event.inferior_thread is not None) : print "thread num: %s" % (event.inferior_thread.num); +def new_objfile_handler (event): + if (isinstance (event, gdb.NewObjFileEvent)): + print "event type: new_objfile" + if (gdb.current_objfile () is not None): + print "new objfile name: %s" % (gdb.current_objfile ().filename) + else: + print "new objfile is None" + class test_events (gdb.Command): """Test events.""" @@ -62,3 +70,15 @@ print "Event testers registered." test_events () + +class test_newobj_events (gdb.Command): + """NewObj events.""" + + def __init__ (self): + gdb.Command.__init__ (self, "test_newobj_events", gdb.COMMAND_STACK) + + def invoke (self, arg, from_tty): + gdb.events.newobjfile.connect (new_objfile_handler) + print "New ObjectFile Event tester registered." + +test_newobj_events () ---- /* Python interface to new object file loading events. Copyright (C) 2011 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 "py-event.h" static PyTypeObject new_objfile_event_object_type; PyObject * create_new_objfile_event_object (void) { return create_event_object (&new_objfile_event_object_type); } /* Callback function which notifies observers when a new objfile event occurs. This function will create a new Python new_objfile event object. Return -1 if emit fails. */ int emit_new_objfile_event (struct objfile *objfile) { PyObject *event; if (evregpy_no_listeners_p (gdb_py_events.newobjfile)) return 0; event = create_new_objfile_event_object (); if (event) return evpy_emit_event (event, gdb_py_events.newobjfile); return -1; } GDBPY_NEW_EVENT_TYPE (new_objfile, "gdb.NewObjFileEvent", "NewObjFileEvent", "GDB new object file event object", event_object_type, static); ---- /* This testcase is part of GDB, the GNU debugger. Copyright 2011 Free Software Foundation, Inc. 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/>. */ void do_nothing (void) {} ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [RFC - Python] New ObjFile event 2011-03-29 11:11 ` Kevin Pouget @ 2011-03-30 11:32 ` Kevin Pouget 2011-03-30 12:14 ` Phil Muldoon 0 siblings, 1 reply; 26+ messages in thread From: Kevin Pouget @ 2011-03-30 11:32 UTC (permalink / raw) To: Tom Tromey; +Cc: gdb-patches Hello, here is a patch with all the modifications we discussed so far, including the ChangeLog entry and the new files Cordially, Kevin -- From eec9cb69818ec72184b59ca0d928fcbc590c558f Mon Sep 17 00:00:00 2001 From: Kevin Pouget <kevin.pouget@st.com> Date: Tue, 29 Mar 2011 11:26:34 -0400 Subject: [PATCH] Allow Python notification of new object-file loadings Signed-off-by: Kevin Pouget <kevin.pouget@st.com> --- gdb/ChangeLog | 28 +++++++++++++++ gdb/Makefile.in | 11 +++++- gdb/doc/gdb.texinfo | 14 +++++-- gdb/python/py-event.h | 1 + gdb/python/py-events.h | 1 + gdb/python/py-evts.c | 3 ++ gdb/python/py-inferior.c | 27 ++++++++++++++ gdb/python/py-newobjfileevent.c | 53 ++++++++++++++++++++++++++++ gdb/python/python-internal.h | 2 + gdb/python/python.c | 6 ++- gdb/testsuite/gdb.python/py-events-shlib.c | 20 ++++++++++ gdb/testsuite/gdb.python/py-events.c | 3 ++ gdb/testsuite/gdb.python/py-events.exp | 32 ++++++++++++++--- gdb/testsuite/gdb.python/py-events.py | 20 ++++++++++ 14 files changed, 208 insertions(+), 13 deletions(-) create mode 100644 gdb/python/py-newobjfileevent.c create mode 100644 gdb/testsuite/gdb.python/py-events-shlib.c diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 03885dd..2bceb45 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,31 @@ +2011-03-29 Kevin Pouget <kevin.pouget@st.com> + + Allow Python notification of new object-file loadings + * Makefile.in (SUBDIR_PYTHON_SRCS): Add py-newobjfilevent.c. + (SUBDIR_PYTHON_OBS): Add py-newobjfileevent.o. + Add build rule for this file. + * python/py-event.h (emit_new_objfile_event): New prototype. + (newobjfile): New Python event emitter. + * python/py-evts.c (gdbpy_initialize_py_events): Add newobjfile to + Python event registry. + * python/py-inferior.c: Include objfiles.h + (python_new_objfile): New function. + (gdbpy_initialize_inferior): Add python_new_objfile to the new objfile + observers. + * python/py-newobjfileevent.c: New file. + * python-internal.h (gdbpy_initialize_new_objfile_event): New + prototype. + (gdbpy_current_objfile): New global variable. + * python/python.c (gdbpy_current_objfile): Make global. + (_initialize_python): Add gdbpy_initialize_new_objfile_event call. + * testsuite/gdb.python/py-events-shlib.c: New file. + * testsuite/gdb.python/py-events.c (do_nothing): New global. + (main): Add call to shared library. + * testsuite/gdb.python/py-events.exp: Link the execfile to a shared + library and check newobjfile event notification. + * testsuite/gdb.python/py-events.py (new_objfile_handler): New Class. + (test_newobj_events): New class. + 2011-03-28 Jan Kratochvil <jan.kratochvil@redhat.com> Support resolution of STT_GNU_IFUNC via breakpoints. diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 489b1e9..3b79087 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -296,7 +296,8 @@ SUBDIR_PYTHON_OBS = \ py-threadevent.o \ py-type.o \ py-utils.o \ - py-value.o + py-value.o \ + py-newobjfileevent.o SUBDIR_PYTHON_SRCS = \ python/python.c \ @@ -326,7 +327,8 @@ SUBDIR_PYTHON_SRCS = \ python/py-threadevent.c \ python/py-type.c \ python/py-utils.c \ - python/py-value.c + python/py-value.c \ + python/py-newobjfileevent.c SUBDIR_PYTHON_DEPS = SUBDIR_PYTHON_LDFLAGS= SUBDIR_PYTHON_CFLAGS= @@ -2110,6 +2112,11 @@ py-value.o: $(srcdir)/python/py-value.c $(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-value.c $(POSTCOMPILE) +py-newobjfileevent.o: $(srcdir)/python/py-newobjfileevent.c + $(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-newobjfileevent.c + $(POSTCOMPILE) + + # # Dependency tracking. Most of this is conditional on GNU Make being # found by configure; if GNU Make is not found, we fall back to a diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index a4e976f..360ff51 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -22057,6 +22057,12 @@ Emits @code{gdb.SignalEvent} which extends @code{gdb.StopEvent}. This event indicates that the inferior or one of its threads has received as signal. @code{gdb.SignalEvent} has the following attributes: +@item events.newobjfile +Emits @code{gdb.NewObjFileEvent} which indicates that a new object-file has +been loaded in the inferior. + +During the callback, ``current objfile'' will be set to the new object file. + @table @code @defivar SignalEvent stop_signal A string representing the signal received by the inferior. A list of possible @@ -22699,10 +22705,10 @@ The following objfile-related functions are available in the @findex gdb.current_objfile @defun current_objfile -When auto-loading a Python script (@pxref{Auto-loading}), @value{GDBN} -sets the ``current objfile'' to the corresponding objfile. This -function returns the current objfile. If there is no current objfile, -this function returns @code{None}. +When auto-loading a Python script (@pxref{Auto-loading}) and during new +object-file callbacks, @value{GDBN} sets the ``current objfile'' to the +corresponding objfile. This function returns the current objfile. If +there is no current objfile, this function returns @code{None}. @end defun @findex gdb.objfiles diff --git a/gdb/python/py-event.h b/gdb/python/py-event.h index bc95521..9a3ba82 100644 --- a/gdb/python/py-event.h +++ b/gdb/python/py-event.h @@ -105,6 +105,7 @@ typedef struct extern int emit_continue_event (ptid_t ptid); extern int emit_exited_event (LONGEST exit_code); +extern int emit_new_objfile_event (struct objfile *objfile); extern int evpy_emit_event (PyObject *event, eventregistry_object *registry); diff --git a/gdb/python/py-events.h b/gdb/python/py-events.h index 6d4dae5..9c42e39 100644 --- a/gdb/python/py-events.h +++ b/gdb/python/py-events.h @@ -45,6 +45,7 @@ typedef struct eventregistry_object *stop; eventregistry_object *cont; eventregistry_object *exited; + eventregistry_object *newobjfile; PyObject *module; diff --git a/gdb/python/py-evts.c b/gdb/python/py-evts.c index 50c05f3..ee60560 100644 --- a/gdb/python/py-evts.c +++ b/gdb/python/py-evts.c @@ -57,6 +57,9 @@ gdbpy_initialize_py_events (void) if (add_new_registry (&gdb_py_events.exited, "exited") < 0) goto fail; + + if (add_new_registry (&gdb_py_events.newobjfile, "newobjfile") < 0) + goto fail; Py_INCREF (gdb_py_events.module); if (PyModule_AddObject (gdb_module, diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index b9df394..ebd2a2a 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -22,6 +22,7 @@ #include "gdbcore.h" #include "gdbthread.h" #include "inferior.h" +#include "objfiles.h" #include "observer.h" #include "python-internal.h" #include "arch-utils.h" @@ -129,6 +130,31 @@ python_inferior_exit (struct inferior *inf) do_cleanups (cleanup); } +/* Callback used to notify Python listeners about new objfiles loaded in the + inferior. Python global variable 'gdb.current_objfile ()' will be set + during the notifications. + */ + +static void +python_new_objfile (struct objfile *objfile) +{ + struct cleanup *cleanup; + + /* Will be NULL when clearing the symtab. */ + if (objfile) + cleanup = ensure_python_env (get_objfile_arch (objfile), current_language); + else + cleanup = ensure_python_env (get_current_arch (), current_language); + + gdbpy_current_objfile = objfile; + + if (emit_new_objfile_event (objfile) < 0) + gdbpy_print_stack (); + + do_cleanups (cleanup); + gdbpy_current_objfile = NULL; +} + /* Return a borrowed reference to the Python object of type Inferior representing INFERIOR. If the object has already been created, return it, otherwise, create it. Return NULL on failure. */ @@ -669,6 +695,7 @@ gdbpy_initialize_inferior (void) observer_attach_normal_stop (python_on_normal_stop); observer_attach_target_resumed (python_on_resume); observer_attach_inferior_exit (python_inferior_exit); + observer_attach_new_objfile (python_new_objfile); if (PyType_Ready (&membuf_object_type) < 0) return; diff --git a/gdb/python/py-newobjfileevent.c b/gdb/python/py-newobjfileevent.c new file mode 100644 index 0000000..20e2b31 --- /dev/null +++ b/gdb/python/py-newobjfileevent.c @@ -0,0 +1,53 @@ +/* Python interface to new object file loading events. + + Copyright (C) 2011 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 "py-event.h" + +static PyTypeObject new_objfile_event_object_type; + +PyObject * +create_new_objfile_event_object (void) +{ + return create_event_object (&new_objfile_event_object_type); +} + +/* Callback function which notifies observers when a new objfile event occurs. + This function will create a new Python new_objfile event object. + Return -1 if emit fails. */ + +int +emit_new_objfile_event (struct objfile *objfile) +{ + PyObject *event; + + if (evregpy_no_listeners_p (gdb_py_events.newobjfile)) + return 0; + + event = create_new_objfile_event_object (); + if (event) + return evpy_emit_event (event, gdb_py_events.newobjfile); + return -1; +} + +GDBPY_NEW_EVENT_TYPE (new_objfile, + "gdb.NewObjFileEvent", + "NewObjFileEvent", + "GDB new object file event object", + event_object_type, + static); diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index d3cb788..cc9108f 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -207,6 +207,7 @@ void gdbpy_initialize_breakpoint_event (void); void gdbpy_initialize_continue_event (void); void gdbpy_initialize_exited_event (void); void gdbpy_initialize_thread_event (void); +void gdbpy_initialize_new_objfile_event (void); struct cleanup *make_cleanup_py_decref (PyObject *py); @@ -215,6 +216,7 @@ struct cleanup *ensure_python_env (struct gdbarch *gdbarch, extern struct gdbarch *python_gdbarch; extern const struct language_defn *python_language; +extern struct objfile *gdbpy_current_objfile; /* 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 90d5dc8..df883ef 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -811,8 +811,9 @@ gdbpy_progspaces (PyObject *unused1, PyObject *unused2) /* The "current" objfile. This is set when gdb detects that a new objfile has been loaded. It is only set for the duration of a call to - source_python_script_for_objfile; it is NULL at other times. */ -static struct objfile *gdbpy_current_objfile; + source_python_script_for_objfile and new_objfile callbacks; it is NULL at + other times. */ +struct objfile *gdbpy_current_objfile; /* Set the current objfile to OBJFILE and then read STREAM,FILE as Python code. */ @@ -1074,6 +1075,7 @@ Enables or disables printing of Python stack traces."), gdbpy_initialize_continue_event (); gdbpy_initialize_exited_event (); gdbpy_initialize_thread_event (); + gdbpy_initialize_new_objfile_event () ; PyRun_SimpleString ("import gdb"); PyRun_SimpleString ("gdb.pretty_printers = []"); diff --git a/gdb/testsuite/gdb.python/py-events-shlib.c b/gdb/testsuite/gdb.python/py-events-shlib.c new file mode 100644 index 0000000..2a17bbf --- /dev/null +++ b/gdb/testsuite/gdb.python/py-events-shlib.c @@ -0,0 +1,20 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2011 Free Software Foundation, Inc. + + 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/>. */ + + +void do_nothing (void) {} + diff --git a/gdb/testsuite/gdb.python/py-events.c b/gdb/testsuite/gdb.python/py-events.c index ceb697e..1cf7cc7 100644 --- a/gdb/testsuite/gdb.python/py-events.c +++ b/gdb/testsuite/gdb.python/py-events.c @@ -16,6 +16,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ +extern void do_nothing (void) ; + int second(){ return 12; } @@ -25,5 +27,6 @@ int first(){ } int main (){ + do_nothing() ; return first(); } diff --git a/gdb/testsuite/gdb.python/py-events.exp b/gdb/testsuite/gdb.python/py-events.exp index e5d6daf..56e8f86 100644 --- a/gdb/testsuite/gdb.python/py-events.exp +++ b/gdb/testsuite/gdb.python/py-events.exp @@ -24,23 +24,45 @@ if $tracelevel then { load_lib gdb-python.exp +set libfile "py-events-shlib" +set libsrc $srcdir/$subdir/$libfile.c +set lib_sl $objdir/$subdir/$libfile.so +set lib_opts debug + set testfile "py-events" set srcfile ${testfile}.c set binfile ${objdir}/${subdir}/${testfile} +set exec_opts [list debug shlib=$lib_sl] set pyfile ${srcdir}/${subdir}/${testfile}.py -if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } { +if [get_compiler_info ${binfile}] { + return -1 +} + +if { [gdb_compile_shlib $libsrc $lib_sl $lib_opts] != "" + || [gdb_compile $srcdir/$subdir/$srcfile $binfile executable $exec_opts] != ""} { + untested "Could not compile either $libsrc or $srcdir/$subdir/$srcfile." return -1 } +# Start with a fresh gdb. + +gdb_exit +gdb_start + if { [skip_python_tests] } { continue } +gdb_reinitialize_dir $srcdir/$subdir +gdb_load ${binfile} + gdb_test_no_output "python execfile ('${pyfile}')" "" -if ![runto_main ] then { - fail "Can't run to main" - return -1 -} +gdb_test "Test_Newobj_Events" "New ObjectFile Event tester registered." + +gdb_breakpoint "main" {temporary} + +gdb_test "run" ".*event type: new_objfile.* +.*new objfile name.*" gdb_test "Test_Events" "Event testers registered." diff --git a/gdb/testsuite/gdb.python/py-events.py b/gdb/testsuite/gdb.python/py-events.py index 9f05b9f..12e36e8 100644 --- a/gdb/testsuite/gdb.python/py-events.py +++ b/gdb/testsuite/gdb.python/py-events.py @@ -48,6 +48,14 @@ def continue_handler (event): if ( event.inferior_thread is not None) : print "thread num: %s" % (event.inferior_thread.num); +def new_objfile_handler (event): + if (isinstance (event, gdb.NewObjFileEvent)): + print "event type: new_objfile" + if (gdb.current_objfile () is not None): + print "new objfile name: %s" % (gdb.current_objfile ().filename) + else: + print "new objfile is None" + class test_events (gdb.Command): """Test events.""" @@ -62,3 +70,15 @@ class test_events (gdb.Command): print "Event testers registered." test_events () + +class test_newobj_events (gdb.Command): + """NewObj events.""" + + def __init__ (self): + gdb.Command.__init__ (self, "test_newobj_events", gdb.COMMAND_STACK) + + def invoke (self, arg, from_tty): + gdb.events.newobjfile.connect (new_objfile_handler) + print "New ObjectFile Event tester registered." + +test_newobj_events () -- 1.7.4 On Tue, Mar 29, 2011 at 4:21 AM, Kevin Pouget <kevin.pouget@gmail.com> wrote: > > Hello, > > > > Do you have copyright assignment paperwork in place? If not, let me > > know and I can get you started. This is a requirement before we can > > accept a non-trivial patch. > > No, not yet, what I am supposed to ? > > > Kevin> and, likewise, allows Python's "gdb.current_objfile ()" to return > > Kevin> the current object file. > > > > I'm ambivalent about this part. > > > > It seems to me that the objfile should be an attribute of the event. > > Also setting it globally is ok, if you really want that, but it isn't a > > necessity. > > I was ambivalent about this point too; I chose "gdb.current_objfile > ()" because autoloading > (http://sourceware.org/gdb/current/onlinedocs/gdb/Auto_002dloading.html) > already does it this way, and the two features are pretty similar. I > felt that it would have been strange to access "gdb.current_objfile > ()" in one case, and sth like "event.new_objfile" in the other case > > let me know if you agree with this perspective; it wouldn't be a big > deal to flip to the other solution otherwise > > > Kevin> There is still one thing I'm not happy with in the code, it's how to > > Kevin> 'properly' access "gdbpy_current_objfile" ? I commented out "static" > > Kevin> and declared it "extern" when I needed it, but that's certainly not > > Kevin> the best way: > > > > Dropping the `static' is ok. GDB is already full of this kind of thing. > > > > Kevin> +extern struct objfile *gdbpy_current_objfile; > > > > Should be in python-internal.h. > > fixed > > > Kevin> +static void > > Kevin> +python_new_objfile (struct objfile *objfile) > > > > New functions need an introductory comment. > > > > Kevin> + /* Will be NULL when clearing the symtab. */ > > Kevin> + if (objfile) > > Kevin> + cleanup = ensure_python_env (get_objfile_arch (objfile), current_language); > > > > Is this comment stale? It doesn't seem relevant. When can objfile==NULL? > > I've been _once_ notified with NULL objfiles, coming from > >> gdb/symfile.c:clear_symtab_users "observer_notify_new_objfile (NULL);" > > that's why I added the null-checking, but I didn't investigate what's > behind this 'clear_symtab_users' and assumed > 'observer_notify_new_objfile (NULL)' justifies the verification by > itself > > is it okay for you? > > > Kevin> - > > Kevin> + observer_attach_new_objfile (python_new_objfile); > > Kevin> + > > > > Gratuitous whitespace change. > > removed > > > Kevin> int > > Kevin> emit_new_objfile_event (struct objfile *objfile) > > Kevin> { > > Kevin> PyObject *event; > > [...] > > Kevin> event = create_new_objfile_event_object (); > > Kevin> if (event) > > Kevin> return evpy_emit_event (event, gdb_py_events.newobjfile); > > Kevin> return -1; > > > > I think this should attach the objfile to the event object. > > that's discussed above, I'm waiting for your point of view > > * I added to the patch a few lines of documentation and updated the > python event test-case. The exec is now linked with a shared-library > automatically created, so that we know that the new objfile observers > have to be notified. I'm not 100% confident about the test-case > protocol, please don't hesitate to tell me if it's not resilient > enough > > * I don't know how to add gdb/python/py-newobjfileevent.c and > gdb/testsuite/gdb.python/py-events-shlib.c to the CVS diff, I'll move > to git today so it should be fixed for the next patchs I send ; they > are at the bottom of the mail, separated with '----' > > * I don't know if there is an 'automatic' way to update the ChangeLog > (based on CVS/GIT), or if I should do it manually ? > > > Cheers, > > Kevin > > > ---- > > ? gdb/python/py-newobjfileevent.c > ? gdb/testsuite/gdb.python/py-events-shlib.c > Index: gdb/Makefile.in > =================================================================== > RCS file: /cvs/src/src/gdb/Makefile.in,v > retrieving revision 1.1163 > diff -u -r1.1163 Makefile.in > --- gdb/Makefile.in 9 Mar 2011 06:10:37 -0000 1.1163 > +++ gdb/Makefile.in 29 Mar 2011 08:08:41 -0000 > @@ -296,7 +296,8 @@ > py-threadevent.o \ > py-type.o \ > py-utils.o \ > - py-value.o > + py-value.o \ > + py-newobjfileevent.o > > SUBDIR_PYTHON_SRCS = \ > python/python.c \ > @@ -326,7 +327,8 @@ > python/py-threadevent.c \ > python/py-type.c \ > python/py-utils.c \ > - python/py-value.c > + python/py-value.c \ > + python/py-newobjfileevent.c > SUBDIR_PYTHON_DEPS = > SUBDIR_PYTHON_LDFLAGS= > SUBDIR_PYTHON_CFLAGS= > @@ -2110,6 +2112,11 @@ > $(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-value.c > $(POSTCOMPILE) > > +py-newobjfileevent.o: $(srcdir)/python/py-newobjfileevent.c > + $(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-newobjfileevent.c > + $(POSTCOMPILE) > + > + > # > # Dependency tracking. Most of this is conditional on GNU Make being > # found by configure; if GNU Make is not found, we fall back to a > Index: gdb/doc/gdb.texinfo > =================================================================== > RCS file: /cvs/src/src/gdb/doc/gdb.texinfo,v > retrieving revision 1.814 > diff -u -r1.814 gdb.texinfo > --- gdb/doc/gdb.texinfo 18 Mar 2011 08:44:46 -0000 1.814 > +++ gdb/doc/gdb.texinfo 29 Mar 2011 08:08:45 -0000 > @@ -22054,6 +22054,12 @@ > This event indicates that the inferior or one of its threads has received as > signal. @code{gdb.SignalEvent} has the following attributes: > > +@item events.newobjfile > +Emits @code{gdb.NewObjFileEvent} which indicates that a new object-file has > +been loaded in the inferior. > + > +During the callback, ``current objfile'' will be set to the new object file. > + > @table @code > @defivar SignalEvent stop_signal > A string representing the signal received by the inferior. A list of possible > @@ -22696,10 +22702,10 @@ > > @findex gdb.current_objfile > @defun current_objfile > -When auto-loading a Python script (@pxref{Auto-loading}), @value{GDBN} > -sets the ``current objfile'' to the corresponding objfile. This > -function returns the current objfile. If there is no current objfile, > -this function returns @code{None}. > +When auto-loading a Python script (@pxref{Auto-loading}) and during new > +object-file callbacks, @value{GDBN} sets the ``current objfile'' to the > +corresponding objfile. This function returns the current objfile. If > +there is no current objfile, this function returns @code{None}. > @end defun > > @findex gdb.objfiles > Index: gdb/python/py-event.h > =================================================================== > RCS file: /cvs/src/src/gdb/python/py-event.h,v > retrieving revision 1.1 > diff -u -r1.1 py-event.h > --- gdb/python/py-event.h 5 Feb 2011 05:27:23 -0000 1.1 > +++ gdb/python/py-event.h 29 Mar 2011 08:08:46 -0000 > @@ -105,6 +105,7 @@ > > extern int emit_continue_event (ptid_t ptid); > extern int emit_exited_event (LONGEST exit_code); > +extern int emit_new_objfile_event (struct objfile *objfile); > > extern int evpy_emit_event (PyObject *event, > eventregistry_object *registry); > Index: gdb/python/py-events.h > =================================================================== > RCS file: /cvs/src/src/gdb/python/py-events.h,v > retrieving revision 1.1 > diff -u -r1.1 py-events.h > --- gdb/python/py-events.h 5 Feb 2011 05:27:23 -0000 1.1 > +++ gdb/python/py-events.h 29 Mar 2011 08:08:46 -0000 > @@ -45,6 +45,7 @@ > eventregistry_object *stop; > eventregistry_object *cont; > eventregistry_object *exited; > + eventregistry_object *newobjfile; > > PyObject *module; > > Index: gdb/python/py-evts.c > =================================================================== > RCS file: /cvs/src/src/gdb/python/py-evts.c,v > retrieving revision 1.2 > diff -u -r1.2 py-evts.c > --- gdb/python/py-evts.c 14 Mar 2011 15:43:51 -0000 1.2 > +++ gdb/python/py-evts.c 29 Mar 2011 08:08:46 -0000 > @@ -57,6 +57,9 @@ > > if (add_new_registry (&gdb_py_events.exited, "exited") < 0) > goto fail; > + > + if (add_new_registry (&gdb_py_events.newobjfile, "newobjfile") < 0) > + goto fail; > > Py_INCREF (gdb_py_events.module); > if (PyModule_AddObject (gdb_module, > Index: gdb/python/py-inferior.c > =================================================================== > RCS file: /cvs/src/src/gdb/python/py-inferior.c,v > retrieving revision 1.7 > diff -u -r1.7 py-inferior.c > --- gdb/python/py-inferior.c 17 Mar 2011 09:36:16 -0000 1.7 > +++ gdb/python/py-inferior.c 29 Mar 2011 08:08:46 -0000 > @@ -22,6 +22,7 @@ > #include "gdbcore.h" > #include "gdbthread.h" > #include "inferior.h" > +#include "objfiles.h" > #include "observer.h" > #include "python-internal.h" > #include "arch-utils.h" > @@ -129,6 +130,31 @@ > do_cleanups (cleanup); > } > > +/* Callback used to notify Python listeners about new objfiles loaded in the > + inferior. Python global variable 'gdb.current_objfile ()' will be set > + during the notifications. > + */ > + > +static void > +python_new_objfile (struct objfile *objfile) > +{ > + struct cleanup *cleanup; > + > + /* Will be NULL when clearing the symtab. */ > + if (objfile) > + cleanup = ensure_python_env (get_objfile_arch (objfile), current_language); > + else > + cleanup = ensure_python_env (get_current_arch (), current_language); > + > + gdbpy_current_objfile = objfile; > + > + if (emit_new_objfile_event (objfile) < 0) > + gdbpy_print_stack (); > + > + do_cleanups (cleanup); > + gdbpy_current_objfile = NULL; > +} > + > /* Return a borrowed reference to the Python object of type Inferior > representing INFERIOR. If the object has already been created, > return it, otherwise, create it. Return NULL on failure. */ > @@ -669,6 +695,7 @@ > observer_attach_normal_stop (python_on_normal_stop); > observer_attach_target_resumed (python_on_resume); > observer_attach_inferior_exit (python_inferior_exit); > + observer_attach_new_objfile (python_new_objfile); > > if (PyType_Ready (&membuf_object_type) < 0) > return; > Index: gdb/python/python-internal.h > =================================================================== > RCS file: /cvs/src/src/gdb/python/python-internal.h,v > retrieving revision 1.44 > diff -u -r1.44 python-internal.h > --- gdb/python/python-internal.h 28 Feb 2011 19:38:34 -0000 1.44 > +++ gdb/python/python-internal.h 29 Mar 2011 08:08:46 -0000 > @@ -207,6 +207,7 @@ > void gdbpy_initialize_continue_event (void); > void gdbpy_initialize_exited_event (void); > void gdbpy_initialize_thread_event (void); > +void gdbpy_initialize_new_objfile_event (void); > > struct cleanup *make_cleanup_py_decref (PyObject *py); > > @@ -215,6 +216,7 @@ > > extern struct gdbarch *python_gdbarch; > extern const struct language_defn *python_language; > +extern struct objfile *gdbpy_current_objfile; > > /* Use this after a TRY_EXCEPT to throw the appropriate Python > exception. */ > Index: gdb/python/python.c > =================================================================== > RCS file: /cvs/src/src/gdb/python/python.c,v > retrieving revision 1.63 > diff -u -r1.63 python.c > --- gdb/python/python.c 18 Mar 2011 08:44:47 -0000 1.63 > +++ gdb/python/python.c 29 Mar 2011 08:08:46 -0000 > @@ -811,8 +811,9 @@ > > /* The "current" objfile. This is set when gdb detects that a new > objfile has been loaded. It is only set for the duration of a call to > - source_python_script_for_objfile; it is NULL at other times. */ > -static struct objfile *gdbpy_current_objfile; > + source_python_script_for_objfile and new_objfile callbacks; it is NULL at > + other times. */ > +struct objfile *gdbpy_current_objfile; > > /* Set the current objfile to OBJFILE and then read STREAM,FILE as > Python code. */ > @@ -1074,6 +1075,7 @@ > gdbpy_initialize_continue_event (); > gdbpy_initialize_exited_event (); > gdbpy_initialize_thread_event (); > + gdbpy_initialize_new_objfile_event () ; > > PyRun_SimpleString ("import gdb"); > PyRun_SimpleString ("gdb.pretty_printers = []"); > Index: gdb/testsuite/gdb.python/py-events.c > =================================================================== > RCS file: /cvs/src/src/gdb/testsuite/gdb.python/py-events.c,v > retrieving revision 1.1 > diff -u -r1.1 py-events.c > --- gdb/testsuite/gdb.python/py-events.c 5 Feb 2011 05:27:23 -0000 1.1 > +++ gdb/testsuite/gdb.python/py-events.c 29 Mar 2011 08:08:46 -0000 > @@ -16,6 +16,8 @@ > along with this program. If not, see <http://www.gnu.org/licenses/>. > */ > > +extern void do_nothing (void) ; > + > int second(){ > return 12; > } > @@ -25,5 +27,6 @@ > } > > int main (){ > + do_nothing() ; > return first(); > } > Index: gdb/testsuite/gdb.python/py-events.exp > =================================================================== > RCS file: /cvs/src/src/gdb/testsuite/gdb.python/py-events.exp,v > retrieving revision 1.1 > diff -u -r1.1 py-events.exp > --- gdb/testsuite/gdb.python/py-events.exp 5 Feb 2011 05:27:23 -0000 1.1 > +++ gdb/testsuite/gdb.python/py-events.exp 29 Mar 2011 08:08:46 -0000 > @@ -24,23 +24,45 @@ > > load_lib gdb-python.exp > > +set libfile "py-events-shlib" > +set libsrc $srcdir/$subdir/$libfile.c > +set lib_sl $objdir/$subdir/$libfile.so > +set lib_opts debug > + > set testfile "py-events" > set srcfile ${testfile}.c > set binfile ${objdir}/${subdir}/${testfile} > +set exec_opts [list debug shlib=$lib_sl] > set pyfile ${srcdir}/${subdir}/${testfile}.py > > -if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } { > +if [get_compiler_info ${binfile}] { > + return -1 > +} > + > +if { [gdb_compile_shlib $libsrc $lib_sl $lib_opts] != "" > + || [gdb_compile $srcdir/$subdir/$srcfile $binfile executable > $exec_opts] != ""} { > + untested "Could not compile either $libsrc or $srcdir/$subdir/$srcfile." > return -1 > } > > +# Start with a fresh gdb. > + > +gdb_exit > +gdb_start > + > if { [skip_python_tests] } { continue } > > +gdb_reinitialize_dir $srcdir/$subdir > +gdb_load ${binfile} > + > gdb_test_no_output "python execfile ('${pyfile}')" "" > > -if ![runto_main ] then { > - fail "Can't run to main" > - return -1 > -} > +gdb_test "Test_Newobj_Events" "New ObjectFile Event tester registered." > + > +gdb_breakpoint "main" {temporary} > + > +gdb_test "run" ".*event type: new_objfile.* > +.*new objfile name.*" > > gdb_test "Test_Events" "Event testers registered." > > Index: gdb/testsuite/gdb.python/py-events.py > =================================================================== > RCS file: /cvs/src/src/gdb/testsuite/gdb.python/py-events.py,v > retrieving revision 1.1 > diff -u -r1.1 py-events.py > --- gdb/testsuite/gdb.python/py-events.py 5 Feb 2011 05:27:23 -0000 1.1 > +++ gdb/testsuite/gdb.python/py-events.py 29 Mar 2011 08:08:46 -0000 > @@ -48,6 +48,14 @@ > if ( event.inferior_thread is not None) : > print "thread num: %s" % (event.inferior_thread.num); > > +def new_objfile_handler (event): > + if (isinstance (event, gdb.NewObjFileEvent)): > + print "event type: new_objfile" > + if (gdb.current_objfile () is not None): > + print "new objfile name: %s" % (gdb.current_objfile ().filename) > + else: > + print "new objfile is None" > + > class test_events (gdb.Command): > """Test events.""" > > @@ -62,3 +70,15 @@ > print "Event testers registered." > > test_events () > + > +class test_newobj_events (gdb.Command): > + """NewObj events.""" > + > + def __init__ (self): > + gdb.Command.__init__ (self, "test_newobj_events", gdb.COMMAND_STACK) > + > + def invoke (self, arg, from_tty): > + gdb.events.newobjfile.connect (new_objfile_handler) > + print "New ObjectFile Event tester registered." > + > +test_newobj_events () > > ---- > > /* Python interface to new object file loading events. > > Copyright (C) 2011 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 "py-event.h" > > static PyTypeObject new_objfile_event_object_type; > > PyObject * > create_new_objfile_event_object (void) > { > return create_event_object (&new_objfile_event_object_type); > } > > /* Callback function which notifies observers when a new objfile event occurs. > This function will create a new Python new_objfile event object. > Return -1 if emit fails. */ > > int > emit_new_objfile_event (struct objfile *objfile) > { > PyObject *event; > > if (evregpy_no_listeners_p (gdb_py_events.newobjfile)) > return 0; > > event = create_new_objfile_event_object (); > if (event) > return evpy_emit_event (event, gdb_py_events.newobjfile); > return -1; > } > > GDBPY_NEW_EVENT_TYPE (new_objfile, > "gdb.NewObjFileEvent", > "NewObjFileEvent", > "GDB new object file event object", > event_object_type, > static); > > ---- > > /* This testcase is part of GDB, the GNU debugger. > > Copyright 2011 Free Software Foundation, Inc. > > 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/>. */ > > > void do_nothing (void) {} ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [RFC - Python] New ObjFile event 2011-03-30 11:32 ` Kevin Pouget @ 2011-03-30 12:14 ` Phil Muldoon 2011-03-30 12:15 ` Kevin Pouget 0 siblings, 1 reply; 26+ messages in thread From: Phil Muldoon @ 2011-03-30 12:14 UTC (permalink / raw) To: Kevin Pouget; +Cc: Tom Tromey, gdb-patches Kevin Pouget <kevin.pouget@gmail.com> writes: > Hello, > > here is a patch with all the modifications we discussed so far, > including the ChangeLog entry and the new files > Really nice patch! Just one tiny nit... > gdb/ChangeLog | 28 +++++++++++++++ This is the ChangeLog for GDB code, so things in gdb/ and python/ code related files have ChangeLog entries here. > @@ -1,3 +1,31 @@ > +2011-03-29 Kevin Pouget <kevin.pouget@st.com> > + > + Allow Python notification of new object-file loadings > + * Makefile.in (SUBDIR_PYTHON_SRCS): Add py-newobjfilevent.c. > + (SUBDIR_PYTHON_OBS): Add py-newobjfileevent.o. > + Add build rule for this file. > + * python/py-event.h (emit_new_objfile_event): New prototype. > + (newobjfile): New Python event emitter. > + * python/py-evts.c (gdbpy_initialize_py_events): Add newobjfile to > + Python event registry. > + * python/py-inferior.c: Include objfiles.h > + (python_new_objfile): New function. > + (gdbpy_initialize_inferior): Add python_new_objfile to the new objfile > + observers. > + * python/py-newobjfileevent.c: New file. > + * python-internal.h (gdbpy_initialize_new_objfile_event): New > + prototype. > + (gdbpy_current_objfile): New global variable. > + * python/python.c (gdbpy_current_objfile): Make global. > + (_initialize_python): Add gdbpy_initialize_new_objfile_event call. But the entries below belong in the testsuite/ChangeLog and without the preceding "testsuite" in the log. > + * testsuite/gdb.python/py-events-shlib.c: New file. > + * testsuite/gdb.python/py-events.c (do_nothing): New global. > + (main): Add call to shared library. > + * testsuite/gdb.python/py-events.exp: Link the execfile to a shared > + library and check newobjfile event notification. > + * testsuite/gdb.python/py-events.py (new_objfile_handler): New Class. > + (test_newobj_events): New class. > + I don't see a documentation entry. Similarly documentation entries are added in the doc/ChangeLog But this patch looks great. Tom or another maintainer will look at it soon. Cheers Phil ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [RFC - Python] New ObjFile event 2011-03-30 12:14 ` Phil Muldoon @ 2011-03-30 12:15 ` Kevin Pouget 2011-05-19 20:21 ` Tom Tromey 0 siblings, 1 reply; 26+ messages in thread From: Kevin Pouget @ 2011-03-30 12:15 UTC (permalink / raw) To: pmuldoon; +Cc: Tom Tromey, gdb-patches > Really nice patch! thanks :) > This is the ChangeLog for GDB code, so things in gdb/ and python/ code > related files have ChangeLog entries here. > ... > But the entries below belong in the testsuite/ChangeLog and without the > preceding "testsuite" in the log. > ... > I don't see a documentation entry. Similarly documentation entries are > added in the doc/ChangeLog oh, now it makes sense, I couldn't grep any 'gdb.texinfo' modification in gdb/ChangeLog, which was somehow strange, that's why I didn't write anything about this file; that should be fixed now : From 9e219b36244625880b085f853383a8ed2e6f0759 Mon Sep 17 00:00:00 2001 From: Kevin Pouget <kevin.pouget@st.com> Date: Wed, 30 Mar 2011 13:23:19 -0400 Subject: [PATCH] Allow Python notification of new object-file loadings Signed-off-by: Kevin Pouget <kevin.pouget@st.com> --- gdb/ChangeLog | 21 +++++++++++ gdb/Makefile.in | 10 ++++- gdb/doc/ChangeLog | 6 +++ gdb/doc/gdb.texinfo | 14 +++++-- gdb/python/py-event.h | 1 + gdb/python/py-events.h | 1 + gdb/python/py-evts.c | 3 ++ gdb/python/py-inferior.c | 27 ++++++++++++++ gdb/python/py-newobjfileevent.c | 53 ++++++++++++++++++++++++++++ gdb/python/python-internal.h | 2 + gdb/python/python.c | 6 ++- gdb/testsuite/ChangeLog | 9 +++++ gdb/testsuite/gdb.python/py-events-shlib.c | 20 ++++++++++ gdb/testsuite/gdb.python/py-events.c | 3 ++ gdb/testsuite/gdb.python/py-events.exp | 32 ++++++++++++++--- gdb/testsuite/gdb.python/py-events.py | 20 ++++++++++ 16 files changed, 215 insertions(+), 13 deletions(-) create mode 100644 gdb/python/py-newobjfileevent.c create mode 100644 gdb/testsuite/gdb.python/py-events-shlib.c diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 03885dd..880781a 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,24 @@ +2011-03-29 Kevin Pouget <kevin.pouget@st.com> + + Allow Python notification of new object-file loadings. + * Makefile.in (SUBDIR_PYTHON_SRCS): Add py-newobjfilevent.c. + (SUBDIR_PYTHON_OBS): Add py-newobjfileevent.o. + Add build rule for this file. + * python/py-event.h (emit_new_objfile_event): New prototype. + (newobjfile): New Python event emitter. + * python/py-evts.c (gdbpy_initialize_py_events): Add newobjfile to + Python event registry. + * python/py-inferior.c: Include objfiles.h + (python_new_objfile): New function. + (gdbpy_initialize_inferior): Add python_new_objfile to the new objfile + observers. + * python/py-newobjfileevent.c: New file. + * python-internal.h (gdbpy_initialize_new_objfile_event): New + prototype. + (gdbpy_current_objfile): New global variable. + * python/python.c (gdbpy_current_objfile): Make global. + (_initialize_python): Add gdbpy_initialize_new_objfile_event call. + 2011-03-28 Jan Kratochvil <jan.kratochvil@redhat.com> Support resolution of STT_GNU_IFUNC via breakpoints. diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 489b1e9..c15b566 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -296,7 +296,8 @@ SUBDIR_PYTHON_OBS = \ py-threadevent.o \ py-type.o \ py-utils.o \ - py-value.o + py-value.o \ + py-newobjfileevent.o SUBDIR_PYTHON_SRCS = \ python/python.c \ @@ -326,7 +327,8 @@ SUBDIR_PYTHON_SRCS = \ python/py-threadevent.c \ python/py-type.c \ python/py-utils.c \ - python/py-value.c + python/py-value.c \ + python/py-newobjfileevent.c SUBDIR_PYTHON_DEPS = SUBDIR_PYTHON_LDFLAGS= SUBDIR_PYTHON_CFLAGS= @@ -2110,6 +2112,10 @@ py-value.o: $(srcdir)/python/py-value.c $(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-value.c $(POSTCOMPILE) +py-newobjfileevent.o: $(srcdir)/python/py-newobjfileevent.c + $(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-newobjfileevent.c + $(POSTCOMPILE) + # # Dependency tracking. Most of this is conditional on GNU Make being # found by configure; if GNU Make is not found, we fall back to a diff --git a/gdb/doc/ChangeLog b/gdb/doc/ChangeLog index 08d6c8c..3ea5068 100644 --- a/gdb/doc/ChangeLog +++ b/gdb/doc/ChangeLog @@ -1,3 +1,9 @@ +2011-03-29 Kevin Pouget <kevin.pouget@st.com> + * gdb.textinfo (Events In Python): Document `gdb.NewObjFileEvent' + events emitter. + (Objfiles In Python): Indicate that `gdb.current_objfile' is also set + during new object-file callbacks. + 2011-03-28 Tom Tromey <tromey@redhat.com> * gdb.texinfo (Set Tracepoints): Fix typo. diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index a4e976f..360ff51 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -22057,6 +22057,12 @@ Emits @code{gdb.SignalEvent} which extends @code{gdb.StopEvent}. This event indicates that the inferior or one of its threads has received as signal. @code{gdb.SignalEvent} has the following attributes: +@item events.newobjfile +Emits @code{gdb.NewObjFileEvent} which indicates that a new object-file has +been loaded in the inferior. + +During the callback, ``current objfile'' will be set to the new object file. + @table @code @defivar SignalEvent stop_signal A string representing the signal received by the inferior. A list of possible @@ -22699,10 +22705,10 @@ The following objfile-related functions are available in the @findex gdb.current_objfile @defun current_objfile -When auto-loading a Python script (@pxref{Auto-loading}), @value{GDBN} -sets the ``current objfile'' to the corresponding objfile. This -function returns the current objfile. If there is no current objfile, -this function returns @code{None}. +When auto-loading a Python script (@pxref{Auto-loading}) and during new +object-file callbacks, @value{GDBN} sets the ``current objfile'' to the +corresponding objfile. This function returns the current objfile. If +there is no current objfile, this function returns @code{None}. @end defun @findex gdb.objfiles diff --git a/gdb/python/py-event.h b/gdb/python/py-event.h index bc95521..9a3ba82 100644 --- a/gdb/python/py-event.h +++ b/gdb/python/py-event.h @@ -105,6 +105,7 @@ typedef struct extern int emit_continue_event (ptid_t ptid); extern int emit_exited_event (LONGEST exit_code); +extern int emit_new_objfile_event (struct objfile *objfile); extern int evpy_emit_event (PyObject *event, eventregistry_object *registry); diff --git a/gdb/python/py-events.h b/gdb/python/py-events.h index 6d4dae5..9c42e39 100644 --- a/gdb/python/py-events.h +++ b/gdb/python/py-events.h @@ -45,6 +45,7 @@ typedef struct eventregistry_object *stop; eventregistry_object *cont; eventregistry_object *exited; + eventregistry_object *newobjfile; PyObject *module; diff --git a/gdb/python/py-evts.c b/gdb/python/py-evts.c index 50c05f3..ee60560 100644 --- a/gdb/python/py-evts.c +++ b/gdb/python/py-evts.c @@ -57,6 +57,9 @@ gdbpy_initialize_py_events (void) if (add_new_registry (&gdb_py_events.exited, "exited") < 0) goto fail; + + if (add_new_registry (&gdb_py_events.newobjfile, "newobjfile") < 0) + goto fail; Py_INCREF (gdb_py_events.module); if (PyModule_AddObject (gdb_module, diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index b9df394..ebd2a2a 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -22,6 +22,7 @@ #include "gdbcore.h" #include "gdbthread.h" #include "inferior.h" +#include "objfiles.h" #include "observer.h" #include "python-internal.h" #include "arch-utils.h" @@ -129,6 +130,31 @@ python_inferior_exit (struct inferior *inf) do_cleanups (cleanup); } +/* Callback used to notify Python listeners about new objfiles loaded in the + inferior. Python global variable 'gdb.current_objfile ()' will be set + during the notifications. + */ + +static void +python_new_objfile (struct objfile *objfile) +{ + struct cleanup *cleanup; + + /* Will be NULL when clearing the symtab. */ + if (objfile) + cleanup = ensure_python_env (get_objfile_arch (objfile), current_language); + else + cleanup = ensure_python_env (get_current_arch (), current_language); + + gdbpy_current_objfile = objfile; + + if (emit_new_objfile_event (objfile) < 0) + gdbpy_print_stack (); + + do_cleanups (cleanup); + gdbpy_current_objfile = NULL; +} + /* Return a borrowed reference to the Python object of type Inferior representing INFERIOR. If the object has already been created, return it, otherwise, create it. Return NULL on failure. */ @@ -669,6 +695,7 @@ gdbpy_initialize_inferior (void) observer_attach_normal_stop (python_on_normal_stop); observer_attach_target_resumed (python_on_resume); observer_attach_inferior_exit (python_inferior_exit); + observer_attach_new_objfile (python_new_objfile); if (PyType_Ready (&membuf_object_type) < 0) return; diff --git a/gdb/python/py-newobjfileevent.c b/gdb/python/py-newobjfileevent.c new file mode 100644 index 0000000..20e2b31 --- /dev/null +++ b/gdb/python/py-newobjfileevent.c @@ -0,0 +1,53 @@ +/* Python interface to new object file loading events. + + Copyright (C) 2011 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 "py-event.h" + +static PyTypeObject new_objfile_event_object_type; + +PyObject * +create_new_objfile_event_object (void) +{ + return create_event_object (&new_objfile_event_object_type); +} + +/* Callback function which notifies observers when a new objfile event occurs. + This function will create a new Python new_objfile event object. + Return -1 if emit fails. */ + +int +emit_new_objfile_event (struct objfile *objfile) +{ + PyObject *event; + + if (evregpy_no_listeners_p (gdb_py_events.newobjfile)) + return 0; + + event = create_new_objfile_event_object (); + if (event) + return evpy_emit_event (event, gdb_py_events.newobjfile); + return -1; +} + +GDBPY_NEW_EVENT_TYPE (new_objfile, + "gdb.NewObjFileEvent", + "NewObjFileEvent", + "GDB new object file event object", + event_object_type, + static); diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index d3cb788..cc9108f 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -207,6 +207,7 @@ void gdbpy_initialize_breakpoint_event (void); void gdbpy_initialize_continue_event (void); void gdbpy_initialize_exited_event (void); void gdbpy_initialize_thread_event (void); +void gdbpy_initialize_new_objfile_event (void); struct cleanup *make_cleanup_py_decref (PyObject *py); @@ -215,6 +216,7 @@ struct cleanup *ensure_python_env (struct gdbarch *gdbarch, extern struct gdbarch *python_gdbarch; extern const struct language_defn *python_language; +extern struct objfile *gdbpy_current_objfile; /* 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 90d5dc8..df883ef 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -811,8 +811,9 @@ gdbpy_progspaces (PyObject *unused1, PyObject *unused2) /* The "current" objfile. This is set when gdb detects that a new objfile has been loaded. It is only set for the duration of a call to - source_python_script_for_objfile; it is NULL at other times. */ -static struct objfile *gdbpy_current_objfile; + source_python_script_for_objfile and new_objfile callbacks; it is NULL at + other times. */ +struct objfile *gdbpy_current_objfile; /* Set the current objfile to OBJFILE and then read STREAM,FILE as Python code. */ @@ -1074,6 +1075,7 @@ Enables or disables printing of Python stack traces."), gdbpy_initialize_continue_event (); gdbpy_initialize_exited_event (); gdbpy_initialize_thread_event (); + gdbpy_initialize_new_objfile_event () ; PyRun_SimpleString ("import gdb"); PyRun_SimpleString ("gdb.pretty_printers = []"); diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index fa575d0..bc48a56 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,12 @@ +2011-03-29 Kevin Pouget <kevin.pouget@st.com> + * testsuite/gdb.python/py-events-shlib.c: New file. + * testsuite/gdb.python/py-events.c (do_nothing): New global. + (main): Add call to shared library. + * testsuite/gdb.python/py-events.exp: Link the execfile to a shared + library and check newobjfile event notification. + * testsuite/gdb.python/py-events.py (new_objfile_handler): New Class. + (test_newobj_events): New class. + 2011-03-28 Jan Kratochvil <jan.kratochvil@redhat.com> Test STT_GNU_IFUNC support. diff --git a/gdb/testsuite/gdb.python/py-events-shlib.c b/gdb/testsuite/gdb.python/py-events-shlib.c new file mode 100644 index 0000000..2a17bbf --- /dev/null +++ b/gdb/testsuite/gdb.python/py-events-shlib.c @@ -0,0 +1,20 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2011 Free Software Foundation, Inc. + + 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/>. */ + + +void do_nothing (void) {} + diff --git a/gdb/testsuite/gdb.python/py-events.c b/gdb/testsuite/gdb.python/py-events.c index ceb697e..1cf7cc7 100644 --- a/gdb/testsuite/gdb.python/py-events.c +++ b/gdb/testsuite/gdb.python/py-events.c @@ -16,6 +16,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ +extern void do_nothing (void) ; + int second(){ return 12; } @@ -25,5 +27,6 @@ int first(){ } int main (){ + do_nothing() ; return first(); } diff --git a/gdb/testsuite/gdb.python/py-events.exp b/gdb/testsuite/gdb.python/py-events.exp index e5d6daf..56e8f86 100644 --- a/gdb/testsuite/gdb.python/py-events.exp +++ b/gdb/testsuite/gdb.python/py-events.exp @@ -24,23 +24,45 @@ if $tracelevel then { load_lib gdb-python.exp +set libfile "py-events-shlib" +set libsrc $srcdir/$subdir/$libfile.c +set lib_sl $objdir/$subdir/$libfile.so +set lib_opts debug + set testfile "py-events" set srcfile ${testfile}.c set binfile ${objdir}/${subdir}/${testfile} +set exec_opts [list debug shlib=$lib_sl] set pyfile ${srcdir}/${subdir}/${testfile}.py -if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } { +if [get_compiler_info ${binfile}] { + return -1 +} + +if { [gdb_compile_shlib $libsrc $lib_sl $lib_opts] != "" + || [gdb_compile $srcdir/$subdir/$srcfile $binfile executable $exec_opts] != ""} { + untested "Could not compile either $libsrc or $srcdir/$subdir/$srcfile." return -1 } +# Start with a fresh gdb. + +gdb_exit +gdb_start + if { [skip_python_tests] } { continue } +gdb_reinitialize_dir $srcdir/$subdir +gdb_load ${binfile} + gdb_test_no_output "python execfile ('${pyfile}')" "" -if ![runto_main ] then { - fail "Can't run to main" - return -1 -} +gdb_test "Test_Newobj_Events" "New ObjectFile Event tester registered." + +gdb_breakpoint "main" {temporary} + +gdb_test "run" ".*event type: new_objfile.* +.*new objfile name.*" gdb_test "Test_Events" "Event testers registered." diff --git a/gdb/testsuite/gdb.python/py-events.py b/gdb/testsuite/gdb.python/py-events.py index 9f05b9f..12e36e8 100644 --- a/gdb/testsuite/gdb.python/py-events.py +++ b/gdb/testsuite/gdb.python/py-events.py @@ -48,6 +48,14 @@ def continue_handler (event): if ( event.inferior_thread is not None) : print "thread num: %s" % (event.inferior_thread.num); +def new_objfile_handler (event): + if (isinstance (event, gdb.NewObjFileEvent)): + print "event type: new_objfile" + if (gdb.current_objfile () is not None): + print "new objfile name: %s" % (gdb.current_objfile ().filename) + else: + print "new objfile is None" + class test_events (gdb.Command): """Test events.""" @@ -62,3 +70,15 @@ class test_events (gdb.Command): print "Event testers registered." test_events () + +class test_newobj_events (gdb.Command): + """NewObj events.""" + + def __init__ (self): + gdb.Command.__init__ (self, "test_newobj_events", gdb.COMMAND_STACK) + + def invoke (self, arg, from_tty): + gdb.events.newobjfile.connect (new_objfile_handler) + print "New ObjectFile Event tester registered." + +test_newobj_events () -- 1.7.4 ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [RFC - Python] New ObjFile event 2011-03-30 12:15 ` Kevin Pouget @ 2011-05-19 20:21 ` Tom Tromey 2011-05-23 8:44 ` Kevin Pouget 0 siblings, 1 reply; 26+ messages in thread From: Tom Tromey @ 2011-05-19 20:21 UTC (permalink / raw) To: Kevin Pouget; +Cc: pmuldoon, gdb-patches >>>>> "Kevin" == Kevin Pouget <kevin.pouget@gmail.com> writes: Kevin> + py-value.o \ Kevin> + py-newobjfileevent.o Alphabetical, please. Applies elsewhere in the Makefile patch too. Kevin> # found by configure; if GNU Make is not found, we fall back to a Kevin> diff --git a/gdb/doc/ChangeLog b/gdb/doc/ChangeLog Kevin> index 08d6c8c..3ea5068 100644 Kevin> --- a/gdb/doc/ChangeLog Kevin> +++ b/gdb/doc/ChangeLog Kevin> @@ -1,3 +1,9 @@ Kevin> +2011-03-29 Kevin Pouget <kevin.pouget@st.com> Kevin> + * gdb.textinfo (Events In Python): Document `gdb.NewObjFileEvent' Missing blank line after the date line, and typo: "texinfo", not "textinfo". Kevin> +@item events.newobjfile Kevin> +Emits @code{gdb.NewObjFileEvent} which indicates that a new object-file has Kevin> +been loaded in the inferior. This is not an accurate description -- it may or may not be related to a change in the inferior. E.g., if the user did "set auto-solib-add off", then no objfile will be made when a shared library is loaded. Or, if the user does "add-symbol-file", then a new objfile will be made even though nothing has changed in the inferior. Maybe this isn't the event you actually wanted? I mean -- it is fine to have this, and I'd like it if you followed through :-), but it may not do what you are expecting. Kevin> + /* Will be NULL when clearing the symtab. */ Kevin> + if (objfile) Kevin> + cleanup = ensure_python_env (get_objfile_arch (objfile), current_language); Kevin> + else Kevin> + cleanup = ensure_python_env (get_current_arch (), current_language); I think it is better to just return early if objfile==NULL. Or, emit a different event in this case. Kevin> +2011-03-29 Kevin Pouget <kevin.pouget@st.com> Kevin> + * testsuite/gdb.python/py-events-shlib.c: New file. Missing newline. Kevin> +# Start with a fresh gdb. Kevin> + Kevin> +gdb_exit Kevin> +gdb_start I think clean_restart is preferred here. But maybe skip_python_tests has to be inserted in the middle of the sequence? Kevin> +gdb_test "run" ".*event type: new_objfile.* Kevin> +.*new objfile name.*" This style of test is hard to read. I'd prefer a \n in there. Tom ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [RFC - Python] New ObjFile event 2011-05-19 20:21 ` Tom Tromey @ 2011-05-23 8:44 ` Kevin Pouget 2011-09-01 9:56 ` Kevin Pouget 0 siblings, 1 reply; 26+ messages in thread From: Kevin Pouget @ 2011-05-23 8:44 UTC (permalink / raw) To: Tom Tromey; +Cc: pmuldoon, gdb-patches [-- Attachment #1: Type: text/plain, Size: 4948 bytes --] Hello, > Kevin> + py-value.o \ > Kevin> + py-newobjfileevent.o > > Alphabetical, please. Applies elsewhere in the Makefile patch too. fixed, sorry about that > Kevin> # found by configure; if GNU Make is not found, we fall back to a > Kevin> diff --git a/gdb/doc/ChangeLog b/gdb/doc/ChangeLog > Kevin> index 08d6c8c..3ea5068 100644 > Kevin> --- a/gdb/doc/ChangeLog > Kevin> +++ b/gdb/doc/ChangeLog > Kevin> @@ -1,3 +1,9 @@ > Kevin> +2011-03-29 Kevin Pouget <kevin.pouget@st.com> > Kevin> + * gdb.textinfo (Events In Python): Document `gdb.NewObjFileEvent' > > Missing blank line after the date line, and typo: "texinfo", not > "textinfo". I removed the Changelog entries from the patch as suggested by Pedro in another thread, typo fixed at the bottom of the mail > Kevin> +@item events.newobjfile > Kevin> +Emits @code{gdb.NewObjFileEvent} which indicates that a new object-file has > Kevin> +been loaded in the inferior. > > This is not an accurate description -- it may or may not be related to a > change in the inferior. > > E.g., if the user did "set auto-solib-add off", then no objfile will be > made when a shared library is loaded. > > Or, if the user does "add-symbol-file", then a new objfile will be made > even though nothing has changed in the inferior. > > Maybe this isn't the event you actually wanted? I mean -- it is fine to > have this, and I'd like it if you followed through :-), but it may not > do what you are expecting. you're right, I changed it to: +been loaded by @value{GDBN}. About the behaviour, I actually wanted to mimic the way GDB decides to enable [linux/thread-db] thread-debugging, ie checking the name of the shared library loaded by GDB. As you mentioned that's not perfect, "set auto-solib-add off" or "add-symbol-file" might change the behaviour, but I guess it's easy enough to cope with it > Kevin> + /* Will be NULL when clearing the symtab. */ > Kevin> + if (objfile) > Kevin> + cleanup = ensure_python_env (get_objfile_arch (objfile), current_language); > Kevin> + else > Kevin> + cleanup = ensure_python_env (get_current_arch (), current_language); > > I think it is better to just return early if objfile==NULL. > Or, emit a different event in this case. I found some more documentation about objfile == NULL in the Internals: > Called with objfile equal to NULL to indicate previously loaded symbol table data has now been invalidated. so as you suggested, I just return immediately and leave symbol table invalidation for another patch > Kevin> +2011-03-29 Kevin Pouget <kevin.pouget@st.com> > Kevin> + * testsuite/gdb.python/py-events-shlib.c: New file. > > Missing newline. cf above, ChangeLog removed from the diff patch and fixed at the bottom > Kevin> +# Start with a fresh gdb. > Kevin> + > Kevin> +gdb_exit > Kevin> +gdb_start > > I think clean_restart is preferred here. > But maybe skip_python_tests has to be inserted in the middle of the > sequence? fixed > Kevin> +gdb_test "run" ".*event type: new_objfile.* > Kevin> +.*new objfile name.*" > > This style of test is hard to read. I'd prefer a \n in there. Fixed (I didn't want to test the '\n' specifically but just make it more readable:). However, JFYI the test file already contains such formatting: # Test continue event and breakpoint stop event gdb_test "continue" ".*event type: continue.* .*event type: stop.* .*stop reason: breakpoint.* .*breakpoint number: 2.* all threads stopped" #test exited event. gdb_test "continue" ".*event type: continue.* .*event type: exit.* .*exit code: 12.*" thanks for your review, Kevin -- 2011-03-29 Kevin Pouget <kevin.pouget@st.com> Allow Python notification of new object-file loadings. * Makefile.in (SUBDIR_PYTHON_SRCS): Add py-newobjfilevent.c. (SUBDIR_PYTHON_OBS): Add py-newobjfileevent.o. Add build rule for this file. * python/py-event.h (emit_new_objfile_event): New prototype. (newobjfile): New Python event emitter. * python/py-evts.c (gdbpy_initialize_py_events): Add newobjfile to Python event registry. * python/py-inferior.c: Include objfiles.h (python_new_objfile): New function. (gdbpy_initialize_inferior): Add python_new_objfile to the new objfile observers. * python/py-newobjfileevent.c: New file. * python-internal.h (gdbpy_initialize_new_objfile_event): New prototype. (gdbpy_current_objfile): New global variable. * python/python.c (gdbpy_current_objfile): Make global. (_initialize_python): Add gdbpy_initialize_new_objfile_event call. 2011-03-29 Kevin Pouget <kevin.pouget@st.com> Allow Python notification of new object-file loadings. * gdb.texinfo (Events In Python): Document `gdb.NewObjFileEvent' events emitter. (Objfiles In Python): Indicate that `gdb.current_objfile' is also set during new object-file callbacks. [-- Attachment #2: new-obj-file.txt --] [-- Type: text/plain, Size: 13421 bytes --] diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 5bab360..1de1666 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -290,6 +290,7 @@ SUBDIR_PYTHON_OBS = \ py-inferior.o \ py-infthread.o \ py-lazy-string.o \ + py-newobjfileevent.o \ py-objfile.o \ py-param.o \ py-prettyprint.o \ @@ -320,6 +321,7 @@ SUBDIR_PYTHON_SRCS = \ python/py-inferior.c \ python/py-infthread.c \ python/py-lazy-string.c \ + python/py-newobjfileevent.c \ python/py-objfile.c \ python/py-param.c \ python/py-prettyprint.c \ @@ -2082,6 +2084,10 @@ py-lazy-string.o: $(srcdir)/python/py-lazy-string.c $(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-lazy-string.c $(POSTCOMPILE) +py-newobjfileevent.o: $(srcdir)/python/py-newobjfileevent.c + $(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-newobjfileevent.c + $(POSTCOMPILE) + py-objfile.o: $(srcdir)/python/py-objfile.c $(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-objfile.c $(POSTCOMPILE) @@ -2130,6 +2136,7 @@ py-value.o: $(srcdir)/python/py-value.c $(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-value.c $(POSTCOMPILE) + # # Dependency tracking. Most of this is conditional on GNU Make being # found by configure; if GNU Make is not found, we fall back to a diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 584a520..1512ea3 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -22147,6 +22147,12 @@ Emits @code{gdb.SignalEvent} which extends @code{gdb.StopEvent}. This event indicates that the inferior or one of its threads has received as signal. @code{gdb.SignalEvent} has the following attributes: +@item events.newobjfile +Emits @code{gdb.NewObjFileEvent} which indicates that a new object-file has +been loaded by @value{GDBN}. + +During the callback, ``current objfile'' will be set to the new object file. + @table @code @defivar SignalEvent stop_signal A string representing the signal received by the inferior. A list of possible @@ -22789,10 +22795,10 @@ The following objfile-related functions are available in the @findex gdb.current_objfile @defun current_objfile -When auto-loading a Python script (@pxref{Auto-loading}), @value{GDBN} -sets the ``current objfile'' to the corresponding objfile. This -function returns the current objfile. If there is no current objfile, -this function returns @code{None}. +When auto-loading a Python script (@pxref{Auto-loading}) and during new +object-file callbacks, @value{GDBN} sets the ``current objfile'' to the +corresponding objfile. This function returns the current objfile. If +there is no current objfile, this function returns @code{None}. @end defun @findex gdb.objfiles diff --git a/gdb/python/py-event.h b/gdb/python/py-event.h index bc95521..9a3ba82 100644 --- a/gdb/python/py-event.h +++ b/gdb/python/py-event.h @@ -105,6 +105,7 @@ typedef struct extern int emit_continue_event (ptid_t ptid); extern int emit_exited_event (LONGEST exit_code); +extern int emit_new_objfile_event (struct objfile *objfile); extern int evpy_emit_event (PyObject *event, eventregistry_object *registry); diff --git a/gdb/python/py-events.h b/gdb/python/py-events.h index 6d4dae5..9c42e39 100644 --- a/gdb/python/py-events.h +++ b/gdb/python/py-events.h @@ -45,6 +45,7 @@ typedef struct eventregistry_object *stop; eventregistry_object *cont; eventregistry_object *exited; + eventregistry_object *newobjfile; PyObject *module; diff --git a/gdb/python/py-evts.c b/gdb/python/py-evts.c index 50c05f3..c8d1044 100644 --- a/gdb/python/py-evts.c +++ b/gdb/python/py-evts.c @@ -58,6 +58,9 @@ gdbpy_initialize_py_events (void) if (add_new_registry (&gdb_py_events.exited, "exited") < 0) goto fail; + if (add_new_registry (&gdb_py_events.newobjfile, "newobjfile") < 0) + goto fail; + Py_INCREF (gdb_py_events.module); if (PyModule_AddObject (gdb_module, "events", diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index 45f5f04..0d1d3ce 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -22,6 +22,7 @@ #include "gdbcore.h" #include "gdbthread.h" #include "inferior.h" +#include "objfiles.h" #include "observer.h" #include "python-internal.h" #include "arch-utils.h" @@ -129,6 +130,29 @@ python_inferior_exit (struct inferior *inf) do_cleanups (cleanup); } +/* Callback used to notify Python listeners about new objfiles loaded in the + inferior. Python global variable 'gdb.current_objfile ()' will be set + during the notifications. */ + +static void +python_new_objfile (struct objfile *objfile) +{ + struct cleanup *cleanup; + + if (objfile == NULL) + return; + + cleanup = ensure_python_env (get_objfile_arch (objfile), current_language); + + gdbpy_current_objfile = objfile; + + if (emit_new_objfile_event (objfile) < 0) + gdbpy_print_stack (); + + do_cleanups (cleanup); + gdbpy_current_objfile = NULL; +} + /* Return a borrowed reference to the Python object of type Inferior representing INFERIOR. If the object has already been created, return it, otherwise, create it. Return NULL on failure. */ @@ -669,6 +693,7 @@ gdbpy_initialize_inferior (void) observer_attach_normal_stop (python_on_normal_stop); observer_attach_target_resumed (python_on_resume); observer_attach_inferior_exit (python_inferior_exit); + observer_attach_new_objfile (python_new_objfile); if (PyType_Ready (&membuf_object_type) < 0) return; diff --git a/gdb/python/py-newobjfileevent.c b/gdb/python/py-newobjfileevent.c new file mode 100644 index 0000000..20e2b31 --- /dev/null +++ b/gdb/python/py-newobjfileevent.c @@ -0,0 +1,53 @@ +/* Python interface to new object file loading events. + + Copyright (C) 2011 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 "py-event.h" + +static PyTypeObject new_objfile_event_object_type; + +PyObject * +create_new_objfile_event_object (void) +{ + return create_event_object (&new_objfile_event_object_type); +} + +/* Callback function which notifies observers when a new objfile event occurs. + This function will create a new Python new_objfile event object. + Return -1 if emit fails. */ + +int +emit_new_objfile_event (struct objfile *objfile) +{ + PyObject *event; + + if (evregpy_no_listeners_p (gdb_py_events.newobjfile)) + return 0; + + event = create_new_objfile_event_object (); + if (event) + return evpy_emit_event (event, gdb_py_events.newobjfile); + return -1; +} + +GDBPY_NEW_EVENT_TYPE (new_objfile, + "gdb.NewObjFileEvent", + "NewObjFileEvent", + "GDB new object file event object", + event_object_type, + static); diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index d3cb788..cc9108f 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -207,6 +207,7 @@ void gdbpy_initialize_breakpoint_event (void); void gdbpy_initialize_continue_event (void); void gdbpy_initialize_exited_event (void); void gdbpy_initialize_thread_event (void); +void gdbpy_initialize_new_objfile_event (void); struct cleanup *make_cleanup_py_decref (PyObject *py); @@ -215,6 +216,7 @@ struct cleanup *ensure_python_env (struct gdbarch *gdbarch, extern struct gdbarch *python_gdbarch; extern const struct language_defn *python_language; +extern struct objfile *gdbpy_current_objfile; /* 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 8a7bc66..c96984c 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -811,8 +811,9 @@ gdbpy_progspaces (PyObject *unused1, PyObject *unused2) /* The "current" objfile. This is set when gdb detects that a new objfile has been loaded. It is only set for the duration of a call to - source_python_script_for_objfile; it is NULL at other times. */ -static struct objfile *gdbpy_current_objfile; + source_python_script_for_objfile and new_objfile callbacks; it is NULL at + other times. */ +struct objfile *gdbpy_current_objfile; /* Set the current objfile to OBJFILE and then read STREAM,FILE as Python code. */ @@ -1074,6 +1075,7 @@ Enables or disables printing of Python stack traces."), gdbpy_initialize_continue_event (); gdbpy_initialize_exited_event (); gdbpy_initialize_thread_event (); + gdbpy_initialize_new_objfile_event () ; PyRun_SimpleString ("import gdb"); PyRun_SimpleString ("gdb.pretty_printers = []"); diff --git a/gdb/testsuite/gdb.python/py-events-shlib.c b/gdb/testsuite/gdb.python/py-events-shlib.c new file mode 100644 index 0000000..2a17bbf --- /dev/null +++ b/gdb/testsuite/gdb.python/py-events-shlib.c @@ -0,0 +1,20 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2011 Free Software Foundation, Inc. + + 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/>. */ + + +void do_nothing (void) {} + diff --git a/gdb/testsuite/gdb.python/py-events.c b/gdb/testsuite/gdb.python/py-events.c index ceb697e..1cf7cc7 100644 --- a/gdb/testsuite/gdb.python/py-events.c +++ b/gdb/testsuite/gdb.python/py-events.c @@ -16,6 +16,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ +extern void do_nothing (void) ; + int second(){ return 12; } @@ -25,5 +27,6 @@ int first(){ } int main (){ + do_nothing() ; return first(); } diff --git a/gdb/testsuite/gdb.python/py-events.exp b/gdb/testsuite/gdb.python/py-events.exp index e5d6daf..11d7e00 100644 --- a/gdb/testsuite/gdb.python/py-events.exp +++ b/gdb/testsuite/gdb.python/py-events.exp @@ -24,23 +24,39 @@ if $tracelevel then { load_lib gdb-python.exp +set libfile "py-events-shlib" +set libsrc $srcdir/$subdir/$libfile.c +set lib_sl $objdir/$subdir/$libfile.so +set lib_opts debug + set testfile "py-events" set srcfile ${testfile}.c set binfile ${objdir}/${subdir}/${testfile} +set exec_opts [list debug shlib=$lib_sl] set pyfile ${srcdir}/${subdir}/${testfile}.py -if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } { +if [get_compiler_info ${binfile}] { return -1 } +if { [gdb_compile_shlib $libsrc $lib_sl $lib_opts] != "" + || [gdb_compile $srcdir/$subdir/$srcfile $binfile executable $exec_opts] != ""} { + untested "Could not compile either $libsrc or $srcdir/$subdir/$srcfile." + return -1 +} + +# Start with a fresh gdb. +clean_restart ${testfile} + if { [skip_python_tests] } { continue } gdb_test_no_output "python execfile ('${pyfile}')" "" -if ![runto_main ] then { - fail "Can't run to main" - return -1 -} +gdb_test "Test_Newobj_Events" "New ObjectFile Event tester registered." "Register new objfile event handler" + +gdb_breakpoint "main" {temporary} + +gdb_test "run" ".*event type: new_objfile.*new objfile name.*" "New objfile notification" gdb_test "Test_Events" "Event testers registered." diff --git a/gdb/testsuite/gdb.python/py-events.py b/gdb/testsuite/gdb.python/py-events.py index 9f05b9f..12e36e8 100644 --- a/gdb/testsuite/gdb.python/py-events.py +++ b/gdb/testsuite/gdb.python/py-events.py @@ -48,6 +48,14 @@ def continue_handler (event): if ( event.inferior_thread is not None) : print "thread num: %s" % (event.inferior_thread.num); +def new_objfile_handler (event): + if (isinstance (event, gdb.NewObjFileEvent)): + print "event type: new_objfile" + if (gdb.current_objfile () is not None): + print "new objfile name: %s" % (gdb.current_objfile ().filename) + else: + print "new objfile is None" + class test_events (gdb.Command): """Test events.""" @@ -62,3 +70,15 @@ class test_events (gdb.Command): print "Event testers registered." test_events () + +class test_newobj_events (gdb.Command): + """NewObj events.""" + + def __init__ (self): + gdb.Command.__init__ (self, "test_newobj_events", gdb.COMMAND_STACK) + + def invoke (self, arg, from_tty): + gdb.events.newobjfile.connect (new_objfile_handler) + print "New ObjectFile Event tester registered." + +test_newobj_events () ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [RFC - Python] New ObjFile event 2011-05-23 8:44 ` Kevin Pouget @ 2011-09-01 9:56 ` Kevin Pouget 2011-09-01 11:01 ` Eli Zaretskii 0 siblings, 1 reply; 26+ messages in thread From: Kevin Pouget @ 2011-09-01 9:56 UTC (permalink / raw) To: Tom Tromey; +Cc: pmuldoon, gdb-patches [-- Attachment #1: Type: text/plain, Size: 5414 bytes --] On Mon, May 23, 2011 at 10:43 AM, Kevin Pouget <kevin.pouget@gmail.com> wrote: > Hello, > > >> Kevin> + py-value.o \ >> Kevin> + py-newobjfileevent.o >> >> Alphabetical, please. Applies elsewhere in the Makefile patch too. > > fixed, sorry about that > >> Kevin> # found by configure; if GNU Make is not found, we fall back to a >> Kevin> diff --git a/gdb/doc/ChangeLog b/gdb/doc/ChangeLog >> Kevin> index 08d6c8c..3ea5068 100644 >> Kevin> --- a/gdb/doc/ChangeLog >> Kevin> +++ b/gdb/doc/ChangeLog >> Kevin> @@ -1,3 +1,9 @@ >> Kevin> +2011-03-29 Kevin Pouget <kevin.pouget@st.com> >> Kevin> + * gdb.textinfo (Events In Python): Document `gdb.NewObjFileEvent' >> >> Missing blank line after the date line, and typo: "texinfo", not >> "textinfo". > > I removed the Changelog entries from the patch as suggested by Pedro > in another thread, > typo fixed at the bottom of the mail > >> Kevin> +@item events.newobjfile >> Kevin> +Emits @code{gdb.NewObjFileEvent} which indicates that a new object-file has >> Kevin> +been loaded in the inferior. >> >> This is not an accurate description -- it may or may not be related to a >> change in the inferior. >> >> E.g., if the user did "set auto-solib-add off", then no objfile will be >> made when a shared library is loaded. >> >> Or, if the user does "add-symbol-file", then a new objfile will be made >> even though nothing has changed in the inferior. >> >> Maybe this isn't the event you actually wanted? I mean -- it is fine to >> have this, and I'd like it if you followed through :-), but it may not >> do what you are expecting. > > you're right, I changed it to: > +been loaded by @value{GDBN}. > > About the behaviour, I actually wanted to mimic the way GDB decides to > enable [linux/thread-db] thread-debugging, ie checking the name of the > shared library loaded by GDB. As you mentioned that's not perfect, > "set auto-solib-add off" or "add-symbol-file" might change the > behaviour, but I guess it's easy enough to cope with it > >> Kevin> + /* Will be NULL when clearing the symtab. */ >> Kevin> + if (objfile) >> Kevin> + cleanup = ensure_python_env (get_objfile_arch (objfile), current_language); >> Kevin> + else >> Kevin> + cleanup = ensure_python_env (get_current_arch (), current_language); >> >> I think it is better to just return early if objfile==NULL. >> Or, emit a different event in this case. > > I found some more documentation about objfile == NULL in the Internals: >> Called with objfile equal to NULL to indicate previously loaded symbol table data has now been invalidated. > > so as you suggested, I just return immediately and leave symbol table > invalidation for another patch > >> Kevin> +2011-03-29 Kevin Pouget <kevin.pouget@st.com> >> Kevin> + * testsuite/gdb.python/py-events-shlib.c: New file. >> >> Missing newline. > > cf above, ChangeLog removed from the diff patch and fixed at the bottom > >> Kevin> +# Start with a fresh gdb. >> Kevin> + >> Kevin> +gdb_exit >> Kevin> +gdb_start >> >> I think clean_restart is preferred here. >> But maybe skip_python_tests has to be inserted in the middle of the >> sequence? > > fixed > >> Kevin> +gdb_test "run" ".*event type: new_objfile.* >> Kevin> +.*new objfile name.*" >> >> This style of test is hard to read. I'd prefer a \n in there. > > Fixed (I didn't want to test the '\n' specifically but just make it > more readable:). > However, JFYI the test file already contains such formatting: > > # Test continue event and breakpoint stop event > gdb_test "continue" ".*event type: continue.* > .*event type: stop.* > .*stop reason: breakpoint.* > .*breakpoint number: 2.* > all threads stopped" > > #test exited event. > gdb_test "continue" ".*event type: continue.* > .*event type: exit.* > .*exit code: 12.*" > > > > thanks for your review, > > Kevin Hello, could you please tell me what you think about this patch, I think I address all the points you mentioned in your last mail I added a NEWS entry, and as far as I've seen, the documentation was not reviewed. Cordially, Kevin 2011-03-29 Kevin Pouget <kevin.pouget@st.com> Allow Python notification of new object-file loadings. * Makefile.in (SUBDIR_PYTHON_SRCS): Add py-newobjfilevent.c. (SUBDIR_PYTHON_OBS): Add py-newobjfileevent.o. Add build rule for this file. * python/py-event.h (emit_new_objfile_event): New prototype. (newobjfile): New Python event emitter. * python/py-evts.c (gdbpy_initialize_py_events): Add newobjfile to Python event registry. * python/py-inferior.c: Include objfiles.h (python_new_objfile): New function. (gdbpy_initialize_inferior): Add python_new_objfile to the new objfile observers. * python/py-newobjfileevent.c: New file. * python-internal.h (gdbpy_initialize_new_objfile_event): New prototype. (gdbpy_current_objfile): New global variable. * python/python.c (gdbpy_current_objfile): Make global. (_initialize_python): Add gdbpy_initialize_new_objfile_event call. 2011-03-29 Kevin Pouget <kevin.pouget@st.com> Allow Python notification of new object-file loadings. * gdb.texinfo (Events In Python): Document `gdb.NewObjFileEvent' events emitter. (Objfiles In Python): Indicate that `gdb.current_objfile' is also set during new object-file callbacks. [-- Attachment #2: 0001-New-ObjFile-event --] [-- Type: application/octet-stream, Size: 11635 bytes --] From f5e444f2873a79e492a202be9758639da2463f16 Mon Sep 17 00:00:00 2001 From: Kevin Pouget <kevin.pouget@st.com> Date: Thu, 1 Sep 2011 11:08:23 +0200 Subject: [PATCH] New ObjFile event --- gdb/Makefile.in | 6 ++++++ gdb/NEWS | 2 ++ gdb/doc/gdb.texinfo | 14 ++++++++++---- gdb/python/py-event.h | 1 + gdb/python/py-events.h | 1 + gdb/python/py-evts.c | 3 +++ gdb/python/py-inferior.c | 25 +++++++++++++++++++++++++ gdb/python/python-internal.h | 2 ++ gdb/python/python.c | 6 ++++-- gdb/testsuite/gdb.python/py-events.c | 3 +++ gdb/testsuite/gdb.python/py-events.exp | 26 +++++++++++++++++++++----- gdb/testsuite/gdb.python/py-events.py | 20 ++++++++++++++++++++ 12 files changed, 98 insertions(+), 11 deletions(-) diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 826d339..986588b 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -289,6 +289,7 @@ SUBDIR_PYTHON_OBS = \ py-inferior.o \ py-infthread.o \ py-lazy-string.o \ + py-newobjfileevent.o \ py-objfile.o \ py-param.o \ py-prettyprint.o \ @@ -319,6 +320,7 @@ SUBDIR_PYTHON_SRCS = \ python/py-inferior.c \ python/py-infthread.c \ python/py-lazy-string.c \ + python/py-newobjfileevent.c \ python/py-objfile.c \ python/py-param.c \ python/py-prettyprint.c \ @@ -2115,6 +2117,10 @@ py-lazy-string.o: $(srcdir)/python/py-lazy-string.c $(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-lazy-string.c $(POSTCOMPILE) +py-newobjfileevent.o: $(srcdir)/python/py-newobjfileevent.c + $(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-newobjfileevent.c + $(POSTCOMPILE) + py-objfile.o: $(srcdir)/python/py-objfile.c $(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-objfile.c $(POSTCOMPILE) diff --git a/gdb/NEWS b/gdb/NEWS index 255a22e..bb5795c 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -38,6 +38,8 @@ ** Symbols now provide the "type" attribute, the type of the symbol. + ** A new event "gdb.newobjfile" has been added. + * libthread-db-search-path now supports two special values: $sdir and $pdir. $sdir specifies the default system locations of shared libraries. $pdir specifies the directory where the libpthread used by the application diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 23b2a98..118182d 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -22307,6 +22307,12 @@ Emits @code{gdb.SignalEvent} which extends @code{gdb.StopEvent}. This event indicates that the inferior or one of its threads has received as signal. @code{gdb.SignalEvent} has the following attributes: +@item events.newobjfile +Emits @code{gdb.NewObjFileEvent} which indicates that a new object-file has +been loaded by @value{GDBN}. + +During the callback, ``current objfile'' will be set to the new object file. + @table @code @defivar SignalEvent stop_signal A string representing the signal received by the inferior. A list of possible @@ -22949,10 +22955,10 @@ The following objfile-related functions are available in the @findex gdb.current_objfile @defun current_objfile -When auto-loading a Python script (@pxref{Auto-loading}), @value{GDBN} -sets the ``current objfile'' to the corresponding objfile. This -function returns the current objfile. If there is no current objfile, -this function returns @code{None}. +When auto-loading a Python script (@pxref{Auto-loading}) and during new +object-file callbacks, @value{GDBN} sets the ``current objfile'' to the +corresponding objfile. This function returns the current objfile. If +there is no current objfile, this function returns @code{None}. @end defun @findex gdb.objfiles diff --git a/gdb/python/py-event.h b/gdb/python/py-event.h index f4b3ee2..9069a51 100644 --- a/gdb/python/py-event.h +++ b/gdb/python/py-event.h @@ -111,6 +111,7 @@ extern int evpy_emit_event (PyObject *event, extern PyObject *create_event_object (PyTypeObject *py_type); extern PyObject *create_thread_event_object (PyTypeObject *py_type); +extern int emit_new_objfile_event (struct objfile *objfile); extern void evpy_dealloc (PyObject *self); extern int evpy_add_attribute (PyObject *event, diff --git a/gdb/python/py-events.h b/gdb/python/py-events.h index bd54418..70f45a4 100644 --- a/gdb/python/py-events.h +++ b/gdb/python/py-events.h @@ -45,6 +45,7 @@ typedef struct eventregistry_object *stop; eventregistry_object *cont; eventregistry_object *exited; + eventregistry_object *newobjfile; PyObject *module; diff --git a/gdb/python/py-evts.c b/gdb/python/py-evts.c index 50c05f3..c8d1044 100644 --- a/gdb/python/py-evts.c +++ b/gdb/python/py-evts.c @@ -58,6 +58,9 @@ gdbpy_initialize_py_events (void) if (add_new_registry (&gdb_py_events.exited, "exited") < 0) goto fail; + if (add_new_registry (&gdb_py_events.newobjfile, "newobjfile") < 0) + goto fail; + Py_INCREF (gdb_py_events.module); if (PyModule_AddObject (gdb_module, "events", diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index 6add681..63ffc2b 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -22,6 +22,7 @@ #include "gdbcore.h" #include "gdbthread.h" #include "inferior.h" +#include "objfiles.h" #include "observer.h" #include "python-internal.h" #include "arch-utils.h" @@ -125,6 +126,29 @@ python_inferior_exit (struct inferior *inf) do_cleanups (cleanup); } +/* Callback used to notify Python listeners about new objfiles loaded in the + inferior. Python global variable 'gdb.current_objfile ()' will be set + during the notifications. */ + +static void +python_new_objfile (struct objfile *objfile) +{ + struct cleanup *cleanup; + + if (objfile == NULL) + return; + + cleanup = ensure_python_env (get_objfile_arch (objfile), current_language); + + gdbpy_current_objfile = objfile; + + if (emit_new_objfile_event (objfile) < 0) + gdbpy_print_stack (); + + do_cleanups (cleanup); + gdbpy_current_objfile = NULL; +} + /* Return a reference to the Python object of type Inferior representing INFERIOR. If the object has already been created, return it and increment the reference count, otherwise, create it. @@ -701,6 +725,7 @@ gdbpy_initialize_inferior (void) observer_attach_normal_stop (python_on_normal_stop); observer_attach_target_resumed (python_on_resume); observer_attach_inferior_exit (python_inferior_exit); + observer_attach_new_objfile (python_new_objfile); membuf_object_type.tp_new = PyType_GenericNew; if (PyType_Ready (&membuf_object_type) < 0) diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 996b23b..a64ded2 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -211,6 +211,7 @@ void gdbpy_initialize_breakpoint_event (void); void gdbpy_initialize_continue_event (void); void gdbpy_initialize_exited_event (void); void gdbpy_initialize_thread_event (void); +void gdbpy_initialize_new_objfile_event (void); struct cleanup *make_cleanup_py_decref (PyObject *py); @@ -219,6 +220,7 @@ struct cleanup *ensure_python_env (struct gdbarch *gdbarch, extern struct gdbarch *python_gdbarch; extern const struct language_defn *python_language; +extern struct objfile *gdbpy_current_objfile; /* 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 03edce9..812fc61 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -891,8 +891,9 @@ gdbpy_progspaces (PyObject *unused1, PyObject *unused2) /* The "current" objfile. This is set when gdb detects that a new objfile has been loaded. It is only set for the duration of a call to - source_python_script_for_objfile; it is NULL at other times. */ -static struct objfile *gdbpy_current_objfile; + source_python_script_for_objfile and new_objfile callbacks; it is NULL at + other times. */ +struct objfile *gdbpy_current_objfile; /* Set the current objfile to OBJFILE and then read STREAM,FILE as Python code. */ @@ -1209,6 +1210,7 @@ Enables or disables printing of Python stack traces."), gdbpy_initialize_continue_event (); gdbpy_initialize_exited_event (); gdbpy_initialize_thread_event (); + gdbpy_initialize_new_objfile_event () ; observer_attach_before_prompt (before_prompt_hook); diff --git a/gdb/testsuite/gdb.python/py-events.c b/gdb/testsuite/gdb.python/py-events.c index ceb697e..5ac72f2 100644 --- a/gdb/testsuite/gdb.python/py-events.c +++ b/gdb/testsuite/gdb.python/py-events.c @@ -16,6 +16,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ +extern void do_nothing (void); + int second(){ return 12; } @@ -25,5 +27,6 @@ int first(){ } int main (){ + do_nothing(); return first(); } diff --git a/gdb/testsuite/gdb.python/py-events.exp b/gdb/testsuite/gdb.python/py-events.exp index e5d6daf..11d7e00 100644 --- a/gdb/testsuite/gdb.python/py-events.exp +++ b/gdb/testsuite/gdb.python/py-events.exp @@ -24,23 +24,39 @@ if $tracelevel then { load_lib gdb-python.exp +set libfile "py-events-shlib" +set libsrc $srcdir/$subdir/$libfile.c +set lib_sl $objdir/$subdir/$libfile.so +set lib_opts debug + set testfile "py-events" set srcfile ${testfile}.c set binfile ${objdir}/${subdir}/${testfile} +set exec_opts [list debug shlib=$lib_sl] set pyfile ${srcdir}/${subdir}/${testfile}.py -if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } { +if [get_compiler_info ${binfile}] { return -1 } +if { [gdb_compile_shlib $libsrc $lib_sl $lib_opts] != "" + || [gdb_compile $srcdir/$subdir/$srcfile $binfile executable $exec_opts] != ""} { + untested "Could not compile either $libsrc or $srcdir/$subdir/$srcfile." + return -1 +} + +# Start with a fresh gdb. +clean_restart ${testfile} + if { [skip_python_tests] } { continue } gdb_test_no_output "python execfile ('${pyfile}')" "" -if ![runto_main ] then { - fail "Can't run to main" - return -1 -} +gdb_test "Test_Newobj_Events" "New ObjectFile Event tester registered." "Register new objfile event handler" + +gdb_breakpoint "main" {temporary} + +gdb_test "run" ".*event type: new_objfile.*new objfile name.*" "New objfile notification" gdb_test "Test_Events" "Event testers registered." diff --git a/gdb/testsuite/gdb.python/py-events.py b/gdb/testsuite/gdb.python/py-events.py index 9f05b9f..12e36e8 100644 --- a/gdb/testsuite/gdb.python/py-events.py +++ b/gdb/testsuite/gdb.python/py-events.py @@ -48,6 +48,14 @@ def continue_handler (event): if ( event.inferior_thread is not None) : print "thread num: %s" % (event.inferior_thread.num); +def new_objfile_handler (event): + if (isinstance (event, gdb.NewObjFileEvent)): + print "event type: new_objfile" + if (gdb.current_objfile () is not None): + print "new objfile name: %s" % (gdb.current_objfile ().filename) + else: + print "new objfile is None" + class test_events (gdb.Command): """Test events.""" @@ -62,3 +70,15 @@ class test_events (gdb.Command): print "Event testers registered." test_events () + +class test_newobj_events (gdb.Command): + """NewObj events.""" + + def __init__ (self): + gdb.Command.__init__ (self, "test_newobj_events", gdb.COMMAND_STACK) + + def invoke (self, arg, from_tty): + gdb.events.newobjfile.connect (new_objfile_handler) + print "New ObjectFile Event tester registered." + +test_newobj_events () -- 1.7.6 ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [RFC - Python] New ObjFile event 2011-09-01 9:56 ` Kevin Pouget @ 2011-09-01 11:01 ` Eli Zaretskii 2011-09-01 11:09 ` Kevin Pouget 0 siblings, 1 reply; 26+ messages in thread From: Eli Zaretskii @ 2011-09-01 11:01 UTC (permalink / raw) To: Kevin Pouget; +Cc: tromey, pmuldoon, gdb-patches > From: Kevin Pouget <kevin.pouget@gmail.com> > Date: Thu, 1 Sep 2011 11:17:51 +0200 > Cc: pmuldoon@redhat.com, gdb-patches@sourceware.org > > This event indicates that the inferior or one of its threads has received as > signal. @code{gdb.SignalEvent} has the following attributes: > > +@item events.newobjfile > +Emits @code{gdb.NewObjFileEvent} which indicates that a new object-file has > +been loaded by @value{GDBN}. > + > +During the callback, ``current objfile'' will be set to the new object file. > + > @table @code > @defivar SignalEvent stop_signal > A string representing the signal received by the inferior. A list of possible > @@ -22949,10 +22955,10 @@ The following objfile-related functions are available in the The addition doesn't look right: shouldn't it be _after_ the following @table line? events.newobjfile is an attribute of the same object as stop_signal, right? > +When auto-loading a Python script (@pxref{Auto-loading}) and during new > +object-file callbacks, @value{GDBN} sets the ``current objfile'' to the Why did you use "new" here? New in relation to what? Thanks. ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [RFC - Python] New ObjFile event 2011-09-01 11:01 ` Eli Zaretskii @ 2011-09-01 11:09 ` Kevin Pouget 2011-10-03 16:32 ` Tom Tromey 0 siblings, 1 reply; 26+ messages in thread From: Kevin Pouget @ 2011-09-01 11:09 UTC (permalink / raw) To: Eli Zaretskii; +Cc: tromey, pmuldoon, gdb-patches [-- Attachment #1: Type: text/plain, Size: 3320 bytes --] On Thu, Sep 1, 2011 at 12:18 PM, Eli Zaretskii <eliz@gnu.org> wrote: >> From: Kevin Pouget <kevin.pouget@gmail.com> >> Date: Thu, 1 Sep 2011 11:17:51 +0200 >> Cc: pmuldoon@redhat.com, gdb-patches@sourceware.org >> >> This event indicates that the inferior or one of its threads has received as >> signal. @code{gdb.SignalEvent} has the following attributes: >> >> +@item events.newobjfile >> +Emits @code{gdb.NewObjFileEvent} which indicates that a new object-file has >> +been loaded by @value{GDBN}. >> + >> +During the callback, ``current objfile'' will be set to the new object file. >> + >> @table @code >> @defivar SignalEvent stop_signal >> A string representing the signal received by the inferior. A list of possible >> @@ -22949,10 +22955,10 @@ The following objfile-related functions are available in the > > The addition doesn't look right: shouldn't it be _after_ the following > @table line? events.newobjfile is an attribute of the same object as > stop_signal, right? sorry; I moved the paragraph to the right place. >> +When auto-loading a Python script (@pxref{Auto-loading}) and during new >> +object-file callbacks, @value{GDBN} sets the ``current objfile'' to the > > Why did you use "new" here? New in relation to what? "new" here refers to object-file, but I'm not sure I can write "during new-object-file callbacks", can I? I also added a label reference to make things clearer here: > During the callback, ``current objfile'' will be set to the new object file > (@pxref{Objfiles In Python}). (I'm not sure why there is no `_' between 'current' and 'objfile', I copied it from another place --- @value{GDBN} sets the ``current objfile'' to the corresponding objfile.) Many thanks, Kevin 2011-09-01 Kevin Pouget <kevin.pouget@st.com> Allow Python notification of new object-file loadings. * Makefile.in (SUBDIR_PYTHON_SRCS): Add py-newobjfilevent.c. (SUBDIR_PYTHON_OBS): Add py-newobjfileevent.o. Add build rule for this file. * python/py-event.h (emit_new_objfile_event): New prototype. (newobjfile): New Python event emitter. * python/py-evts.c (gdbpy_initialize_py_events): Add newobjfile to Python event registry. * python/py-inferior.c: Include objfiles.h (python_new_objfile): New function. (gdbpy_initialize_inferior): Add python_new_objfile to the new objfile observers. * python/py-newobjfileevent.c: New file. * python-internal.h (gdbpy_initialize_new_objfile_event): New prototype. (gdbpy_current_objfile): New global variable. * python/python.c (gdbpy_current_objfile): Make global. (_initialize_python): Add gdbpy_initialize_new_objfile_event call. 2011-09-01 Kevin Pouget <kevin.pouget@st.com> Allow Python notification of new object-file loadings. * gdb.texinfo (Events In Python): Document `gdb.NewObjFileEvent' events emitter. (Objfiles In Python): Indicate that `gdb.current_objfile' is also set during new object-file callbacks. 2011-09-01 Kevin Pouget <kevin.pouget@st.com> Allow Python notification of new object-file loadings. * gdb.python/py-events.exp: Test newobjfile event. * gdb.python/py-events.py: Register newobjfile callback. * gdb.python/py-events.c: Add call to shared library * gdb.python/py-events-shlib.c: New file. [-- Attachment #2: 0001-New-ObjFile-event.patch --] [-- Type: text/x-patch, Size: 14688 bytes --] From 2bce2763858358b753f1da7b2bb3da0215ed8bfc Mon Sep 17 00:00:00 2001 From: Kevin Pouget <kevin.pouget@st.com> Date: Thu, 1 Sep 2011 11:08:23 +0200 Subject: [PATCH] New ObjFile event --- gdb/Makefile.in | 6 +++ gdb/NEWS | 2 + gdb/doc/gdb.texinfo | 15 ++++++-- gdb/python/py-event.h | 1 + gdb/python/py-events.h | 1 + gdb/python/py-evts.c | 3 ++ gdb/python/py-inferior.c | 25 +++++++++++++ gdb/python/py-newobjfileevent.c | 53 ++++++++++++++++++++++++++++ gdb/python/python-internal.h | 2 + gdb/python/python.c | 6 ++- gdb/testsuite/gdb.python/py-events-shlib.c | 20 ++++++++++ gdb/testsuite/gdb.python/py-events.c | 3 ++ gdb/testsuite/gdb.python/py-events.exp | 26 +++++++++++--- gdb/testsuite/gdb.python/py-events.py | 20 ++++++++++ 14 files changed, 172 insertions(+), 11 deletions(-) create mode 100644 gdb/python/py-newobjfileevent.c create mode 100644 gdb/testsuite/gdb.python/py-events-shlib.c diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 826d339..986588b 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -289,6 +289,7 @@ SUBDIR_PYTHON_OBS = \ py-inferior.o \ py-infthread.o \ py-lazy-string.o \ + py-newobjfileevent.o \ py-objfile.o \ py-param.o \ py-prettyprint.o \ @@ -319,6 +320,7 @@ SUBDIR_PYTHON_SRCS = \ python/py-inferior.c \ python/py-infthread.c \ python/py-lazy-string.c \ + python/py-newobjfileevent.c \ python/py-objfile.c \ python/py-param.c \ python/py-prettyprint.c \ @@ -2115,6 +2117,10 @@ py-lazy-string.o: $(srcdir)/python/py-lazy-string.c $(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-lazy-string.c $(POSTCOMPILE) +py-newobjfileevent.o: $(srcdir)/python/py-newobjfileevent.c + $(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-newobjfileevent.c + $(POSTCOMPILE) + py-objfile.o: $(srcdir)/python/py-objfile.c $(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-objfile.c $(POSTCOMPILE) diff --git a/gdb/NEWS b/gdb/NEWS index 255a22e..bb5795c 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -38,6 +38,8 @@ ** Symbols now provide the "type" attribute, the type of the symbol. + ** A new event "gdb.newobjfile" has been added. + * libthread-db-search-path now supports two special values: $sdir and $pdir. $sdir specifies the default system locations of shared libraries. $pdir specifies the directory where the libpthread used by the application diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 23b2a98..056776a 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -22327,6 +22327,13 @@ A reference to the breakpoint that was hit of type @code{gdb.Breakpoint}. @end defivar @end table +@item events.newobjfile +Emits @code{gdb.NewObjFileEvent} which indicates that a new object-file has +been loaded by @value{GDBN}. + +During the callback, ``current objfile'' will be set to the new object file +(@pxref{Objfiles In Python}). + @end table @node Threads In Python @@ -22949,10 +22956,10 @@ The following objfile-related functions are available in the @findex gdb.current_objfile @defun current_objfile -When auto-loading a Python script (@pxref{Auto-loading}), @value{GDBN} -sets the ``current objfile'' to the corresponding objfile. This -function returns the current objfile. If there is no current objfile, -this function returns @code{None}. +When auto-loading a Python script (@pxref{Auto-loading}) and during +new-object-file callbacks, @value{GDBN} sets the ``current objfile'' to +the corresponding objfile. This function returns the current objfile. +If there is no current objfile, this function returns @code{None}. @end defun @findex gdb.objfiles diff --git a/gdb/python/py-event.h b/gdb/python/py-event.h index f4b3ee2..9069a51 100644 --- a/gdb/python/py-event.h +++ b/gdb/python/py-event.h @@ -111,6 +111,7 @@ extern int evpy_emit_event (PyObject *event, extern PyObject *create_event_object (PyTypeObject *py_type); extern PyObject *create_thread_event_object (PyTypeObject *py_type); +extern int emit_new_objfile_event (struct objfile *objfile); extern void evpy_dealloc (PyObject *self); extern int evpy_add_attribute (PyObject *event, diff --git a/gdb/python/py-events.h b/gdb/python/py-events.h index bd54418..70f45a4 100644 --- a/gdb/python/py-events.h +++ b/gdb/python/py-events.h @@ -45,6 +45,7 @@ typedef struct eventregistry_object *stop; eventregistry_object *cont; eventregistry_object *exited; + eventregistry_object *newobjfile; PyObject *module; diff --git a/gdb/python/py-evts.c b/gdb/python/py-evts.c index 50c05f3..c8d1044 100644 --- a/gdb/python/py-evts.c +++ b/gdb/python/py-evts.c @@ -58,6 +58,9 @@ gdbpy_initialize_py_events (void) if (add_new_registry (&gdb_py_events.exited, "exited") < 0) goto fail; + if (add_new_registry (&gdb_py_events.newobjfile, "newobjfile") < 0) + goto fail; + Py_INCREF (gdb_py_events.module); if (PyModule_AddObject (gdb_module, "events", diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index 6add681..63ffc2b 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -22,6 +22,7 @@ #include "gdbcore.h" #include "gdbthread.h" #include "inferior.h" +#include "objfiles.h" #include "observer.h" #include "python-internal.h" #include "arch-utils.h" @@ -125,6 +126,29 @@ python_inferior_exit (struct inferior *inf) do_cleanups (cleanup); } +/* Callback used to notify Python listeners about new objfiles loaded in the + inferior. Python global variable 'gdb.current_objfile ()' will be set + during the notifications. */ + +static void +python_new_objfile (struct objfile *objfile) +{ + struct cleanup *cleanup; + + if (objfile == NULL) + return; + + cleanup = ensure_python_env (get_objfile_arch (objfile), current_language); + + gdbpy_current_objfile = objfile; + + if (emit_new_objfile_event (objfile) < 0) + gdbpy_print_stack (); + + do_cleanups (cleanup); + gdbpy_current_objfile = NULL; +} + /* Return a reference to the Python object of type Inferior representing INFERIOR. If the object has already been created, return it and increment the reference count, otherwise, create it. @@ -701,6 +725,7 @@ gdbpy_initialize_inferior (void) observer_attach_normal_stop (python_on_normal_stop); observer_attach_target_resumed (python_on_resume); observer_attach_inferior_exit (python_inferior_exit); + observer_attach_new_objfile (python_new_objfile); membuf_object_type.tp_new = PyType_GenericNew; if (PyType_Ready (&membuf_object_type) < 0) diff --git a/gdb/python/py-newobjfileevent.c b/gdb/python/py-newobjfileevent.c new file mode 100644 index 0000000..20e2b31 --- /dev/null +++ b/gdb/python/py-newobjfileevent.c @@ -0,0 +1,53 @@ +/* Python interface to new object file loading events. + + Copyright (C) 2011 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 "py-event.h" + +static PyTypeObject new_objfile_event_object_type; + +PyObject * +create_new_objfile_event_object (void) +{ + return create_event_object (&new_objfile_event_object_type); +} + +/* Callback function which notifies observers when a new objfile event occurs. + This function will create a new Python new_objfile event object. + Return -1 if emit fails. */ + +int +emit_new_objfile_event (struct objfile *objfile) +{ + PyObject *event; + + if (evregpy_no_listeners_p (gdb_py_events.newobjfile)) + return 0; + + event = create_new_objfile_event_object (); + if (event) + return evpy_emit_event (event, gdb_py_events.newobjfile); + return -1; +} + +GDBPY_NEW_EVENT_TYPE (new_objfile, + "gdb.NewObjFileEvent", + "NewObjFileEvent", + "GDB new object file event object", + event_object_type, + static); diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 996b23b..a64ded2 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -211,6 +211,7 @@ void gdbpy_initialize_breakpoint_event (void); void gdbpy_initialize_continue_event (void); void gdbpy_initialize_exited_event (void); void gdbpy_initialize_thread_event (void); +void gdbpy_initialize_new_objfile_event (void); struct cleanup *make_cleanup_py_decref (PyObject *py); @@ -219,6 +220,7 @@ struct cleanup *ensure_python_env (struct gdbarch *gdbarch, extern struct gdbarch *python_gdbarch; extern const struct language_defn *python_language; +extern struct objfile *gdbpy_current_objfile; /* 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 03edce9..812fc61 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -891,8 +891,9 @@ gdbpy_progspaces (PyObject *unused1, PyObject *unused2) /* The "current" objfile. This is set when gdb detects that a new objfile has been loaded. It is only set for the duration of a call to - source_python_script_for_objfile; it is NULL at other times. */ -static struct objfile *gdbpy_current_objfile; + source_python_script_for_objfile and new_objfile callbacks; it is NULL at + other times. */ +struct objfile *gdbpy_current_objfile; /* Set the current objfile to OBJFILE and then read STREAM,FILE as Python code. */ @@ -1209,6 +1210,7 @@ Enables or disables printing of Python stack traces."), gdbpy_initialize_continue_event (); gdbpy_initialize_exited_event (); gdbpy_initialize_thread_event (); + gdbpy_initialize_new_objfile_event () ; observer_attach_before_prompt (before_prompt_hook); diff --git a/gdb/testsuite/gdb.python/py-events-shlib.c b/gdb/testsuite/gdb.python/py-events-shlib.c new file mode 100644 index 0000000..2a17bbf --- /dev/null +++ b/gdb/testsuite/gdb.python/py-events-shlib.c @@ -0,0 +1,20 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2011 Free Software Foundation, Inc. + + 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/>. */ + + +void do_nothing (void) {} + diff --git a/gdb/testsuite/gdb.python/py-events.c b/gdb/testsuite/gdb.python/py-events.c index ceb697e..5ac72f2 100644 --- a/gdb/testsuite/gdb.python/py-events.c +++ b/gdb/testsuite/gdb.python/py-events.c @@ -16,6 +16,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ +extern void do_nothing (void); + int second(){ return 12; } @@ -25,5 +27,6 @@ int first(){ } int main (){ + do_nothing(); return first(); } diff --git a/gdb/testsuite/gdb.python/py-events.exp b/gdb/testsuite/gdb.python/py-events.exp index e5d6daf..11d7e00 100644 --- a/gdb/testsuite/gdb.python/py-events.exp +++ b/gdb/testsuite/gdb.python/py-events.exp @@ -24,23 +24,39 @@ if $tracelevel then { load_lib gdb-python.exp +set libfile "py-events-shlib" +set libsrc $srcdir/$subdir/$libfile.c +set lib_sl $objdir/$subdir/$libfile.so +set lib_opts debug + set testfile "py-events" set srcfile ${testfile}.c set binfile ${objdir}/${subdir}/${testfile} +set exec_opts [list debug shlib=$lib_sl] set pyfile ${srcdir}/${subdir}/${testfile}.py -if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } { +if [get_compiler_info ${binfile}] { return -1 } +if { [gdb_compile_shlib $libsrc $lib_sl $lib_opts] != "" + || [gdb_compile $srcdir/$subdir/$srcfile $binfile executable $exec_opts] != ""} { + untested "Could not compile either $libsrc or $srcdir/$subdir/$srcfile." + return -1 +} + +# Start with a fresh gdb. +clean_restart ${testfile} + if { [skip_python_tests] } { continue } gdb_test_no_output "python execfile ('${pyfile}')" "" -if ![runto_main ] then { - fail "Can't run to main" - return -1 -} +gdb_test "Test_Newobj_Events" "New ObjectFile Event tester registered." "Register new objfile event handler" + +gdb_breakpoint "main" {temporary} + +gdb_test "run" ".*event type: new_objfile.*new objfile name.*" "New objfile notification" gdb_test "Test_Events" "Event testers registered." diff --git a/gdb/testsuite/gdb.python/py-events.py b/gdb/testsuite/gdb.python/py-events.py index 9f05b9f..12e36e8 100644 --- a/gdb/testsuite/gdb.python/py-events.py +++ b/gdb/testsuite/gdb.python/py-events.py @@ -48,6 +48,14 @@ def continue_handler (event): if ( event.inferior_thread is not None) : print "thread num: %s" % (event.inferior_thread.num); +def new_objfile_handler (event): + if (isinstance (event, gdb.NewObjFileEvent)): + print "event type: new_objfile" + if (gdb.current_objfile () is not None): + print "new objfile name: %s" % (gdb.current_objfile ().filename) + else: + print "new objfile is None" + class test_events (gdb.Command): """Test events.""" @@ -62,3 +70,15 @@ class test_events (gdb.Command): print "Event testers registered." test_events () + +class test_newobj_events (gdb.Command): + """NewObj events.""" + + def __init__ (self): + gdb.Command.__init__ (self, "test_newobj_events", gdb.COMMAND_STACK) + + def invoke (self, arg, from_tty): + gdb.events.newobjfile.connect (new_objfile_handler) + print "New ObjectFile Event tester registered." + +test_newobj_events () -- 1.7.6 ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [RFC - Python] New ObjFile event 2011-09-01 11:09 ` Kevin Pouget @ 2011-10-03 16:32 ` Tom Tromey [not found] ` <CAPftXULEe9R4m7tF=vtJe6NTXHSFAkXgsHCPb3r0mU4wKx0FFg@mail.gmail.com> 0 siblings, 1 reply; 26+ messages in thread From: Tom Tromey @ 2011-10-03 16:32 UTC (permalink / raw) To: Kevin Pouget; +Cc: Eli Zaretskii, pmuldoon, gdb-patches >>>>> "Kevin" == Kevin Pouget <kevin.pouget@gmail.com> writes: Kevin> Allow Python notification of new object-file loadings. [...] Kevin> +During the callback, ``current objfile'' will be set to the new object file Kevin> +(@pxref{Objfiles In Python}). Why not simply make the objfile object an attribute of the event? That seems cleaner to me. Kevin> + if (objfile == NULL) Kevin> + return; Indentation looks off. Tom ^ permalink raw reply [flat|nested] 26+ messages in thread
[parent not found: <CAPftXULEe9R4m7tF=vtJe6NTXHSFAkXgsHCPb3r0mU4wKx0FFg@mail.gmail.com>]
* Re: [RFC - Python] New ObjFile event [not found] ` <CAPftXULEe9R4m7tF=vtJe6NTXHSFAkXgsHCPb3r0mU4wKx0FFg@mail.gmail.com> @ 2011-10-04 8:25 ` Kevin Pouget 2011-10-04 17:02 ` Tom Tromey 0 siblings, 1 reply; 26+ messages in thread From: Kevin Pouget @ 2011-10-04 8:25 UTC (permalink / raw) To: gdb-patches (sorry for the resend) On Mon, Oct 3, 2011 at 6:31 PM, Tom Tromey <tromey@redhat.com> wrote: >>>>>> "Kevin" == Kevin Pouget <kevin.pouget@gmail.com> writes: > > Kevin> Allow Python notification of new object-file loadings. > [...] > > Kevin> +During the callback, ``current objfile'' will be set to the new object file > Kevin> +(@pxref{Objfiles In Python}). > > Why not simply make the objfile object an attribute of the event? > That seems cleaner to me. We already discussed this point, but it was at the end of ... March :) > > Kevin> and, likewise, allows Python's "gdb.current_objfile ()" to return > > Kevin> the current object file. > > > > Tom: > > I'm ambivalent about this part. > > > > It seems to me that the objfile should be an attribute of the event. > > Also setting it globally is ok, if you really want that, but it isn't a > > necessity. > > Kevin: > I was ambivalent about this point too; I chose "gdb.current_objfile()" because autoloading > (http://sourceware.org/gdb/current/onlinedocs/gdb/Auto_002dloading.html) > already does it this way, and the two features are pretty similar. I > felt that it would have been strange to access "gdb.current_objfile ()" > in one case, and sth like "event.new_objfile" in the other case > > let me know if you agree with this perspective; it wouldn't be a big > deal to flip to the other solution otherwise (the discussion didn't go any further, I was busy fighting with the copyright assignment!) Let me know what you prefer, I'll change the code accordingly > Kevin> + if (objfile == NULL) > Kevin> + return; > > Indentation looks off. will be fixed in the next submission Thanks for your reviews, Kevin ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [RFC - Python] New ObjFile event 2011-10-04 8:25 ` Kevin Pouget @ 2011-10-04 17:02 ` Tom Tromey 2011-10-05 11:10 ` Kevin Pouget 0 siblings, 1 reply; 26+ messages in thread From: Tom Tromey @ 2011-10-04 17:02 UTC (permalink / raw) To: Kevin Pouget; +Cc: gdb-patches >>>>> "Kevin" == Kevin Pouget <kevin.pouget@gmail.com> writes: Tom> Why not simply make the objfile object an attribute of the event? Tom> That seems cleaner to me. Kevin> We already discussed this point, but it was at the end of ... March :) Thanks for the reminder. I completely forgot about that. Kevin> I was ambivalent about this point too; I chose Kevin> "gdb.current_objfile()" because autoloading Kevin> (http://sourceware.org/gdb/current/onlinedocs/gdb/Auto_002dloading.html) Kevin> already does it this way, and the two features are pretty similar. I Kevin> felt that it would have been strange to access "gdb.current_objfile ()" Kevin> in one case, and sth like "event.new_objfile" in the other case Ok, I see. I think it is best to pass it as part of the event. It is ok with me if you also want to have it set the current objfile. Tom ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [RFC - Python] New ObjFile event 2011-10-04 17:02 ` Tom Tromey @ 2011-10-05 11:10 ` Kevin Pouget 2011-10-05 14:16 ` Tom Tromey 2011-10-05 17:40 ` Eli Zaretskii 0 siblings, 2 replies; 26+ messages in thread From: Kevin Pouget @ 2011-10-05 11:10 UTC (permalink / raw) To: Tom Tromey, Eli Zaretskii; +Cc: gdb-patches [-- Attachment #1: Type: text/plain, Size: 2708 bytes --] On Tue, Oct 4, 2011 at 7:01 PM, Tom Tromey <tromey@redhat.com> wrote: > > >>>>> "Kevin" == Kevin Pouget <kevin.pouget@gmail.com> writes: > > Tom> Why not simply make the objfile object an attribute of the event? > Tom> That seems cleaner to me. > > Kevin> We already discussed this point, but it was at the end of ... March :) > > Thanks for the reminder. > I completely forgot about that. > > Kevin> I was ambivalent about this point too; I chose > Kevin> "gdb.current_objfile()" because autoloading > Kevin> (http://sourceware.org/gdb/current/onlinedocs/gdb/Auto_002dloading.html) > Kevin> already does it this way, and the two features are pretty similar. I > Kevin> felt that it would have been strange to access "gdb.current_objfile ()" > Kevin> in one case, and sth like "event.new_objfile" in the other case > > Ok, I see. > > I think it is best to pass it as part of the event. > It is ok with me if you also want to have it set the current objfile. > > Tom I got rid of the "gdb.current_objfile ()" access to the objfile and kept only the "NewObjFile.new_objfile" attribute of the Event object, which indeed makes more sense. I've updated the code and documentation accordingly; could you please tell me if it seems good for you? Thanks, Kevin 2011-10-05 Kevin Pouget <kevin.pouget@st.com> Allow Python notification of new object-file loadings. * Makefile.in (SUBDIR_PYTHON_SRCS): Add py-newobjfilevent.c. (SUBDIR_PYTHON_OBS): Add py-newobjfileevent.o. Add build rule for this file. * python/py-event.h (emit_new_objfile_event): New prototype. (newobjfile): New Python event emitter. * python/py-evts.c (gdbpy_initialize_py_events): Add new_objfile to Python event registry. * python/py-inferior.c: Include objfiles.h (python_new_objfile): New function. (gdbpy_initialize_inferior): Add python_new_objfile to the new objfile observers. * python/py-newobjfileevent.c: New file. * python-internal.h (gdbpy_initialize_new_objfile_event): New prototype. (gdbpy_current_objfile): New global variable. * python/python.c (gdbpy_current_objfile): Make global. (_initialize_python): Add gdbpy_initialize_new_objfile_event call. * NEWS: Add item for new Python event "gdb.newobjfile" 2011-10-05 Kevin Pouget <kevin.pouget@st.com> Allow Python notification of new object-file loadings. * gdb.texinfo (Events In Python): Document `gdb.NewObjFileEvent' event type. 2011-10-05 Kevin Pouget <kevin.pouget@st.com> Allow Python notification of new object-file loadings. * gdb.python/py-events.exp: Test newobjfile event. * gdb.python/py-events.py: Register newobjfile callback. * gdb.python/py-events.c: Add call to shared library * gdb.python/py-events-shlib.c: New file. [-- Attachment #2: 0001-New-ObjFile-event.patch --] [-- Type: text/x-patch, Size: 14423 bytes --] From 5984c89f5a5ae980c09daf942c9722b03fd0cd41 Mon Sep 17 00:00:00 2001 From: Kevin Pouget <kevin.pouget@st.com> Date: Thu, 1 Sep 2011 11:08:23 +0200 Subject: [PATCH] New ObjFile event --- gdb/Makefile.in | 6 ++ gdb/NEWS | 2 + gdb/doc/gdb.texinfo | 11 ++++ gdb/python/py-event.h | 1 + gdb/python/py-events.h | 1 + gdb/python/py-evts.c | 3 + gdb/python/py-inferior.c | 21 ++++++++ gdb/python/py-newobjfileevent.c | 70 ++++++++++++++++++++++++++++ gdb/python/python-internal.h | 2 + gdb/python/python.c | 6 ++- gdb/testsuite/gdb.python/py-events-shlib.c | 20 ++++++++ gdb/testsuite/gdb.python/py-events.c | 3 + gdb/testsuite/gdb.python/py-events.exp | 26 ++++++++-- gdb/testsuite/gdb.python/py-events.py | 20 ++++++++ 14 files changed, 185 insertions(+), 7 deletions(-) create mode 100644 gdb/python/py-newobjfileevent.c create mode 100644 gdb/testsuite/gdb.python/py-events-shlib.c diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 826d339..986588b 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -289,6 +289,7 @@ SUBDIR_PYTHON_OBS = \ py-inferior.o \ py-infthread.o \ py-lazy-string.o \ + py-newobjfileevent.o \ py-objfile.o \ py-param.o \ py-prettyprint.o \ @@ -319,6 +320,7 @@ SUBDIR_PYTHON_SRCS = \ python/py-inferior.c \ python/py-infthread.c \ python/py-lazy-string.c \ + python/py-newobjfileevent.c \ python/py-objfile.c \ python/py-param.c \ python/py-prettyprint.c \ @@ -2115,6 +2117,10 @@ py-lazy-string.o: $(srcdir)/python/py-lazy-string.c $(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-lazy-string.c $(POSTCOMPILE) +py-newobjfileevent.o: $(srcdir)/python/py-newobjfileevent.c + $(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-newobjfileevent.c + $(POSTCOMPILE) + py-objfile.o: $(srcdir)/python/py-objfile.c $(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-objfile.c $(POSTCOMPILE) diff --git a/gdb/NEWS b/gdb/NEWS index 20e58a0..788498f 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -41,6 +41,8 @@ ** The "gdb.breakpoint" function has been deprecated in favor of "gdb.breakpoints". + ** A new event "gdb.newobjfile" has been added. + * libthread-db-search-path now supports two special values: $sdir and $pdir. $sdir specifies the default system locations of shared libraries. $pdir specifies the directory where the libpthread used by the application diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index dd0f7f6..5be090f 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -22358,6 +22358,17 @@ in favor of the @code{gdb.BreakpointEvent.breakpoints} attribute. @end defvar @end table +@item events.new_objfile +Emits @code{gdb.NewObjFileEvent} which indicates that a new object-file has +been loaded by @value{GDBN}. @code{gdb.NewObjFileEvent} has one attribute: + +@table @code +@defvar NewObjFileEvent.new_objfile +A reference to the object file (@code{gdb.Objfile}) which has been loaded. +@xref{Objfiles In Python}, for details of the @code{gdb.Objfile} object. +@end defvar +@end table + @end table @node Threads In Python diff --git a/gdb/python/py-event.h b/gdb/python/py-event.h index 49d7b6a..716aabd 100644 --- a/gdb/python/py-event.h +++ b/gdb/python/py-event.h @@ -111,6 +111,7 @@ extern int evpy_emit_event (PyObject *event, extern PyObject *create_event_object (PyTypeObject *py_type); extern PyObject *create_thread_event_object (PyTypeObject *py_type); +extern int emit_new_objfile_event (struct objfile *objfile); extern void evpy_dealloc (PyObject *self); extern int evpy_add_attribute (PyObject *event, diff --git a/gdb/python/py-events.h b/gdb/python/py-events.h index bd54418..70f45a4 100644 --- a/gdb/python/py-events.h +++ b/gdb/python/py-events.h @@ -45,6 +45,7 @@ typedef struct eventregistry_object *stop; eventregistry_object *cont; eventregistry_object *exited; + eventregistry_object *newobjfile; PyObject *module; diff --git a/gdb/python/py-evts.c b/gdb/python/py-evts.c index 50c05f3..c24a04c 100644 --- a/gdb/python/py-evts.c +++ b/gdb/python/py-evts.c @@ -58,6 +58,9 @@ gdbpy_initialize_py_events (void) if (add_new_registry (&gdb_py_events.exited, "exited") < 0) goto fail; + if (add_new_registry (&gdb_py_events.newobjfile, "new_objfile") < 0) + goto fail; + Py_INCREF (gdb_py_events.module); if (PyModule_AddObject (gdb_module, "events", diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index 275caea..afc3eb0 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -22,6 +22,7 @@ #include "gdbcore.h" #include "gdbthread.h" #include "inferior.h" +#include "objfiles.h" #include "observer.h" #include "python-internal.h" #include "arch-utils.h" @@ -125,6 +126,25 @@ python_inferior_exit (struct inferior *inf) do_cleanups (cleanup); } +/* Callback used to notify Python listeners about new objfiles loaded in the + inferior. */ + +static void +python_new_objfile (struct objfile *objfile) +{ + struct cleanup *cleanup; + + if (objfile == NULL) + return; + + cleanup = ensure_python_env (get_objfile_arch (objfile), current_language); + + if (emit_new_objfile_event (objfile) < 0) + gdbpy_print_stack (); + + do_cleanups (cleanup); +} + /* Return a reference to the Python object of type Inferior representing INFERIOR. If the object has already been created, return it and increment the reference count, otherwise, create it. @@ -715,6 +735,7 @@ gdbpy_initialize_inferior (void) observer_attach_normal_stop (python_on_normal_stop); observer_attach_target_resumed (python_on_resume); observer_attach_inferior_exit (python_inferior_exit); + observer_attach_new_objfile (python_new_objfile); membuf_object_type.tp_new = PyType_GenericNew; if (PyType_Ready (&membuf_object_type) < 0) diff --git a/gdb/python/py-newobjfileevent.c b/gdb/python/py-newobjfileevent.c new file mode 100644 index 0000000..e9716a6 --- /dev/null +++ b/gdb/python/py-newobjfileevent.c @@ -0,0 +1,70 @@ +/* Python interface to new object file loading events. + + Copyright (C) 2011 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 "py-event.h" + +static PyTypeObject new_objfile_event_object_type; + +PyObject * +create_new_objfile_event_object (struct objfile *objfile) +{ + PyObject *objfile_event; + PyObject *py_objfile; + + objfile_event = create_event_object (&new_objfile_event_object_type); + if (!objfile_event) + goto fail; + + py_objfile = objfile_to_objfile_object (objfile); + if (!py_objfile || evpy_add_attribute (objfile_event, + "new_objfile", + py_objfile) < 0) + goto fail; + + return objfile_event; + + fail: + Py_XDECREF (objfile_event); + return NULL; +} + +/* Callback function which notifies observers when a new objfile event occurs. + This function will create a new Python new_objfile event object. + Return -1 if emit fails. */ + +int +emit_new_objfile_event (struct objfile *objfile) +{ + PyObject *event; + + if (evregpy_no_listeners_p (gdb_py_events.newobjfile)) + return 0; + + event = create_new_objfile_event_object (objfile); + if (event) + return evpy_emit_event (event, gdb_py_events.newobjfile); + return -1; +} + +GDBPY_NEW_EVENT_TYPE (new_objfile, + "gdb.NewObjFileEvent", + "NewObjFileEvent", + "GDB new object file event object", + event_object_type, + static); diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index bff40b5..31bccaa 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -214,6 +214,7 @@ void gdbpy_initialize_breakpoint_event (void); void gdbpy_initialize_continue_event (void); void gdbpy_initialize_exited_event (void); void gdbpy_initialize_thread_event (void); +void gdbpy_initialize_new_objfile_event (void); struct cleanup *make_cleanup_py_decref (PyObject *py); @@ -222,6 +223,7 @@ struct cleanup *ensure_python_env (struct gdbarch *gdbarch, extern struct gdbarch *python_gdbarch; extern const struct language_defn *python_language; +extern struct objfile *gdbpy_current_objfile; /* 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 61c5420..f95995a 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -938,8 +938,9 @@ gdbpy_progspaces (PyObject *unused1, PyObject *unused2) /* The "current" objfile. This is set when gdb detects that a new objfile has been loaded. It is only set for the duration of a call to - source_python_script_for_objfile; it is NULL at other times. */ -static struct objfile *gdbpy_current_objfile; + source_python_script_for_objfile and new_objfile callbacks; it is NULL at + other times. */ +struct objfile *gdbpy_current_objfile; /* Set the current objfile to OBJFILE and then read STREAM,FILE as Python code. @@ -1257,6 +1258,7 @@ Enables or disables printing of Python stack traces."), gdbpy_initialize_continue_event (); gdbpy_initialize_exited_event (); gdbpy_initialize_thread_event (); + gdbpy_initialize_new_objfile_event () ; observer_attach_before_prompt (before_prompt_hook); diff --git a/gdb/testsuite/gdb.python/py-events-shlib.c b/gdb/testsuite/gdb.python/py-events-shlib.c new file mode 100644 index 0000000..2a17bbf --- /dev/null +++ b/gdb/testsuite/gdb.python/py-events-shlib.c @@ -0,0 +1,20 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2011 Free Software Foundation, Inc. + + 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/>. */ + + +void do_nothing (void) {} + diff --git a/gdb/testsuite/gdb.python/py-events.c b/gdb/testsuite/gdb.python/py-events.c index 665ca51..c20cc84 100644 --- a/gdb/testsuite/gdb.python/py-events.c +++ b/gdb/testsuite/gdb.python/py-events.c @@ -16,6 +16,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ +extern void do_nothing (void); + int second(){ fork() ; return 12; @@ -26,5 +28,6 @@ int first(){ } int main (){ + do_nothing(); return first(); } diff --git a/gdb/testsuite/gdb.python/py-events.exp b/gdb/testsuite/gdb.python/py-events.exp index 18419fa..e420389 100644 --- a/gdb/testsuite/gdb.python/py-events.exp +++ b/gdb/testsuite/gdb.python/py-events.exp @@ -19,23 +19,39 @@ if $tracelevel then { load_lib gdb-python.exp +set libfile "py-events-shlib" +set libsrc $srcdir/$subdir/$libfile.c +set lib_sl $objdir/$subdir/$libfile.so +set lib_opts debug + set testfile "py-events" set srcfile ${testfile}.c set binfile ${objdir}/${subdir}/${testfile} +set exec_opts [list debug shlib=$lib_sl] set pyfile ${srcdir}/${subdir}/${testfile}.py -if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } { +if [get_compiler_info ${binfile}] { return -1 } +if { [gdb_compile_shlib $libsrc $lib_sl $lib_opts] != "" + || [gdb_compile $srcdir/$subdir/$srcfile $binfile executable $exec_opts] != ""} { + untested "Could not compile either $libsrc or $srcdir/$subdir/$srcfile." + return -1 +} + +# Start with a fresh gdb. +clean_restart ${testfile} + if { [skip_python_tests] } { continue } gdb_test_no_output "python execfile ('${pyfile}')" "" -if ![runto_main ] then { - fail "Can't run to main" - return -1 -} +gdb_test "Test_Newobj_Events" "New ObjectFile Event tester registered." "Register new objfile event handler" + +gdb_breakpoint "main" {temporary} + +gdb_test "run" ".*event type: new_objfile.*new objfile name.*" "New objfile notification" gdb_test_no_output "set detach-on-fork off" "" diff --git a/gdb/testsuite/gdb.python/py-events.py b/gdb/testsuite/gdb.python/py-events.py index 6bdd935..ab549c4 100644 --- a/gdb/testsuite/gdb.python/py-events.py +++ b/gdb/testsuite/gdb.python/py-events.py @@ -51,6 +51,14 @@ def continue_handler (event): if ( event.inferior_thread is not None) : print "thread num: %s" % (event.inferior_thread.num); +def new_objfile_handler (event): + if (isinstance (event, gdb.NewObjFileEvent)): + print "event type: new_objfile" + if (event.new_objfile is not None): + print "new objfile name: %s" % (event.new_objfile.filename) + else: + print "new objfile is None" + class test_events (gdb.Command): """Test events.""" @@ -65,3 +73,15 @@ class test_events (gdb.Command): print "Event testers registered." test_events () + +class test_newobj_events (gdb.Command): + """NewObj events.""" + + def __init__ (self): + gdb.Command.__init__ (self, "test_newobj_events", gdb.COMMAND_STACK) + + def invoke (self, arg, from_tty): + gdb.events.new_objfile.connect (new_objfile_handler) + print "New ObjectFile Event tester registered." + +test_newobj_events () -- 1.7.6.2 ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [RFC - Python] New ObjFile event 2011-10-05 11:10 ` Kevin Pouget @ 2011-10-05 14:16 ` Tom Tromey 2011-10-05 17:40 ` Eli Zaretskii 1 sibling, 0 replies; 26+ messages in thread From: Tom Tromey @ 2011-10-05 14:16 UTC (permalink / raw) To: Kevin Pouget; +Cc: Eli Zaretskii, gdb-patches >>>>> "Kevin" == Kevin Pouget <kevin.pouget@gmail.com> writes: Tom> I think it is best to pass it as part of the event. Tom> It is ok with me if you also want to have it set the current objfile. Kevin> I got rid of the "gdb.current_objfile ()" access to the objfile and Kevin> kept only the "NewObjFile.new_objfile" attribute of the Event object, Kevin> which indeed makes more sense. Kevin> I've updated the code and documentation accordingly; could you please Kevin> tell me if it seems good for you? It needs another doc review. One nit on the code: Kevin> (gdbpy_current_objfile): New global variable. Kevin> * python/python.c (gdbpy_current_objfile): Make global. Now that you aren't setting this in the event generator, there is no need for these hunks. The code bits are ok with that changed. Tom ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [RFC - Python] New ObjFile event 2011-10-05 11:10 ` Kevin Pouget 2011-10-05 14:16 ` Tom Tromey @ 2011-10-05 17:40 ` Eli Zaretskii 2011-10-07 7:40 ` Kevin Pouget 1 sibling, 1 reply; 26+ messages in thread From: Eli Zaretskii @ 2011-10-05 17:40 UTC (permalink / raw) To: Kevin Pouget; +Cc: tromey, gdb-patches > From: Kevin Pouget <kevin.pouget@gmail.com> > Date: Wed, 5 Oct 2011 13:10:18 +0200 > Cc: gdb-patches@sourceware.org > > + ** A new event "gdb.newobjfile" has been added. > + Maybe say also something like "triggered by loading a new object file." > +@item events.new_objfile > +Emits @code{gdb.NewObjFileEvent} which indicates that a new object-file has "object file", without the hyphen. > +been loaded by @value{GDBN}. @code{gdb.NewObjFileEvent} has one attribute: ^^ Two spaces, please. Okay with those changes. Thanks. ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [RFC - Python] New ObjFile event 2011-10-05 17:40 ` Eli Zaretskii @ 2011-10-07 7:40 ` Kevin Pouget 2011-10-07 7:54 ` Kevin Pouget 0 siblings, 1 reply; 26+ messages in thread From: Kevin Pouget @ 2011-10-07 7:40 UTC (permalink / raw) To: Eli Zaretskii, tromey; +Cc: gdb-patches On Wed, Oct 5, 2011 at 7:40 PM, Eli Zaretskii <eliz@gnu.org> wrote: >> From: Kevin Pouget <kevin.pouget@gmail.com> >> Date: Wed, 5 Oct 2011 13:10:18 +0200 >> Cc: gdb-patches@sourceware.org >> >> + ** A new event "gdb.newobjfile" has been added. >> + > > Maybe say also something like "triggered by loading a new object file." > >> +@item events.new_objfile >> +Emits @code{gdb.NewObjFileEvent} which indicates that a new object-file has > > "object file", without the hyphen. > >> +been loaded by @value{GDBN}. @code{gdb.NewObjFileEvent} has one attribute: > ^^ > Two spaces, please. > > Okay with those changes. > > Thanks. > commited with the modifications you asked for: http://sourceware.org/ml/gdb-cvs/2011-10/msg00034.html thanks, Kevin ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [RFC - Python] New ObjFile event 2011-10-07 7:40 ` Kevin Pouget @ 2011-10-07 7:54 ` Kevin Pouget 0 siblings, 0 replies; 26+ messages in thread From: Kevin Pouget @ 2011-10-07 7:54 UTC (permalink / raw) To: Eli Zaretskii, tromey; +Cc: gdb-patches On Fri, Oct 7, 2011 at 9:40 AM, Kevin Pouget <kevin.pouget@gmail.com> wrote: > On Wed, Oct 5, 2011 at 7:40 PM, Eli Zaretskii <eliz@gnu.org> wrote: >>> From: Kevin Pouget <kevin.pouget@gmail.com> >>> Date: Wed, 5 Oct 2011 13:10:18 +0200 >>> Cc: gdb-patches@sourceware.org >>> >>> + ** A new event "gdb.newobjfile" has been added. >>> + >> >> Maybe say also something like "triggered by loading a new object file." >> >>> +@item events.new_objfile >>> +Emits @code{gdb.NewObjFileEvent} which indicates that a new object-file has >> >> "object file", without the hyphen. >> >>> +been loaded by @value{GDBN}. @code{gdb.NewObjFileEvent} has one attribute: >> ^^ >> Two spaces, please. >> >> Okay with those changes. >> >> Thanks. >> > > commited with the modifications you asked for: > http://sourceware.org/ml/gdb-cvs/2011-10/msg00034.html > > thanks, > > Kevin > I've also commited http://sourceware.org/ml/gdb-cvs/2011-10/msg00035.html to fix the mistake I did with ChangeLogs, everything was is gdb/ChangeLog instead of testsuite/ChangeLog and doc/ChangeLog, sorry for that ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [RFC - Python] New ObjFile event @ 2011-10-07 8:37 Andreas Tobler 2011-10-07 8:44 ` Kevin Pouget 0 siblings, 1 reply; 26+ messages in thread From: Andreas Tobler @ 2011-10-07 8:37 UTC (permalink / raw) To: KevinPouget; +Cc: gdb-patches --------- Original Message -------- From: Kevin Pouget <kevin.pouget@gmail.com> To: Eli Zaretskii <eliz@gnu.org>, tromey@redhat.com <tromey@redhat.com> Cc: gdb-patches@sourceware.org Subject: Re: [RFC - Python] New ObjFile event Date: 07/10/11 09:40 > On Wed, Oct 5, 2011 at 7:40 PM, Eli Zaretskii <eliz@gnu.org> wrote: > >> From: Kevin Pouget <kevin.pouget@gmail.com> > >> Date: Wed, 5 Oct 2011 13:10:18 +0200 > >> Cc: gdb-patches@sourceware.org > >> > >> + Â ** A new event "gdb.newobjfile" has been added. > >> + > > > > Maybe say also something like "triggered by loading a new object file." > > > >> +@item events.new_objfile > >> +Emits @code{gdb.NewObjFileEvent} which indicates that a new object-file has > > > > "object file", without the hyphen. > > > >> +been loaded by @value{GDBN}. @code{gdb.NewObjFileEvent} has one attribute: > > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ^^ > > Two spaces, please. > > > > Okay with those changes. > > > > Thanks. > > > > commited with the modifications you asked for: > http://sourceware.org/ml/gdb-cvs/2011-10/msg00034.html python/py-newobjfileevent.c: seems to be missing. Gruss, Andreas ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [RFC - Python] New ObjFile event 2011-10-07 8:37 Andreas Tobler @ 2011-10-07 8:44 ` Kevin Pouget 0 siblings, 0 replies; 26+ messages in thread From: Kevin Pouget @ 2011-10-07 8:44 UTC (permalink / raw) To: Andreas Tobler; +Cc: gdb-patches On Fri, Oct 7, 2011 at 10:36 AM, Andreas Tobler <andreast-list@fgznet.ch> wrote: > --------- Original Message -------- > From: Kevin Pouget <kevin.pouget@gmail.com> > To: Eli Zaretskii <eliz@gnu.org>, tromey@redhat.com <tromey@redhat.com> > Cc: gdb-patches@sourceware.org > Subject: Re: [RFC - Python] New ObjFile event > Date: 07/10/11 09:40 > >> On Wed, Oct 5, 2011 at 7:40 PM, Eli Zaretskii <eliz@gnu.org> wrote: >> >> From: Kevin Pouget <kevin.pouget@gmail.com> >> >> Date: Wed, 5 Oct 2011 13:10:18 +0200 >> >> Cc: gdb-patches@sourceware.org >> >> >> >> + ** A new event "gdb.newobjfile" has been added. >> >> + >> > >> > Maybe say also something like "triggered by loading a new object > file." >> > >> >> +@item events.new_objfile >> >> +Emits @code{gdb.NewObjFileEvent} which indicates that a new > object-file has >> > >> > "object file", without the hyphen. >> > >> >> +been loaded by @value{GDBN}. @code{gdb.NewObjFileEvent} has one > attribute: >> > ^^ >> > Two spaces, please. >> > >> > Okay with those changes. >> > >> > Thanks. >> > >> >> commited with the modifications you asked for: >> http://sourceware.org/ml/gdb-cvs/2011-10/msg00034.html > > python/py-newobjfileevent.c: seems to be missing. > > Gruss, > Andreas just commited, should be okay now -- These two files gdb/python : py-newobjfileevent.c gdb/testsuite/gdb.python: py-events-shlib.c were missing in my commit, I've just fixed it with http://sourceware.org/ml/gdb-cvs/2011-10/msg00036.html ... I'm looking forward to the day when GIT will be the main version manager, I didn't know/notice that CVS don't automatically send new files ... sorry for my newbiness :( ^ permalink raw reply [flat|nested] 26+ messages in thread
end of thread, other threads:[~2011-10-07 8:44 UTC | newest]
Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <AANLkTimybJtpUzy13FuLxQQMJfeWQuf3-cgqGmu23Bq5@mail.gmail.com>
2011-03-28 8:49 ` [RFC - Python] New ObjFile event Kevin Pouget
2011-03-28 8:51 ` Kevin Pouget
2011-03-28 9:00 ` Phil Muldoon
2011-03-28 8:54 ` Phil Muldoon
2011-03-28 9:03 ` Phil Muldoon
2011-03-28 10:02 ` Kevin Pouget
2011-03-28 20:28 ` Tom Tromey
2011-03-29 11:11 ` Kevin Pouget
2011-03-30 11:32 ` Kevin Pouget
2011-03-30 12:14 ` Phil Muldoon
2011-03-30 12:15 ` Kevin Pouget
2011-05-19 20:21 ` Tom Tromey
2011-05-23 8:44 ` Kevin Pouget
2011-09-01 9:56 ` Kevin Pouget
2011-09-01 11:01 ` Eli Zaretskii
2011-09-01 11:09 ` Kevin Pouget
2011-10-03 16:32 ` Tom Tromey
[not found] ` <CAPftXULEe9R4m7tF=vtJe6NTXHSFAkXgsHCPb3r0mU4wKx0FFg@mail.gmail.com>
2011-10-04 8:25 ` Kevin Pouget
2011-10-04 17:02 ` Tom Tromey
2011-10-05 11:10 ` Kevin Pouget
2011-10-05 14:16 ` Tom Tromey
2011-10-05 17:40 ` Eli Zaretskii
2011-10-07 7:40 ` Kevin Pouget
2011-10-07 7:54 ` Kevin Pouget
2011-10-07 8:37 Andreas Tobler
2011-10-07 8:44 ` Kevin Pouget
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox