[PATCH] PR/12691 Add the inferior to Python exited event

Kevin Pouget
2011-04-21T09:19:25Z

Following Tom's advise
(http://sourceware.org/ml/gdb/2011-04/msg00116.html), here is a patch
which hooks the inferior to the `exited' event object
let me know what you think about it

Cordially,

Kevin

2011-04-21 Kevin Pouget <kevin.pouget@st.com>

PR python/12691: Add the inferior to Python exited event
* python/py-exitedevent.c (create_exited_event_object): Add inferior
to exited_event.
* python/py-event.h (emit_exited_event): Likewise
* python/-inferior.c (python_inferior_exit): Likewise

2011-04-21 Kevin Pouget <kevin.pouget@st.com>

PR python/12691: Add the inferior to Python exited event
* gdb.python/py-events.exp: Test the inferior attribute of exited
event with a fork.
* gdb.python/py-events.py: Print inferior number on exit.
* gdb.python/py-events.c: Fork the inferior.

2011-04-21 Kevin Pouget <kevin.pouget@st.com>

PR python/12691: Add the inferior to Python exited event
* gdb.texinfo (Events In Python): Describe exited inferior attribute.

--

From 181089a16134e1c6a900e42bb542adb165158ec2 Mon Sep 17 00:00:00 2001
From: Kevin Pouget <kevin.pouget@st.com>
Date: Thu, 21 Apr 2011 10:27:03 -0400
Subject: [PATCH] Add the inferior to Python exited event

---
gdb/doc/gdb.texinfo | 5 ++++-
gdb/python/py-event.h | 2 +-
gdb/python/py-exitedevent.c | 10 +++++++---
gdb/python/py-inferior.c | 2 +-
gdb/testsuite/gdb.python/py-events.c | 1 +
gdb/testsuite/gdb.python/py-events.exp | 11 ++++++++++-
gdb/testsuite/gdb.python/py-events.py | 1 +
7 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index edcf5c2..6e06bde 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -22056,11 +22056,14 @@ inherited attribute refer to
@code{gdb.ThreadEvent} above.

@item events.exited
Emits @code{events.ExitedEvent} which indicates that the inferior has exited.
-@code{events.ExitedEvent} has one attribute:
+@code{events.ExitedEvent} has two attributes:
@table @code
@defivar ExitedEvent exit_code
An integer representing the exit code which the inferior has returned.
@end defivar
+@defivar ExitedEvent inferior
+A reference to the inferior which triggered the exited event.
+@end defivar
@end table

@item events.stop
diff --git a/gdb/python/py-event.h b/gdb/python/py-event.h
index bc95521..5ac73b4 100644
--- a/gdb/python/py-event.h
+++ b/gdb/python/py-event.h
@@ -104,7 +104,7 @@ typedef struct
} event_object;

extern int emit_continue_event (ptid_t ptid);
-extern int emit_exited_event (LONGEST exit_code);
+extern int emit_exited_event (LONGEST exit_code, struct inferior *inf);

extern int evpy_emit_event (PyObject *event,
eventregistry_object *registry);
diff --git a/gdb/python/py-exitedevent.c b/gdb/python/py-exitedevent.c
index 457a4fe..7d2a512 100644
--- a/gdb/python/py-exitedevent.c
+++ b/gdb/python/py-exitedevent.c
@@ -22,7 +22,7 @@
static PyTypeObject exited_event_object_type;

PyObject *
-create_exited_event_object (LONGEST exit_code)
+create_exited_event_object (LONGEST exit_code, struct inferior *inf)
{
PyObject *exited_event;

@@ -35,6 +35,10 @@ create_exited_event_object (LONGEST exit_code)
"exit_code",
PyLong_FromLongLong (exit_code)) < 0)
goto fail;
+ if (evpy_add_attribute (exited_event,
+ "inferior",
+ inferior_to_inferior_object (inf)) < 0)
+ goto fail;

return exited_event;

@@ -47,14 +51,14 @@ create_exited_event_object (LONGEST exit_code)
will create a new Python exited event object. */

int
-emit_exited_event (LONGEST exit_code)
+emit_exited_event (LONGEST exit_code, struct inferior *inf)
{
PyObject *event;

if (evregpy_no_listeners_p (gdb_py_events.exited))
return 0;

- event = create_exited_event_object (exit_code);
+ event = create_exited_event_object (exit_code, inf);

if (event)
return evpy_emit_event (event, gdb_py_events.exited);
diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c
index b9df394..19d45d7 100644
--- a/gdb/python/py-inferior.c
+++ b/gdb/python/py-inferior.c
@@ -123,7 +123,7 @@ python_inferior_exit (struct inferior *inf)
exit_code = status.value.integer;

if (exit_code >= 0
- && emit_exited_event (exit_code) < 0)
+ && emit_exited_event (exit_code, inf) < 0)
gdbpy_print_stack ();

do_cleanups (cleanup);
diff --git a/gdb/testsuite/gdb.python/py-events.c
b/gdb/testsuite/gdb.python/py-events.c
index ceb697e..665ca51 100644
--- a/gdb/testsuite/gdb.python/py-events.c
+++ b/gdb/testsuite/gdb.python/py-events.c
@@ -17,6 +17,7 @@
*/

int second(){
+ fork() ;
return 12;
}

diff --git a/gdb/testsuite/gdb.python/py-events.exp
b/gdb/testsuite/gdb.python/py-events.exp
index e5d6daf..1dd7e9f 100644
--- a/gdb/testsuite/gdb.python/py-events.exp
+++ b/gdb/testsuite/gdb.python/py-events.exp
@@ -42,6 +42,8 @@ if ![runto_main ] then {
return -1
}

+gdb_test_no_output "set detach-on-fork off" ""
+
gdb_test "Test_Events" "Event testers registered."

gdb_breakpoint "first"
@@ -56,4 +58,11 @@ all threads stopped"
#test exited event.
gdb_test "continue" ".*event type: continue.*
.*event type: exit.*
-.*exit code: 12.*"
+.*exit code: 12.*
+.*exit inf: 1.*"
+
+gdb_test "inferior 2" ".*Switching to inferior 2.*"
+gdb_test "continue" ".*event type: continue.*
+.*event type: exit.*
+.*exit code: 12.*
+.*exit inf: 2.*"
diff --git a/gdb/testsuite/gdb.python/py-events.py
b/gdb/testsuite/gdb.python/py-events.py
index 9f05b9f..b40f074 100644
--- a/gdb/testsuite/gdb.python/py-events.py
+++ b/gdb/testsuite/gdb.python/py-events.py
@@ -41,6 +41,7 @@ def exit_handler (event):
if (isinstance (event, gdb.ExitedEvent)):
print "event type: exit"
print "exit code: %d" % (event.exit_code)
+ print "exit inf: %d" % (event.inferior.num)

def continue_handler (event):
if (isinstance (event, gdb.ContinueEvent)):
--
1.7.4.4