diff --git a/gdb/progspace-and-thread.c b/gdb/progspace-and-thread.c
index 3c92b5c8e0..f66aabea40 100644
--- a/gdb/progspace-and-thread.c
+++ b/gdb/progspace-and-thread.c
@@ -39,6 +39,5 @@ switch_to_program_space_and_thread (program_space *pspace)
        }
     }
 
-  switch_to_no_thread ();
-  set_current_program_space (pspace);
+  switch_to_inferior_no_thread (inf);
 }

This fixes the case when an inferior has PID = 0.
The problem is that in the current state GDB would switch to no_thread and also set the program space
but because the inferior is not switched, potentially an incorrect target would remain.

Here is a sample scenario that exploits this flow:

# On terminal 1, start a gdbserver on a program named foo:
$ gdbserver :1234 ./foo

# On terminal 2, start gdb on a program named bar. Suppose foo and bar are compiled from foo.c and bar.c.
They are completely separate. So, bar.c:2 has no meaning for foo.

$ gdb -q ./bar
Reading symbols from ./bar...
(gdb) add-inferior
[New inferior 2]
Added inferior 2
(gdb) inferior 2
[Switching to inferior 2 [<null>] (<noexec>)]
(gdb) target remote :1234
...
(gdb) set debug remote 2
(gdb) break bar.c:2
Sending packet: $Hgp0.0#ad...Packet received: OK
Sending packet: $m5fa,12#f8...Packet received: E01
Sending packet: $m5fa,1#c6...Packet received: E01
Sending packet: $m5fb,3#c9...Packet received: E01
Sending packet: $m5fe,1#ca...Packet received: E01
Breakpoint 1 at 0x5fe: file bar.c, line 2.
(gdb)

Here we have an unnecessary sending of the packets to the gdbserver.
But with the proposed change in progspace-and-thread.c there is this

(gdb) break bar.c:2
Breakpoint 1 at 0x5fe: file bar.c, line 2.
(gdb) 

Now there is no sending of the packets to the gdbserver.

-----Original Message-----
From: gdb-patches-owner@sourceware.org <gdb-patches-owner@sourceware.org> On Behalf Of Pedro Alves
Sent: Friday, October 18, 2019 12:50 AM
To: gdb-patches@sourceware.org
Subject: [PATCH v2 08/24] Introduce switch_to_inferior_no_thread

Several places want to switch context to an inferior and its pspace, while at the same time switch to "no thread selected".  This commit adds a function that does that, and uses it in a few places.

gdb/ChangeLog:
yyyy-mm-dd  Pedro Alves <palves@redhat.com>

	* inferior.c (switch_to_inferior_no_thread): New function,
	factored out from ...
	(inferior_command): ... here.
	* inferior.h (switch_to_inferior_no_thread): Declare.
	* mi/mi-main.c (run_one_inferior): Use
	switch_to_inferior_no_thread.
---
 gdb/inferior.c   | 21 +++++++++++++--------
 gdb/inferior.h   |  4 ++++
 gdb/mi/mi-main.c |  6 +-----
 3 files changed, 18 insertions(+), 13 deletions(-)

diff --git a/gdb/inferior.c b/gdb/inferior.c index 310b8f5dd3..619b8caac7 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -585,6 +585,16 @@ kill_inferior_command (const char *args, int from_tty)
   bfd_cache_close_all ();
 }
 
+/* See inferior.h.  */
+
+void
+switch_to_inferior_no_thread (inferior *inf) {
+  set_current_inferior (inf);
+  switch_to_no_thread ();
+  set_current_program_space (inf->pspace); }
+
 static void
 inferior_command (const char *args, int from_tty)  { @@ -615,9 +625,7 @@ inferior_command (const char *args, int from_tty)
     }
   else
     {
-      set_current_inferior (inf);
-      switch_to_no_thread ();
-      set_current_program_space (inf->pspace);
+      switch_to_inferior_no_thread (inf);
 
       gdb::observers::user_selected_context_changed.notify
 	(USER_SELECTED_INFERIOR);
@@ -747,11 +755,8 @@ add_inferior_command (const char *args, int from_tty)
       if (exec != NULL)
 	{
 	  /* Switch over temporarily, while reading executable and
-	     symbols.q.  */
-	  set_current_program_space (inf->pspace);
-	  set_current_inferior (inf);
-	  switch_to_no_thread ();
-
+	     symbols.  */
+	  switch_to_inferior_no_thread (inf);
 	  exec_file_attach (exec.get (), from_tty);
 	  symbol_file_add_main (exec.get (), add_flags);
 	}
diff --git a/gdb/inferior.h b/gdb/inferior.h index 720d570fa3..2e19b751ca 100644
--- a/gdb/inferior.h
+++ b/gdb/inferior.h
@@ -310,6 +310,10 @@ extern inferior *current_inferior ();
 
 extern void set_current_inferior (inferior *);
 
+/* Switch inferior (and program space) to INF, and switch to no thread
+   selected.  */
+extern void switch_to_inferior_no_thread (inferior *inf);
+
 /* GDB represents the state of each program execution with an object
    called an inferior.  An inferior typically corresponds to a process
    but is more general and applies also to targets that do not have a diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c index 9a0b7f97b2..4fa7ffaf90 100644
--- a/gdb/mi/mi-main.c
+++ b/gdb/mi/mi-main.c
@@ -414,11 +414,7 @@ run_one_inferior (struct inferior *inf, void *arg)
       switch_to_thread (tp);
     }
   else
-    {
-      set_current_inferior (inf);
-      switch_to_no_thread ();
-      set_current_program_space (inf->pspace);
-    }
+    switch_to_inferior_no_thread (inf);
   mi_execute_cli_command (run_cmd, async_p,
 			  async_p ? "&" : NULL);
   return 0;
