• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

GNU Binutils with patches for OS216


Commit MetaInfo

Revisión7c96f8c1dae023c7d0b1cabc5e50c4d18fd06960 (tree)
Tiempo2017-09-12 05:15:20
AutorTom Tromey <tom@trom...>
CommiterTom Tromey

Log Message

Add new_inferior, inferior_deleted, and new_thread events

This adds a few new events to gdb's Python layer: new_inferior,
inferior_deleted, and new_thread. I wanted to be able to add a
combined inferior/thread display window to my GUI, and I needed a few
events to make this work. This is PR python/15622.

ChangeLog
2017-09-11 Tom Tromey <tom@tromey.com>

PR python/15622:
* NEWS: Add entry.
* python/python.c (do_start_initialization): Initialize new event
types.
* python/python-internal.h (gdbpy_initialize_new_inferior_event)
(gdbpy_initialize_inferior_deleted_event)
(gdbpy_initialize_new_thread_event): Declare.
* python/py-threadevent.c (create_thread_event_object): Add option
"thread" parameter.
* python/py-inferior.c (new_thread_event_object_type)
(new_inferior_event_object_type)
(inferior_deleted_event_object_type): Declare.
(python_new_inferior, python_inferior_deleted): New functions.
(add_thread_object): Emit new_thread event.
(gdbpy_initialize_inferior): Attach new functions to corresponding
observers.
(new_thread, new_inferior, inferior_deleted): Define new event
types.
* python/py-evts.c (gdbpy_initialize_py_events): Add new
registries.
* python/py-events.h (events_object) <new_inferior,
inferior_deleted, new_thread>: New fields.
* python/py-event.h (create_thread_event_breakpoint): Add optional
"thread" parameter.

doc/ChangeLog
2017-09-11 Tom Tromey <tom@tromey.com>

* python.texi (Events In Python): Document new events.

testsuite/ChangeLog
2017-09-11 Tom Tromey <tom@tromey.com>

* gdb.python/py-infthread.exp: Add tests for new_thread event.
* gdb.python/py-inferior.exp: Add tests for new inferior events.

Cambiar Resumen

Diferencia incremental

--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,30 @@
1+2017-09-11 Tom Tromey <tom@tromey.com>
2+
3+ PR python/15622:
4+ * NEWS: Add entry.
5+ * python/python.c (do_start_initialization): Initialize new event
6+ types.
7+ * python/python-internal.h (gdbpy_initialize_new_inferior_event)
8+ (gdbpy_initialize_inferior_deleted_event)
9+ (gdbpy_initialize_new_thread_event): Declare.
10+ * python/py-threadevent.c (create_thread_event_object): Add option
11+ "thread" parameter.
12+ * python/py-inferior.c (new_thread_event_object_type)
13+ (new_inferior_event_object_type)
14+ (inferior_deleted_event_object_type): Declare.
15+ (python_new_inferior, python_inferior_deleted): New functions.
16+ (add_thread_object): Emit new_thread event.
17+ (gdbpy_initialize_inferior): Attach new functions to corresponding
18+ observers.
19+ (new_thread, new_inferior, inferior_deleted): Define new event
20+ types.
21+ * python/py-evts.c (gdbpy_initialize_py_events): Add new
22+ registries.
23+ * python/py-events.h (events_object) <new_inferior,
24+ inferior_deleted, new_thread>: New fields.
25+ * python/py-event.h (create_thread_event_breakpoint): Add optional
26+ "thread" parameter.
27+
128 2017-09-10 Andrew Burgess <andrew.burgess@embecosm.com>
229
330 * utils.c (abort_with_message): Don't compare gdb_stderr to NULL,
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -15,6 +15,12 @@
1515 the remote inferior is started by the GDBserver, use the "unset
1616 environment" command.
1717
18+* Python Scripting
19+
20+ ** New events gdb.new_inferior, gdb.inferior_deleted, and
21+ gdb.new_thread are emitted. See the manual for further
22+ description of these.
23+
1824 * New features in the GDB remote stub, GDBserver
1925
2026 ** New "--selftest" command line option runs some GDBserver self
--- a/gdb/doc/ChangeLog
+++ b/gdb/doc/ChangeLog
@@ -1,3 +1,7 @@
1+2017-09-11 Tom Tromey <tom@tromey.com>
2+
3+ * python.texi (Events In Python): Document new events.
4+
15 2017-09-04 Pedro Alves <palves@redhat.com>
26
37 * gdb.texinfo (Variables) <Program Variables>: Document inspecting
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -2989,6 +2989,39 @@ invalid state; that is, the @code{is_valid} method will return
29892989 This event carries no payload. It is emitted each time @value{GDBN}
29902990 presents a prompt to the user.
29912991
2992+@item events.new_inferior
2993+This is emitted when a new inferior is created. Note that the
2994+inferior is not necessarily running; in fact, it may not even have an
2995+associated executable.
2996+
2997+The event is of type @code{gdb.NewInferiorEvent}. This has a single
2998+attribute:
2999+
3000+@defvar NewInferiorEvent.inferior
3001+The new inferior, a @code{gdb.Inferior} object.
3002+@end defvar
3003+
3004+@item events.inferior_deleted
3005+This is emitted when an inferior has been deleted. Note that this is
3006+not the same as process exit; it is notified when the inferior itself
3007+is removed, say via @code{remove-inferiors}.
3008+
3009+The event is of type @code{gdb.InferiorDeletedEvent}. This has a single
3010+attribute:
3011+
3012+@defvar NewInferiorEvent.inferior
3013+The inferior that is being removed, a @code{gdb.Inferior} object.
3014+@end defvar
3015+
3016+@item events.new_thread
3017+This is emitted when @value{GDBN} notices a new thread. The event is of
3018+type @code{gdb.NewThreadEvent}, which extends @code{gdb.ThreadEvent}.
3019+This has a single attribute:
3020+
3021+@defvar NewThreadEvent.inferior_thread
3022+The new thread.
3023+@end defvar
3024+
29923025 @end table
29933026
29943027 @node Threads In Python
--- a/gdb/python/py-event.h
+++ b/gdb/python/py-event.h
@@ -125,7 +125,8 @@ extern int evpy_emit_event (PyObject *event,
125125 eventregistry_object *registry);
126126
127127 extern PyObject *create_event_object (PyTypeObject *py_type);
128-extern PyObject *create_thread_event_object (PyTypeObject *py_type);
128+extern PyObject *create_thread_event_object (PyTypeObject *py_type,
129+ PyObject *thread = nullptr);
129130 extern int emit_new_objfile_event (struct objfile *objfile);
130131 extern int emit_clear_objfiles_event (void);
131132
--- a/gdb/python/py-events.h
+++ b/gdb/python/py-events.h
@@ -47,6 +47,9 @@ typedef struct
4747 eventregistry_object *exited;
4848 eventregistry_object *new_objfile;
4949 eventregistry_object *clear_objfiles;
50+ eventregistry_object *new_inferior;
51+ eventregistry_object *inferior_deleted;
52+ eventregistry_object *new_thread;
5053 eventregistry_object *inferior_call;
5154 eventregistry_object *memory_changed;
5255 eventregistry_object *register_changed;
--- a/gdb/python/py-evts.c
+++ b/gdb/python/py-evts.c
@@ -89,6 +89,15 @@ gdbpy_initialize_py_events (void)
8989 if (add_new_registry (&gdb_py_events.clear_objfiles, "clear_objfiles") < 0)
9090 return -1;
9191
92+ if (add_new_registry (&gdb_py_events.new_inferior, "new_inferior") < 0)
93+ return -1;
94+
95+ if (add_new_registry (&gdb_py_events.inferior_deleted, "inferior_deleted") < 0)
96+ return -1;
97+
98+ if (add_new_registry (&gdb_py_events.new_thread, "new_thread") < 0)
99+ return -1;
100+
92101 if (add_new_registry (&gdb_py_events.breakpoint_created,
93102 "breakpoint_created") < 0)
94103 return -1;
--- a/gdb/python/py-inferior.c
+++ b/gdb/python/py-inferior.c
@@ -30,6 +30,13 @@
3030 #include "py-event.h"
3131 #include "py-stopevent.h"
3232
33+extern PyTypeObject new_thread_event_object_type
34+ CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("event_object");
35+extern PyTypeObject new_inferior_event_object_type
36+ CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("event_object");
37+extern PyTypeObject inferior_deleted_event_object_type
38+ CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("event_object");
39+
3340 struct threadlist_entry {
3441 thread_object *thread_obj;
3542 struct threadlist_entry *next;
@@ -235,6 +242,60 @@ inferior_to_inferior_object (struct inferior *inferior)
235242 return (PyObject *) inf_obj;
236243 }
237244
245+/* Called when a new inferior is created. Notifies any Python event
246+ listeners. */
247+static void
248+python_new_inferior (struct inferior *inf)
249+{
250+ if (!gdb_python_initialized)
251+ return;
252+
253+ gdbpy_enter enter_py (python_gdbarch, python_language);
254+
255+ if (evregpy_no_listeners_p (gdb_py_events.new_inferior))
256+ return;
257+
258+ gdbpy_ref<> inf_obj (inferior_to_inferior_object (inf));
259+ if (inf_obj == NULL)
260+ {
261+ gdbpy_print_stack ();
262+ return;
263+ }
264+
265+ gdbpy_ref<> event (create_event_object (&new_inferior_event_object_type));
266+ if (event == NULL
267+ || evpy_add_attribute (event.get (), "inferior", inf_obj.get ()) < 0
268+ || evpy_emit_event (event.get (), gdb_py_events.new_inferior) < 0)
269+ gdbpy_print_stack ();
270+}
271+
272+/* Called when an inferior is removed. Notifies any Python event
273+ listeners. */
274+static void
275+python_inferior_deleted (struct inferior *inf)
276+{
277+ if (!gdb_python_initialized)
278+ return;
279+
280+ gdbpy_enter enter_py (python_gdbarch, python_language);
281+
282+ if (evregpy_no_listeners_p (gdb_py_events.inferior_deleted))
283+ return;
284+
285+ gdbpy_ref<> inf_obj (inferior_to_inferior_object (inf));
286+ if (inf_obj == NULL)
287+ {
288+ gdbpy_print_stack ();
289+ return;
290+ }
291+
292+ gdbpy_ref<> event (create_event_object (&inferior_deleted_event_object_type));
293+ if (event == NULL
294+ || evpy_add_attribute (event.get (), "inferior", inf_obj.get ()) < 0
295+ || evpy_emit_event (event.get (), gdb_py_events.inferior_deleted) < 0)
296+ gdbpy_print_stack ();
297+}
298+
238299 /* Finds the Python Inferior object for the given PID. Returns a
239300 reference, or NULL if PID does not match any inferior object. */
240301
@@ -298,6 +359,15 @@ add_thread_object (struct thread_info *tp)
298359
299360 inf_obj->threads = entry;
300361 inf_obj->nthreads++;
362+
363+ if (evregpy_no_listeners_p (gdb_py_events.new_thread))
364+ return;
365+
366+ gdbpy_ref<> event (create_thread_event_object (&new_thread_event_object_type,
367+ (PyObject *) thread_obj));
368+ if (event == NULL
369+ || evpy_emit_event (event.get (), gdb_py_events.new_thread) < 0)
370+ gdbpy_print_stack ();
301371 }
302372
303373 static void
@@ -823,6 +893,8 @@ gdbpy_initialize_inferior (void)
823893 observer_attach_register_changed (python_on_register_change);
824894 observer_attach_inferior_exit (python_inferior_exit);
825895 observer_attach_new_objfile (python_new_objfile);
896+ observer_attach_inferior_added (python_new_inferior);
897+ observer_attach_inferior_removed (python_inferior_deleted);
826898
827899 membuf_object_type.tp_new = PyType_GenericNew;
828900 if (PyType_Ready (&membuf_object_type) < 0)
@@ -970,3 +1042,19 @@ PyTypeObject membuf_object_type = {
9701042 0, /* tp_init */
9711043 0, /* tp_alloc */
9721044 };
1045+
1046+GDBPY_NEW_EVENT_TYPE (new_thread,
1047+ "gdb.NewThreadEvent",
1048+ "NewThreadEvent",
1049+ "GDB new thread event object",
1050+ thread_event_object_type);
1051+GDBPY_NEW_EVENT_TYPE (new_inferior,
1052+ "gdb.NewInferiorEvent",
1053+ "NewInferiorEvent",
1054+ "GDB new inferior event object",
1055+ event_object_type);
1056+GDBPY_NEW_EVENT_TYPE (inferior_deleted,
1057+ "gdb.InferiorDeletedEvent",
1058+ "InferiorDeletedEvent",
1059+ "GDB inferior deleted event object",
1060+ event_object_type);
--- a/gdb/python/py-threadevent.c
+++ b/gdb/python/py-threadevent.c
@@ -48,17 +48,18 @@ get_event_thread (void)
4848 }
4949
5050 PyObject *
51-create_thread_event_object (PyTypeObject *py_type)
51+create_thread_event_object (PyTypeObject *py_type, PyObject *thread)
5252 {
53- PyObject *thread = NULL;
54-
5553 gdbpy_ref<> thread_event_obj (create_event_object (py_type));
5654 if (thread_event_obj == NULL)
5755 return NULL;
5856
59- thread = get_event_thread ();
60- if (!thread)
61- return NULL;
57+ if (thread == NULL)
58+ {
59+ thread = get_event_thread ();
60+ if (!thread)
61+ return NULL;
62+ }
6263
6364 if (evpy_add_attribute (thread_event_obj.get (),
6465 "inferior_thread",
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -630,6 +630,12 @@ int gdbpy_initialize_new_objfile_event (void)
630630 CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
631631 int gdbpy_initialize_clear_objfiles_event (void)
632632 CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
633+int gdbpy_initialize_new_inferior_event (void)
634+ CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
635+int gdbpy_initialize_inferior_deleted_event (void)
636+ CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
637+int gdbpy_initialize_new_thread_event (void)
638+ CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
633639 int gdbpy_initialize_arch (void)
634640 CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
635641 int gdbpy_initialize_xmethods (void)
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1603,6 +1603,9 @@ do_start_initialization ()
16031603 || gdbpy_initialize_thread_event () < 0
16041604 || gdbpy_initialize_new_objfile_event () < 0
16051605 || gdbpy_initialize_clear_objfiles_event () < 0
1606+ || gdbpy_initialize_new_inferior_event () < 0
1607+ || gdbpy_initialize_inferior_deleted_event () < 0
1608+ || gdbpy_initialize_new_thread_event () < 0
16061609 || gdbpy_initialize_arch () < 0
16071610 || gdbpy_initialize_xmethods () < 0
16081611 || gdbpy_initialize_unwind () < 0)
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,8 @@
1+2017-09-11 Tom Tromey <tom@tromey.com>
2+
3+ * gdb.python/py-infthread.exp: Add tests for new_thread event.
4+ * gdb.python/py-inferior.exp: Add tests for new inferior events.
5+
16 2017-09-08 Christoph Weinmann <christoph.t.weinmann@intel.com>
27
38 * gdb.fortran/printing-types.exp: New file.
--- a/gdb/testsuite/gdb.python/py-inferior.exp
+++ b/gdb/testsuite/gdb.python/py-inferior.exp
@@ -214,12 +214,33 @@ with_test_prefix "is_valid" {
214214 gdb_test "python print (inf_list\[0\].is_valid())" "True" \
215215 "check inferior validity 1"
216216
217+ gdb_py_test_multiple "install new inferior event handler" \
218+ "python" "" \
219+ "my_inferior_count = 1" "" \
220+ "def new_inf_handler(evt):" "" \
221+ " global my_inferior_count" "" \
222+ " if evt.inferior is not None:" "" \
223+ " my_inferior_count = my_inferior_count + 1" "" \
224+ "gdb.events.new_inferior.connect(new_inf_handler)" "" \
225+ "end" ""
226+ gdb_py_test_multiple "install inferior deleted event handler" \
227+ "python" "" \
228+ "def del_inf_handler(evt):" "" \
229+ " global my_inferior_count" "" \
230+ " if evt.inferior is not None:" "" \
231+ " my_inferior_count = my_inferior_count - 1" "" \
232+ "gdb.events.inferior_deleted.connect(del_inf_handler)" "" \
233+ "end" ""
234+
217235 gdb_test "add-inferior" "Added inferior 2.*" "add empty inferior 2"
218236 gdb_py_test_silent_cmd "python inf_list = gdb.inferiors()" "get new list" 1
219237 gdb_test "python print (len(inf_list))" "2" "get inferior list length 2"
220238 gdb_test "python print (inf_list\[0\].is_valid())" "True" \
221239 "check inferior validity 2"
222240
241+ gdb_test "python print (my_inferior_count)" "2" \
242+ "test new-inferior event handler"
243+
223244 gdb_test "python print (inf_list\[1\].is_valid())" "True" \
224245 "check inferior validity 3"
225246
@@ -229,6 +250,9 @@ with_test_prefix "is_valid" {
229250
230251 gdb_test "python print (inf_list\[1\].is_valid())" "False" \
231252 "check inferior validity 5"
253+
254+ gdb_test "python print (my_inferior_count)" "1" \
255+ "test inferior-deleted event handler"
232256 }
233257
234258 # Test gdb.selected_inferior()
--- a/gdb/testsuite/gdb.python/py-infthread.exp
+++ b/gdb/testsuite/gdb.python/py-infthread.exp
@@ -30,6 +30,16 @@ clean_restart ${testfile}
3030 # Skip all tests if Python scripting is not enabled.
3131 if { [skip_python_tests] } { continue }
3232
33+gdb_py_test_multiple "install new_thread event handler" \
34+ "python" "" \
35+ "seen_a_thread = False" "" \
36+ "def thread_handler(evt):" "" \
37+ " if evt.inferior_thread is not None:" "" \
38+ " global seen_a_thread" "" \
39+ " seen_a_thread = True" "" \
40+ "gdb.events.new_thread.connect(thread_handler)" "" \
41+ "end" ""
42+
3343 # The following tests require execution.
3444
3545 if ![runto_main] then {
@@ -37,6 +47,8 @@ if ![runto_main] then {
3747 return 0
3848 }
3949
50+gdb_test "python print(seen_a_thread)" "True"
51+
4052 # Test basic gdb.Inferior attributes and methods.
4153
4254 gdb_py_test_silent_cmd "python t0 = gdb.selected_thread ()" "test gdb.selected_thread" 1