diff --git a/gdb/testsuite/gdb.server/non-existing-program.exp b/gdb/testsuite/gdb.server/non-existing-program.exp
index eea1eb1d911..99b98e1222c 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 30d94fd7eb6..aa22315ef92 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
