[v2,1/6] gdb.server/non-existing-program.exp: Use gdbserver_start.

Message ID 20240119115659.491195-2-ahajkova@redhat.com
State New
Headers
Series Add vDefaultInferiorFd feature |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed

Commit Message

Alexandra Petlanova Hajkova Jan. 19, 2024, 11:56 a.m. UTC
  Also modify GDBserver_start to match more messages from the server
and return them to the caller.

This test tests if GDBserver exits cleanly when GDBserver is called using
stdio with some program that does not exist. My series modifies the GDBserver
in such a way, it defers starting the inferior till certain packets arrive
when stdio is used. The result was, when GDBserver was called using stdio with
some nonexistent file, it was waiting for those packets to come but since GDB
was never attached, they would never do so and the test timed out. Running
GDBserver using stdio without planning to attach there with GDB is not a
realistic situation and what the user would normally do. The test  was
modifyied to start GDBserver with a port number instead of stdio by calling
GDBserver_start proc which was modifyied to return a message from
GDBserver.
---
 .../gdb.server/non-existing-program.exp       | 54 +++++-----------
 gdb/testsuite/lib/gdbserver-support.exp       | 62 +++++++++++++------
 2 files changed, 58 insertions(+), 58 deletions(-)
  

Patch

diff --git a/gdb/testsuite/gdb.server/non-existing-program.exp b/gdb/testsuite/gdb.server/non-existing-program.exp
index 2d08e500def..751a55822b6 100644
--- a/gdb/testsuite/gdb.server/non-existing-program.exp
+++ b/gdb/testsuite/gdb.server/non-existing-program.exp
@@ -30,44 +30,20 @@  if { $gdbserver == "" } {
     return
 }
 
-# Fire off gdbserver.  The port doesn't really matter, gdbserver tries
-# to spawn the program before opening the connection.
-set spawn_id [remote_spawn target "$gdbserver stdio non-existing-program"]
-
-set msg "gdbserver exits cleanly"
-set saw_exiting 0
-expect {
-    # This is what we get on ptrace-based targets with
-    # startup-with-shell disabled (e.g., when the SHELL variable is
-    # unset).
-    -re "stdin/stdout redirected.*gdbserver: Cannot exec non-existing-program\r\ngdbserver: Error: No such file or directory\r\n\r\nDuring startup program exited with code 127\.\r\nExiting\r\n" {
-	set saw_exiting 1
-	exp_continue
-    }
-    # Likewise, but with startup-with-shell enabled, which is the
-    # default behaviour.
-    -re "stdin/stdout redirected.*exec: non-existing-program: not found\r\nDuring startup program exited with code 127\.\r\nExiting\r\n" {
-	set saw_exiting 1
-	exp_continue
-    }
-    # This is what we get on Windows.
-    -re "Error creating process\r\n\r\nExiting\r\n" {
-	set saw_exiting 1
-	exp_continue
-    }
-    -re "A problem internal to GDBserver has been detected" {
-	fail "$msg (GDBserver internal error)"
-	wait
-    }
-    eof {
-	gdb_assert $saw_exiting $msg
-	wait
+foreach_with_prefix start_with_shell { on off } {
+    if { $start_with_shell } {
+	set arg "--startup-with-shell"
+    } else {
+	set arg "--no-startup-with-shell"
     }
-    timeout {
-	fail "$msg (timeout)"
-    }
-}
 
-# expect defaults to spawn_id in many places.  Avoid confusing any
-# following code.
-unset spawn_id
+    set msg "GDBserver exits cleanly"
+    set res [gdbserver_start $arg non-existent-file]
+    set status [lindex $res 2]
+
+    # We expect GDBserver to fail with the message indicating executable
+    # does not exist.
+    gdb_assert { $status == "During startup program exited with code 127\.\r\nExiting\r\n"
+      || $status == "Error creating process\r\n\r\nExiting\r\n"} $msg
+
+}
diff --git a/gdb/testsuite/lib/gdbserver-support.exp b/gdb/testsuite/lib/gdbserver-support.exp
index 8bcf4fbbb01..92afd6a4642 100644
--- a/gdb/testsuite/lib/gdbserver-support.exp
+++ b/gdb/testsuite/lib/gdbserver-support.exp
@@ -244,7 +244,8 @@  proc gdbserver_default_get_comm_port { port } {
 # Start a gdbserver process with initial OPTIONS and trailing ARGUMENTS.
 # The port will be filled in between them automatically.
 #
-# Returns the target protocol and socket to connect to.
+# Returns the target protocol, socket to connect to and the status
+# message from the gdbserver.
 
 proc gdbserver_start { options arguments } {
     global portnum
@@ -363,33 +364,56 @@  proc gdbserver_start { options arguments } {
 	# talk to the program using GDBserver's tty instead.
 	global inferior_spawn_id
 	set inferior_spawn_id $server_spawn_id
+        set msg 0
 
 	# Wait for the server to open its TCP socket, so that GDB can connect.
 	expect {
-	    -i $server_spawn_id
-	    -timeout 120
-	    -notransfer
-	    -re "Listening on" { }
-	    -re "Can't (bind address|listen on socket): Address already in use\\.\r\n" {
-		verbose -log "Port $portnum is already in use."
-		if ![target_info exists gdb,socketport] {
-		    # Bump the port number to avoid the conflict.
-		    wait -i $expect_out(spawn_id)
-		    incr portnum
-		    continue
-		}
-	    }
-	    -re ".*: cannot resolve name: .*\r\n" {
-		error "gdbserver cannot resolve name."
-	    }
-	    timeout {
+          -i $server_spawn_id
+          -timeout 120
+          -notransfer
+          -re "Listening on" {
+            set msg $expect_out(0,string)
+	  }
+	  -re "Can't (bind address|listen on socket): Address already in use\\.\r\n" {
+	    verbose -log "Port $portnum is already in use."
+	    set msg "Port is already in use"
+            if ![target_info exists gdb,socketport] {
+              # Bump the port number to avoid the conflict.
+              wait -i $expect_out(spawn_id)
+              incr portnum
+              continue
+            }
+          }
+          -re ".*: cannot resolve name: .*\r\n" {
+            error "gdbserver cannot resolve name."
+          }
+          # Likewise, but with startup-with-shell enabled, which is the
+          # default behaviour.
+          -re "During startup program exited with code 127\.\r\nExiting\r\n" {
+            set msg $expect_out(0,string)
+            exp_continue
+          }
+          -re "Can't bind address: Address already in use\.\r\nExiting\r\n" {
+            set msg $expect_out(0,string)
+            exp_continue
+          }
+          # This is what we get on Windows.
+          -re "Error creating process\r\n\r\nExiting\r\n" {
+            set msg $expect_out(0,string)
+            exp_continue
+          }
+          -re "A problem internal to GDBserver has been detected" {
+            set msg $expect_out(0,string)
+            wait
+          }
+          timeout {
 		error "Timeout waiting for gdbserver response."
 	    }
 	}
 	break
     }
 
-    return [list $protocol [$get_remote_address $debughost $portnum]]
+    return [list $protocol [$get_remote_address $debughost $portnum] $msg]
 }
 
 # Start a gdbserver process running SERVER_EXEC, and connect GDB