[1/2] gdb/python: update events test to handle missing exit_code

Message ID 8cf30444ed3de7be0362df05ba2ae20c79379995.1631108877.git.andrew.burgess@embecosm.com
State New
Headers show
Series
  • Adding a Python event for when GDB exits
Related show

Commit Message

Andrew Burgess Sept. 8, 2021, 1:56 p.m.
The test gdb.python/py-events.exp sets up a handler for the gdb.exited
event.  Unfortunately the handler is slightly broken, it assumes that
the exit_code attribute will always be present.  This is not always
the case.

In a later commit I am going to add more tests to py-events.exp test
script, and in so doing I expose the bug in our handling of gdb.exited
events.

Just to be clear, GDB itself is fine, it is the test that is not
written correctly according to the Python Events API.

So, in this commit I fix the Python code in the test, and extend the
test case to exercise more paths through the Python code.

Additionally, I noticed that the gdb.exited event is used as an
example in the documentation for how to write an event handler.
Unfortunately the same bug that we had in our test was also present in
the example code in the manual.

So I've fixed that too.

After this commit there is no functional change to GDB.
---
 gdb/doc/python.texi                    |  5 ++++-
 gdb/testsuite/gdb.python/py-events.exp | 20 ++++++++++++++++++++
 gdb/testsuite/gdb.python/py-events.py  |  5 ++++-
 3 files changed, 28 insertions(+), 2 deletions(-)

-- 
2.25.4

Comments

Tom de Vries via Gdb-patches Sept. 8, 2021, 2:07 p.m. | #1
> From: Andrew Burgess <andrew.burgess@embecosm.com>

> Date: Wed,  8 Sep 2021 14:56:39 +0100

> 

> diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi

> index d8f682a091c..b123b240980 100644

> --- a/gdb/doc/python.texi

> +++ b/gdb/doc/python.texi

> @@ -3202,7 +3202,10 @@

>  @smallexample

>  def exit_handler (event):

>      print ("event type: exit")

> -    print ("exit code: %d" % (event.exit_code))

> +    if hasattr (event, 'exit_code'):

> +        print ("exit code: %d" % (event.exit_code))

> +    else:

> +        print ("exit code not available")

>  

>  gdb.events.exited.connect (exit_handler)

>  @end smallexample


This part is OK, thanks.

Patch

diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index d8f682a091c..b123b240980 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -3202,7 +3202,10 @@ 
 @smallexample
 def exit_handler (event):
     print ("event type: exit")
-    print ("exit code: %d" % (event.exit_code))
+    if hasattr (event, 'exit_code'):
+        print ("exit code: %d" % (event.exit_code))
+    else:
+        print ("exit code not available")
 
 gdb.events.exited.connect (exit_handler)
 @end smallexample
diff --git a/gdb/testsuite/gdb.python/py-events.exp b/gdb/testsuite/gdb.python/py-events.exp
index 753709361f5..310258618c3 100644
--- a/gdb/testsuite/gdb.python/py-events.exp
+++ b/gdb/testsuite/gdb.python/py-events.exp
@@ -276,3 +276,23 @@  with_test_prefix "inferior continue exit" {
     gdb_test "continue" "exited with code.*" "continue to exit"
     gdb_test "print \$_foo" "= 2" "check foo after start continue"
 }
+
+# Check that when GDB exits, we see gdb.ExitedEvent objects with no
+# 'exit_code' attribute.
+with_test_prefix "gdb exiting" {
+    set saw_inferior_exit 0
+    gdb_test_multiple "quit" "" {
+	-re "Quit anyway\\? \\(y or n\\) $" {
+	    send_gdb "y\n"
+	    exp_continue
+	}
+	-re "event type: exit\r\nexit code: not-present\r\nexit inf: $decimal\r\nexit pid: $decimal\r\ndir ok: False\r\n" {
+	    incr saw_inferior_exit
+	    exp_continue
+	}
+	eof {
+	    gdb_assert { $saw_inferior_exit == 2 }
+	    pass $gdb_test_name
+	}
+    }
+}
diff --git a/gdb/testsuite/gdb.python/py-events.py b/gdb/testsuite/gdb.python/py-events.py
index 1524267117d..b21f562bfd3 100644
--- a/gdb/testsuite/gdb.python/py-events.py
+++ b/gdb/testsuite/gdb.python/py-events.py
@@ -45,7 +45,10 @@  def breakpoint_stop_handler(event):
 def exit_handler(event):
     assert isinstance(event, gdb.ExitedEvent)
     print("event type: exit")
-    print("exit code: %d" % (event.exit_code))
+    if hasattr(event, 'exit_code'):
+        print("exit code: %d" % (event.exit_code))
+    else:
+        print("exit code: not-present")
     print("exit inf: %d" % (event.inferior.num))
     print("exit pid: %d" % (event.inferior.pid))
     print("dir ok: %s" % str("exit_code" in dir(event)))