public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v2 16/24] Fix reconnecting to a gdbserver already debugging multiple processes, I
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
  2019-10-17 22:50 ` [PATCH v2 12/24] Use all_non_exited_inferiors in infrun.c Pedro Alves
@ 2019-10-17 22:50 ` Pedro Alves
  2019-10-17 22:50 ` [PATCH v2 21/24] Revert 'Remove unused struct serial::name field' Pedro Alves
                   ` (25 subsequent siblings)
  27 siblings, 0 replies; 73+ messages in thread
From: Pedro Alves @ 2019-10-17 22:50 UTC (permalink / raw)
  To: gdb-patches

The multi-target patch will change the remote target's behavior when:

- the current inferior is connected to an extended-remote target.
- the current inferior is attached to any process.
- some other inferior than than the current one is live.

In current master, we get:

 (gdb) tar extended-remote :9999
 A program is being debugged already.  Kill it? (y or n)

While after multi-target, since each inferior may have its own target
connection, we'll get:

 (gdb) tar extended-remote :9999
 Already connected to a remote target.  Disconnect? (y or n)

That change made gdb.server/extended-remote-restart.exp expose a gdb
bug, because it made "target remote", via gdb_reconnect, just
disconnect from the previous connection, while in current master that
command would kill the inferior before disconnecting.  In turn, that
would make a multi-target gdb find processes already running under
control of gdbserver as soon as it reconnects, while in current master
there is never any process around when gdb reconnects, since they'd
all been killed prior to disconnection.

The bug this exposed is that remote_target::remote_add_inferior was
always reusing current_inferior() for the new process, even if the
current inferior was already bound to a process.  In the testcase's
case, when we reconnect, the remote is debugging two processes.  So
we'd bind the first remote process to the empty current inferior the
first time, and then bind the second remote process to the same
inferior again, essencially losing track of the first process.  That
resulted in failed assertions when we look up the inferior for the
first process by PID.  The fix is to still prefer binding to the
current inferior (so that plain "target remote" keeps doing what you'd
expect), but not reuse the current inferior if it is already bound to
a process.

This patch tweaks the test to explicitly disconnect before
reconnecting, to avoid GDB killing processes, thus making current GDB
behave the same as it will behave when the multi-target work lands.
That change alone without the GDB fix exposes the bug like so:

 (gdb) PASS: gdb.server/extended-remote-restart.exp: kill: 0, follow-child 0: disconnect
 target extended-remote localhost:2350
 Remote debugging using localhost:2350
 src/gdb/thread.c:93: internal-error: thread_info* inferior_thread(): Assertion `tp' failed.
 A problem internal to GDB has been detected,
 further debugging may prove unreliable.
 Quit this debugging session? (y or n)

The original bug that the testcase was written for was related to
killing, (git 9d4a934ce604 ("gdb: Fix assert for extended-remote
target (PR gdb/18050)")), but since the testcase tries reconnecting
with both explicitly killing and not explicitly killing, I think we're
covering the original bug with this testcase change.

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

	* remote.c (remote_target::remote_add_inferior): Don't bind a
	process to the current inferior if the current inferior is already
	bound to a process.

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

	* gdb.server/extended-remote-restart.exp (test_reload): Explicitly
	disconnect before reconnecting.
---
 gdb/remote.c                                         | 20 ++++++++++++++++++++
 gdb/testsuite/gdb.server/extended-remote-restart.exp |  4 +++-
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/gdb/remote.c b/gdb/remote.c
index e33dedae26..bc1054b192 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -2369,6 +2369,26 @@ remote_target::remote_add_inferior (bool fake_pid_p, int pid, int attached,
 	 between program/address spaces.  We simply bind the inferior
 	 to the program space's address space.  */
       inf = current_inferior ();
+
+      /* However, if the current inferior is already bound to a
+	 process, find some other empty inferior.  */
+      if (inf->pid != 0)
+	{
+	  inf = nullptr;
+	  for (inferior *it : all_inferiors ())
+	    if (it->pid == 0)
+	      {
+		inf = it;
+		break;
+	      }
+	}
+      if (inf == nullptr)
+	{
+	  /* Since all inferiors were already bound to a process, add
+	     a new inferior.  */
+	  inf = add_inferior_with_spaces ();
+	}
+      switch_to_inferior_no_thread (inf);
       inferior_appeared (inf, pid);
     }
 
diff --git a/gdb/testsuite/gdb.server/extended-remote-restart.exp b/gdb/testsuite/gdb.server/extended-remote-restart.exp
index 1fa46f2073..c78342c010 100644
--- a/gdb/testsuite/gdb.server/extended-remote-restart.exp
+++ b/gdb/testsuite/gdb.server/extended-remote-restart.exp
@@ -113,7 +113,9 @@ proc test_reload { do_kill_p follow_child_p } {
 	    "Check inferior was killed"
     }
 
-    # Reconnect to the target.
+    # Disconnect, and reconnect to the target.
+    gdb_test "disconnect" ".*"
+
     if { [gdb_reconnect] == 0 } {
 	pass "reconnect after fork"
     } else {
-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [PATCH v2 11/24] tfile_target::close: trace_fd can't be -1
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
                   ` (3 preceding siblings ...)
  2019-10-17 22:50 ` [PATCH v2 17/24] Fix reconnecting to a gdbserver already debugging multiple processes, II Pedro Alves
@ 2019-10-17 22:50 ` Pedro Alves
  2019-10-17 22:50 ` [PATCH v2 02/24] Don't rely on inferior_ptid in record_full_wait Pedro Alves
                   ` (22 subsequent siblings)
  27 siblings, 0 replies; 73+ messages in thread
From: Pedro Alves @ 2019-10-17 22:50 UTC (permalink / raw)
  To: gdb-patches

It's not possible to open a tfile target with an invalid trace_fd, and
it's not possible to close a closed target, so this early return is dead.

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

	* tracefile-tfile.c (tfile_target::close): Assert that trace_fd is
	not -1.
---
 gdb/tracefile-tfile.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/gdb/tracefile-tfile.c b/gdb/tracefile-tfile.c
index 5c3837ec5b..c5063e66b0 100644
--- a/gdb/tracefile-tfile.c
+++ b/gdb/tracefile-tfile.c
@@ -616,8 +616,7 @@ tfile_interp_line (char *line, struct uploaded_tp **utpp,
 void
 tfile_target::close ()
 {
-  if (trace_fd < 0)
-    return;
+  gdb_assert (trace_fd != -1);
 
   inferior_ptid = null_ptid;	/* Avoid confusion from thread stuff.  */
   exit_inferior_silent (current_inferior ());
-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [PATCH v2 15/24] Avoid another inferior_ptid reference in gdb/remote.c
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
                   ` (10 preceding siblings ...)
  2019-10-17 22:50 ` [PATCH v2 09/24] switch inferior/thread before calling target methods Pedro Alves
@ 2019-10-17 22:50 ` Pedro Alves
  2019-10-17 22:51 ` [PATCH v2 22/24] Add "info connections" command, "info inferiors" connection number/string Pedro Alves
                   ` (15 subsequent siblings)
  27 siblings, 0 replies; 73+ messages in thread
From: Pedro Alves @ 2019-10-17 22:50 UTC (permalink / raw)
  To: gdb-patches

From: Tankut Baris Aktemur <tankut.baris.aktemur@intel.com>

The multi-target patch makes inferior_ptid point to null_ptid before
calling into target_wait, which catches bad uses of inferior_ptid,
since the current selected thread in gdb shouldn't have much relation
to the thread that reports an event.

One such bad use is found in remote_target::remote_parse_stop_reply,
where we handle the 'W' or 'X' packets (process exit), and the remote
target does not support the multi-process extensions, i.e., it does
not report the PID of the process that exited.

With the multi-target patch, that would result in a failed assertion,
trying to find the inferior for process pid 0.

gdb/ChangeLog:
yyyy-mm-dd  Tankut Baris Aktemur  <tankut.baris.aktemur@intel.com>
	    Pedro Alves	 <palves@redhat.com>

	* remote.c (remote_target::remote_parse_stop_reply) <W/X packets>:
	If no process is specified, return null_ptid instead of
	inferior_ptid.
	(remote_target::wait_as): Handle TARGET_WAITKIND_EXITED /
	TARGET_WAITKIND_SIGNALLED with no pid.

gdb/testsuite/ChangeLog:
yyyy-mm-dd  Tankut Baris Aktemur  <tankut.baris.aktemur@intel.com>
	    Pedro Alves	 <palves@redhat.com>

	* gdb.server/connect-without-multi-process.exp: Also test
	continuing to end.
---
 gdb/remote.c                                           | 18 +++++++++++++-----
 .../gdb.server/connect-without-multi-process.exp       |  7 ++++++-
 2 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/gdb/remote.c b/gdb/remote.c
index 553f09a6e0..e33dedae26 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -7446,7 +7446,6 @@ Packet: '%s'\n"),
     case 'W':		/* Target exited.  */
     case 'X':
       {
-	int pid;
 	ULONGEST value;
 
 	/* GDB used to accept only 2 hex chars here.  Stubs should
@@ -7470,8 +7469,9 @@ Packet: '%s'\n"),
 	      event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
 	  }
 
-	/* If no process is specified, assume inferior_ptid.  */
-	pid = inferior_ptid.pid ();
+	/* If no process is specified, return null_ptid, and let the
+	   caller figure out the right process to use.  */
+	int pid = 0;
 	if (*p == '\0')
 	  ;
 	else if (*p == ';')
@@ -7847,8 +7847,16 @@ remote_target::wait_as (ptid_t ptid, target_waitstatus *status, int options)
 	event_ptid = first_remote_resumed_thread ();
     }
   else
-    /* A process exit.  Invalidate our notion of current thread.  */
-    record_currthread (rs, minus_one_ptid);
+    {
+      /* A process exit.  Invalidate our notion of current thread.  */
+      record_currthread (rs, minus_one_ptid);
+      /* It's possible that the packet did not include a pid.  */
+      if (event_ptid == null_ptid)
+	event_ptid = first_remote_resumed_thread ();
+      /* EVENT_PTID could still be NULL_PTID.  Double-check.  */
+      if (event_ptid == null_ptid)
+	event_ptid = magic_null_ptid;
+    }
 
   return event_ptid;
 }
diff --git a/gdb/testsuite/gdb.server/connect-without-multi-process.exp b/gdb/testsuite/gdb.server/connect-without-multi-process.exp
index fba20a6a0b..6ba35bf508 100644
--- a/gdb/testsuite/gdb.server/connect-without-multi-process.exp
+++ b/gdb/testsuite/gdb.server/connect-without-multi-process.exp
@@ -14,7 +14,7 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 # Check that we can connect to GDBserver with the multiprocess
-# extensions disabled, and run to main.
+# extensions disabled, run to main, and finish the process.
 
 load_lib gdbserver-support.exp
 
@@ -52,6 +52,11 @@ proc do_test {multiprocess} {
 	"target $gdbserver_protocol"
 
     gdb_test "continue" "main .*" "continue to main"
+
+    # The W/X packets do not include the PID of the exiting process
+    # without the multi-process extensions.  Check that we handle
+    # process exit correctly in that case.
+    gdb_continue_to_end
 }
 
 foreach multiprocess { "off" "auto" } {
-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [PATCH v2 03/24] Make "show remote exec-file" inferior-aware
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
                   ` (6 preceding siblings ...)
  2019-10-17 22:50 ` [PATCH v2 06/24] Don't check target is running in remote_target::mourn_inferior Pedro Alves
@ 2019-10-17 22:50 ` Pedro Alves
  2019-10-17 22:50 ` [PATCH v2 10/24] Some get_last_target_status tweaks Pedro Alves
                   ` (19 subsequent siblings)
  27 siblings, 0 replies; 73+ messages in thread
From: Pedro Alves @ 2019-10-17 22:50 UTC (permalink / raw)
  To: gdb-patches

The "set remote exec-file" setting is per-inferior, but the "show
remote exec-file" command always shows the last set exec-file,
irrespective of the current inferior.  E.g.:

 # Set inferior 1's exec-file:
 (gdb) set remote exec-file prog1

 # Add inferior 2, switch to it, and set its exec-file:
 (gdb) add-inferior
 Added inferior 2
 (gdb) inferior 2
 (gdb) set remote exec-file prog2

 # Switch back to inferior 1, and show its exec-file:
 (gdb) inferior 1
 (gdb) show remote exec-file
 prog2
 ^^^^^ should show "prog1" instead here.

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

	* remote.c (show_remote_exec_file): Show the current inferior's
	exec-file instead of the command variable's value.

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

	* gdb.base/remote-exec-file.exp: New file.
---
 gdb/remote.c                                |  2 +-
 gdb/testsuite/gdb.base/remote-exec-file.exp | 46 +++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+), 1 deletion(-)
 create mode 100644 gdb/testsuite/gdb.base/remote-exec-file.exp

diff --git a/gdb/remote.c b/gdb/remote.c
index f616569ff1..11853a9c12 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -1272,7 +1272,7 @@ static void
 show_remote_exec_file (struct ui_file *file, int from_tty,
 		       struct cmd_list_element *cmd, const char *value)
 {
-  fprintf_filtered (file, "%s\n", remote_exec_file_var);
+  fprintf_filtered (file, "%s\n", get_remote_exec_file ());
 }
 
 static int
diff --git a/gdb/testsuite/gdb.base/remote-exec-file.exp b/gdb/testsuite/gdb.base/remote-exec-file.exp
new file mode 100644
index 0000000000..3db009b830
--- /dev/null
+++ b/gdb/testsuite/gdb.base/remote-exec-file.exp
@@ -0,0 +1,46 @@
+# Copyright 2019 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Check that "show remote exec-file" displays each inferior's
+# exec-file.  Regression test for a bug where "show remote exec-file"
+# would show the last exec-file set, irrespective of the current
+# inferior.
+
+clean_restart
+
+# Set remote exec-file in inferior 1.
+with_test_prefix "set inf 1" {
+    gdb_test_no_output "set remote exec-file prog1"
+}
+
+# Set remote exec-file in inferior 2.
+with_test_prefix "set inf 2" {
+    gdb_test "add-inferior" "Added inferior 2" "add inferior 2"
+    gdb_test "inferior 2" "Switching to inferior 2.*"
+    gdb_test_no_output "set remote exec-file prog2"
+}
+
+# Check that "show remote exec-file" diplays each inferior's
+# exec-file.
+
+with_test_prefix "show inf 1" {
+    gdb_test "inferior 1" "Switching to inferior 1.*"
+    gdb_test "show remote exec-file" "prog1"
+}
+
+with_test_prefix "show inf 2" {
+    gdb_test "inferior 2" "Switching to inferior 2.*"
+    gdb_test "show remote exec-file" "prog2"
+}
-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [PATCH v2 01/24] Preserve selected thread in all-stop w/ background execution
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
                   ` (8 preceding siblings ...)
  2019-10-17 22:50 ` [PATCH v2 10/24] Some get_last_target_status tweaks Pedro Alves
@ 2019-10-17 22:50 ` Pedro Alves
  2019-11-01 13:20   ` Tom Tromey
  2019-10-17 22:50 ` [PATCH v2 09/24] switch inferior/thread before calling target methods Pedro Alves
                   ` (17 subsequent siblings)
  27 siblings, 1 reply; 73+ messages in thread
From: Pedro Alves @ 2019-10-17 22:50 UTC (permalink / raw)
  To: gdb-patches

In non-stop mode, if you resume the program in the background (with
"continue&", for example), then gdb makes sure to not switch the
current thread behind your back.  That means that you can be sure that
the commands you type apply to the thread you selected, even if some
other thread that was running in the background hits some event just
while you're typing.

In all-stop mode, however, if you resume the program in the
background, gdb let's the current thread switch behind your back.

This is bogus, of course.  All-stop and non-stop background
resumptions should behave the same.

This patch fixes that, and adds a testcase that exposes the bad
behavior in current master.

The fork-running-state.exp changes are necessary because that
preexisting testcase was expecting the old behavior:

Before:

  continue &
  Continuing.
  (gdb)
  [Attaching after process 8199 fork to child process 8203]
  [New inferior 2 (process 8203)]
  info threads
    Id   Target Id                      Frame
    1.1  process 8199 "fork-running-st" (running)
  * 2.1  process 8203 "fork-running-st" (running)
  (gdb)

After:

  continue &
  Continuing.
  (gdb)
  [Attaching after process 24660 fork to child process 24664]
  [New inferior 2 (process 24664)]
  info threads
    Id   Target Id                       Frame
  * 1.1  process 24660 "fork-running-st" (running)
    2.1  process 24664 "fork-running-st" (running)
  (gdb)

Here we see that before this patch GDB switches current inferior to
the new inferior behind the user's back, as a side effect of handling
the fork.

The delete_exited_threads call in inferior_appeared is there to fix an
issue that Baris found in a previous version of this patch.  The
fetch_inferior_event change increases the refcount of the current
thread, and in case the fetched inferior event denotes a thread exit,
the thread will not be deleted right away.  A non-deleted but exited
thread stays in the inferior's thread list.  This, in turn, causes the
"init_thread_list" call in inferior.c to be skipped.  A consequence is
that the global thread ID counter is not restarted if the current
thread exits, and then the inferior is restarted:

 (gdb) start
 Temporary breakpoint 1 at 0x4004d6: file main.c, line 21.
 Starting program: /tmp/main

 Temporary breakpoint 1, main () at main.c:21
 21        foo ();
 (gdb) info threads -gid
   Id   GId  Target Id            Frame
 * 1    1    process 16106 "main" main () at main.c:21
 (gdb) c
 Continuing.
 [Inferior 1 (process 16106) exited normally]
 (gdb) start
 Temporary breakpoint 2 at 0x4004d6: file main.c, line 21.
 Starting program: /tmp/main

 Temporary breakpoint 2, main () at main.c:21
 21        foo ();
 (gdb) info threads -gid
   Id   GId  Target Id            Frame
 * 1    2    process 16138 "main" main () at main.c:21
       ^^^

Notice that GId == 2 above.  It should have been "1" instead.

The new tids-git-reset.exp testcase exercises the problem above.

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

	* gdbthread.h (scoped_restore_current_thread)
	<dont_restore, restore, m_dont_restore>: Declare.
	* thread.c (thread_alive): Add assertion.  Return bool.
	(switch_to_thread_if_alive): New.
	(prune_threads): Switch inferior/thread.
	(print_thread_info_1): Switch thread before calling target methods.
	(scoped_restore_current_thread::restore): New, factored out from
	...
	(scoped_restore_current_thread::~scoped_restore_current_thread):
	... this.
	(scoped_restore_current_thread::scoped_restore_current_thread):
	Add assertion.
	(thread_apply_all_command, thread_select): Use
	switch_to_thread_if_alive.

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

	* gdb.base/fork-running-state.exp (do_test): Adjust expected
	output.
	* gdb.threads/async.c: New.
	* gdb.threads/async.exp: New.
	* gdb.multi/tids-gid-reset.c: New.
	* gdb.multi/tids-gid-reset.exp: New.
---
 gdb/gdbthread.h                               |  6 ++
 gdb/inferior.c                                |  1 +
 gdb/infrun.c                                  | 31 ++++++---
 gdb/testsuite/gdb.base/fork-running-state.exp | 17 +----
 gdb/testsuite/gdb.multi/tids-gid-reset.c      | 22 ++++++
 gdb/testsuite/gdb.multi/tids-gid-reset.exp    | 96 ++++++++++++++++++++++++++
 gdb/testsuite/gdb.threads/async.c             | 70 +++++++++++++++++++
 gdb/testsuite/gdb.threads/async.exp           | 98 +++++++++++++++++++++++++++
 gdb/thread.c                                  | 19 +++++-
 9 files changed, 337 insertions(+), 23 deletions(-)
 create mode 100644 gdb/testsuite/gdb.multi/tids-gid-reset.c
 create mode 100644 gdb/testsuite/gdb.multi/tids-gid-reset.exp
 create mode 100644 gdb/testsuite/gdb.threads/async.c
 create mode 100644 gdb/testsuite/gdb.threads/async.exp

diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
index 370141e688..62b73fb83b 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -645,7 +645,13 @@ public:
 
   DISABLE_COPY_AND_ASSIGN (scoped_restore_current_thread);
 
+  /* Cancel restoring on scope exit.  */
+  void dont_restore () { m_dont_restore = true; }
+
 private:
+  void restore ();
+
+  bool m_dont_restore = false;
   /* Use the "class" keyword here, because of a clash with a "thread_info"
      function in the Darwin API.  */
   class thread_info *m_thread;
diff --git a/gdb/inferior.c b/gdb/inferior.c
index cf2175494d..fa1fd3fc10 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -255,6 +255,7 @@ inferior_appeared (struct inferior *inf, int pid)
 {
   /* If this is the first inferior with threads, reset the global
      thread id.  */
+  delete_exited_threads ();
   if (!any_thread_p ())
     init_thread_list ();
 
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 07aebfa678..6da95e0584 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -3046,6 +3046,11 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
 
   finish_state.release ();
 
+  /* If we've switched threads above, switch back to the previously
+     current thread.  We don't want the user to see a different
+     selected thread.  */
+  switch_to_thread (cur_thr);
+
   /* Tell the event loop to wait for it to stop.  If the target
      supports asynchronous execution, it'll do this from within
      target_resume.  */
@@ -3700,14 +3705,11 @@ fetch_inferior_event (void *client_data)
 	set_current_traceframe (-1);
       }
 
-    gdb::optional<scoped_restore_current_thread> maybe_restore_thread;
-
-    if (non_stop)
-      /* In non-stop mode, the user/frontend should not notice a thread
-	 switch due to internal events.  Make sure we reverse to the
-	 user selected thread and frame after handling the event and
-	 running any breakpoint commands.  */
-      maybe_restore_thread.emplace ();
+    /* The user/frontend should not notice a thread switch due to
+       internal events.  Make sure we revert to the user selected
+       thread and frame after handling the event and running any
+       breakpoint commands.  */
+    scoped_restore_current_thread restore_thread;
 
     overlay_cache_invalid = 1;
     /* Flush target cache before starting to handle each event.  Target
@@ -3784,6 +3786,19 @@ fetch_inferior_event (void *client_data)
 		inferior_event_handler (INF_EXEC_COMPLETE, NULL);
 		cmd_done = 1;
 	      }
+
+	    /* If we got a TARGET_WAITKIND_NO_RESUMED event, then the
+	       previously selected thread is gone.  We have two
+	       choices - switch to no thread selected, or restore the
+	       previously selected thread (now exited).  We chose the
+	       later, just because that's what GDB used to do.  After
+	       this, "info threads" says "The current thread <Thread
+	       ID 2> has terminated." instead of "No thread
+	       selected.".  */
+	    if (!non_stop
+		&& cmd_done
+		&& ecs->ws.kind != TARGET_WAITKIND_NO_RESUMED)
+	      restore_thread.dont_restore ();
 	  }
       }
 
diff --git a/gdb/testsuite/gdb.base/fork-running-state.exp b/gdb/testsuite/gdb.base/fork-running-state.exp
index 98733a6cc1..143b5ce714 100644
--- a/gdb/testsuite/gdb.base/fork-running-state.exp
+++ b/gdb/testsuite/gdb.base/fork-running-state.exp
@@ -98,30 +98,19 @@ proc do_test { detach_on_fork follow_fork non_stop schedule_multiple } {
 
     set not_nl "\[^\r\n\]*"
 
-    if {$detach_on_fork == "on" && $non_stop == "on" && $follow_fork == "child"} {
+    if {$detach_on_fork == "on" && $follow_fork == "child"} {
 	gdb_test "info threads" \
 	    "  2.1 ${not_nl}\\\(running\\\).*No selected thread.*"
-    } elseif {$detach_on_fork == "on" && $follow_fork == "child"} {
-	gdb_test "info threads" \
-	    "\\\* 2.1 ${not_nl}\\\(running\\\)"
     } elseif {$detach_on_fork == "on"} {
 	gdb_test "info threads" \
 	    "\\\* 1 ${not_nl}\\\(running\\\)"
-    } elseif {$non_stop == "on"
-	      || ($schedule_multiple == "on" && $follow_fork == "parent")} {
+    } elseif {$non_stop == "on" || $schedule_multiple == "on"} {
 	# Both parent and child should be marked running, and the
 	# parent should be selected.
 	gdb_test "info threads" \
 	    [multi_line \
 		 "\\\* 1.1 ${not_nl} \\\(running\\\)${not_nl}" \
 		 "  2.1 ${not_nl} \\\(running\\\)"]
-    } elseif {$schedule_multiple == "on" && $follow_fork == "child"} {
-	# Both parent and child should be marked running, and the
-	# child should be selected.
-	gdb_test "info threads" \
-	    [multi_line \
-		 "  1.1 ${not_nl} \\\(running\\\)${not_nl}" \
-		 "\\\* 2.1 ${not_nl} \\\(running\\\)"]
     } else {
 	set test "only $follow_fork marked running"
 	gdb_test_multiple "info threads" $test {
@@ -131,7 +120,7 @@ proc do_test { detach_on_fork follow_fork non_stop schedule_multiple } {
 	    -re "\\\* 1.1 ${not_nl}\\\(running\\\)\r\n  2.1 ${not_nl}\r\n$gdb_prompt $" {
 		gdb_assert [string eq $follow_fork "parent"] $test
 	    }
-	    -re "1.1 ${not_nl}\r\n\\\* 2.1 ${not_nl}\\\(running\\\)\r\n$gdb_prompt $" {
+	    -re "\\\* 1.1 ${not_nl}\r\n  2.1 ${not_nl}\\\(running\\\)\r\n$gdb_prompt $" {
 		gdb_assert [string eq $follow_fork "child"] $test
 	    }
 	}
diff --git a/gdb/testsuite/gdb.multi/tids-gid-reset.c b/gdb/testsuite/gdb.multi/tids-gid-reset.c
new file mode 100644
index 0000000000..4d3d67b8e4
--- /dev/null
+++ b/gdb/testsuite/gdb.multi/tids-gid-reset.c
@@ -0,0 +1,22 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2019 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+int
+main (void)
+{
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.multi/tids-gid-reset.exp b/gdb/testsuite/gdb.multi/tids-gid-reset.exp
new file mode 100644
index 0000000000..e15049bfad
--- /dev/null
+++ b/gdb/testsuite/gdb.multi/tids-gid-reset.exp
@@ -0,0 +1,96 @@
+# Copyright 2015-2019 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Check that letting the inferior exit and restarting it again resets
+# the global TID counter, and thus the new thread 1.1 should end up
+# with global TID == 1.
+#
+# Also, check the same but with another inferior still running, in
+# which case the new thread 1.1 should end up with global TID == 3.
+
+standard_testfile
+
+if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile} {pthreads debug}] } {
+    return -1
+}
+
+with_test_prefix "single-inferior" {
+    with_test_prefix "before restart" {
+	clean_restart ${testfile}
+
+	if { ![runto_main] } then {
+	    return -1
+	}
+
+	gdb_test "info threads -gid" "\\* 1 +1 +.*"
+    }
+
+    with_test_prefix "restart" {
+	gdb_continue_to_end
+	if { ![runto_main] } then {
+	    return -1
+	}
+    }
+
+    with_test_prefix "after restart" {
+	gdb_test "info threads -gid" "\\* 1 +1 +.*"
+    }
+}
+
+# For the following tests, multiple inferiors are needed, therefore
+# non-extended gdbserver is not supported.
+if [use_gdb_stub] {
+    untested "using gdb stub"
+    return
+}
+
+# Test with multiple inferiors.  This time, since we restart inferior
+# 1 while inferior 2 still has threads, then the new thread 1.1 should
+# end up with GID == 3, since we won't be able to reset the global
+# thread ID counter.
+with_test_prefix "multi-inferior" {
+    gdb_test "add-inferior" "Added inferior 2.*" "add empty inferior 2"
+    gdb_test "inferior 2" "Switching to inferior 2 .*" "switch to inferior 2"
+    gdb_load ${binfile}
+
+    if ![runto_main] then {
+	fail "starting inferior 2"
+	return
+    }
+
+    gdb_test "inferior 1" "Switching to inferior 1 .*" \
+	"switch back to inferior 1"
+
+    with_test_prefix "before restart" {
+	gdb_test "info threads -gid" \
+	    [multi_line \
+		 "\\* 1\.1 +1 +.*" \
+		 "  2\.1 +2 +.*"]
+    }
+
+    with_test_prefix "restart" {
+	gdb_continue_to_end
+	if { ![runto_main] } then {
+	    return -1
+	}
+    }
+
+    with_test_prefix "after restart" {
+	gdb_test "info threads -gid" \
+	    [multi_line \
+		 "\\* 1\.1 +3 +.*" \
+		 "  2\.1 +2 +.*"]
+    }
+}
diff --git a/gdb/testsuite/gdb.threads/async.c b/gdb/testsuite/gdb.threads/async.c
new file mode 100644
index 0000000000..6bffca9c8d
--- /dev/null
+++ b/gdb/testsuite/gdb.threads/async.c
@@ -0,0 +1,70 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2019 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <pthread.h>
+
+#define NUM 2
+
+static pthread_barrier_t threads_started_barrier;
+
+static void *
+thread_function (void *arg)
+{
+  pthread_barrier_wait (&threads_started_barrier);
+
+  while (1)
+    {
+      /* Sleep a bit to give the other threads a chance to run.  */
+      usleep (1); /* set breakpoint here */
+    }
+
+  pthread_exit (NULL);
+}
+
+static void
+all_started (void)
+{
+}
+
+int
+main ()
+{
+  pthread_t threads[NUM];
+  long i;
+
+  pthread_barrier_init (&threads_started_barrier, NULL, NUM + 1);
+
+  for (i = 1; i <= NUM; i++)
+    {
+      int res;
+
+      res = pthread_create (&threads[i - 1],
+			    NULL,
+			    thread_function, NULL);
+    }
+
+  pthread_barrier_wait (&threads_started_barrier);
+
+  all_started ();
+
+  sleep (180);
+
+  exit (EXIT_SUCCESS);
+}
diff --git a/gdb/testsuite/gdb.threads/async.exp b/gdb/testsuite/gdb.threads/async.exp
new file mode 100644
index 0000000000..7047bef2bd
--- /dev/null
+++ b/gdb/testsuite/gdb.threads/async.exp
@@ -0,0 +1,98 @@
+# Copyright (C) 2019 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+standard_testfile
+
+if {[build_executable "failed to prepare" $testfile $srcfile {debug pthreads}] == -1} {
+    return -1
+}
+
+# At this point GDB will be busy handling the breakpoint hits and
+# re-resuming the program.  Even if GDB internally switches thread
+# context, the user should not notice it.  The following part of the
+# testcase ensures that.
+
+# Switch to thread EXPECTED_THR, and then confirm that the thread
+# stays selected.
+
+proc test_current_thread {expected_thr} {
+    global decimal
+    global gdb_prompt
+    global binfile
+
+    clean_restart $binfile
+
+    if {![runto "all_started"]} {
+	fail "could not run to all_started"
+	return
+    }
+
+    # Set a breakpoint that continuously fires but doeesn't cause a stop.
+    gdb_breakpoint [concat [gdb_get_line_number "set breakpoint here"] " if 0"]
+
+    gdb_test "thread $expected_thr" "Switching to thread $expected_thr .*" \
+	"switch to thread $expected_thr"
+
+    # Continue the program in the background.
+    set test "continue&"
+    gdb_test_multiple "continue&" $test {
+	-re "Continuing\\.\r\n$gdb_prompt " {
+	    pass $test
+	}
+    }
+
+    set test "current thread is $expected_thr"
+    set fails 0
+    for {set i 0} {$i < 10} {incr i} {
+	after 200
+
+	set cur_thread 0
+	gdb_test_multiple "thread" $test {
+	    -re "Current thread is ($decimal) .*$gdb_prompt " {
+		set cur_thread $expect_out(1,string)
+	    }
+	}
+
+	if {$cur_thread != $expected_thr} {
+	    incr fails
+	}
+    }
+
+    gdb_assert {$fails == 0} $test
+
+    # Explicitly interrupt the target, because in all-stop/remote,
+    # that's all we can do when the target is running.  If we don't do
+    # this, we'd time out trying to kill the target, while bringing
+    # down gdb & gdbserver.
+    set test "interrupt"
+    gdb_test_multiple $test $test {
+	-re "^interrupt\r\n$gdb_prompt " {
+	    gdb_test_multiple "" $test {
+		-re "Thread .* received signal SIGINT, Interrupt\\." {
+		    pass $test
+		}
+	    }
+	}
+    }
+}
+
+# Try once with each thread as current, to avoid missing a bug just
+# because some part of GDB manages to switch to the right thread by
+# chance.
+for {set thr 1} {$thr <= 3} {incr thr} {
+    with_test_prefix "thread $thr" {
+	test_current_thread $thr
+    }
+}
diff --git a/gdb/thread.c b/gdb/thread.c
index 17bc642b84..2f9cd87ac8 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -1375,7 +1375,8 @@ restore_selected_frame (struct frame_id a_frame_id, int frame_level)
     }
 }
 
-scoped_restore_current_thread::~scoped_restore_current_thread ()
+void
+scoped_restore_current_thread::restore ()
 {
   /* If an entry of thread_info was previously selected, it won't be
      deleted because we've increased its refcount.  The thread represented
@@ -1402,6 +1403,22 @@ scoped_restore_current_thread::~scoped_restore_current_thread ()
       && target_has_stack
       && target_has_memory)
     restore_selected_frame (m_selected_frame_id, m_selected_frame_level);
+}
+
+scoped_restore_current_thread::~scoped_restore_current_thread ()
+{
+  if (!m_dont_restore)
+    {
+      try
+	{
+	  restore ();
+	}
+      catch (const gdb_exception &ex)
+	{
+	  /* We're in a dtor, there's really nothing else we can do
+	     but swallow the exception.  */
+	}
+    }
 
   if (m_thread != NULL)
     m_thread->decref ();
-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [PATCH v2 06/24] Don't check target is running in remote_target::mourn_inferior
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
                   ` (5 preceding siblings ...)
  2019-10-17 22:50 ` [PATCH v2 02/24] Don't rely on inferior_ptid in record_full_wait Pedro Alves
@ 2019-10-17 22:50 ` Pedro Alves
  2019-10-17 22:50 ` [PATCH v2 03/24] Make "show remote exec-file" inferior-aware Pedro Alves
                   ` (20 subsequent siblings)
  27 siblings, 0 replies; 73+ messages in thread
From: Pedro Alves @ 2019-10-17 22:50 UTC (permalink / raw)
  To: gdb-patches

I believe the tail end of remote_target::mourn_inferior is broken, and
it's been broken for too long to even bother trying to fix.  Most
probably nobody needs it.  If the code is reached and we find the
target is running, we'd need to resync the thread list, at least,
since generic_mourn_inferior got rid of all the threads in the
inferior, otherwise, we'd hit an assertion on the next call to
inferior_thread(), for example.  A "correct" fix would probably
involve restarting the whole remote_target::start_remote requence,
exactly as if we had completely disconnected and reconnected from
scratch.

Note that regular stub debugging usually uses plain target remote, but
this code is only reachable in target extended-mode:

- The !remote_multi_process_p check means that it's only reacheable if
  the stub does not support multi-process.  I.e., there can only ever
  be one live process.

- remote_target::mourn_inferior has this at the top:

  /* In 'target remote' mode with one inferior, we close the connection.  */
  if (!rs->extended && number_of_live_inferiors () <= 1)
    {
      unpush_target (this);

      /* remote_close takes care of doing most of the clean up.  */
      generic_mourn_inferior ();
      return;
    }

  Which means that if we only had one live inferior (which for our
  case, must be true), we'll have closed the connection already,
  unless we're in extended-remote mode.

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

	* remote.c (remote_target::mourn_inferior): No longer check
	whether the target is running.
---
 gdb/remote.c | 20 --------------------
 1 file changed, 20 deletions(-)

diff --git a/gdb/remote.c b/gdb/remote.c
index 11853a9c12..f74881a3c1 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -9835,26 +9835,6 @@ remote_target::mourn_inferior ()
 
   /* Call common code to mark the inferior as not running.  */
   generic_mourn_inferior ();
-
-  if (!have_inferiors ())
-    {
-      if (!remote_multi_process_p (rs))
-	{
-	  /* Check whether the target is running now - some remote stubs
-	     automatically restart after kill.	*/
-	  putpkt ("?");
-	  getpkt (&rs->buf, 0);
-
-	  if (rs->buf[0] == 'S' || rs->buf[0] == 'T')
-	    {
-	      /* Assume that the target has been restarted.  Set
-		 inferior_ptid so that bits of core GDB realizes
-		 there's something here, e.g., so that the user can
-		 say "kill" again.  */
-	      inferior_ptid = magic_null_ptid;
-	    }
-	}
-    }
 }
 
 bool
-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [PATCH v2 21/24] Revert 'Remove unused struct serial::name field'
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
  2019-10-17 22:50 ` [PATCH v2 12/24] Use all_non_exited_inferiors in infrun.c Pedro Alves
  2019-10-17 22:50 ` [PATCH v2 16/24] Fix reconnecting to a gdbserver already debugging multiple processes, I Pedro Alves
@ 2019-10-17 22:50 ` Pedro Alves
  2019-10-17 22:50 ` [PATCH v2 17/24] Fix reconnecting to a gdbserver already debugging multiple processes, II Pedro Alves
                   ` (24 subsequent siblings)
  27 siblings, 0 replies; 73+ messages in thread
From: Pedro Alves @ 2019-10-17 22:50 UTC (permalink / raw)
  To: gdb-patches

This commit reverts:

 commit 5f5219fc34f7557296272230123a3837960a6f09
 Author:     Pedro Alves <palves@redhat.com>
 AuthorDate: Tue Apr 12 16:49:30 2016 +0100

     Remove unused struct serial::name field

The following patches will add uses for the field.

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

	Revert:
	2016-04-12  Pedro Alves  <palves@redhat.com>
	* serial.c (serial_open, serial_fdopen_ops, do_serial_close):
	Remove references to name.
	* serial.h (struct serial) <name>: Delete.
---
 gdb/serial.c | 4 ++++
 gdb/serial.h | 1 +
 2 files changed, 5 insertions(+)

diff --git a/gdb/serial.c b/gdb/serial.c
index a881bbc97c..0ed3d37406 100644
--- a/gdb/serial.c
+++ b/gdb/serial.c
@@ -247,6 +247,7 @@ serial_open_ops_1 (const struct serial_ops *ops, const char *open_name)
       return NULL;
     }
 
+  scb->name = open_name != NULL ? xstrdup (open_name) : NULL;
   scb->next = scb_base;
   scb_base = scb;
 
@@ -291,6 +292,7 @@ serial_fdopen_ops (const int fd, const struct serial_ops *ops)
 
   scb = new_serial (ops);
 
+  scb->name = NULL;
   scb->next = scb_base;
   scb_base = scb;
 
@@ -330,6 +332,8 @@ do_serial_close (struct serial *scb, int really_close)
   if (really_close)
     scb->ops->close (scb);
 
+  xfree (scb->name);
+
   /* For serial_is_open.  */
   scb->bufp = NULL;
 
diff --git a/gdb/serial.h b/gdb/serial.h
index b75b3666e7..d58ab660e9 100644
--- a/gdb/serial.h
+++ b/gdb/serial.h
@@ -240,6 +240,7 @@ struct serial
 				   buffer.  -ve for sticky errors.  */
     unsigned char *bufp;	/* Current byte */
     unsigned char buf[BUFSIZ];	/* Da buffer itself */
+    char *name;			/* The name of the device or host */
     struct serial *next;	/* Pointer to the next `struct serial *' */
     int debug_p;		/* Trace this serial devices operation.  */
     int async_state;		/* Async internal state.  */
-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [PATCH v2 12/24] Use all_non_exited_inferiors in infrun.c
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
@ 2019-10-17 22:50 ` Pedro Alves
  2019-10-17 22:50 ` [PATCH v2 16/24] Fix reconnecting to a gdbserver already debugging multiple processes, I Pedro Alves
                   ` (26 subsequent siblings)
  27 siblings, 0 replies; 73+ messages in thread
From: Pedro Alves @ 2019-10-17 22:50 UTC (permalink / raw)
  To: gdb-patches

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

	* infrun.c (handle_no_resumed): Use all_non_exited_inferiors.
---
 gdb/infrun.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/gdb/infrun.c b/gdb/infrun.c
index a759c5c204..ab73a56604 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -4589,11 +4589,8 @@ handle_no_resumed (struct execution_control_state *ecs)
      process exited meanwhile (thus updating the thread list results
      in an empty thread list).  In this case we know we'll be getting
      a process exit event shortly.  */
-  for (inferior *inf : all_inferiors ())
+  for (inferior *inf : all_non_exited_inferiors ())
     {
-      if (inf->pid == 0)
-	continue;
-
       thread_info *thread = any_live_thread_of_inferior (inf);
       if (thread == NULL)
 	{
-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [PATCH v2 10/24] Some get_last_target_status tweaks
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
                   ` (7 preceding siblings ...)
  2019-10-17 22:50 ` [PATCH v2 03/24] Make "show remote exec-file" inferior-aware Pedro Alves
@ 2019-10-17 22:50 ` Pedro Alves
  2019-10-17 22:50 ` [PATCH v2 01/24] Preserve selected thread in all-stop w/ background execution Pedro Alves
                   ` (18 subsequent siblings)
  27 siblings, 0 replies; 73+ messages in thread
From: Pedro Alves @ 2019-10-17 22:50 UTC (permalink / raw)
  To: gdb-patches

- Make get_last_target_status arguments optional.  A following patch
  will add another argument to get_last_target_status (the event's
  target), and passing nullptr when we don't care for some piece of
  info is handier than creating dummy local variables.

- Declare nullify_last_target_wait_ptid in a header, and remove the
  local extern declaration from linux-fork.c.

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

	* break-catch-sig.c (signal_catchpoint_print_it): Don't pass a
	ptid to get_last_target_status.
	* break-catch-syscall.c (print_it_catch_syscall): Don't pass a
	ptid to get_last_target_status.
	* infcmd.c (continue_command): Don't pass a target_waitstatus to
	get_last_target_status.
	(info_program_command): Don't pass a target_waitstatus to
	get_last_target_status.
	* infrun.c (init_wait_for_inferior): Use
	nullify_last_target_wait_ptid.
	(get_last_target_status): Handle nullptr arguments.
	(nullify_last_target_wait_ptid): Clear target_last_waitstatus.
	(print_stop_event): Don't pass a ptid to get_last_target_status.
	(normal_stop): Don't pass a ptid to get_last_target_status.
	* infrun.h (get_last_target_status, set_last_target_status): Move
	comments here and update.
	(nullify_last_target_wait_ptid): Declare.
	* linux-fork.c (fork_load_infrun_state): Remove local extern
	declaration of nullify_last_target_wait_ptid.
	* linux-nat.c (get_detach_signal): Don't pass a target_waitstatus
	to get_last_target_status.
---
 gdb/break-catch-sig.c     |  3 +--
 gdb/break-catch-syscall.c |  3 +--
 gdb/infcmd.c              |  9 ++-------
 gdb/infrun.c              | 28 +++++++++++++---------------
 gdb/infrun.h              |  9 +++++++++
 gdb/linux-fork.c          |  1 -
 gdb/linux-nat.c           |  3 +--
 7 files changed, 27 insertions(+), 29 deletions(-)

diff --git a/gdb/break-catch-sig.c b/gdb/break-catch-sig.c
index c475e8aca5..d1ea404d53 100644
--- a/gdb/break-catch-sig.c
+++ b/gdb/break-catch-sig.c
@@ -181,12 +181,11 @@ static enum print_stop_action
 signal_catchpoint_print_it (bpstat bs)
 {
   struct breakpoint *b = bs->breakpoint_at;
-  ptid_t ptid;
   struct target_waitstatus last;
   const char *signal_name;
   struct ui_out *uiout = current_uiout;
 
-  get_last_target_status (&ptid, &last);
+  get_last_target_status (nullptr, &last);
 
   signal_name = signal_to_name_or_int (last.value.sig);
 
diff --git a/gdb/break-catch-syscall.c b/gdb/break-catch-syscall.c
index dde80b54a2..35d406477c 100644
--- a/gdb/break-catch-syscall.c
+++ b/gdb/break-catch-syscall.c
@@ -182,12 +182,11 @@ print_it_catch_syscall (bpstat bs)
      syscall is.  It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
      or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
      must print "called syscall" or "returned from syscall".  */
-  ptid_t ptid;
   struct target_waitstatus last;
   struct syscall s;
   struct gdbarch *gdbarch = bs->bp_location_at->gdbarch;
 
-  get_last_target_status (&ptid, &last);
+  get_last_target_status (nullptr, &last);
 
   get_syscall_by_number (gdbarch, last.value.syscall_number, &s);
 
diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index ae1044b95a..75df3f3bce 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -856,9 +856,8 @@ continue_command (const char *args, int from_tty)
       else
 	{
 	  ptid_t last_ptid;
-	  struct target_waitstatus ws;
 
-	  get_last_target_status (&last_ptid, &ws);
+	  get_last_target_status (&last_ptid, nullptr);
 	  tp = find_thread_ptid (last_ptid);
 	}
       if (tp != NULL)
@@ -1983,11 +1982,7 @@ info_program_command (const char *args, int from_tty)
   if (non_stop)
     ptid = inferior_ptid;
   else
-    {
-      struct target_waitstatus ws;
-
-      get_last_target_status (&ptid, &ws);
-    }
+    get_last_target_status (&ptid, nullptr);
 
   if (ptid == null_ptid || ptid == minus_one_ptid)
     error (_("No selected thread."));
diff --git a/gdb/infrun.c b/gdb/infrun.c
index bd01d80857..a759c5c204 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -84,8 +84,6 @@ static void follow_inferior_reset_breakpoints (void);
 
 static int currently_stepping (struct thread_info *tp);
 
-void nullify_last_target_wait_ptid (void);
-
 static void insert_hp_step_resume_breakpoint_at_frame (struct frame_info *);
 
 static void insert_step_resume_breakpoint_at_caller (struct frame_info *);
@@ -3110,7 +3108,7 @@ init_wait_for_inferior (void)
 
   clear_proceed_status (0);
 
-  target_last_wait_ptid = minus_one_ptid;
+  nullify_last_target_wait_ptid ();
 
   previous_inferior_ptid = inferior_ptid;
 }
@@ -3854,7 +3852,7 @@ init_thread_stepping_state (struct thread_info *tss)
   tss->step_after_step_resume_breakpoint = 0;
 }
 
-/* Set the cached copy of the last ptid/waitstatus.  */
+/* See infrun.h.  */
 
 void
 set_last_target_status (ptid_t ptid, struct target_waitstatus status)
@@ -3863,22 +3861,24 @@ set_last_target_status (ptid_t ptid, struct target_waitstatus status)
   target_last_waitstatus = status;
 }
 
-/* Return the cached copy of the last pid/waitstatus returned by
-   target_wait()/deprecated_target_wait_hook().  The data is actually
-   cached by handle_inferior_event(), which gets called immediately
-   after target_wait()/deprecated_target_wait_hook().  */
+/* See infrun.h.  */
 
 void
-get_last_target_status (ptid_t *ptidp, struct target_waitstatus *status)
+get_last_target_status (ptid_t *ptid, struct target_waitstatus *status)
 {
-  *ptidp = target_last_wait_ptid;
-  *status = target_last_waitstatus;
+  if (ptid != nullptr)
+    *ptid = target_last_wait_ptid;
+  if (status != nullptr)
+    *status = target_last_waitstatus;
 }
 
+/* See infrun.h.  */
+
 void
 nullify_last_target_wait_ptid (void)
 {
   target_last_wait_ptid = minus_one_ptid;
+  target_last_waitstatus = {};
 }
 
 /* Switch thread contexts.  */
@@ -7835,10 +7835,9 @@ void
 print_stop_event (struct ui_out *uiout, bool displays)
 {
   struct target_waitstatus last;
-  ptid_t last_ptid;
   struct thread_info *tp;
 
-  get_last_target_status (&last_ptid, &last);
+  get_last_target_status (nullptr, &last);
 
   {
     scoped_restore save_uiout = make_scoped_restore (&current_uiout, uiout);
@@ -7957,9 +7956,8 @@ int
 normal_stop (void)
 {
   struct target_waitstatus last;
-  ptid_t last_ptid;
 
-  get_last_target_status (&last_ptid, &last);
+  get_last_target_status (nullptr, &last);
 
   new_stop_id ();
 
diff --git a/gdb/infrun.h b/gdb/infrun.h
index 2b2a3a3e44..e7cf2959cf 100644
--- a/gdb/infrun.h
+++ b/gdb/infrun.h
@@ -101,12 +101,21 @@ extern void wait_for_inferior (void);
    target, false otherwise.  */
 extern int normal_stop (void);
 
+/* Return the cached copy of the last ptid/waitstatus returned
+   by target_wait()/deprecated_target_wait_hook().  The data is
+   actually cached by handle_inferior_event(), which gets called
+   immediately after target_wait()/deprecated_target_wait_hook().  */
 extern void get_last_target_status (ptid_t *ptid,
 				    struct target_waitstatus *status);
 
+/* Set the cached copy of the last ptid/waitstatus.  */
 extern void set_last_target_status (ptid_t ptid,
 				    struct target_waitstatus status);
 
+/* Clear the cached copy of the last ptid/waitstatus returned by
+   target_wait().  */
+extern void nullify_last_target_wait_ptid ();
+
 /* Stop all threads.  Only returns after everything is halted.  */
 extern void stop_all_threads (void);
 
diff --git a/gdb/linux-fork.c b/gdb/linux-fork.c
index 87cfacc8e8..ab96be2f38 100644
--- a/gdb/linux-fork.c
+++ b/gdb/linux-fork.c
@@ -215,7 +215,6 @@ call_lseek (int fd, off_t offset, int whence)
 static void
 fork_load_infrun_state (struct fork_info *fp)
 {
-  extern void nullify_last_target_wait_ptid ();
   int i;
 
   linux_nat_switch_fork (fp->ptid);
diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index 0a8ea5b8de..35f850270b 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -1314,10 +1314,9 @@ get_detach_signal (struct lwp_info *lp)
 	}
       else if (!target_is_non_stop_p ())
 	{
-	  struct target_waitstatus last;
 	  ptid_t last_ptid;
 
-	  get_last_target_status (&last_ptid, &last);
+	  get_last_target_status (&last_ptid, nullptr);
 
 	  if (lp->ptid.lwp () == last_ptid.lwp ())
 	    signo = tp->suspend.stop_signal;
-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [PATCH v2 00/24] Multi-target support
@ 2019-10-17 22:50 Pedro Alves
  2019-10-17 22:50 ` [PATCH v2 12/24] Use all_non_exited_inferiors in infrun.c Pedro Alves
                   ` (27 more replies)
  0 siblings, 28 replies; 73+ messages in thread
From: Pedro Alves @ 2019-10-17 22:50 UTC (permalink / raw)
  To: gdb-patches

Here's v2 of the multi-target patchset, which addresses all the review
comments so far, I believe.  Patch 15 is new, so all following patches
are shifted by one.

This time, I've adjusted the host-specific nat files to function API
changes.  I tried to find spots that would need changes using grep.  I
built the series on AIX, x86/SPARC Solaris, 64-bit Windows, x86
GNU/Linux -m64/-m32, and Aarch64 GNU/Linux.  I'm currently running
this through the buildbot, will have results tomorrow.  I don't expect
any serious major issue, if any, as so far runs that completed seem
OK.

Follows the intro blurb:

This series adds multi-target support to GDB.  What this means is that
with this series, GDB can now be connected to different targets at the
same time.  E.g., you can debug a live native process and a core dump
at the same time, connect to multiple gdbservers, etc.

Patches 1 to 17 are preparatory patches.

Patch 18 is the actual multi-target patch.  This is the largest patch
in the series.  It does a number of things at the same time, but
they're all intertwined, so I gave up on splitting it further.

Patch 19 adds tests.  Split out because patch 18 is already too big as
is.

Patch 22 does some user-visible changes, including a new command to
list open target connections.  This is just the bare minimum I could
think of that is necessary for multi-target work.  I'm sure we'll find
other tweaks to other commands necessary.

Documentation is in patch 24.  Eli already reviewed and approved it.

I've pushed this to users/palves/multi-target-v2 on sourceware.

Pedro Alves (23):
  Preserve selected thread in all-stop w/ background execution
  Don't rely on inferior_ptid in record_full_wait
  Make "show remote exec-file" inferior-aware
  exceptions.c:print_flush: Remove obsolete check
  Make target_ops::has_execution take an 'inferior *' instead of a
    ptid_t
  Don't check target is running in remote_target::mourn_inferior
  Delete unnecessary code from kill_command
  Introduce switch_to_inferior_no_thread
  switch inferior/thread before calling target methods
  Some get_last_target_status tweaks
  tfile_target::close: trace_fd can't be -1
  Use all_non_exited_inferiors in infrun.c
  Delete exit_inferior_silent(int pid)
  Tweak handling of remote errors in response to resumption packet
  Fix reconnecting to a gdbserver already debugging multiple processes,
    I
  Fix reconnecting to a gdbserver already debugging multiple processes,
    II
  Multi-target support
  Add multi-target tests
  gdbarch-selftests.c: No longer error out if debugging something
  Revert 'Remove unused struct serial::name field'
  Add "info connections" command, "info inferiors" connection
    number/string
  Require always-non-stop for multi-target resumptions
  Multi-target: NEWS and user manual

Tankut Baris Aktemur (1):
  Avoid another inferior_ptid reference in gdb/remote.c

 gdb/doc/gdb.texinfo                                | 137 ++--
 gdb/doc/guile.texi                                 |   4 +-
 gdb/doc/python.texi                                |   6 +-
 gdb/NEWS                                           |  29 +
 gdb/Makefile.in                                    |   1 +
 gdb/aarch64-linux-nat.c                            |   2 +-
 gdb/ada-tasks.c                                    |   4 +-
 gdb/aix-thread.c                                   |  24 +-
 gdb/amd64-fbsd-tdep.c                              |   4 +-
 gdb/amd64-linux-nat.c                              |   2 +-
 gdb/break-catch-sig.c                              |   3 +-
 gdb/break-catch-syscall.c                          |   3 +-
 gdb/breakpoint.c                                   |  25 +-
 gdb/bsd-uthread.c                                  |  20 +-
 gdb/btrace.c                                       |   2 +-
 gdb/corelow.c                                      |  10 +-
 gdb/event-top.c                                    |  14 +-
 gdb/exceptions.c                                   |   6 +-
 gdb/exec.c                                         |  51 +-
 gdb/exec.h                                         |   7 +
 gdb/fbsd-tdep.c                                    |   3 +-
 gdb/fork-child.c                                   |   7 +-
 gdb/gdbarch-selftests.c                            |   5 -
 gdb/gdbserver/fork-child.c                         |   3 +-
 gdb/gdbserver/inferiors.c                          |   2 +-
 gdb/gdbserver/linux-low.c                          |   2 +-
 gdb/gdbserver/lynx-low.c                           |   2 +-
 gdb/gdbserver/nto-low.c                            |   2 +-
 gdb/gdbserver/remote-utils.c                       |   2 +-
 gdb/gdbserver/target.c                             |   8 +-
 gdb/gdbserver/target.h                             |  11 +-
 gdb/gdbserver/win32-low.c                          |   4 +-
 gdb/gdbsupport/common-gdbthread.h                  |   5 +-
 gdb/gdbthread.h                                    | 133 ++--
 gdb/i386-fbsd-tdep.c                               |   4 +-
 gdb/i386-linux-nat.c                               |   2 +-
 gdb/inf-child.c                                    |   2 +-
 gdb/inf-ptrace.c                                   |   6 +-
 gdb/infcall.c                                      |   3 +-
 gdb/infcmd.c                                       | 129 ++--
 gdb/inferior-iter.h                                |  76 ++-
 gdb/inferior.c                                     | 156 +++--
 gdb/inferior.h                                     |  71 +-
 gdb/infrun.c                                       | 720 ++++++++++++++++-----
 gdb/infrun.h                                       |  23 +-
 gdb/inline-frame.c                                 |  51 +-
 gdb/inline-frame.h                                 |  12 +-
 gdb/linux-fork.c                                   |   5 +-
 gdb/linux-nat.c                                    |  76 ++-
 gdb/linux-nat.h                                    |   1 +
 gdb/linux-tdep.c                                   |   3 +-
 gdb/linux-thread-db.c                              | 112 ++--
 gdb/mi/mi-interp.c                                 |  10 +-
 gdb/mi/mi-main.c                                   |   6 +-
 gdb/nat/fork-inferior.c                            |   8 +-
 gdb/nat/fork-inferior.h                            |   5 +-
 gdb/nto-procfs.c                                   |   2 +-
 gdb/ppc-fbsd-tdep.c                                |   4 +-
 gdb/proc-service.c                                 |  17 +-
 gdb/process-stratum-target.c                       |  12 +-
 gdb/process-stratum-target.h                       |  31 +-
 gdb/procfs.c                                       |  49 +-
 gdb/python/py-threadevent.c                        |   4 +-
 gdb/ravenscar-thread.c                             |  15 +-
 gdb/record-btrace.c                                |  41 +-
 gdb/record-full.c                                  |  22 +-
 gdb/regcache.c                                     | 162 +++--
 gdb/regcache.h                                     |  30 +-
 gdb/remote.c                                       | 307 +++++----
 gdb/riscv-fbsd-tdep.c                              |   4 +-
 gdb/serial.c                                       |   4 +
 gdb/serial.h                                       |   1 +
 gdb/sol-thread.c                                   |  28 +-
 gdb/sol2-tdep.c                                    |   2 +-
 gdb/solib-svr4.c                                   |   3 +-
 gdb/target-connection.c                            | 159 +++++
 gdb/target-connection.h                            |  40 ++
 gdb/target-delegates.c                             |  27 +
 gdb/target.c                                       | 192 +++---
 gdb/target.h                                       |  41 +-
 gdb/testsuite/gdb.base/fork-running-state.exp      |  17 +-
 .../gdb.base/kill-detach-inferiors-cmd.exp         |   4 +-
 gdb/testsuite/gdb.base/quit-live.exp               |   2 +-
 gdb/testsuite/gdb.base/remote-exec-file.exp        |  46 ++
 gdb/testsuite/gdb.guile/scm-progspace.exp          |   2 +-
 gdb/testsuite/gdb.linespec/linespec.exp            |   2 +-
 gdb/testsuite/gdb.mi/new-ui-mi-sync.exp            |   2 +-
 .../gdb.mi/user-selected-context-sync.exp          |   2 +-
 gdb/testsuite/gdb.multi/multi-target.c             | 100 +++
 gdb/testsuite/gdb.multi/multi-target.exp           | 387 +++++++++++
 gdb/testsuite/gdb.multi/remove-inferiors.exp       |   2 +-
 gdb/testsuite/gdb.multi/tids-gid-reset.c           |  22 +
 gdb/testsuite/gdb.multi/tids-gid-reset.exp         |  96 +++
 gdb/testsuite/gdb.multi/watchpoint-multi.exp       |   2 +-
 gdb/testsuite/gdb.python/py-inferior.exp           |   4 +-
 .../gdb.server/connect-without-multi-process.exp   |   7 +-
 .../gdb.server/extended-remote-restart.exp         |  22 +-
 gdb/testsuite/gdb.threads/async.c                  |  70 ++
 gdb/testsuite/gdb.threads/async.exp                |  98 +++
 gdb/testsuite/gdb.threads/fork-plus-threads.exp    |   2 +-
 .../forking-threads-plus-breakpoint.exp            |   2 +-
 gdb/testsuite/gdb.trace/report.exp                 |   2 +-
 gdb/testsuite/lib/gdbserver-support.exp            |   4 +
 gdb/thread-iter.c                                  |  14 +-
 gdb/thread-iter.h                                  |  25 +-
 gdb/thread.c                                       | 230 ++++---
 gdb/top.c                                          |  17 +-
 gdb/tracectf.c                                     |   2 +-
 gdb/tracefile-tfile.c                              |   5 +-
 gdb/tracefile.h                                    |   2 +-
 gdb/windows-nat.c                                  |  20 +-
 111 files changed, 3360 insertions(+), 1073 deletions(-)
 create mode 100644 gdb/target-connection.c
 create mode 100644 gdb/target-connection.h
 create mode 100644 gdb/testsuite/gdb.base/remote-exec-file.exp
 create mode 100644 gdb/testsuite/gdb.multi/multi-target.c
 create mode 100644 gdb/testsuite/gdb.multi/multi-target.exp
 create mode 100644 gdb/testsuite/gdb.multi/tids-gid-reset.c
 create mode 100644 gdb/testsuite/gdb.multi/tids-gid-reset.exp
 create mode 100644 gdb/testsuite/gdb.threads/async.c
 create mode 100644 gdb/testsuite/gdb.threads/async.exp

-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [PATCH v2 02/24] Don't rely on inferior_ptid in record_full_wait
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
                   ` (4 preceding siblings ...)
  2019-10-17 22:50 ` [PATCH v2 11/24] tfile_target::close: trace_fd can't be -1 Pedro Alves
@ 2019-10-17 22:50 ` Pedro Alves
  2019-11-01 14:54   ` Tom Tromey
  2019-10-17 22:50 ` [PATCH v2 06/24] Don't check target is running in remote_target::mourn_inferior Pedro Alves
                   ` (21 subsequent siblings)
  27 siblings, 1 reply; 73+ messages in thread
From: Pedro Alves @ 2019-10-17 22:50 UTC (permalink / raw)
  To: gdb-patches

The multi-target patch sets inferior_ptid to null_ptid before handling
a target event, and thus before calling target_wait, in order to catch
places in target_ops::wait implementations that are incorrectly
relying on inferior_ptid (which could otherwise be a ptid of a
different target, for example).  That caught this instance in
record-full.c.

Fix it by saving the last resumed ptid, and then using it in
record_full_wait_1, just like how the last "step" argument passed to
record_full_target::resume is handled too.

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

	* record-full.c (record_full_resume_ptid): New global.
	(record_full_target::resume): Set it.
	(record_full_wait_1): Use record_full_resume_ptid instead of
	inferior_ptid.
---
 gdb/record-full.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/gdb/record-full.c b/gdb/record-full.c
index 0c6cb62163..5168b0b61c 100644
--- a/gdb/record-full.c
+++ b/gdb/record-full.c
@@ -1036,6 +1036,9 @@ record_full_base_target::async (int enable)
   beneath ()->async (enable);
 }
 
+/* The PTID and STEP arguments last passed to
+   record_full_target::resume.  */
+static ptid_t record_full_resume_ptid = null_ptid;
 static int record_full_resume_step = 0;
 
 /* True if we've been resumed, and so each record_full_wait call should
@@ -1064,6 +1067,7 @@ static enum exec_direction_kind record_full_execution_dir = EXEC_FORWARD;
 void
 record_full_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
 {
+  record_full_resume_ptid = inferior_ptid;
   record_full_resume_step = step;
   record_full_resumed = 1;
   record_full_execution_dir = ::execution_direction;
@@ -1190,7 +1194,8 @@ record_full_wait_1 (struct target_ops *ops,
 	  /* This is not a single step.  */
 	  ptid_t ret;
 	  CORE_ADDR tmp_pc;
-	  struct gdbarch *gdbarch = target_thread_architecture (inferior_ptid);
+	  struct gdbarch *gdbarch
+	    = target_thread_architecture (record_full_resume_ptid);
 
 	  while (1)
 	    {
-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [PATCH v2 09/24] switch inferior/thread before calling target methods
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
                   ` (9 preceding siblings ...)
  2019-10-17 22:50 ` [PATCH v2 01/24] Preserve selected thread in all-stop w/ background execution Pedro Alves
@ 2019-10-17 22:50 ` Pedro Alves
  2019-10-17 22:50 ` [PATCH v2 15/24] Avoid another inferior_ptid reference in gdb/remote.c Pedro Alves
                   ` (16 subsequent siblings)
  27 siblings, 0 replies; 73+ messages in thread
From: Pedro Alves @ 2019-10-17 22:50 UTC (permalink / raw)
  To: gdb-patches

Once each inferior has its own target stack, we'll need to make sure
that the right inferior is selected before we call into target
methods.

It kind of sounds worse than it is in practice.  Not that many places
need to be concerned.

In thread.c, we add a new switch_to_thread_if_alive function that
centralizes the switching before calls to target_thread_alive.  Other
cases are handled with explicit switching.

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

	* gdbthread.h (scoped_restore_current_thread)
	<dont_restore, restore, m_dont_restore>: Declare.
	* thread.c (thread_alive): Add assertion.  Return bool.
	(switch_to_thread_if_alive): New.
	(prune_threads): Switch inferior/thread.
	(print_thread_info_1): Switch thread before calling target methods.
	(scoped_restore_current_thread::restore): New, factored out from
	...
	(scoped_restore_current_thread::~scoped_restore_current_thread):
	... this.
	(scoped_restore_current_thread::scoped_restore_current_thread):
	Add assertion.
	(thread_apply_all_command, thread_select): Use
	switch_to_thread_if_alive.
	* infrun.c (proceed, restart_threads, handle_signal_stop)
	(switch_back_to_stepped_thread): Switch current thread before
	calling target methods.
---
 gdb/infrun.c | 16 ++++++++++++--
 gdb/thread.c | 72 +++++++++++++++++++++++++++++++++++++++++++-----------------
 2 files changed, 66 insertions(+), 22 deletions(-)

diff --git a/gdb/infrun.c b/gdb/infrun.c
index 6da95e0584..bd01d80857 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -2947,6 +2947,8 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
     {
       for (thread_info *tp : all_non_exited_threads (resume_ptid))
 	{
+	  switch_to_thread_no_regs (tp);
+
 	  /* Ignore the current thread here.  It's handled
 	     afterwards.  */
 	  if (tp == cur_thr)
@@ -2964,6 +2966,8 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
 
 	  thread_step_over_chain_enqueue (tp);
 	}
+
+      switch_to_thread (cur_thr);
     }
 
   /* Enqueue the current thread last, so that we move all other
@@ -3000,6 +3004,8 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
 	   Start all other threads that are implicitly resumed too.  */
       for (thread_info *tp : all_non_exited_threads (resume_ptid))
         {
+	  switch_to_thread_no_regs (tp);
+
 	  if (tp->resumed)
 	    {
 	      if (debug_infrun)
@@ -4345,6 +4351,7 @@ stop_all_threads (void)
 					    "infrun:   %s executing, "
 					    "need stop\n",
 					    target_pid_to_str (t->ptid).c_str ());
+		      switch_to_thread_no_regs (t);
 		      target_stop (t->ptid);
 		      t->stop_requested = 1;
 		    }
@@ -5189,6 +5196,8 @@ restart_threads (struct thread_info *event_thread)
 
   for (thread_info *tp : all_non_exited_threads ())
     {
+      switch_to_thread_no_regs (tp);
+
       if (tp == event_thread)
 	{
 	  if (debug_infrun)
@@ -5447,9 +5456,8 @@ handle_signal_stop (struct execution_control_state *ecs)
     {
       struct regcache *regcache = get_thread_regcache (ecs->event_thread);
       struct gdbarch *reg_gdbarch = regcache->arch ();
-      scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
 
-      inferior_ptid = ecs->ptid;
+      switch_to_thread (ecs->event_thread);
 
       fprintf_unfiltered (gdb_stdlog, "infrun: stop_pc = %s\n",
 			  paddress (reg_gdbarch,
@@ -6883,6 +6891,8 @@ switch_back_to_stepped_thread (struct execution_control_state *ecs)
 
       for (thread_info *tp : all_non_exited_threads ())
         {
+	  switch_to_thread_no_regs (tp);
+
 	  /* Ignore threads of processes the caller is not
 	     resuming.  */
 	  if (!sched_multi
@@ -6934,6 +6944,8 @@ switch_back_to_stepped_thread (struct execution_control_state *ecs)
 	      return 1;
 	    }
 	}
+
+      switch_to_thread (ecs->event_thread);
     }
 
   return 0;
diff --git a/gdb/thread.c b/gdb/thread.c
index 2f9cd87ac8..ae6e6e5205 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -62,8 +62,6 @@ static int highest_thread_num;
    spawned new threads we haven't heard of yet.  */
 static int threads_executing;
 
-static int thread_alive (struct thread_info *);
-
 /* RAII type used to increase / decrease the refcount of each thread
    in a given list of threads.  */
 
@@ -679,14 +677,38 @@ any_live_thread_of_inferior (inferior *inf)
 }
 
 /* Return true if TP is an active thread.  */
-static int
-thread_alive (struct thread_info *tp)
+static bool
+thread_alive (thread_info *tp)
 {
   if (tp->state == THREAD_EXITED)
-    return 0;
-  if (!target_thread_alive (tp->ptid))
-    return 0;
-  return 1;
+    return false;
+
+  /* Ensure we're looking at the right target stack.  */
+  gdb_assert (tp->inf == current_inferior ());
+
+  return target_thread_alive (tp->ptid);
+}
+
+/* Switch to thread TP if it is alive.  Returns true if successfully
+   switched, false otherwise.  */
+
+static bool
+switch_to_thread_if_alive (thread_info *thr)
+{
+  scoped_restore_current_thread restore_thread;
+
+  /* Switch inferior first, so that we're looking at the right target
+     stack.  */
+  switch_to_inferior_no_thread (thr->inf);
+
+  if (thread_alive (thr))
+    {
+      switch_to_thread (thr);
+      restore_thread.dont_restore ();
+      return true;
+    }
+
+  return false;
 }
 
 /* See gdbthreads.h.  */
@@ -694,9 +716,15 @@ thread_alive (struct thread_info *tp)
 void
 prune_threads (void)
 {
+  scoped_restore_current_thread restore_thread;
+
   for (thread_info *tp : all_threads_safe ())
-    if (!thread_alive (tp))
-      delete_thread (tp);
+    {
+      switch_to_inferior_no_thread (tp->inf);
+
+      if (!thread_alive (tp))
+	delete_thread (tp);
+    }
 }
 
 /* See gdbthreads.h.  */
@@ -1037,6 +1065,9 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads,
     gdb::optional<ui_out_emit_list> list_emitter;
     gdb::optional<ui_out_emit_table> table_emitter;
 
+    /* We'll be switching threads temporarily below.  */
+    scoped_restore_current_thread restore_thread;
+
     if (uiout->is_mi_like_p ())
       list_emitter.emplace (uiout, "threads");
     else
@@ -1054,6 +1085,10 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads,
 
 	    if (!uiout->is_mi_like_p ())
 	      {
+		/* Switch inferiors so we're looking at the right
+		   target stack.  */
+		switch_to_inferior_no_thread (tp->inf);
+
 		target_id_col_width
 		  = std::max (target_id_col_width,
 			      thread_target_id_str (tp).size ());
@@ -1085,9 +1120,6 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads,
 	uiout->table_body ();
       }
 
-    /* We'll be switching threads temporarily.  */
-    scoped_restore_current_thread restore_thread;
-
     for (inferior *inf : all_inferiors ())
       for (thread_info *tp : inf->threads ())
       {
@@ -1116,6 +1148,9 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads,
 	if (show_global_ids || uiout->is_mi_like_p ())
 	  uiout->field_signed ("id", tp->global_num);
 
+	/* Switch to the thread (and inferior / target).  */
+	switch_to_thread (tp);
+
 	/* For the CLI, we stuff everything into the target-id field.
 	   This is a gross hack to make the output come out looking
 	   correct.  The underlying problem here is that ui-out has no
@@ -1147,9 +1182,8 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads,
 	  uiout->text ("(running)\n");
 	else
 	  {
-	    /* The switch below puts us at the top of the stack (leaf
+	    /* The switch above put us at the top of the stack (leaf
 	       frame).  */
-	    switch_to_thread (tp);
 	    print_stack_frame (get_selected_frame (NULL),
 			       /* For MI output, print frame level.  */
 			       uiout->is_mi_like_p (),
@@ -1662,7 +1696,7 @@ thread_apply_all_command (const char *cmd, int from_tty)
       scoped_restore_current_thread restore_thread;
 
       for (thread_info *thr : thr_list_cpy)
-	if (thread_alive (thr))
+	if (switch_to_thread_if_alive (thr))
 	  thr_try_catch_cmd (thr, cmd, from_tty, flags);
     }
 }
@@ -1819,7 +1853,7 @@ thread_apply_command (const char *tidlist, int from_tty)
 	  continue;
 	}
 
-      if (!thread_alive (tp))
+      if (!switch_to_thread_if_alive (tp))
 	{
 	  warning (_("Thread %s has terminated."), print_thread_id (tp));
 	  continue;
@@ -1983,11 +2017,9 @@ show_print_thread_events (struct ui_file *file, int from_tty,
 void
 thread_select (const char *tidstr, thread_info *tp)
 {
-  if (!thread_alive (tp))
+  if (!switch_to_thread_if_alive (tp))
     error (_("Thread ID %s has terminated."), tidstr);
 
-  switch_to_thread (tp);
-
   annotate_thread_changed ();
 
   /* Since the current thread may have changed, see if there is any
-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [PATCH v2 17/24] Fix reconnecting to a gdbserver already debugging multiple processes, II
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
                   ` (2 preceding siblings ...)
  2019-10-17 22:50 ` [PATCH v2 21/24] Revert 'Remove unused struct serial::name field' Pedro Alves
@ 2019-10-17 22:50 ` Pedro Alves
  2019-10-17 22:50 ` [PATCH v2 11/24] tfile_target::close: trace_fd can't be -1 Pedro Alves
                   ` (23 subsequent siblings)
  27 siblings, 0 replies; 73+ messages in thread
From: Pedro Alves @ 2019-10-17 22:50 UTC (permalink / raw)
  To: gdb-patches

Another bug exposed by gdb.server/extended-remote-restart.exp in the
multi-target work is that remote_target::start_remote can leave
inferior_ptid and current_inferior() out of sync:

 (top-gdb) p current_inferior_->pid
 $1 = 29541
 (top-gdb) p inferior_ptid
 $2 = {m_pid = 29540, m_lwp = 29540, m_tid = 0}

This is caused by writing to inferior_ptid directly instead of using
switch_to_thread.  Also, "inferior_list->thread_list->ptid" assumes
that we want the first thread of the first inferior, but that inferior
may not have threads, or with multi-target, that target may be
connected to some other target.

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

	* remote.c (remote_target::start_remote): Don't set inferior_ptid
	directly.  Instead find the first thread in the thread list and
	use switch_to_thread.
---
 gdb/remote.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/gdb/remote.c b/gdb/remote.c
index bc1054b192..ae8720a5f4 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -4707,8 +4707,8 @@ remote_target::start_remote (int from_tty, int extended_p)
 	     says should be current.  If we're reconnecting to a
 	     multi-threaded program, this will ideally be the thread
 	     that last reported an event before GDB disconnected.  */
-	  inferior_ptid = get_current_thread (wait_status);
-	  if (inferior_ptid == null_ptid)
+	  ptid_t curr_thread = get_current_thread (wait_status);
+	  if (curr_thread == null_ptid)
 	    {
 	      /* Odd... The target was able to list threads, but not
 		 tell us which thread was current (no "thread"
@@ -4720,8 +4720,14 @@ remote_target::start_remote (int from_tty, int extended_p)
 		                    "warning: couldn't determine remote "
 				    "current thread; picking first in list.\n");
 
-	      inferior_ptid = inferior_list->thread_list->ptid;
+	      for (thread_info *tp : all_non_exited_threads ())
+		{
+		  switch_to_thread (tp);
+		  break;
+		}
 	    }
+	  else
+	    switch_to_thread (find_thread_ptid (curr_thread));
 	}
 
       /* init_wait_for_inferior should be called before get_offsets in order
-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [PATCH v2 18/24] Multi-target support
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
                   ` (13 preceding siblings ...)
  2019-10-17 22:51 ` [PATCH v2 04/24] exceptions.c:print_flush: Remove obsolete check Pedro Alves
@ 2019-10-17 22:51 ` Pedro Alves
  2020-01-11  3:12   ` Simon Marchi
                     ` (5 more replies)
  2019-10-17 22:51 ` [PATCH v2 19/24] Add multi-target tests Pedro Alves
                   ` (12 subsequent siblings)
  27 siblings, 6 replies; 73+ messages in thread
From: Pedro Alves @ 2019-10-17 22:51 UTC (permalink / raw)
  To: gdb-patches

This commit adds multi-target support to GDB.  What this means is that
with this commit, GDB can now be connected to different targets at the
same time.  E.g., you can debug a live native process and a core dump
at the same time, connect to multiple gdbservers, etc.

Actually, the word "target" is overloaded in gdb.  We already have a
target stack, with pushes several target_ops instances on top of one
another.  We also have "info target" already, which means something
completely different to what this patch does.

So from here on, I'll be using the "target connections" term, to mean
an open process_stratum target, pushed on a target stack.  This patch
makes gdb have multiple target stacks, and multiple process_stratum
targets open simultaneously.  The user-visible changes / commands will
also use this terminology, but of course it's all open to debate.

User-interface-wise, not that much changes.  The main difference is
that each inferior may have its own target connection.

A target connection (e.g., a target extended-remote connection) may
support debugging multiple processes, just as before.

Say you're debugging against gdbserver in extended-remote mode, and
you do "add-inferior" to prepare to spawn a new process, like:

 (gdb) target extended-remote :9999
 ...
 (gdb) start
 ...
 (gdb) add-inferior
 Added inferior 2
 (gdb) inferior 2
 [Switching to inferior 2 [<null>] (<noexec>)]
 (gdb) file a.out
 ...
 (gdb) start
 ...

At this point, you have two inferiors connected to the same gdbserver.

With this commit, GDB will maintain a target stack per inferior,
instead of a global target stack.

To preserve the behavior above, by default, "add-inferior" makes the
new inferior inherit a copy of the target stack of the current
inferior.  Same across a fork - the child inherits a copy of the
target stack of the parent.  While the target stacks are copied, the
targets themselves are not.  Instead, target_ops is made a
refcounted_object, which means that target_ops instances are
refcounted, which each inferior counting for a reference.

What if you want to create an inferior and connect it to some _other_
target?  For that, this commit introduces a new "add-inferior
-no-connection" option that makes the new inferior not share the
current inferior's target.  So you could do:

 (gdb) target extended-remote :9999
 Remote debugging using :9999
 ...
 (gdb) add-inferior -no-connection
 [New inferior 2]
 Added inferior 2
 (gdb) inferior 2
 [Switching to inferior 2 [<null>] (<noexec>)]
 (gdb) info inferiors
   Num  Description       Executable
   1    process 18401     target:/home/pedro/tmp/main
 * 2    <null>
 (gdb) tar extended-remote :10000
 Remote debugging using :10000
 ...
 (gdb) info inferiors
   Num  Description       Executable
   1    process 18401     target:/home/pedro/tmp/main
 * 2    process 18450     target:/home/pedro/tmp/main
 (gdb)

A following patch will extended "info inferiors" to include a column
indicating which connection an inferior is bound to, along with a
couple other UI tweaks.

Other than that, debugging is the same as before.  Users interact with
inferiors and threads as before.  The only difference is that
inferiors may be bound to processes running in different machines.

That's pretty much all there is to it in terms of noticeable UI
changes.

On to implementation.

Since we can be connected to different systems at the same time, a
ptid_t is no longer a unique identifier.  Instead a thread can be
identified by a pair of ptid_t and 'process_stratum_target *', the
later being the instance of the process_stratum target that owns the
process/thread.  Note that process_stratum_target inherits from
target_ops, and all process_stratum targets inherit from
process_stratum_target.  In earlier patches, many places in gdb were
converted to refer to threads by thread_info pointer instead of
ptid_t, but there are still places in gdb where we start with a
pid/tid and need to find the corresponding inferior or thread_info
objects.  So you'll see in the patch many places adding a
process_stratum_target parameter to functions that used to take only a
ptid_t.

Since each inferior has its own target stack now, we can always find
the process_stratum target for an inferior.  That is done via a
inf->process_target() convenience method.

Since each inferior has its own target stack, we need to handle the
"beneath" calls when servicing target calls.  The solution I settled
with is just to make sure to switch the current inferior to the
inferior you want before making a target call.  Not relying on global
context is just not feasible in current GDB.  Fortunately, there
aren't that many places that need to do that, because generally most
code that calls target methods already has the current context
pointing to the right inferior/thread.  Note, to emphasize -- there's
no method to "switch to this target stack".  Instead, you switch the
current inferior, and that implicitly switches the target stack.

In some spots, we need to iterate over all inferiors so that we reach
all target stacks.

Native targets are still singletons.  There's always only a single
instance of such targets.

Remote targets however, we'll have one instance per remote connection.

The exec target is still a singleton.  There's only one instance.  I
did not see the point of instanciating more than one exec_target
object.

After vfork, we need to make sure to push the exec target on the new
inferior.  See exec_on_vfork.

For type safety, functions that need a {target, ptid} pair to identify
a thread, take a process_stratum_target pointer for target parameter
instead of target_ops *.  Some shared code in gdb/nat/ also need to
gain a target pointer parameter.  This poses an issue, since gdbserver
doesn't have process_stratum_target, only target_ops.  To fix this,
this commit renames gdbserver's target_ops to process_stratum_target.
I think this makes sense.  There's no concept of target stack in
gdbserver, and gdbserver's target_ops really implements a
process_stratum-like target.

The thread and inferior iterator functions also gain
process_stratum_target parameters.  These are used to be able to
iterate over threads and inferiors of a given target.  Following usual
conventions, if the target pointer is null, then we iterate over
threads and inferiors of all targets.

I tried converting "add-inferior" to the gdb::option framework, as a
preparatory patch, but that stumbled on the fact that gdb::option does
not support file options yet, for "add-inferior -exec".  I have a WIP
patchset that adds that, but it's not a trivial patch, mainly due to
need to integrate readline's filename completion, so I deferred that
to some other time.

In infrun.c/infcmd.c, the main change is that we need to poll events
out of all targets.  See do_target_wait.  Right after collecting an
event, we switch the current inferior to an inferior bound to the
target that reported the event, so that target methods can be used
while handling the event.  This makes most of the code transparent to
multi-targets.  See fetch_inferior_event.

infrun.c:stop_all_threads is interesting -- in this function we need
to stop all threads of all targets.  What the function does is send an
asynchronous stop request to all threads, and then synchronously waits
for events, with target_wait, rinse repeat, until all it finds are
stopped threads.  Now that we have multiple targets, it's not
efficient to synchronously block in target_wait waiting for events out
of one target.  Instead, we implement a mini event loop, with
interruptible_select, select'ing on one file descriptor per target.
For this to work, we need to be able to ask the target for a waitable
file descriptor.  Such file descriptors already exist, they are the
descriptors registered in the main event loop with add_file_handler,
inside the target_async implementations.  This commit adds a new
target_async_wait_fd target method that just returns the file
descriptor in question.  See wait_one / stop_all_threads in infrun.c.

The 'threads_executing' global is made a per-target variable.  Since
it is only relevant to process_stratum_target targets, this is where
it is put, instead of in target_ops.

You'll notice that remote.c includes some FIXME notes.  These refer to
the fact that the global arrays that hold data for the remote packets
supported are still globals.  For example, if we connect to two
different servers/stubs, then each might support different remote
protocol features.  They might even be different architectures, like
e.g., one ARM baremetal stub, and a x86 gdbserver, to debug a
host/controller scenario as a single program.  That isn't going to
work correctly today, because of said globals.  I'm leaving fixing
that for another pass, since it does not appear to be trivial, and I'd
rather land the base work first.  It's already useful to be able to
debug multiple instances of the same server (e.g., a distributed
cluster, where you have full control over the servers installed), so I
think as is it's already reasonable incremental progress.

Current limitations:

 - You can only resume more that one target at the same time if all
   targets support asynchronous debugging, and support non-stop mode.
   It should be possible to support mixed all-stop + non-stop
   backends, but that is left for another time.  This means that
   currently in order to do multi-target with gdbserver you need to
   issue "maint set target-non-stop on".  I would like to make that
   mode be the default, but we're not there yet.  Note that I'm
   talking about how the target backend works, only.  User-visible
   all-stop mode works just fine.

 - As explained above, connecting to different remote servers at the
   same time is likely to produce bad results if they don't support the
   exact set of RSP features.

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

	* aarch64-linux-nat.c
	(aarch64_linux_nat_target::thread_architecture): Adjust.
	* ada-tasks.c (print_ada_task_info): Adjust find_thread_ptid call.
	(task_command_1): Likewise.
	* aix-thread.c (sync_threadlists, aix_thread_target::resume)
	(aix_thread_target::wait, aix_thread_target::fetch_registers)
	(aix_thread_target::store_registers)
	(aix_thread_target::thread_alive): Adjust.
	* amd64-fbsd-tdep.c: Include "inferior.h".
	(amd64fbsd_get_thread_local_address): Pass down target.
	* amd64-linux-nat.c (ps_get_thread_area): Use ps_prochandle
	thread's gdbarch instead of target_gdbarch.
	* break-catch-sig.c (signal_catchpoint_print_it): Adjust call to
	get_last_target_status.
	* break-catch-syscall.c (print_it_catch_syscall): Likewise.
	* breakpoint.c (breakpoints_should_be_inserted_now): Consider all
	inferiors.
	(update_inserted_breakpoint_locations): Skip if inferiors with no
	execution.
	(update_global_location_list): When handling moribund locations,
	find representative inferior for location's pspace, and use thread
	count of its process_stratum target.
	* bsd-uthread.c (bsd_uthread_target::wait): Use
	as_process_stratum_target and adjust thread_change_ptid and
	add_thread calls.
	(bsd_uthread_target::update_thread_list): Use
	as_process_stratum_target and adjust find_thread_ptid,
	thread_change_ptid and add_thread calls.
	* btrace.c (maint_btrace_packet_history_cmd): Adjust
	find_thread_ptid call.
	* corelow.c (add_to_thread_list): Adjust add_thread call.
	(core_target_open): Adjust add_thread_silent and thread_count
	calls.
	(core_target::pid_to_str): Adjust find_inferior_ptid call.
	* ctf.c (ctf_target_open): Adjust add_thread_silent call.
	* event-top.c (async_disconnect): Pop targets from all inferiors.
	* exec.c (add_target_sections): Push exec target on all inferiors
	sharing the program space.
	(remove_target_sections): Remove the exec target from all
	inferiors sharing the program space.
	(exec_on_vfork): New.
	* exec.h (exec_on_vfork): Declare.
	* fbsd-tdep.c (fbsd_corefile_thread): Adjust
	get_thread_arch_regcache call.
	* fork-child.c (gdb_startup_inferior): Pass target down to
	startup_inferior and set_executing.
	* gdbthread.h (struct process_stratum_target): Forward declare.
	(add_thread, add_thread_silent, add_thread_with_info)
	(in_thread_list): Add process_stratum_target parameter.
	(find_thread_ptid(inferior*, ptid_t)): New overload.
	(find_thread_ptid, thread_change_ptid): Add process_stratum_target
	parameter.
	(all_threads()): Delete overload.
	(all_threads, all_non_exited_threads): Add process_stratum_target
	parameter.
	(all_threads_safe): Use brace initialization.
	(thread_count): Add process_stratum_target parameter.
	(set_resumed, set_running, set_stop_requested, set_executing)
	(threads_are_executing, finish_thread_state): Add
	process_stratum_target parameter.
	(switch_to_thread): Use is_current_thread.
	* i386-fbsd-tdep.c: Include "inferior.h".
	(i386fbsd_get_thread_local_address): Pass down target.
	* i386-linux-nat.c (i386_linux_nat_target::low_resume): Adjust.
	* inf-child.c (inf_child_target::maybe_unpush_target): Remove
	have_inferiors check.
	* inf-ptrace.c (inf_ptrace_target::create_inferior)
	(inf_ptrace_target::attach): Adjust.
	* infcall.c (run_inferior_call): Adjust.
	* infcmd.c (run_command_1): Pass target to
	scoped_finish_thread_state.
	(proceed_thread_callback): Skip inferiors with no execution.
	(continue_command): Rename 'all_threads' local to avoid hiding
	'all_threads' function.  Adjust get_last_target_status call.
	(prepare_one_step): Adjust set_running call.
	(signal_command): Use user_visible_resume_target.  Compare thread
	pointers instead of inferior_ptid.
	(info_program_command): Adjust to pass down target.
	(attach_command): Mark target's 'thread_executing' flag.
	(stop_current_target_threads_ns): New, factored out from ...
	(interrupt_target_1): ... this.  Switch inferior before making
	target calls.
	* inferior-iter.h
	(struct all_inferiors_iterator, struct all_inferiors_range)
	(struct all_inferiors_safe_range)
	(struct all_non_exited_inferiors_range): Filter on
	process_stratum_target too.  Remove explicit.
	* inferior.c (inferior::inferior): Push dummy target on target
	stack.
	(find_inferior_pid, find_inferior_ptid, number_of_live_inferiors):
	Add process_stratum_target parameter, and pass it down.
	(have_live_inferiors): Adjust.
	(switch_to_inferior_and_push_target): New.
	(add_inferior_command, clone_inferior_command): Handle
	"-no-connection" parameter.  Use
	switch_to_inferior_and_push_target.
	(_initialize_inferior): Mention "-no-connection" option in
	the help of "add-inferior" and "clone-inferior" commands.
	* inferior.h: Include "process-stratum-target.h".
	(interrupt_target_1): Use bool.
	(struct inferior) <push_target, unpush_target, target_is_pushed,
	find_target_beneath, top_target, process_target, target_at,
	m_stack>: New.
	(discard_all_inferiors): Delete.
	(find_inferior_pid, find_inferior_ptid, number_of_live_inferiors)
	(all_inferiors, all_non_exited_inferiors): Add
	process_stratum_target parameter.
	* infrun.c: Include "gdb_select.h" and <unordered_map>.
	(target_last_proc_target): New global.
	(follow_fork_inferior): Push target on new inferior.  Pass target
	to add_thread_silent.  Call exec_on_vfork.  Handle target's
	reference count.
	(follow_fork): Adjust get_last_target_status call.  Also consider
	target.
	(follow_exec): Push target on new inferior.
	(struct execution_control_state) <target>: New field.
	(user_visible_resume_target): New.
	(do_target_resume): Call target_async.
	(resume_1): Set target's threads_executing flag.  Consider resume
	target.
	(commit_resume_all_targets): New.
	(proceed): Also consider resume target.  Skip threads of inferiors
	with no execution.  Commit resumtion in all targets.
	(start_remote): Pass current inferior to wait_for_inferior.
	(infrun_thread_stop_requested): Consider target as well.  Pass
	thread_info pointer to clear_inline_frame_state instead of ptid.
	(infrun_thread_thread_exit): Consider target as well.
	(random_pending_event_thread): New inferior parameter.  Use it.
	(do_target_wait): Rename to ...
	(do_target_wait_1): ... this.  Add inferior parameter, and pass it
	down.
	(threads_are_resumed_pending_p, do_target_wait): New.
	(prepare_for_detach): Adjust calls.
	(wait_for_inferior): New inferior parameter.  Handle it.  Use
	do_target_wait_1 instead of do_target_wait.
	(fetch_inferior_event): Adjust.  Switch to representative
	inferior.  Pass target down.
	(set_last_target_status): Add process_stratum_target parameter.
	Save target in global.
	(get_last_target_status): Add process_stratum_target parameter and
	handle it.
	(nullify_last_target_wait_ptid): Clear 'target_last_proc_target'.
	(context_switch): Check inferior_ptid == null_ptid before calling
	inferior_thread().
	(get_inferior_stop_soon): Pass down target.
	(wait_one): Rename to ...
	(poll_one_curr_target): ... this.
	(struct wait_one_event): New.
	(wait_one): New.
	(stop_all_threads): Adjust.
	(handle_no_resumed, handle_inferior_event): Adjust to consider the
	event's target.
	(switch_back_to_stepped_thread): Also consider target.
	(print_stop_event): Update.
	(normal_stop): Update.  Also consider the resume target.
	* infrun.h (wait_for_inferior): Remove declaration.
	(user_visible_resume_target): New declaration.
	(get_last_target_status, set_last_target_status): New
	process_stratum_target parameter.
	* inline-frame.c (clear_inline_frame_state(ptid_t)): Add
	process_stratum_target parameter, and use it.
	(clear_inline_frame_state (thread_info*)): New.
	* inline-frame.c (clear_inline_frame_state(ptid_t)): Add
	process_stratum_target parameter.
	(clear_inline_frame_state (thread_info*)): Declare.
	* linux-fork.c (delete_checkpoint_command): Pass target down to
	find_thread_ptid.
	(checkpoint_command): Adjust.
	* linux-nat.c (linux_nat_target::follow_fork): Switch to thread
	instead of just tweaking inferior_ptid.
	(linux_nat_switch_fork): Pass target down to thread_change_ptid.
	(exit_lwp): Pass target down to find_thread_ptid.
	(attach_proc_task_lwp_callback): Pass target down to
	add_thread/set_running/set_executing.
	(linux_nat_target::attach): Pass target down to
	thread_change_ptid.
	(get_detach_signal): Pass target down to find_thread_ptid.
	Consider last target status's target.
	(linux_resume_one_lwp_throw, resume_lwp)
	(linux_handle_syscall_trap, linux_handle_extended_wait, wait_lwp)
	(stop_wait_callback, save_stop_reason, linux_nat_filter_event)
	(linux_nat_wait_1, resume_stopped_resumed_lwps): Pass target down.
	(linux_nat_target::async_wait_fd): New.
	(linux_nat_stop_lwp, linux_nat_target::thread_address_space): Pass
	target down.
	* linux-nat.h (linux_nat_target::async_wait_fd): Declare.
	* linux-tdep.c (get_thread_arch_regcache): Pass target down.
	* linux-thread-db.c (struct thread_db_info::process_target): New
	field.
	(add_thread_db_info): Save target.
	(get_thread_db_info): New process_stratum_target parameter.  Also
	match target.
	(delete_thread_db_info): New process_stratum_target parameter.
	Also match target.
	(thread_from_lwp): Adjust to pass down target.
	(thread_db_notice_clone): Pass down target.
	(check_thread_db_callback): Pass down target.
	(try_thread_db_load_1): Always push the thread_db target.
	(try_thread_db_load, record_thread): Pass target down.
	(thread_db_target::detach): Pass target down.  Always unpush the
	thread_db target.
	(thread_db_target::wait, thread_db_target::mourn_inferior): Pass
	target down.  Always unpush the thread_db target.
	(find_new_threads_callback, thread_db_find_new_threads_2)
	(thread_db_target::update_thread_list): Pass target down.
	(thread_db_target::pid_to_str): Pass current inferior down.
	(thread_db_target::get_thread_local_address): Pass target down.
	(thread_db_target::resume, maintenance_check_libthread_db): Pass
	target down.
	* nto-procfs.c (nto_procfs_target::update_thread_list): Adjust.
	* procfs.c (procfs_target::procfs_init_inferior): Declare.
	(proc_set_current_signal, do_attach, procfs_target::wait): Adjust.
	(procfs_init_inferior): Rename to ...
	(procfs_target::procfs_init_inferior): ... this and adjust.
	(procfs_target::create_inferior, procfs_notice_thread)
	(procfs_do_thread_registers): Adjust.
	* ppc-fbsd-tdep.c: Include "inferior.h".
	(ppcfbsd_get_thread_local_address): Pass down target.
	* proc-service.c (ps_xfer_memory): Switch current inferior and
	program space as well.
	(get_ps_regcache): Pass target down.
	* process-stratum-target.c
	(process_stratum_target::thread_address_space)
	(process_stratum_target::thread_architecture): Pass target down.
	* process-stratum-target.h
	(process_stratum_target::threads_executing): New field.
	(as_process_stratum_target): New.
	* ravenscar-thread.c
	(ravenscar_thread_target::update_inferior_ptid): Pass target down.
	(ravenscar_thread_target::wait, ravenscar_add_thread): Pass target
	down.
	* record-btrace.c (record_btrace_target::info_record): Adjust.
	(record_btrace_target::record_method)
	(record_btrace_target::record_is_replaying)
	(record_btrace_target::fetch_registers)
	(get_thread_current_frame_id, record_btrace_target::resume)
	(record_btrace_target::wait, record_btrace_target::stop): Pass
	target down.
	* record-full.c (record_full_wait_1): Switch to event thread.
	Pass target down.
	* regcache.c (regcache::regcache)
	(get_thread_arch_aspace_regcache, get_thread_arch_regcache): Add
	process_stratum_target parameter and handle it.
	(current_thread_target): New global.
	(get_thread_regcache): Add process_stratum_target parameter and
	handle it.  Switch inferior before calling target method.
	(get_thread_regcache): Pass target down.
	(get_thread_regcache_for_ptid): Pass target down.
	(registers_changed_ptid): Add process_stratum_target parameter and
	handle it.
	(registers_changed_thread, registers_changed): Pass target down.
	(test_get_thread_arch_aspace_regcache): New.
	(current_regcache_test): Define a couple local test_target_ops
	instances and use them for testing.
	(readwrite_regcache): Pass process_stratum_target parameter.
	(cooked_read_test, cooked_write_test): Pass mock_target down.
	* regcache.h (get_thread_regcache, get_thread_arch_regcache)
	(get_thread_arch_aspace_regcache): Add process_stratum_target
	parameter.
	(regcache::target): New method.
	(regcache::regcache, regcache::get_thread_arch_aspace_regcache)
	(regcache::registers_changed_ptid): Add process_stratum_target
	parameter.
	(regcache::m_target): New field.
	(registers_changed_ptid): Add process_stratum_target parameter.
	* remote.c (remote_state::supports_vCont_probed): New field.
	(remote_target::async_wait_fd): New method.
	(remote_unpush_and_throw): Add remote_target parameter.
	(get_current_remote_target): Adjust.
	(remote_target::remote_add_inferior): Push target.
	(remote_target::remote_add_thread)
	(remote_target::remote_notice_new_inferior)
	(get_remote_thread_info): Pass target down.
	(remote_target::update_thread_list): Skip threads of inferiors
	bound to other targets.  (remote_target::close): Don't discard
	inferiors.  (remote_target::add_current_inferior_and_thread)
	(remote_target::process_initial_stop_replies)
	(remote_target::start_remote)
	(remote_target::remote_serial_quit_handler): Pass down target.
	(remote_target::remote_unpush_target): New remote_target
	parameter.  Unpush the target from all inferiors.
	(remote_target::remote_unpush_and_throw): New remote_target
	parameter.  Pass it down.
	(remote_target::open_1): Check whether the current inferior has
	execution instead of checking whether any inferior is live.  Pass
	target down.
	(remote_target::remote_detach_1): Pass down target.  Use
	remote_unpush_target.
	(extended_remote_target::attach): Pass down target.
	(remote_target::remote_vcont_probe): Set supports_vCont_probed.
	(remote_target::append_resumption): Pass down target.
	(remote_target::append_pending_thread_resumptions)
	(remote_target::remote_resume_with_hc, remote_target::resume)
	(remote_target::commit_resume): Pass down target.
	(remote_target::remote_stop_ns): Check supports_vCont_probed.
	(remote_target::interrupt_query)
	(remote_target::remove_new_fork_children)
	(remote_target::check_pending_events_prevent_wildcard_vcont)
	(remote_target::remote_parse_stop_reply)
	(remote_target::process_stop_reply): Pass down target.
	(first_remote_resumed_thread): New remote_target parameter.  Pass
	it down.
	(remote_target::wait_as): Pass down target.
	(unpush_and_perror): New remote_target parameter.  Pass it down.
	(remote_target::readchar, remote_target::remote_serial_write)
	(remote_target::getpkt_or_notif_sane_1)
	(remote_target::kill_new_fork_children, remote_target::kill): Pass
	down target.
	(remote_target::mourn_inferior): Pass down target.  Use
	remote_unpush_target.
	(remote_target::core_of_thread)
	(remote_target::remote_btrace_maybe_reopen): Pass down target.
	(remote_target::pid_to_exec_file)
	(remote_target::thread_handle_to_thread_info): Pass down target.
	(remote_target::async_wait_fd): New.
	* riscv-fbsd-tdep.c: Include "inferior.h".
	(riscv_fbsd_get_thread_local_address): Pass down target.
	* sol2-tdep.c (sol2_core_pid_to_str): Pass down target.
	* sol-thread.c (sol_thread_target::wait, ps_lgetregs, ps_lsetregs)
	(ps_lgetfpregs, ps_lsetfpregs, sol_update_thread_list_callback):
	Adjust.
	* solib-spu.c (spu_skip_standalone_loader): Pass down target.
	* solib-svr4.c (enable_break): Pass down target.
	* spu-multiarch.c (parse_spufs_run): Pass down target.
	* spu-tdep.c (spu2ppu_sniffer): Pass down target.
	* target-delegates.c: Regenerate.
	* target.c (g_target_stack): Delete.
	(current_top_target): Return the current inferior's top target.
	(target_has_execution_1): Refer to the passed-in inferior's top
	target.
	(target_supports_terminal_ours): Check whether the initial
	inferior was already created.
	(decref_target): New.
	(target_stack::push): Incref/decref the target.
	(push_target, push_target, unpush_target): Adjust.
	(target_stack::unpush): Defref target.
	(target_is_pushed): Return bool.  Adjust to refer to the current
	inferior's target stack.
	(dispose_inferior): Delete, and inline parts ...
	(target_preopen): ... here.  Only dispose of the current inferior.
	(target_detach): Hold strong target reference while detaching.
	Pass target down.
	(target_thread_name): Add assertion.
	(target_resume): Pass down target.
	(target_ops::beneath, find_target_at): Adjust to refer to the
	current inferior's target stack.
	(get_dummy_target): New.
	(target_pass_ctrlc): Pass the Ctrl-C to the first inferior that
	has a thread running.
	(initialize_targets): Rename to ...
	(_initialize_target): ... this.
	* target.h: Include "gdbsupport/refcounted-object.h".
	(struct target_ops): Inherit refcounted_object.
	(target_ops::shortname, target_ops::longname): Make const.
	(target_ops::async_wait_fd): New method.
	(decref_target): Declare.
	(struct target_ops_ref_policy): New.
	(target_ops_ref): New typedef.
	(get_dummy_target): Declare function.
	(target_is_pushed): Return bool.
	* thread-iter.c (all_matching_threads_iterator::m_inf_matches)
	(all_matching_threads_iterator::all_matching_threads_iterator):
	Handle filter target.
	* thread-iter.h (struct all_matching_threads_iterator, struct
	all_matching_threads_range, class all_non_exited_threads_range):
	Filter by target too.  Remove explicit.
	* thread.c (threads_executing): Delete.
	(inferior_thread): Pass down current inferior.
	(clear_thread_inferior_resources): Pass down thread pointer
	instead of ptid_t.
	(add_thread_silent, add_thread_with_info, add_thread): Add
	process_stratum_target parameter.  Use it for thread and inferior
	searches.
	(is_current_thread): New.
	(thread_info::deletable): Use it.
	(find_thread_ptid, thread_count, in_thread_list)
	(thread_change_ptid, set_resumed, set_running): New
	process_stratum_target parameter.  Pass it down.
	(set_executing): New process_stratum_target parameter.  Pass it
	down.  Adjust reference to 'threads_executing'.
	(threads_are_executing): New process_stratum_target parameter.
	Adjust reference to 'threads_executing'.
	(set_stop_requested, finish_thread_state): New
	process_stratum_target parameter.  Pass it down.
	(switch_to_thread): Also match inferior.
	(switch_to_thread): New process_stratum_target parameter.  Pass it
	down.
	(update_threads_executing): Reimplement.
	* top.c (quit_force): Pop targets from all inferior.
	(gdb_init): Don't call initialize_targets.
	* windows-nat.c (windows_nat_target) <get_windows_debug_event>:
	Declare.
	(windows_add_thread, windows_delete_thread): Adjust.
	(get_windows_debug_event): Rename to ...
	(windows_nat_target::get_windows_debug_event): ... this.  Adjust.
	* tracefile-tfile.c (tfile_target_open): Pass down target.
	* gdbsupport/common-gdbthread.h (struct process_stratum_target):
	Forward declare.
	(switch_to_thread): Add process_stratum_target parameter.
	* mi/mi-interp.c (mi_on_resume_1): Add process_stratum_target
	parameter.  Use it.
	(mi_on_resume): Pass target down.
	* nat/fork-inferior.c (startup_inferior): Add
	process_stratum_target parameter.  Pass it down.
	* nat/fork-inferior.h (startup_inferior): Add
	process_stratum_target parameter.
	* python/py-threadevent.c (py_get_event_thread): Pass target down.

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

	* fork-child.c (post_fork_inferior): Pass target down to
	startup_inferior.
	* inferiors.c (switch_to_thread): Add process_stratum_target
	parameter.
	* lynx-low.c (lynx_target_ops): Now a process_stratum_target.
	* nto-low.c (nto_target_ops): Now a process_stratum_target.
	* linux-low.c (linux_target_ops): Now a process_stratum_target.
	* remote-utils.c (prepare_resume_reply): Pass the target to
	switch_to_thread.
	* target.c (the_target): Now a process_stratum_target.
	(done_accessing_memory): Pass the target to switch_to_thread.
	(set_target_ops): Ajust to use process_stratum_target.
	* target.h (struct target_ops): Rename to ...
	(struct process_stratum_target): ... this.
	(the_target, set_target_ops): Adjust.
	(prepare_to_access_memory): Adjust comment.
	* win32-low.c (child_xfer_memory): Adjust to use
	process_stratum_target.
	(win32_target_ops): Now a process_stratum_target.
---
 gdb/aarch64-linux-nat.c           |   2 +-
 gdb/ada-tasks.c                   |   4 +-
 gdb/aix-thread.c                  |  24 +-
 gdb/amd64-fbsd-tdep.c             |   4 +-
 gdb/amd64-linux-nat.c             |   2 +-
 gdb/break-catch-sig.c             |   2 +-
 gdb/break-catch-syscall.c         |   2 +-
 gdb/breakpoint.c                  |  25 +-
 gdb/bsd-uthread.c                 |  20 +-
 gdb/btrace.c                      |   2 +-
 gdb/corelow.c                     |   8 +-
 gdb/event-top.c                   |  14 +-
 gdb/exec.c                        |  51 +++-
 gdb/exec.h                        |   7 +
 gdb/fbsd-tdep.c                   |   3 +-
 gdb/fork-child.c                  |   7 +-
 gdb/gdbserver/fork-child.c        |   3 +-
 gdb/gdbserver/inferiors.c         |   2 +-
 gdb/gdbserver/linux-low.c         |   2 +-
 gdb/gdbserver/lynx-low.c          |   2 +-
 gdb/gdbserver/nto-low.c           |   2 +-
 gdb/gdbserver/remote-utils.c      |   2 +-
 gdb/gdbserver/target.c            |   8 +-
 gdb/gdbserver/target.h            |  11 +-
 gdb/gdbserver/win32-low.c         |   4 +-
 gdb/gdbsupport/common-gdbthread.h |   5 +-
 gdb/gdbthread.h                   | 127 ++++----
 gdb/i386-fbsd-tdep.c              |   4 +-
 gdb/i386-linux-nat.c              |   2 +-
 gdb/inf-child.c                   |   2 +-
 gdb/inf-ptrace.c                  |   6 +-
 gdb/infcall.c                     |   3 +-
 gdb/infcmd.c                      | 112 ++++---
 gdb/inferior-iter.h               |  76 ++++-
 gdb/inferior.c                    |  78 +++--
 gdb/inferior.h                    |  64 +++-
 gdb/infrun.c                      | 597 +++++++++++++++++++++++++++++---------
 gdb/infrun.h                      |  18 +-
 gdb/inline-frame.c                |  51 ++--
 gdb/inline-frame.h                |  12 +-
 gdb/linux-fork.c                  |   4 +-
 gdb/linux-nat.c                   |  75 +++--
 gdb/linux-nat.h                   |   1 +
 gdb/linux-tdep.c                  |   3 +-
 gdb/linux-thread-db.c             | 110 +++----
 gdb/mi/mi-interp.c                |  10 +-
 gdb/nat/fork-inferior.c           |   8 +-
 gdb/nat/fork-inferior.h           |   5 +-
 gdb/nto-procfs.c                  |   2 +-
 gdb/ppc-fbsd-tdep.c               |   4 +-
 gdb/proc-service.c                |  17 +-
 gdb/process-stratum-target.c      |   4 +-
 gdb/process-stratum-target.h      |  16 +
 gdb/procfs.c                      |  49 ++--
 gdb/python/py-threadevent.c       |   4 +-
 gdb/ravenscar-thread.c            |  15 +-
 gdb/record-btrace.c               |  41 ++-
 gdb/record-full.c                 |  11 +-
 gdb/regcache.c                    | 162 +++++++----
 gdb/regcache.h                    |  30 +-
 gdb/remote.c                      | 221 ++++++++------
 gdb/riscv-fbsd-tdep.c             |   4 +-
 gdb/sol-thread.c                  |  28 +-
 gdb/sol2-tdep.c                   |   2 +-
 gdb/solib-svr4.c                  |   3 +-
 gdb/target-delegates.c            |  27 ++
 gdb/target.c                      | 172 ++++++-----
 gdb/target.h                      |  34 ++-
 gdb/thread-iter.c                 |  14 +-
 gdb/thread-iter.h                 |  25 +-
 gdb/thread.c                      | 139 +++++----
 gdb/top.c                         |  17 +-
 gdb/tracectf.c                    |   2 +-
 gdb/tracefile-tfile.c             |   2 +-
 gdb/windows-nat.c                 |  20 +-
 75 files changed, 1801 insertions(+), 850 deletions(-)

diff --git a/gdb/aarch64-linux-nat.c b/gdb/aarch64-linux-nat.c
index 2c1f4d9f98..d984680037 100644
--- a/gdb/aarch64-linux-nat.c
+++ b/gdb/aarch64-linux-nat.c
@@ -959,7 +959,7 @@ aarch64_linux_nat_target::thread_architecture (ptid_t ptid)
 
   /* Find the current gdbarch the same way as process_stratum_target.  Only
      return it if the current vector length matches the one in the tdep.  */
-  inferior *inf = find_inferior_ptid (ptid);
+  inferior *inf = find_inferior_ptid (this, ptid);
   gdb_assert (inf != NULL);
   if (vq == gdbarch_tdep (inf->gdbarch)->vq)
     return inf->gdbarch;
diff --git a/gdb/ada-tasks.c b/gdb/ada-tasks.c
index e4a52976dc..c43369c662 100644
--- a/gdb/ada-tasks.c
+++ b/gdb/ada-tasks.c
@@ -1127,7 +1127,7 @@ print_ada_task_info (struct ui_out *uiout,
       /* Print the associated Thread ID.  */
       if (uiout->is_mi_like_p ())
         {
-	  thread_info *thread = find_thread_ptid (task_info->ptid);
+	  thread_info *thread = find_thread_ptid (inf, task_info->ptid);
 
 	  if (thread != NULL)
 	    uiout->field_signed ("thread-id", thread->global_num);
@@ -1340,7 +1340,7 @@ task_command_1 (const char *taskno_str, int from_tty, struct inferior *inf)
      computed if target_get_ada_task_ptid has not been implemented for
      our target (yet).  Rather than cause an assertion error in that case,
      it's nicer for the user to just refuse to perform the task switch.  */
-  thread_info *tp = find_thread_ptid (task_info->ptid);
+  thread_info *tp = find_thread_ptid (inf, task_info->ptid);
   if (tp == NULL)
     error (_("Unable to compute thread ID for task %s.\n"
              "Cannot switch to this task."),
diff --git a/gdb/aix-thread.c b/gdb/aix-thread.c
index ffa3352d03..ec545221fb 100644
--- a/gdb/aix-thread.c
+++ b/gdb/aix-thread.c
@@ -805,7 +805,11 @@ sync_threadlists (void)
 	  priv->pdtid = pbuf[pi].pdtid;
 	  priv->tid = pbuf[pi].tid;
 
-	  thread = add_thread_with_info (ptid_t (infpid, 0, pbuf[pi].pthid), priv);
+	  process_stratum_target *proc_target
+	    = current_inferior ()->process_target ();
+	  thread = add_thread_with_info (proc_target,
+					 ptid_t (infpid, 0, pbuf[pi].pthid),
+					 priv);
 
 	  pi++;
 	}
@@ -837,7 +841,9 @@ sync_threadlists (void)
 	    }
 	  else
 	    {
-	      thread = add_thread (pptid);
+	      process_stratum_target *proc_target
+		= current_inferior ()->process_target ();
+	      thread = add_thread (proc_target, pptid);
 
 	      aix_thread_info *priv = new aix_thread_info;
 	      thread->priv.reset (priv);
@@ -1043,7 +1049,7 @@ aix_thread_target::resume (ptid_t ptid, int step, enum gdb_signal sig)
     }
   else
     {
-      thread = find_thread_ptid (ptid);
+      thread = find_thread_ptid (current_inferior (), ptid);
       if (!thread)
 	error (_("aix-thread resume: unknown pthread %ld"),
 	       ptid.lwp ());
@@ -1089,7 +1095,9 @@ aix_thread_target::wait (ptid_t ptid, struct target_waitstatus *status,
   if (!pd_active && status->kind == TARGET_WAITKIND_STOPPED
       && status->value.sig == GDB_SIGNAL_TRAP)
     {
-      struct regcache *regcache = get_thread_regcache (ptid);
+      process_stratum_target *proc_target
+	= current_inferior ()->process_target ();
+      struct regcache *regcache = get_thread_regcache (proc_target, ptid);
       struct gdbarch *gdbarch = regcache->arch ();
 
       if (regcache_read_pc (regcache)
@@ -1354,7 +1362,7 @@ aix_thread_target::fetch_registers (struct regcache *regcache, int regno)
     beneath ()->fetch_registers (regcache, regno);
   else
     {
-      thread = find_thread_ptid (regcache->ptid ());
+      thread = find_thread_ptid (current_inferior (), regcache->ptid ());
       aix_thread_info *priv = get_aix_thread_info (thread);
       tid = priv->tid;
 
@@ -1692,7 +1700,7 @@ aix_thread_target::store_registers (struct regcache *regcache, int regno)
     beneath ()->store_registers (regcache, regno);
   else
     {
-      thread = find_thread_ptid (regcache->ptid ());
+      thread = find_thread_ptid (current_inferior (), regcache->ptid ());
       aix_thread_info *priv = get_aix_thread_info (thread);
       tid = priv->tid;
 
@@ -1740,7 +1748,9 @@ aix_thread_target::thread_alive (ptid_t ptid)
 
   /* We update the thread list every time the child stops, so all
      valid threads should be in the thread list.  */
-  return in_thread_list (ptid);
+  process_stratum_target *proc_target
+    = current_inferior ()->process_target ();
+  return in_thread_list (proc_target, ptid);
 }
 
 /* Return a printable representation of composite PID for use in
diff --git a/gdb/amd64-fbsd-tdep.c b/gdb/amd64-fbsd-tdep.c
index 493e630974..416a34974c 100644
--- a/gdb/amd64-fbsd-tdep.c
+++ b/gdb/amd64-fbsd-tdep.c
@@ -30,6 +30,7 @@
 #include "amd64-tdep.h"
 #include "fbsd-tdep.h"
 #include "solib-svr4.h"
+#include "inferior.h"
 
 /* Support for signal handlers.  */
 
@@ -212,7 +213,8 @@ amd64fbsd_get_thread_local_address (struct gdbarch *gdbarch, ptid_t ptid,
 {
   struct regcache *regcache;
 
-  regcache = get_thread_arch_regcache (ptid, gdbarch);
+  regcache = get_thread_arch_regcache (current_inferior ()->process_target (),
+				       ptid, gdbarch);
 
   target_fetch_registers (regcache, AMD64_FSBASE_REGNUM);
 
diff --git a/gdb/amd64-linux-nat.c b/gdb/amd64-linux-nat.c
index d0328b677d..1e69d8a946 100644
--- a/gdb/amd64-linux-nat.c
+++ b/gdb/amd64-linux-nat.c
@@ -383,7 +383,7 @@ ps_err_e
 ps_get_thread_area (struct ps_prochandle *ph,
                     lwpid_t lwpid, int idx, void **base)
 {
-  if (gdbarch_bfd_arch_info (target_gdbarch ())->bits_per_word == 32)
+  if (gdbarch_bfd_arch_info (ph->thread->inf->gdbarch)->bits_per_word == 32)
     {
       unsigned int base_addr;
       ps_err_e result;
diff --git a/gdb/break-catch-sig.c b/gdb/break-catch-sig.c
index d1ea404d53..1ec303439e 100644
--- a/gdb/break-catch-sig.c
+++ b/gdb/break-catch-sig.c
@@ -185,7 +185,7 @@ signal_catchpoint_print_it (bpstat bs)
   const char *signal_name;
   struct ui_out *uiout = current_uiout;
 
-  get_last_target_status (nullptr, &last);
+  get_last_target_status (nullptr, nullptr, &last);
 
   signal_name = signal_to_name_or_int (last.value.sig);
 
diff --git a/gdb/break-catch-syscall.c b/gdb/break-catch-syscall.c
index 35d406477c..b93a1fe4d9 100644
--- a/gdb/break-catch-syscall.c
+++ b/gdb/break-catch-syscall.c
@@ -186,7 +186,7 @@ print_it_catch_syscall (bpstat bs)
   struct syscall s;
   struct gdbarch *gdbarch = bs->bp_location_at->gdbarch;
 
-  get_last_target_status (nullptr, &last);
+  get_last_target_status (nullptr, nullptr, &last);
 
   get_syscall_by_number (gdbarch, last.value.syscall_number, &s);
 
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 2fd2438e61..080e847de1 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -386,7 +386,7 @@ breakpoints_should_be_inserted_now (void)
 	 no threads under GDB's control yet.  */
       return 1;
     }
-  else if (target_has_execution)
+  else
     {
       if (always_inserted_mode)
 	{
@@ -395,8 +395,10 @@ breakpoints_should_be_inserted_now (void)
 	  return 1;
 	}
 
-      if (threads_are_executing ())
-	return 1;
+      for (inferior *inf : all_inferiors ())
+	if (inf->has_execution ()
+	    && threads_are_executing (inf->process_target ()))
+	  return 1;
 
       /* Don't remove breakpoints yet if, even though all threads are
 	 stopped, we still have events to process.  */
@@ -2884,7 +2886,7 @@ update_inserted_breakpoint_locations (void)
 	 if we aren't attached to any process yet, we should still
 	 insert breakpoints.  */
       if (!gdbarch_has_global_breakpoints (target_gdbarch ())
-	  && inferior_ptid == null_ptid)
+	  && (inferior_ptid == null_ptid || !target_has_execution))
 	continue;
 
       val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
@@ -2940,7 +2942,7 @@ insert_breakpoint_locations (void)
 	 if we aren't attached to any process yet, we should still
 	 insert breakpoints.  */
       if (!gdbarch_has_global_breakpoints (target_gdbarch ())
-	  && inferior_ptid == null_ptid)
+	  && (inferior_ptid == null_ptid || !target_has_execution))
 	continue;
 
       val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
@@ -11876,7 +11878,18 @@ update_global_location_list (enum ugll_insert_mode insert_mode)
 		 around.  We simply always ignore hardware watchpoint
 		 traps we can no longer explain.  */
 
-	      old_loc->events_till_retirement = 3 * (thread_count () + 1);
+	      process_stratum_target *proc_target = nullptr;
+	      for (inferior *inf : all_inferiors ())
+		if (inf->pspace == old_loc->pspace)
+		  {
+		    proc_target = inf->process_target ();
+		    break;
+		  }
+	      if (proc_target != nullptr)
+		old_loc->events_till_retirement
+		  = 3 * (thread_count (proc_target) + 1);
+	      else
+		old_loc->events_till_retirement = 1;
 	      old_loc->owner = NULL;
 
 	      moribund_locations.push_back (old_loc);
diff --git a/gdb/bsd-uthread.c b/gdb/bsd-uthread.c
index af048f7a18..cdf8e2cb9c 100644
--- a/gdb/bsd-uthread.c
+++ b/gdb/bsd-uthread.c
@@ -381,9 +381,11 @@ bsd_uthread_target::wait (ptid_t ptid, struct target_waitstatus *status,
 {
   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
   CORE_ADDR addr;
+  process_stratum_target *beneath
+    = as_process_stratum_target (this->beneath ());
 
   /* Pass the request to the layer beneath.  */
-  ptid = beneath ()->wait (ptid, status, options);
+  ptid = beneath->wait (ptid, status, options);
 
   /* If the process is no longer alive, there's no point in figuring
      out the thread ID.  It will fail anyway.  */
@@ -414,13 +416,13 @@ bsd_uthread_target::wait (ptid_t ptid, struct target_waitstatus *status,
      ptid with tid set, then ptid is still the initial thread of
      the process.  Notify GDB core about it.  */
   if (inferior_ptid.tid () == 0
-      && ptid.tid () != 0 && !in_thread_list (ptid))
-    thread_change_ptid (inferior_ptid, ptid);
+      && ptid.tid () != 0 && !in_thread_list (beneath, ptid))
+    thread_change_ptid (beneath, inferior_ptid, ptid);
 
   /* Don't let the core see a ptid without a corresponding thread.  */
-  thread_info *thread = find_thread_ptid (ptid);
+  thread_info *thread = find_thread_ptid (beneath, ptid);
   if (thread == NULL || thread->state == THREAD_EXITED)
-    add_thread (ptid);
+    add_thread (beneath, ptid);
 
   return ptid;
 }
@@ -467,16 +469,18 @@ bsd_uthread_target::update_thread_list ()
     {
       ptid_t ptid = ptid_t (pid, 0, addr);
 
-      thread_info *thread = find_thread_ptid (ptid);
+      process_stratum_target *proc_target
+	= as_process_stratum_target (this->beneath ());
+      thread_info *thread = find_thread_ptid (proc_target, ptid);
       if (thread == nullptr || thread->state == THREAD_EXITED)
 	{
 	  /* If INFERIOR_PTID doesn't have a tid member yet, then ptid
 	     is still the initial thread of the process.  Notify GDB
 	     core about it.  */
 	  if (inferior_ptid.tid () == 0)
-	    thread_change_ptid (inferior_ptid, ptid);
+	    thread_change_ptid (proc_target, inferior_ptid, ptid);
 	  else
-	    add_thread (ptid);
+	    add_thread (proc_target, ptid);
 	}
 
       addr = bsd_uthread_read_memory_address (addr + offset);
diff --git a/gdb/btrace.c b/gdb/btrace.c
index e2443a2d23..3dc1510609 100644
--- a/gdb/btrace.c
+++ b/gdb/btrace.c
@@ -3228,7 +3228,7 @@ maint_btrace_packet_history_cmd (const char *arg, int from_tty)
   struct btrace_thread_info *btinfo;
   unsigned int size, begin, end, from, to;
 
-  thread_info *tp = find_thread_ptid (inferior_ptid);
+  thread_info *tp = find_thread_ptid (current_inferior (), inferior_ptid);
   if (tp == NULL)
     error (_("No thread."));
 
diff --git a/gdb/corelow.c b/gdb/corelow.c
index a5af5c2742..53dd4628e7 100644
--- a/gdb/corelow.c
+++ b/gdb/corelow.c
@@ -312,7 +312,7 @@ add_to_thread_list (bfd *abfd, asection *asect, void *reg_sect_arg)
 
   ptid = ptid_t (pid, lwpid, 0);
 
-  add_thread (ptid);
+  add_thread (inf->process_target (), ptid);
 
 /* Warning, Will Robinson, looking at BFD private data! */
 
@@ -450,7 +450,7 @@ core_target_open (const char *arg, int from_tty)
 	{
 	  inferior_appeared (current_inferior (), CORELOW_PID);
 	  inferior_ptid = ptid_t (CORELOW_PID);
-	  add_thread_silent (inferior_ptid);
+	  add_thread_silent (target, inferior_ptid);
 	}
       else
 	switch_to_thread (thread);
@@ -515,7 +515,7 @@ core_target_open (const char *arg, int from_tty)
   /* Current thread should be NUM 1 but the user does not know that.
      If a program is single threaded gdb in general does not mention
      anything about threads.  That is why the test is >= 2.  */
-  if (thread_count () >= 2)
+  if (thread_count (target) >= 2)
     {
       try
 	{
@@ -919,7 +919,7 @@ core_target::pid_to_str (ptid_t ptid)
 
   /* Otherwise, this isn't a "threaded" core -- use the PID field, but
      only if it isn't a fake PID.  */
-  inf = find_inferior_ptid (ptid);
+  inf = find_inferior_ptid (this, ptid);
   if (inf != NULL && !inf->fake_pid_p)
     return normal_pid_to_str (ptid);
 
diff --git a/gdb/event-top.c b/gdb/event-top.c
index 0396dbcc52..4c02cc1634 100644
--- a/gdb/event-top.c
+++ b/gdb/event-top.c
@@ -1095,12 +1095,16 @@ async_disconnect (gdb_client_data arg)
       exception_print (gdb_stderr, exception);
     }
 
-  try
-    {
-      pop_all_targets ();
-    }
-  catch (const gdb_exception &exception)
+  for (inferior *inf : all_inferiors ())
     {
+      switch_to_inferior_no_thread (inf);
+      try
+	{
+	  pop_all_targets ();
+	}
+      catch (const gdb_exception &exception)
+	{
+	}
     }
 
   signal (SIGHUP, SIG_DFL);	/*FIXME: ???????????  */
diff --git a/gdb/exec.c b/gdb/exec.c
index 6bdf9abb3e..f95713b8d8 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -547,10 +547,23 @@ add_target_sections (void *owner,
 	  table->sections[space + i].owner = owner;
 	}
 
+      scoped_restore_current_thread restore_thread;
+      program_space *curr_pspace = current_program_space;
+
       /* If these are the first file sections we can provide memory
-	 from, push the file_stratum target.  */
-      if (!target_is_pushed (&exec_ops))
-	push_target (&exec_ops);
+	 from, push the file_stratum target.  Must do this in all
+	 inferiors sharing the program space.  */
+      for (inferior *inf : all_inferiors ())
+	{
+	  if (inf->pspace != curr_pspace)
+	    continue;
+
+	  if (inf->target_is_pushed (&exec_ops))
+	    continue;
+
+	  switch_to_inferior_no_thread (inf);
+	  push_target (&exec_ops);
+	}
     }
 }
 
@@ -628,21 +641,39 @@ remove_target_sections (void *owner)
       old_count = resize_section_table (table, dest - src);
 
       /* If we don't have any more sections to read memory from,
-	 remove the file_stratum target from the stack.  */
+	 remove the file_stratum target from the stack of each
+	 inferior sharing the program space.  */
       if (old_count + (dest - src) == 0)
 	{
-	  struct program_space *pspace;
+	  scoped_restore_current_thread restore_thread;
+	  program_space *curr_pspace = current_program_space;
+
+	  for (inferior *inf : all_inferiors ())
+	    {
+	      if (inf->pspace != curr_pspace)
+		continue;
 
-	  ALL_PSPACES (pspace)
-	    if (pspace->target_sections.sections
-		!= pspace->target_sections.sections_end)
-	      return;
+	      if (inf->pspace->target_sections.sections
+		  != inf->pspace->target_sections.sections_end)
+		continue;
 
-	  unpush_target (&exec_ops);
+	      switch_to_inferior_no_thread (inf);
+	      unpush_target (&exec_ops);
+	    }
 	}
     }
 }
 
+/* See exec.h.  */
+
+void
+exec_on_vfork ()
+{
+  if (current_program_space->target_sections.sections
+      != current_program_space->target_sections.sections_end)
+    push_target (&exec_ops);
+}
+
 \f
 
 enum target_xfer_status
diff --git a/gdb/exec.h b/gdb/exec.h
index e9af480287..539e33f417 100644
--- a/gdb/exec.h
+++ b/gdb/exec.h
@@ -44,6 +44,13 @@ extern int build_section_table (struct bfd *, struct target_section **,
 
 extern void clear_section_table (struct target_section_table *table);
 
+/* The current inferior is a child vforked and its program space is
+   shared with its parent.  This pushes the exec target on the
+   current/child inferior's target stack if there are sections in the
+   program space's section table.  */
+
+extern void exec_on_vfork ();
+
 /* Read from mappable read-only sections of BFD executable files.
    Return TARGET_XFER_OK, if read is successful.  Return
    TARGET_XFER_EOF if read is done.  Return TARGET_XFER_E_IO
diff --git a/gdb/fbsd-tdep.c b/gdb/fbsd-tdep.c
index 9422e3c1a7..fe8681ee9b 100644
--- a/gdb/fbsd-tdep.c
+++ b/gdb/fbsd-tdep.c
@@ -673,7 +673,8 @@ fbsd_corefile_thread (struct thread_info *info,
 {
   struct regcache *regcache;
 
-  regcache = get_thread_arch_regcache (info->ptid, args->gdbarch);
+  regcache = get_thread_arch_regcache (info->inf->process_target (),
+				       info->ptid, args->gdbarch);
 
   target_fetch_registers (regcache, -1);
 
diff --git a/gdb/fork-child.c b/gdb/fork-child.c
index edfb6a3c5a..384e45b2e0 100644
--- a/gdb/fork-child.c
+++ b/gdb/fork-child.c
@@ -128,10 +128,13 @@ postfork_child_hook ()
 ptid_t
 gdb_startup_inferior (pid_t pid, int num_traps)
 {
-  ptid_t ptid = startup_inferior (pid, num_traps, NULL, NULL);
+  inferior *inf = current_inferior ();
+  process_stratum_target *proc_target = inf->process_target ();
+
+  ptid_t ptid = startup_inferior (proc_target, pid, num_traps, NULL, NULL);
 
   /* Mark all threads non-executing.  */
-  set_executing (ptid, 0);
+  set_executing (proc_target, ptid, 0);
 
   return ptid;
 }
diff --git a/gdb/gdbserver/fork-child.c b/gdb/gdbserver/fork-child.c
index ddd73094e6..24b31320fd 100644
--- a/gdb/gdbserver/fork-child.c
+++ b/gdb/gdbserver/fork-child.c
@@ -107,7 +107,8 @@ post_fork_inferior (int pid, const char *program)
   atexit (restore_old_foreground_pgrp);
 #endif
 
-  startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED,
+  startup_inferior (the_target, pid,
+		    START_INFERIOR_TRAPS_EXPECTED,
 		    &cs.last_status, &cs.last_ptid);
   current_thread->last_resume_kind = resume_stop;
   current_thread->last_status = cs.last_status;
diff --git a/gdb/gdbserver/inferiors.c b/gdb/gdbserver/inferiors.c
index e35186bda1..09a98a9145 100644
--- a/gdb/gdbserver/inferiors.c
+++ b/gdb/gdbserver/inferiors.c
@@ -216,7 +216,7 @@ current_process (void)
 /* See gdbsupport/common-gdbthread.h.  */
 
 void
-switch_to_thread (ptid_t ptid)
+switch_to_thread (process_stratum_target *ops, ptid_t ptid)
 {
   gdb_assert (ptid != minus_one_ptid);
   current_thread = find_thread_ptid (ptid);
diff --git a/gdb/gdbserver/linux-low.c b/gdb/gdbserver/linux-low.c
index 0e4b14e365..92084a39cc 100644
--- a/gdb/gdbserver/linux-low.c
+++ b/gdb/gdbserver/linux-low.c
@@ -7353,7 +7353,7 @@ linux_get_hwcap2 (int wordsize)
   return hwcap2;
 }
 
-static struct target_ops linux_target_ops = {
+static process_stratum_target linux_target_ops = {
   linux_create_inferior,
   linux_post_create_inferior,
   linux_attach,
diff --git a/gdb/gdbserver/lynx-low.c b/gdb/gdbserver/lynx-low.c
index 2bd24e7cee..ea328d7433 100644
--- a/gdb/gdbserver/lynx-low.c
+++ b/gdb/gdbserver/lynx-low.c
@@ -721,7 +721,7 @@ lynx_request_interrupt (void)
 
 /* The LynxOS target_ops vector.  */
 
-static struct target_ops lynx_target_ops = {
+static process_stratum_target lynx_target_ops = {
   lynx_create_inferior,
   NULL,  /* post_create_inferior */
   lynx_attach,
diff --git a/gdb/gdbserver/nto-low.c b/gdb/gdbserver/nto-low.c
index d77fda54b8..f950cd0458 100644
--- a/gdb/gdbserver/nto-low.c
+++ b/gdb/gdbserver/nto-low.c
@@ -931,7 +931,7 @@ nto_sw_breakpoint_from_kind (int kind, int *size)
 }
 
 
-static struct target_ops nto_target_ops = {
+static process_stratum_target nto_target_ops = {
   nto_create_inferior,
   NULL,  /* post_create_inferior */
   nto_attach,
diff --git a/gdb/gdbserver/remote-utils.c b/gdb/gdbserver/remote-utils.c
index d7da4b7aed..407dda5a7d 100644
--- a/gdb/gdbserver/remote-utils.c
+++ b/gdb/gdbserver/remote-utils.c
@@ -1207,7 +1207,7 @@ prepare_resume_reply (char *buf, ptid_t ptid,
 
 	saved_thread = current_thread;
 
-	switch_to_thread (ptid);
+	switch_to_thread (the_target, ptid);
 
 	regp = current_target_desc ()->expedite_regs;
 
diff --git a/gdb/gdbserver/target.c b/gdb/gdbserver/target.c
index 9018118a06..99fc7d9294 100644
--- a/gdb/gdbserver/target.c
+++ b/gdb/gdbserver/target.c
@@ -22,7 +22,7 @@
 #include "tracepoint.h"
 #include "gdbsupport/byte-vector.h"
 
-struct target_ops *the_target;
+process_stratum_target *the_target;
 
 int
 set_desired_thread ()
@@ -119,7 +119,7 @@ done_accessing_memory (void)
 
   /* Restore the previous selected thread.  */
   cs.general_thread = prev_general_thread;
-  switch_to_thread (cs.general_thread);
+  switch_to_thread (the_target, cs.general_thread);
 }
 
 int
@@ -284,9 +284,9 @@ start_non_stop (int nonstop)
 }
 
 void
-set_target_ops (struct target_ops *target)
+set_target_ops (process_stratum_target *target)
 {
-  the_target = XNEW (struct target_ops);
+  the_target = XNEW (process_stratum_target);
   memcpy (the_target, target, sizeof (*the_target));
 }
 
diff --git a/gdb/gdbserver/target.h b/gdb/gdbserver/target.h
index 2df135a769..ba14210826 100644
--- a/gdb/gdbserver/target.h
+++ b/gdb/gdbserver/target.h
@@ -63,7 +63,10 @@ struct thread_resume
   CORE_ADDR step_range_end;	/* Exclusive */
 };
 
-struct target_ops
+/* GDBserver doesn't have a concept of strata like GDB, but we call
+   its target vector "process_stratum" anyway for the benefit of
+   shared code.  */
+struct process_stratum_target
 {
   /* Start a new process.
 
@@ -476,9 +479,9 @@ struct target_ops
   bool (*thread_handle) (ptid_t ptid, gdb_byte **handle, int *handle_len);
 };
 
-extern struct target_ops *the_target;
+extern process_stratum_target *the_target;
 
-void set_target_ops (struct target_ops *);
+void set_target_ops (process_stratum_target *);
 
 #define create_inferior(program, program_args)	\
   (*the_target->create_inferior) (program, program_args)
@@ -702,7 +705,7 @@ ptid_t mywait (ptid_t ptid, struct target_waitstatus *ourstatus, int options,
 	       int connected_wait);
 
 /* Prepare to read or write memory from the inferior process.  See the
-   corresponding target_ops methods for more details.  */
+   corresponding process_stratum_target methods for more details.  */
 
 int prepare_to_access_memory (void);
 void done_accessing_memory (void);
diff --git a/gdb/gdbserver/win32-low.c b/gdb/gdbserver/win32-low.c
index 7088ba4dd1..c915bcda09 100644
--- a/gdb/gdbserver/win32-low.c
+++ b/gdb/gdbserver/win32-low.c
@@ -306,7 +306,7 @@ win32_stopped_data_address (void)
 /* Transfer memory from/to the debugged process.  */
 static int
 child_xfer_memory (CORE_ADDR memaddr, char *our, int len,
-		   int write, struct target_ops *target)
+		   int write, process_stratum_target *target)
 {
   BOOL success;
   SIZE_T done = 0;
@@ -1777,7 +1777,7 @@ win32_sw_breakpoint_from_kind (int kind, int *size)
   return the_low_target.breakpoint;
 }
 
-static struct target_ops win32_target_ops = {
+static process_stratum_target win32_target_ops = {
   win32_create_inferior,
   NULL,  /* post_create_inferior */
   win32_attach,
diff --git a/gdb/gdbsupport/common-gdbthread.h b/gdb/gdbsupport/common-gdbthread.h
index d692be209c..20f4d89b26 100644
--- a/gdb/gdbsupport/common-gdbthread.h
+++ b/gdb/gdbsupport/common-gdbthread.h
@@ -19,7 +19,10 @@
 #ifndef COMMON_COMMON_GDBTHREAD_H
 #define COMMON_COMMON_GDBTHREAD_H
 
+struct process_stratum_target;
+
 /* Switch from one thread to another.  */
-extern void switch_to_thread (ptid_t ptid);
+extern void switch_to_thread (process_stratum_target *proc_target,
+			      ptid_t ptid);
 
 #endif /* COMMON_COMMON_GDBTHREAD_H */
diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
index 62b73fb83b..09522ff696 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -34,6 +34,7 @@ struct symtab;
 #include "gdbsupport/forward-scope-exit.h"
 
 struct inferior;
+struct process_stratum_target;
 
 /* Frontend view of the thread state.  Possible extensions: stepping,
    finishing, until(ling),...
@@ -304,7 +305,7 @@ public:
      from saying that there is an active target and we are stopped at
      a breakpoint, for instance.  This is a real indicator whether the
      thread is off and running.  */
-  int executing = 0;
+  bool executing = false;
 
   /* Non-zero if this thread is resumed from infrun's perspective.
      Note that a thread can be marked both as not-executing and
@@ -419,15 +420,18 @@ extern void init_thread_list (void);
    that a new thread is found, and return the pointer to
    the new thread.  Caller my use this pointer to 
    initialize the private thread data.  */
-extern struct thread_info *add_thread (ptid_t ptid);
+extern struct thread_info *add_thread (process_stratum_target *targ,
+				       ptid_t ptid);
 
-/* Same as add_thread, but does not print a message
-   about new thread.  */
-extern struct thread_info *add_thread_silent (ptid_t ptid);
+/* Same as add_thread, but does not print a message about new
+   thread.  */
+extern struct thread_info *add_thread_silent (process_stratum_target *targ,
+					      ptid_t ptid);
 
 /* Same as add_thread, and sets the private info.  */
-extern struct thread_info *add_thread_with_info (ptid_t ptid,
-						 struct private_thread_info *);
+extern struct thread_info *add_thread_with_info (process_stratum_target *targ,
+						 ptid_t ptid,
+						 private_thread_info *);
 
 /* Delete an existing thread list entry.  */
 extern void delete_thread (struct thread_info *thread);
@@ -468,14 +472,18 @@ extern int show_inferior_qualified_tids (void);
 const char *print_thread_id (struct thread_info *thr);
 
 /* Boolean test for an already-known ptid.  */
-extern int in_thread_list (ptid_t ptid);
+extern bool in_thread_list (process_stratum_target *targ, ptid_t ptid);
 
 /* Boolean test for an already-known global thread id (GDB's homegrown
    global id, not the system's).  */
 extern int valid_global_thread_id (int global_id);
 
+/* Find thread PTID of inferior INF.  */
+extern thread_info *find_thread_ptid (inferior *inf, ptid_t ptid);
+
 /* Search function to lookup a thread by 'pid'.  */
-extern struct thread_info *find_thread_ptid (ptid_t ptid);
+extern struct thread_info *find_thread_ptid (process_stratum_target *targ,
+					     ptid_t ptid);
 
 /* Search function to lookup a thread by 'ptid'.  Only searches in
    threads of INF.  */
@@ -500,7 +508,8 @@ extern struct thread_info *any_thread_of_inferior (inferior *inf);
 extern struct thread_info *any_live_thread_of_inferior (inferior *inf);
 
 /* Change the ptid of thread OLD_PTID to NEW_PTID.  */
-void thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid);
+void thread_change_ptid (process_stratum_target *targ,
+			 ptid_t old_ptid, ptid_t new_ptid);
 
 /* Iterator function to call a user-provided callback function
    once for each known thread.  */
@@ -511,34 +520,44 @@ extern struct thread_info *iterate_over_threads (thread_callback_func, void *);
    iterators.  Must be done after struct thread_info is defined.  */
 #include "thread-iter.h"
 
-/* Return a range that can be used to walk over all threads of all
-   inferiors, with range-for.  Used like this:
+/* Return a range that can be used to walk over threads, with
+   range-for.
+
+   Used like this, it walks over all threads of all inferiors of all
+   targets:
 
        for (thread_info *thr : all_threads ())
 	 { .... }
-*/
-inline all_threads_range
-all_threads ()
-{
-  return {};
-}
 
-/* Likewise, but accept a filter PTID.  */
+   FILTER_PTID can be used to filter out threads that don't match.
+   FILTER_PTID can be:
+
+   - minus_one_ptid, meaning walk all threads of all inferiors of
+     PROC_TARGET.  If PROC_TARGET is NULL, then of all targets.
+
+   - A process ptid, in which case walk all threads of the specified
+     process.  PROC_TARGET must be non-NULL in this case.
+
+   - A thread ptid, in which case walk that thread only.  PROC_TARGET
+     must be non-NULL in this case.
+*/
 
 inline all_matching_threads_range
-all_threads (ptid_t filter_ptid)
+all_threads (process_stratum_target *proc_target = nullptr,
+	     ptid_t filter_ptid = minus_one_ptid)
 {
-  return all_matching_threads_range (filter_ptid);
+  return all_matching_threads_range (proc_target, filter_ptid);
 }
 
 /* Return a range that can be used to walk over all non-exited threads
-   of all inferiors, with range-for.  FILTER_PTID can be used to
-   filter out thread that don't match.  */
+   of all inferiors, with range-for.  Arguments are like all_threads
+   above.  */
 
 inline all_non_exited_threads_range
-all_non_exited_threads (ptid_t filter_ptid = minus_one_ptid)
+all_non_exited_threads (process_stratum_target *proc_target = nullptr,
+			ptid_t filter_ptid = minus_one_ptid)
 {
-  return all_non_exited_threads_range (filter_ptid);
+  return all_non_exited_threads_range (proc_target, filter_ptid);
 }
 
 /* Return a range that can be used to walk over all threads of all
@@ -554,10 +573,10 @@ all_non_exited_threads (ptid_t filter_ptid = minus_one_ptid)
 inline all_threads_safe_range
 all_threads_safe ()
 {
-  return all_threads_safe_range ();
+  return {};
 }
 
-extern int thread_count (void);
+extern int thread_count (process_stratum_target *proc_target);
 
 /* Return true if we have any thread in any inferior.  */
 extern bool any_thread_p ();
@@ -571,44 +590,50 @@ extern void switch_to_no_thread ();
 /* Switch from one thread to another.  Does not read registers.  */
 extern void switch_to_thread_no_regs (struct thread_info *thread);
 
-/* Marks or clears thread(s) PTID as resumed.  If PTID is
-   MINUS_ONE_PTID, applies to all threads.  If ptid_is_pid(PTID) is
-   true, applies to all threads of the process pointed at by PTID.  */
-extern void set_resumed (ptid_t ptid, int resumed);
-
-/* Marks thread PTID is running, or stopped. 
-   If PTID is minus_one_ptid, marks all threads.  */
-extern void set_running (ptid_t ptid, int running);
-
-/* Marks or clears thread(s) PTID as having been requested to stop.
-   If PTID is MINUS_ONE_PTID, applies to all threads.  If
+/* Marks or clears thread(s) PTID of TARG as resumed.  If PTID is
+   MINUS_ONE_PTID, applies to all threads of TARG.  If
    ptid_is_pid(PTID) is true, applies to all threads of the process
-   pointed at by PTID.  If STOP, then the THREAD_STOP_REQUESTED
-   observer is called with PTID as argument.  */
-extern void set_stop_requested (ptid_t ptid, int stop);
-
-/* Marks thread PTID as executing, or not.  If PTID is minus_one_ptid,
-   marks all threads.
+   pointed at by {TARG,PTID}.  */
+extern void set_resumed (process_stratum_target *targ,
+			 ptid_t ptid, bool resumed);
+
+/* Marks thread PTID of TARG as running, or as stopped.  If PTID is
+   minus_one_ptid, marks all threads of TARG.  */
+extern void set_running (process_stratum_target *targ,
+			 ptid_t ptid, bool running);
+
+/* Marks or clears thread(s) PTID of TARG as having been requested to
+   stop.  If PTID is MINUS_ONE_PTID, applies to all threads of TARG.
+   If ptid_is_pid(PTID) is true, applies to all threads of the process
+   pointed at by {TARG, PTID}.  If STOP, then the
+   THREAD_STOP_REQUESTED observer is called with PTID as argument.  */
+extern void set_stop_requested (process_stratum_target *targ,
+				ptid_t ptid, bool stop);
+
+/* Marks thread PTID of TARG as executing, or not.  If PTID is
+   minus_one_ptid, marks all threads of TARG.
 
    Note that this is different from the running state.  See the
    description of state and executing fields of struct
    thread_info.  */
-extern void set_executing (ptid_t ptid, int executing);
+extern void set_executing (process_stratum_target *targ,
+			   ptid_t ptid, bool executing);
 
-/* True if any (known or unknown) thread is or may be executing.  */
-extern int threads_are_executing (void);
+/* True if any (known or unknown) thread of TARG is or may be
+   executing.  */
+extern bool threads_are_executing (process_stratum_target *targ);
 
-/* Merge the executing property of thread PTID over to its thread
-   state property (frontend running/stopped view).
+/* Merge the executing property of thread PTID of TARG over to its
+   thread state property (frontend running/stopped view).
 
    "not executing" -> "stopped"
    "executing"     -> "running"
    "exited"        -> "exited"
 
-   If PTID is minus_one_ptid, go over all threads.
+   If PTID is minus_one_ptid, go over all threads of TARG.
 
    Notifications are only emitted if the thread state did change.  */
-extern void finish_thread_state (ptid_t ptid);
+extern void finish_thread_state (process_stratum_target *targ, ptid_t ptid);
 
 /* Calls finish_thread_state on scope exit, unless release() is called
    to disengage.  */
diff --git a/gdb/i386-fbsd-tdep.c b/gdb/i386-fbsd-tdep.c
index 04d0a641b7..5e530cf207 100644
--- a/gdb/i386-fbsd-tdep.c
+++ b/gdb/i386-fbsd-tdep.c
@@ -30,6 +30,7 @@
 #include "i387-tdep.h"
 #include "fbsd-tdep.h"
 #include "solib-svr4.h"
+#include "inferior.h"
 
 /* Support for signal handlers.  */
 
@@ -332,7 +333,8 @@ i386fbsd_get_thread_local_address (struct gdbarch *gdbarch, ptid_t ptid,
   if (tdep->fsbase_regnum == -1)
     error (_("Unable to fetch %%gsbase"));
 
-  regcache = get_thread_arch_regcache (ptid, gdbarch);
+  regcache = get_thread_arch_regcache (current_inferior ()->process_target (),
+				       ptid, gdbarch);
 
   target_fetch_registers (regcache, tdep->fsbase_regnum + 1);
 
diff --git a/gdb/i386-linux-nat.c b/gdb/i386-linux-nat.c
index dd8f141f23..4748616666 100644
--- a/gdb/i386-linux-nat.c
+++ b/gdb/i386-linux-nat.c
@@ -657,7 +657,7 @@ i386_linux_nat_target::low_resume (ptid_t ptid, int step, enum gdb_signal signal
 
   if (step)
     {
-      struct regcache *regcache = get_thread_regcache (ptid);
+      struct regcache *regcache = get_thread_regcache (this, ptid);
       struct gdbarch *gdbarch = regcache->arch ();
       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
       ULONGEST pc;
diff --git a/gdb/inf-child.c b/gdb/inf-child.c
index 0a73965a0b..4631e323ae 100644
--- a/gdb/inf-child.c
+++ b/gdb/inf-child.c
@@ -206,7 +206,7 @@ inf_child_target::mourn_inferior ()
 void
 inf_child_target::maybe_unpush_target ()
 {
-  if (!inf_child_explicitly_opened && !have_inferiors ())
+  if (!inf_child_explicitly_opened)
     unpush_target (this);
 }
 
diff --git a/gdb/inf-ptrace.c b/gdb/inf-ptrace.c
index 4a8e732373..1e36ce60f9 100644
--- a/gdb/inf-ptrace.c
+++ b/gdb/inf-ptrace.c
@@ -136,7 +136,7 @@ inf_ptrace_target::create_inferior (const char *exec_file,
   /* We have something that executes now.  We'll be running through
      the shell at this point (if startup-with-shell is true), but the
      pid shouldn't change.  */
-  add_thread_silent (ptid);
+  add_thread_silent (this, ptid);
 
   unpusher.release ();
 
@@ -236,10 +236,10 @@ inf_ptrace_target::attach (const char *args, int from_tty)
 
   /* Always add a main thread.  If some target extends the ptrace
      target, it should decorate the ptid later with more info.  */
-  thread_info *thr = add_thread_silent (inferior_ptid);
+  thread_info *thr = add_thread_silent (this, inferior_ptid);
   /* Don't consider the thread stopped until we've processed its
      initial SIGSTOP stop.  */
-  set_executing (thr->ptid, true);
+  set_executing (this, thr->ptid, true);
 
   unpusher.release ();
 }
diff --git a/gdb/infcall.c b/gdb/infcall.c
index 726f14d525..7e723dc7fa 100644
--- a/gdb/infcall.c
+++ b/gdb/infcall.c
@@ -650,7 +650,8 @@ run_inferior_call (struct call_thread_fsm *sm,
   if (!was_running
       && call_thread_ptid == inferior_ptid
       && stop_stack_dummy == STOP_STACK_DUMMY)
-    finish_thread_state (user_visible_resume_ptid (0));
+    finish_thread_state (call_thread->inf->process_target (),
+			 user_visible_resume_ptid (0));
 
   enable_watchpoints_after_interactive_call_stop ();
 
diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index 75df3f3bce..795c0decc5 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -650,10 +650,19 @@ run_command_1 (const char *args, int from_tty, enum run_how run_how)
      events --- the frontend shouldn't see them as stopped.  In
      all-stop, always finish the state of all threads, as we may be
      resuming more than just the new process.  */
-  ptid_t finish_ptid = (non_stop
-			? ptid_t (current_inferior ()->pid)
-			: minus_one_ptid);
-  scoped_finish_thread_state finish_state (finish_ptid);
+  process_stratum_target *finish_target;
+  ptid_t finish_ptid;
+  if (non_stop)
+    {
+      finish_target = current_inferior ()->process_target ();
+      finish_ptid = ptid_t (current_inferior ()->pid);
+    }
+  else
+    {
+      finish_target = nullptr;
+      finish_ptid = minus_one_ptid;
+    }
+  scoped_finish_thread_state finish_state (finish_target, finish_ptid);
 
   /* Pass zero for FROM_TTY, because at this point the "run" command
      has done its thing; now we are setting up the running program.  */
@@ -723,6 +732,9 @@ proceed_thread_callback (struct thread_info *thread, void *arg)
   if (thread->state != THREAD_STOPPED)
     return 0;
 
+  if (!thread->inf->has_execution ())
+    return 0;
+
   switch_to_thread (thread);
   clear_proceed_status (0);
   proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
@@ -816,7 +828,7 @@ static void
 continue_command (const char *args, int from_tty)
 {
   int async_exec;
-  int all_threads = 0;
+  bool all_threads_p = false;
 
   ERROR_NO_INFERIOR;
 
@@ -828,17 +840,17 @@ continue_command (const char *args, int from_tty)
     {
       if (startswith (args, "-a"))
 	{
-	  all_threads = 1;
+	  all_threads_p = true;
 	  args += sizeof ("-a") - 1;
 	  if (*args == '\0')
 	    args = NULL;
 	}
     }
 
-  if (!non_stop && all_threads)
+  if (!non_stop && all_threads_p)
     error (_("`-a' is meaningless in all-stop mode."));
 
-  if (args != NULL && all_threads)
+  if (args != NULL && all_threads_p)
     error (_("Can't resume all threads and specify "
 	     "proceed count simultaneously."));
 
@@ -855,10 +867,11 @@ continue_command (const char *args, int from_tty)
 	tp = inferior_thread ();
       else
 	{
+	  process_stratum_target *last_target;
 	  ptid_t last_ptid;
 
-	  get_last_target_status (&last_ptid, nullptr);
-	  tp = find_thread_ptid (last_ptid);
+	  get_last_target_status (&last_target, &last_ptid, nullptr);
+	  tp = find_thread_ptid (last_target, last_ptid);
 	}
       if (tp != NULL)
 	bs = tp->control.stop_bpstat;
@@ -886,7 +899,7 @@ continue_command (const char *args, int from_tty)
   ERROR_NO_INFERIOR;
   ensure_not_tfind_mode ();
 
-  if (!non_stop || !all_threads)
+  if (!non_stop || !all_threads_p)
     {
       ensure_valid_thread ();
       ensure_not_running ();
@@ -897,7 +910,7 @@ continue_command (const char *args, int from_tty)
   if (from_tty)
     printf_filtered (_("Continuing.\n"));
 
-  continue_1 (all_threads);
+  continue_1 (all_threads_p);
 }
 \f
 /* Record the starting point of a "step" or "next" command.  */
@@ -1114,7 +1127,7 @@ prepare_one_step (struct step_command_fsm *sm)
 
 	      /* Pretend that we've ran.  */
 	      resume_ptid = user_visible_resume_ptid (1);
-	      set_running (resume_ptid, 1);
+	      set_running (tp->inf->process_target (), resume_ptid, true);
 
 	      step_into_inline_frame (tp);
 	      sm->count--;
@@ -1306,10 +1319,14 @@ signal_command (const char *signum_exp, int from_tty)
       /* This indicates what will be resumed.  Either a single thread,
 	 a whole process, or all threads of all processes.  */
       ptid_t resume_ptid = user_visible_resume_ptid (0);
+      process_stratum_target *resume_target
+	= user_visible_resume_target (resume_ptid);
 
-      for (thread_info *tp : all_non_exited_threads (resume_ptid))
+      thread_info *current = inferior_thread ();
+
+      for (thread_info *tp : all_non_exited_threads (resume_target, resume_ptid))
 	{
-	  if (tp->ptid == inferior_ptid)
+	  if (tp == current)
 	    continue;
 
 	  if (tp->suspend.stop_signal != GDB_SIGNAL_0
@@ -1972,6 +1989,7 @@ info_program_command (const char *args, int from_tty)
   bpstat bs;
   int num, stat;
   ptid_t ptid;
+  process_stratum_target *proc_target;
 
   if (!target_has_execution)
     {
@@ -1980,14 +1998,17 @@ info_program_command (const char *args, int from_tty)
     }
 
   if (non_stop)
-    ptid = inferior_ptid;
+    {
+      ptid = inferior_ptid;
+      proc_target = current_inferior ()->process_target ();
+    }
   else
-    get_last_target_status (&ptid, nullptr);
+    get_last_target_status (&proc_target, &ptid, nullptr);
 
   if (ptid == null_ptid || ptid == minus_one_ptid)
     error (_("No selected thread."));
 
-  thread_info *tp = find_thread_ptid (ptid);
+  thread_info *tp = find_thread_ptid (proc_target, ptid);
 
   if (tp->state == THREAD_EXITED)
     error (_("Invalid selected thread."));
@@ -2776,12 +2797,16 @@ attach_command (const char *args, int from_tty)
       add_inferior_continuation (attach_command_continuation, a,
 				 attach_command_continuation_free_args);
 
+      /* Let infrun consider waiting for events out of this
+	 target.  */
+      inferior->process_target ()->threads_executing = true;
+
       if (!target_is_async_p ())
 	mark_infrun_async_event_handler ();
       return;
     }
-
-  attach_post_wait (args, from_tty, mode);
+  else
+    attach_post_wait (args, from_tty, mode);
 }
 
 /* We had just found out that the target was already attached to an
@@ -2898,20 +2923,15 @@ disconnect_command (const char *args, int from_tty)
     deprecated_detach_hook ();
 }
 
-void 
-interrupt_target_1 (int all_threads)
-{
-  ptid_t ptid;
-
-  if (all_threads)
-    ptid = minus_one_ptid;
-  else
-    ptid = inferior_ptid;
+/* Stop PTID in the current target, and tag the PTID threads as having
+   been explicitly requested to stop.  PTID can be a thread, a
+   process, or minus_one_ptid, meaning all threads of all inferiors of
+   the current target.  */
 
-  if (non_stop)
-    target_stop (ptid);
-  else
-    target_interrupt ();
+static void
+stop_current_target_threads_ns (ptid_t ptid)
+{
+  target_stop (ptid);
 
   /* Tag the thread as having been explicitly requested to stop, so
      other parts of gdb know not to resume this thread automatically,
@@ -2919,8 +2939,32 @@ interrupt_target_1 (int all_threads)
      non-stop mode, as when debugging a multi-threaded application in
      all-stop mode, we will only get one stop event --- it's undefined
      which thread will report the event.  */
+  set_stop_requested (current_inferior ()->process_target (),
+		      ptid, 1);
+}
+
+/* See inferior.h.  */
+
+void
+interrupt_target_1 (bool all_threads)
+{
   if (non_stop)
-    set_stop_requested (ptid, 1);
+    {
+      if (all_threads)
+	{
+	  scoped_restore_current_thread restore_thread;
+
+	  for (inferior *inf : all_inferiors ())
+	    {
+	      switch_to_inferior_no_thread (inf);
+	      stop_current_target_threads_ns (minus_one_ptid);
+	    }
+	}
+      else
+	stop_current_target_threads_ns (inferior_ptid);
+    }
+  else
+    target_interrupt ();
 }
 
 /* interrupt [-a]
diff --git a/gdb/inferior-iter.h b/gdb/inferior-iter.h
index b1b595735b..0f484cde3e 100644
--- a/gdb/inferior-iter.h
+++ b/gdb/inferior-iter.h
@@ -36,18 +36,23 @@ public:
   typedef int difference_type;
 
   /* Create an iterator pointing at HEAD.  */
-  explicit all_inferiors_iterator (inferior *head)
-    : m_inf (head)
-  {}
+  all_inferiors_iterator (process_stratum_target *proc_target, inferior *head)
+    : m_proc_target (proc_target)
+  {
+    /* Advance M_INF to the first inferior's position.  */
+    for (m_inf = head; m_inf != NULL; m_inf = m_inf->next)
+      if (m_inf_matches ())
+	return;
+  }
 
   /* Create a one-past-end iterator.  */
   all_inferiors_iterator ()
-    : m_inf (nullptr)
+    : m_proc_target (nullptr), m_inf (nullptr)
   {}
 
   all_inferiors_iterator &operator++ ()
   {
-    m_inf = m_inf->next;
+    advance ();
     return *this;
   }
 
@@ -58,6 +63,30 @@ public:
   { return m_inf != other.m_inf; }
 
 private:
+  /* Advance to next inferior, skipping filtered inferiors.  */
+  void advance ()
+  {
+    /* The loop below is written in the natural way as-if we'd always
+       start at the beginning of the inferior list.  This
+       fast-forwards the algorithm to the actual current position.  */
+    goto start;
+
+    while (m_inf != NULL)
+      {
+	if (m_inf_matches ())
+	  return;
+      start:
+	m_inf = m_inf->next;
+      }
+  }
+
+  bool m_inf_matches ()
+  {
+    return (m_proc_target == nullptr
+	    || m_proc_target == m_inf->process_target ());
+  }
+
+  process_stratum_target *m_proc_target;
   inferior *m_inf;
 };
 
@@ -80,10 +109,17 @@ using all_non_exited_inferiors_iterator
    inferiors with range-for.  */
 struct all_inferiors_range
 {
+  all_inferiors_range (process_stratum_target *proc_target = nullptr)
+    : m_filter_target (proc_target)
+  {}
+
   all_inferiors_iterator begin () const
-  { return all_inferiors_iterator (inferior_list); }
+  { return all_inferiors_iterator (m_filter_target, inferior_list); }
   all_inferiors_iterator end () const
   { return all_inferiors_iterator (); }
+
+private:
+  process_stratum_target *m_filter_target;
 };
 
 /* Iterate over all inferiors, safely.  */
@@ -97,10 +133,22 @@ using all_inferiors_safe_iterator
 
 struct all_inferiors_safe_range
 {
+  explicit all_inferiors_safe_range (process_stratum_target *filter_target)
+    : m_filter_target (filter_target)
+  {}
+
+  all_inferiors_safe_range ()
+    : m_filter_target (nullptr)
+  {}
+
   all_inferiors_safe_iterator begin () const
-  { return all_inferiors_safe_iterator (inferior_list); }
+  { return all_inferiors_safe_iterator (m_filter_target, inferior_list); }
   all_inferiors_safe_iterator end () const
   { return all_inferiors_safe_iterator (); }
+
+private:
+  /* The filter.  */
+  process_stratum_target *m_filter_target;
 };
 
 /* A range adapter that makes it possible to iterate over all
@@ -108,10 +156,22 @@ struct all_inferiors_safe_range
 
 struct all_non_exited_inferiors_range
 {
+  explicit all_non_exited_inferiors_range (process_stratum_target *filter_target)
+    : m_filter_target (filter_target)
+  {}
+
+  all_non_exited_inferiors_range ()
+    : m_filter_target (nullptr)
+  {}
+
   all_non_exited_inferiors_iterator begin () const
-  { return all_non_exited_inferiors_iterator (inferior_list); }
+  { return all_non_exited_inferiors_iterator (m_filter_target, inferior_list); }
   all_non_exited_inferiors_iterator end () const
   { return all_non_exited_inferiors_iterator (); }
+
+private:
+  /* The filter.  */
+  process_stratum_target *m_filter_target;
 };
 
 #endif /* !defined (INFERIOR_ITER_H) */
diff --git a/gdb/inferior.c b/gdb/inferior.c
index d2354fa749..577d2e194a 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -90,6 +90,8 @@ inferior::inferior (int pid_)
     registry_data ()
 {
   inferior_alloc_data (this);
+
+  m_target_stack.push (get_dummy_target ());
 }
 
 struct inferior *
@@ -276,14 +278,14 @@ find_inferior_id (int num)
 }
 
 struct inferior *
-find_inferior_pid (int pid)
+find_inferior_pid (process_stratum_target *targ, int pid)
 {
   /* Looking for inferior pid == 0 is always wrong, and indicative of
      a bug somewhere else.  There may be more than one with pid == 0,
      for instance.  */
   gdb_assert (pid != 0);
 
-  for (inferior *inf : all_inferiors ())
+  for (inferior *inf : all_inferiors (targ))
     if (inf->pid == pid)
       return inf;
 
@@ -293,9 +295,9 @@ find_inferior_pid (int pid)
 /* See inferior.h */
 
 struct inferior *
-find_inferior_ptid (ptid_t ptid)
+find_inferior_ptid (process_stratum_target *targ, ptid_t ptid)
 {
-  return find_inferior_pid (ptid.pid ());
+  return find_inferior_pid (targ, ptid.pid ());
 }
 
 /* See inferior.h.  */
@@ -340,11 +342,11 @@ have_inferiors (void)
    in the middle of a 'mourn' operation.  */
 
 int
-number_of_live_inferiors (void)
+number_of_live_inferiors (process_stratum_target *proc_target)
 {
   int num_inf = 0;
 
-  for (inferior *inf : all_non_exited_inferiors ())
+  for (inferior *inf : all_non_exited_inferiors (proc_target))
     if (inf->has_execution ())
       for (thread_info *tp ATTRIBUTE_UNUSED : inf->non_exited_threads ())
 	{
@@ -362,7 +364,7 @@ number_of_live_inferiors (void)
 int
 have_live_inferiors (void)
 {
-  return number_of_live_inferiors () > 0;
+  return number_of_live_inferiors (NULL) > 0;
 }
 
 /* Prune away any unused inferiors, and then prune away no longer used
@@ -696,7 +698,28 @@ add_inferior_with_spaces (void)
   return inf;
 }
 
-/* add-inferior [-copies N] [-exec FILENAME]  */
+/* Switch to inferior NEW_INF, a new inferior, and unless
+   NO_CONNECTION is true, push the process_stratum_target of ORG_INF
+   to NEW_INF.  */
+
+static void
+switch_to_inferior_and_push_target (inferior *new_inf,
+				    bool no_connection, inferior *org_inf)
+{
+  process_stratum_target *proc_target = org_inf->process_target ();
+
+  /* Switch over temporarily, while reading executable and
+     symbols.  */
+  switch_to_inferior_no_thread (new_inf);
+
+  /* Reuse the target for new inferior.  */
+  if (!no_connection && proc_target != NULL)
+    push_target (proc_target);
+
+  printf_filtered (_("Added inferior %d\n"), new_inf->num);
+}
+
+/* add-inferior [-copies N] [-exec FILENAME] [-no-connection] */
 
 static void
 add_inferior_command (const char *args, int from_tty)
@@ -704,6 +727,7 @@ add_inferior_command (const char *args, int from_tty)
   int i, copies = 1;
   gdb::unique_xmalloc_ptr<char> exec;
   symfile_add_flags add_flags = 0;
+  bool no_connection = false;
 
   if (from_tty)
     add_flags |= SYMFILE_VERBOSE;
@@ -723,6 +747,8 @@ add_inferior_command (const char *args, int from_tty)
 		    error (_("No argument to -copies"));
 		  copies = parse_and_eval_long (*argv);
 		}
+	      else if (strcmp (*argv, "-no-connection") == 0)
+		no_connection = true;
 	      else if (strcmp (*argv, "-exec") == 0)
 		{
 		  ++argv;
@@ -736,32 +762,32 @@ add_inferior_command (const char *args, int from_tty)
 	}
     }
 
+  inferior *orginf = current_inferior ();
+
   scoped_restore_current_pspace_and_thread restore_pspace_thread;
 
   for (i = 0; i < copies; ++i)
     {
-      struct inferior *inf = add_inferior_with_spaces ();
+      inferior *inf = add_inferior_with_spaces ();
 
-      printf_filtered (_("Added inferior %d\n"), inf->num);
+      switch_to_inferior_and_push_target (inf, no_connection, orginf);
 
       if (exec != NULL)
 	{
-	  /* Switch over temporarily, while reading executable and
-	     symbols.  */
-	  switch_to_inferior_no_thread (inf);
 	  exec_file_attach (exec.get (), from_tty);
 	  symbol_file_add_main (exec.get (), add_flags);
 	}
     }
 }
 
-/* clone-inferior [-copies N] [ID] */
+/* clone-inferior [-copies N] [ID] [-no-connection] */
 
 static void
 clone_inferior_command (const char *args, int from_tty)
 {
   int i, copies = 1;
   struct inferior *orginf = NULL;
+  bool no_connection = false;
 
   if (args)
     {
@@ -782,6 +808,8 @@ clone_inferior_command (const char *args, int from_tty)
 		  if (copies < 0)
 		    error (_("Invalid copies number"));
 		}
+	      else if (strcmp (*argv, "-no-connection") == 0)
+		no_connection = true;
 	    }
 	  else
 	    {
@@ -827,15 +855,13 @@ clone_inferior_command (const char *args, int from_tty)
       inf->aspace = pspace->aspace;
       inf->gdbarch = orginf->gdbarch;
 
+      switch_to_inferior_and_push_target (inf, no_connection, orginf);
+
       /* If the original inferior had a user specified target
 	 description, make the clone use it too.  */
       if (target_desc_info_from_user_p (inf->tdesc_info))
 	copy_inferior_target_desc_info (inf, orginf);
 
-      printf_filtered (_("Added inferior %d.\n"), inf->num);
-
-      set_current_inferior (inf);
-      switch_to_no_thread ();
       clone_program_space (pspace, orginf->pspace);
     }
 }
@@ -896,10 +922,13 @@ By default all inferiors are displayed."));
 
   c = add_com ("add-inferior", no_class, add_inferior_command, _("\
 Add a new inferior.\n\
-Usage: add-inferior [-copies N] [-exec FILENAME]\n\
+Usage: add-inferior [-copies N] [-exec FILENAME] [-no-connection]\n\
 N is the optional number of inferiors to add, default is 1.\n\
 FILENAME is the file name of the executable to use\n\
-as main program."));
+as main program.\n\
+By default, the new inferior inherits the current inferior's connection.\n\
+If -no-connection is specified, the new inferior begins with\n\
+no target connection yet."));
   set_cmd_completer (c, filename_completer);
 
   add_com ("remove-inferiors", no_class, remove_inferior_command, _("\
@@ -908,11 +937,14 @@ Usage: remove-inferiors ID..."));
 
   add_com ("clone-inferior", no_class, clone_inferior_command, _("\
 Clone inferior ID.\n\
-Usage: clone-inferior [-copies N] [ID]\n\
-Add N copies of inferior ID.  The new inferior has the same\n\
+Usage: clone-inferior [-copies N] [-no-connection] [ID]\n\
+Add N copies of inferior ID.  The new inferiors have the same\n\
 executable loaded as the copied inferior.  If -copies is not specified,\n\
 adds 1 copy.  If ID is not specified, it is the current inferior\n\
-that is cloned."));
+that is cloned.\n\
+By default, the new inferiors inherit the copied inferior's connection.\n\
+If -no-connection is specified, the new inferiors begin with\n\
+no target connection yet."));
 
   add_cmd ("inferiors", class_run, detach_inferior_command, _("\
 Detach from inferior ID (or list of IDS).\n\
diff --git a/gdb/inferior.h b/gdb/inferior.h
index 2e19b751ca..bf28e6de36 100644
--- a/gdb/inferior.h
+++ b/gdb/inferior.h
@@ -56,6 +56,8 @@ struct thread_info;
 #include "gdbsupport/common-inferior.h"
 #include "gdbthread.h"
 
+#include "process-stratum-target.h"
+
 struct infcall_suspend_state;
 struct infcall_control_state;
 
@@ -208,7 +210,7 @@ extern void registers_info (const char *, int);
 
 extern void continue_1 (int all_threads);
 
-extern void interrupt_target_1 (int all_threads);
+extern void interrupt_target_1 (bool all_threads);
 
 using delete_longjmp_breakpoint_cleanup
   = FORWARD_SCOPE_EXIT (delete_longjmp_breakpoint);
@@ -345,6 +347,35 @@ public:
   /* Returns true if we can delete this inferior.  */
   bool deletable () const { return refcount () == 0; }
 
+  /* Push T in this inferior's target stack.  */
+  void push_target (struct target_ops *t)
+  { m_target_stack.push (t); }
+
+  /* Unpush T from this inferior's target stack.  */
+  int unpush_target (struct target_ops *t)
+  { return m_target_stack.unpush (t); }
+
+  /* Returns true if T is pushed in this inferior's target stack.  */
+  bool target_is_pushed (target_ops *t)
+  { return m_target_stack.is_pushed (t); }
+
+  /* Find the target beneath T in this inferior's target stack.  */
+  target_ops *find_target_beneath (const target_ops *t)
+  { return m_target_stack.find_beneath (t); }
+
+  /* Return the target at the top of this inferior's target stack.  */
+  target_ops *top_target ()
+  { return m_target_stack.top (); }
+
+  /* Return the target at process_stratum level in this inferior's
+     target stack.  */
+  struct process_stratum_target *process_target ()
+  { return (process_stratum_target *) m_target_stack.at (process_stratum); }
+
+  /* Return the target at STRATUM in this inferior's target stack.  */
+  target_ops *target_at (enum strata stratum)
+  { return m_target_stack.at (stratum); }
+
   bool has_execution ()
   { return target_has_execution_1 (this); }
 
@@ -509,6 +540,10 @@ public:
 
   /* Per inferior data-pointers required by other GDB modules.  */
   REGISTRY_FIELDS;
+
+private:
+  /* The inferior's target stack.  */
+  target_stack m_target_stack;
 };
 
 /* Keep a registry of per-inferior data-pointers required by other GDB
@@ -539,14 +574,14 @@ extern void exit_inferior_num_silent (int num);
 
 extern void inferior_appeared (struct inferior *inf, int pid);
 
-/* Get rid of all inferiors.  */
-extern void discard_all_inferiors (void);
+/* Search function to lookup an inferior of TARG by target 'pid'.  */
+extern struct inferior *find_inferior_pid (process_stratum_target *targ,
+					   int pid);
 
-/* Search function to lookup an inferior by target 'pid'.  */
-extern struct inferior *find_inferior_pid (int pid);
-
-/* Search function to lookup an inferior whose pid is equal to 'ptid.pid'. */
-extern struct inferior *find_inferior_ptid (ptid_t ptid);
+/* Search function to lookup an inferior of TARG whose pid is equal to
+   'ptid.pid'. */
+extern struct inferior *find_inferior_ptid (process_stratum_target *targ,
+					    ptid_t ptid);
 
 /* Search function to lookup an inferior by GDB 'num'.  */
 extern struct inferior *find_inferior_id (int num);
@@ -573,8 +608,9 @@ extern struct inferior *iterate_over_inferiors (int (*) (struct inferior *,
 /* Returns true if the inferior list is not empty.  */
 extern int have_inferiors (void);
 
-/* Returns the number of live inferiors (real live processes).  */
-extern int number_of_live_inferiors (void);
+/* Returns the number of live inferiors running on PROC_TARGET (real
+   live processes with execution).  */
+extern int number_of_live_inferiors (process_stratum_target *proc_target);
 
 /* Returns true if there are any live inferiors in the inferior list
    (not cores, not executables, real live processes).  */
@@ -631,18 +667,18 @@ all_inferiors_safe ()
 */
 
 inline all_inferiors_range
-all_inferiors ()
+all_inferiors (process_stratum_target *proc_target = nullptr)
 {
-  return {};
+  return all_inferiors_range (proc_target);
 }
 
 /* Return a range that can be used to walk over all inferiors with PID
    not zero, with range-for.  */
 
 inline all_non_exited_inferiors_range
-all_non_exited_inferiors ()
+all_non_exited_inferiors (process_stratum_target *proc_target = nullptr)
 {
-  return {};
+  return all_non_exited_inferiors_range (proc_target);
 }
 
 /* Prune away automatically added inferiors that aren't required
diff --git a/gdb/infrun.c b/gdb/infrun.c
index ab73a56604..f00761bd54 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -69,6 +69,8 @@
 #include "arch-utils.h"
 #include "gdbsupport/scope-exit.h"
 #include "gdbsupport/forward-scope-exit.h"
+#include "gdb_select.h"
+#include <unordered_map>
 
 /* Prototypes for local functions */
 
@@ -94,6 +96,8 @@ static int maybe_software_singlestep (struct gdbarch *gdbarch, CORE_ADDR pc);
 
 static void resume (gdb_signal sig);
 
+static void wait_for_inferior (inferior *inf);
+
 /* Asynchronous signal handler registered as event loop source for
    when we have pending events ready to be passed to the core.  */
 static struct async_event_handler *infrun_async_inferior_event_token;
@@ -376,9 +380,10 @@ show_stop_on_solib_events (struct ui_file *file, int from_tty,
 
 static int stop_print_frame;
 
-/* This is a cached copy of the pid/waitstatus of the last event
-   returned by target_wait()/deprecated_target_wait_hook().  This
-   information is returned by get_last_target_status().  */
+/* This is a cached copy of the target/ptid/waitstatus of the last
+   event returned by target_wait()/deprecated_target_wait_hook().
+   This information is returned by get_last_target_status().  */
+static process_stratum_target *target_last_proc_target;
 static ptid_t target_last_wait_ptid;
 static struct target_waitstatus target_last_waitstatus;
 
@@ -485,10 +490,12 @@ holding the child stopped.  Try \"set detach-on-fork\" or \
 
 	  scoped_restore_current_pspace_and_thread restore_pspace_thread;
 
-	  inferior_ptid = child_ptid;
-	  add_thread_silent (inferior_ptid);
 	  set_current_inferior (child_inf);
+	  switch_to_no_thread ();
 	  child_inf->symfile_flags = SYMFILE_NO_READ;
+	  push_target (parent_inf->process_target ());
+	  add_thread_silent (child_inf->process_target (), child_ptid);
+	  inferior_ptid = child_ptid;
 
 	  /* If this is a vfork child, then the address-space is
 	     shared with the parent.  */
@@ -497,6 +504,8 @@ holding the child stopped.  Try \"set detach-on-fork\" or \
 	      child_inf->pspace = parent_inf->pspace;
 	      child_inf->aspace = parent_inf->aspace;
 
+	      exec_on_vfork ();
+
 	      /* The parent will be frozen until the child is done
 		 with the shared region.  Keep track of the
 		 parent.  */
@@ -572,52 +581,64 @@ holding the child stopped.  Try \"set detach-on-fork\" or \
 
       parent_pspace = parent_inf->pspace;
 
-      /* If we're vforking, we want to hold on to the parent until the
-	 child exits or execs.  At child exec or exit time we can
-	 remove the old breakpoints from the parent and detach or
-	 resume debugging it.  Otherwise, detach the parent now; we'll
-	 want to reuse it's program/address spaces, but we can't set
-	 them to the child before removing breakpoints from the
-	 parent, otherwise, the breakpoints module could decide to
-	 remove breakpoints from the wrong process (since they'd be
-	 assigned to the same address space).  */
+      process_stratum_target *target = parent_inf->process_target ();
 
-      if (has_vforked)
-	{
-	  gdb_assert (child_inf->vfork_parent == NULL);
-	  gdb_assert (parent_inf->vfork_child == NULL);
-	  child_inf->vfork_parent = parent_inf;
-	  child_inf->pending_detach = 0;
-	  parent_inf->vfork_child = child_inf;
-	  parent_inf->pending_detach = detach_fork;
-	  parent_inf->waiting_for_vfork_done = 0;
-	}
-      else if (detach_fork)
-	{
-	  if (print_inferior_events)
-	    {
-	      /* Ensure that we have a process ptid.  */
-	      ptid_t process_ptid = ptid_t (parent_ptid.pid ());
+      {
+	/* Hold a strong reference to the target while (maybe)
+	   detaching the parent.  Otherwise detaching could close the
+	   target.  */
+	auto target_ref = target_ops_ref::new_reference (target);
+
+	/* If we're vforking, we want to hold on to the parent until
+	   the child exits or execs.  At child exec or exit time we
+	   can remove the old breakpoints from the parent and detach
+	   or resume debugging it.  Otherwise, detach the parent now;
+	   we'll want to reuse it's program/address spaces, but we
+	   can't set them to the child before removing breakpoints
+	   from the parent, otherwise, the breakpoints module could
+	   decide to remove breakpoints from the wrong process (since
+	   they'd be assigned to the same address space).  */
+
+	if (has_vforked)
+	  {
+	    gdb_assert (child_inf->vfork_parent == NULL);
+	    gdb_assert (parent_inf->vfork_child == NULL);
+	    child_inf->vfork_parent = parent_inf;
+	    child_inf->pending_detach = 0;
+	    parent_inf->vfork_child = child_inf;
+	    parent_inf->pending_detach = detach_fork;
+	    parent_inf->waiting_for_vfork_done = 0;
+	  }
+	else if (detach_fork)
+	  {
+	    if (print_inferior_events)
+	      {
+		/* Ensure that we have a process ptid.  */
+		ptid_t process_ptid = ptid_t (parent_ptid.pid ());
+
+		target_terminal::ours_for_output ();
+		fprintf_filtered (gdb_stdlog,
+				  _("[Detaching after fork from "
+				    "parent %s]\n"),
+				  target_pid_to_str (process_ptid).c_str ());
+	      }
 
-	      target_terminal::ours_for_output ();
-	      fprintf_filtered (gdb_stdlog,
-				_("[Detaching after fork from "
-				  "parent %s]\n"),
-				target_pid_to_str (process_ptid).c_str ());
-	    }
+	    target_detach (parent_inf, 0);
+	    parent_inf = NULL;
+	  }
 
-	  target_detach (parent_inf, 0);
-	}
+	/* Note that the detach above makes PARENT_INF dangling.  */
 
-      /* Note that the detach above makes PARENT_INF dangling.  */
+	/* Add the child thread to the appropriate lists, and switch
+	   to this new thread, before cloning the program space, and
+	   informing the solib layer about this new process.  */
 
-      /* Add the child thread to the appropriate lists, and switch to
-	 this new thread, before cloning the program space, and
-	 informing the solib layer about this new process.  */
+	set_current_inferior (child_inf);
+	push_target (target);
+      }
 
+      add_thread_silent (target, child_ptid);
       inferior_ptid = child_ptid;
-      add_thread_silent (inferior_ptid);
-      set_current_inferior (child_inf);
 
       /* If this is a vfork child, then the address-space is shared
 	 with the parent.  If we detached from the parent, then we can
@@ -626,6 +647,8 @@ holding the child stopped.  Try \"set detach-on-fork\" or \
 	{
 	  child_inf->pspace = parent_pspace;
 	  child_inf->aspace = child_inf->pspace->aspace;
+
+	  exec_on_vfork ();
 	}
       else
 	{
@@ -672,11 +695,12 @@ follow_fork (void)
 
   if (!non_stop)
     {
+      process_stratum_target *wait_target;
       ptid_t wait_ptid;
       struct target_waitstatus wait_status;
 
       /* Get the last target status returned by target_wait().  */
-      get_last_target_status (&wait_ptid, &wait_status);
+      get_last_target_status (&wait_target, &wait_ptid, &wait_status);
 
       /* If not stopped at a fork event, then there's nothing else to
 	 do.  */
@@ -687,14 +711,14 @@ follow_fork (void)
       /* Check if we switched over from WAIT_PTID, since the event was
 	 reported.  */
       if (wait_ptid != minus_one_ptid
-	  && inferior_ptid != wait_ptid)
+	  && (current_inferior ()->process_target () != wait_target
+	      || inferior_ptid != wait_ptid))
 	{
 	  /* We did.  Switch back to WAIT_PTID thread, to tell the
 	     target to follow it (in either direction).  We'll
 	     afterwards refuse to resume, and inform the user what
 	     happened.  */
-	  thread_info *wait_thread
-	    = find_thread_ptid (wait_ptid);
+	  thread_info *wait_thread = find_thread_ptid (wait_target, wait_ptid);
 	  switch_to_thread (wait_thread);
 	  should_resume = 0;
 	}
@@ -740,6 +764,7 @@ follow_fork (void)
 	parent = inferior_ptid;
 	child = tp->pending_follow.value.related_pid;
 
+	process_stratum_target *parent_targ = tp->inf->process_target ();
 	/* Set up inferior(s) as specified by the caller, and tell the
 	   target to do whatever is necessary to follow either parent
 	   or child.  */
@@ -755,7 +780,7 @@ follow_fork (void)
 	       or another.  The previous selected thread may be gone
 	       from the lists by now, but if it is still around, need
 	       to clear the pending follow request.  */
-	    tp = find_thread_ptid (parent);
+	    tp = find_thread_ptid (parent_targ, parent);
 	    if (tp)
 	      tp->pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
 
@@ -766,7 +791,7 @@ follow_fork (void)
 	    /* If we followed the child, switch to it...  */
 	    if (follow_child)
 	      {
-		thread_info *child_thr = find_thread_ptid (child);
+		thread_info *child_thr = find_thread_ptid (parent_targ, child);
 		switch_to_thread (child_thr);
 
 		/* ... and preserve the stepping state, in case the
@@ -1195,9 +1220,11 @@ follow_exec (ptid_t ptid, const char *exec_file_target)
       inf->pid = pid;
       target_follow_exec (inf, exec_file_target);
 
-      set_current_inferior (inf);
-      set_current_program_space (inf->pspace);
-      add_thread (ptid);
+      inferior *org_inferior = current_inferior ();
+      switch_to_inferior_no_thread (inf);
+      push_target (org_inferior->process_target ());
+      thread_info *thr = add_thread (inf->process_target (), ptid);
+      switch_to_thread (thr);
     }
   else
     {
@@ -1891,6 +1918,7 @@ displaced_step_fixup (thread_info *event_thread, enum gdb_signal signal)
    discarded between events.  */
 struct execution_control_state
 {
+  process_stratum_target *target;
   ptid_t ptid;
   /* The thread that got the event, if this was a thread event; NULL
      otherwise.  */
@@ -2147,6 +2175,16 @@ user_visible_resume_ptid (int step)
   return resume_ptid;
 }
 
+/* See infrun.h.  */
+
+process_stratum_target *
+user_visible_resume_target (ptid_t resume_ptid)
+{
+  return (resume_ptid == minus_one_ptid && sched_multi
+	  ? NULL
+	  : current_inferior ()->process_target ());
+}
+
 /* Return a ptid representing the set of threads that we will resume,
    in the perspective of the target, assuming run control handling
    does not require leaving some threads stopped (e.g., stepping past
@@ -2211,6 +2249,9 @@ do_target_resume (ptid_t resume_ptid, int step, enum gdb_signal sig)
   target_resume (resume_ptid, step, sig);
 
   target_commit_resume ();
+
+  if (target_can_async_p ())
+    target_async (1);
 }
 
 /* Resume the inferior.  SIG is the signal to give the inferior
@@ -2254,6 +2295,7 @@ resume_1 (enum gdb_signal sig)
 			      currently_stepping (tp));
 	}
 
+      tp->inf->process_target ()->threads_executing = true;
       tp->resumed = 1;
 
       /* FIXME: What should we do if we are supposed to resume this
@@ -2739,10 +2781,12 @@ clear_proceed_status (int step)
   if (!non_stop && inferior_ptid != null_ptid)
     {
       ptid_t resume_ptid = user_visible_resume_ptid (step);
+      process_stratum_target *resume_target
+	= user_visible_resume_target (resume_ptid);
 
       /* In all-stop mode, delete the per-thread status of all threads
 	 we're about to resume, implicitly and explicitly.  */
-      for (thread_info *tp : all_non_exited_threads (resume_ptid))
+      for (thread_info *tp : all_non_exited_threads (resume_target, resume_ptid))
 	clear_proceed_status_thread (tp);
     }
 
@@ -2819,6 +2863,31 @@ schedlock_applies (struct thread_info *tp)
 					    execution_direction)));
 }
 
+/* Calls target_commit_resume on all targets.  */
+
+static void
+commit_resume_all_targets ()
+{
+  scoped_restore_current_thread restore_thread;
+
+  /* Map between process_target and a representative inferior.  This
+     is to avoid committing a resume in the same target more than
+     once.  Resumptions must be idempotent, so this is an
+     optimization.  */
+  std::unordered_map<process_stratum_target *, inferior *> conn_inf;
+
+  for (inferior *inf : all_non_exited_inferiors ())
+    if (inf->has_execution ())
+      conn_inf[inf->process_target ()] = inf;
+
+  for (const auto &ci : conn_inf)
+    {
+      inferior *inf = ci.second;
+      switch_to_inferior_no_thread (inf);
+      target_commit_resume ();
+    }
+}
+
 /* Basic routine for continuing the program in various fashions.
 
    ADDR is the address to resume at, or -1 for resume where stopped.
@@ -2833,7 +2902,6 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
   struct regcache *regcache;
   struct gdbarch *gdbarch;
   CORE_ADDR pc;
-  ptid_t resume_ptid;
   struct execution_control_state ecss;
   struct execution_control_state *ecs = &ecss;
   int started;
@@ -2865,6 +2933,11 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
 
   gdb_assert (!thread_is_in_step_over_chain (cur_thr));
 
+  ptid_t resume_ptid
+    = user_visible_resume_ptid (cur_thr->control.stepping_command);
+  process_stratum_target *resume_target
+    = user_visible_resume_target (resume_ptid);
+
   if (addr == (CORE_ADDR) -1)
     {
       if (pc == cur_thr->suspend.stop_pc
@@ -2894,12 +2967,10 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
   if (siggnal != GDB_SIGNAL_DEFAULT)
     cur_thr->suspend.stop_signal = siggnal;
 
-  resume_ptid = user_visible_resume_ptid (cur_thr->control.stepping_command);
-
   /* If an exception is thrown from this point on, make sure to
      propagate GDB's knowledge of the executing state to the
      frontend/user running state.  */
-  scoped_finish_thread_state finish_state (resume_ptid);
+  scoped_finish_thread_state finish_state (resume_target, resume_ptid);
 
   /* Even if RESUME_PTID is a wildcard, and we end up resuming fewer
      threads (e.g., we might need to set threads stepping over
@@ -2908,7 +2979,7 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
      inferior function, as in that case we pretend the inferior
      doesn't run at all.  */
   if (!cur_thr->control.in_infcall)
-   set_running (resume_ptid, 1);
+    set_running (resume_target, resume_ptid, 1);
 
   if (debug_infrun)
     fprintf_unfiltered (gdb_stdlog,
@@ -2943,7 +3014,8 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
      threads.  */
   if (!non_stop && !schedlock_applies (cur_thr))
     {
-      for (thread_info *tp : all_non_exited_threads (resume_ptid))
+      for (thread_info *tp : all_non_exited_threads (resume_target,
+						     resume_ptid))
 	{
 	  switch_to_thread_no_regs (tp);
 
@@ -3000,9 +3072,20 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
       {
 	/* In all-stop, but the target is always in non-stop mode.
 	   Start all other threads that are implicitly resumed too.  */
-      for (thread_info *tp : all_non_exited_threads (resume_ptid))
-        {
-	  switch_to_thread_no_regs (tp);
+	for (thread_info *tp : all_non_exited_threads (resume_target,
+						       resume_ptid))
+	  {
+	    switch_to_thread_no_regs (tp);
+
+	  if (!tp->inf->has_execution ())
+	    {
+	      if (debug_infrun)
+		fprintf_unfiltered (gdb_stdlog,
+				    "infrun: proceed: [%s] target has "
+				    "no execution\n",
+				    target_pid_to_str (tp->ptid).c_str ());
+	      continue;
+	    }
 
 	  if (tp->resumed)
 	    {
@@ -3046,7 +3129,7 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
       }
   }
 
-  target_commit_resume ();
+  commit_resume_all_targets ();
 
   finish_state.release ();
 
@@ -3068,10 +3151,8 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
 void
 start_remote (int from_tty)
 {
-  struct inferior *inferior;
-
-  inferior = current_inferior ();
-  inferior->control.stop_soon = STOP_QUIETLY_REMOTE;
+  inferior *inf = current_inferior ();
+  inf->control.stop_soon = STOP_QUIETLY_REMOTE;
 
   /* Always go on waiting for the target, regardless of the mode.  */
   /* FIXME: cagney/1999-09-23: At present it isn't possible to
@@ -3087,7 +3168,7 @@ start_remote (int from_tty)
      target_open() return to the caller an indication that the target
      is currently running and GDB state should be set to the same as
      for an async run.  */
-  wait_for_inferior ();
+  wait_for_inferior (inf);
 
   /* Now that the inferior has stopped, do any bookkeeping like
      loading shared libraries.  We want to do this before normal_stop,
@@ -3138,11 +3219,13 @@ static int switch_back_to_stepped_thread (struct execution_control_state *ecs);
 static void
 infrun_thread_stop_requested (ptid_t ptid)
 {
+  process_stratum_target *curr_target = current_inferior ()->process_target ();
+
   /* PTID was requested to stop.  If the thread was already stopped,
      but the user/frontend doesn't know about that yet (e.g., the
      thread had been temporarily paused for some step-over), set up
      for reporting the stop now.  */
-  for (thread_info *tp : all_threads (ptid))
+  for (thread_info *tp : all_threads (curr_target, ptid))
     {
       if (tp->state != THREAD_RUNNING)
 	continue;
@@ -3168,7 +3251,7 @@ infrun_thread_stop_requested (ptid_t ptid)
 
       /* Clear the inline-frame state, since we're re-processing the
 	 stop.  */
-      clear_inline_frame_state (tp->ptid);
+      clear_inline_frame_state (tp);
 
       /* If this thread was paused because some other thread was
 	 doing an inline-step over, let that finish first.  Once
@@ -3187,7 +3270,8 @@ infrun_thread_stop_requested (ptid_t ptid)
 static void
 infrun_thread_thread_exit (struct thread_info *tp, int silent)
 {
-  if (target_last_wait_ptid == tp->ptid)
+  if (target_last_proc_target == tp->inf->process_target ()
+      && target_last_wait_ptid == tp->ptid)
     nullify_last_target_wait_ptid ();
 }
 
@@ -3283,19 +3367,20 @@ print_target_wait_results (ptid_t waiton_ptid, ptid_t result_ptid,
    had events.  */
 
 static struct thread_info *
-random_pending_event_thread (ptid_t waiton_ptid)
+random_pending_event_thread (inferior *inf, ptid_t waiton_ptid)
 {
   int num_events = 0;
 
-  auto has_event = [] (thread_info *tp)
+  auto has_event = [&] (thread_info *tp)
     {
-      return (tp->resumed
+      return (tp->ptid.matches (waiton_ptid)
+	      && tp->resumed
 	      && tp->suspend.waitstatus_pending_p);
     };
 
   /* First see how many events we have.  Count only resumed threads
      that have an event pending.  */
-  for (thread_info *tp : all_non_exited_threads (waiton_ptid))
+  for (thread_info *tp : inf->non_exited_threads ())
     if (has_event (tp))
       num_events++;
 
@@ -3312,7 +3397,7 @@ random_pending_event_thread (ptid_t waiton_ptid)
 			num_events, random_selector);
 
   /* Select the Nth thread that has had an event.  */
-  for (thread_info *tp : all_non_exited_threads (waiton_ptid))
+  for (thread_info *tp : inf->non_exited_threads ())
     if (has_event (tp))
       if (random_selector-- == 0)
 	return tp;
@@ -3322,10 +3407,12 @@ random_pending_event_thread (ptid_t waiton_ptid)
 
 /* Wrapper for target_wait that first checks whether threads have
    pending statuses to report before actually asking the target for
-   more events.  */
+   more events.  INF is the inferior we're using to call target_wait
+   on.  */
 
 static ptid_t
-do_target_wait (ptid_t ptid, struct target_waitstatus *status, int options)
+do_target_wait_1 (inferior *inf, ptid_t ptid,
+		  target_waitstatus *status, int options)
 {
   ptid_t event_ptid;
   struct thread_info *tp;
@@ -3334,7 +3421,7 @@ do_target_wait (ptid_t ptid, struct target_waitstatus *status, int options)
      pending.  */
   if (ptid == minus_one_ptid || ptid.is_pid ())
     {
-      tp = random_pending_event_thread (ptid);
+      tp = random_pending_event_thread (inf, ptid);
     }
   else
     {
@@ -3344,7 +3431,7 @@ do_target_wait (ptid_t ptid, struct target_waitstatus *status, int options)
 			    target_pid_to_str (ptid).c_str ());
 
       /* We have a specific thread to check.  */
-      tp = find_thread_ptid (ptid);
+      tp = find_thread_ptid (inf, ptid);
       gdb_assert (tp != NULL);
       if (!tp->suspend.waitstatus_pending_p)
 	tp = NULL;
@@ -3451,6 +3538,109 @@ do_target_wait (ptid_t ptid, struct target_waitstatus *status, int options)
   return event_ptid;
 }
 
+/* Returns true if INF has any resumed thread with a status
+   pending.  */
+
+static bool
+threads_are_resumed_pending_p (inferior *inf)
+{
+  for (thread_info *tp : inf->non_exited_threads ())
+    if (tp->resumed
+	&& tp->suspend.waitstatus_pending_p)
+      return true;
+
+  return false;
+}
+
+/* Wrapper for target_wait that first checks whether threads have
+   pending statuses to report before actually asking the target for
+   more events. Polls for events from all inferiors/targets.  */
+
+static bool
+do_target_wait (ptid_t wait_ptid, execution_control_state *ecs, int options)
+{
+  int num_inferiors = 0;
+  int random_selector;
+
+  /* For fairness, we pick the first inferior/target to poll at
+     random, and then continue polling the rest of the inferior list
+     starting from that one in a circular fashion until the whole list
+     is polled once.  */
+
+  auto inferior_matches = [&wait_ptid] (inferior *inf)
+    {
+      return (inf->process_target () != NULL
+	      && (threads_are_executing (inf->process_target ())
+		  || threads_are_resumed_pending_p (inf))
+	      && ptid_t (inf->pid).matches (wait_ptid));
+    };
+
+  /* First see how many resumed inferiors we have.  */
+  for (inferior *inf : all_inferiors ())
+    if (inferior_matches (inf))
+      num_inferiors++;
+
+  if (num_inferiors == 0)
+    {
+      ecs->ws.kind = TARGET_WAITKIND_IGNORE;
+      return false;
+    }
+
+  /* Now randomly pick an inferior out of those that were resumed.  */
+  random_selector = (int)
+    ((num_inferiors * (double) rand ()) / (RAND_MAX + 1.0));
+
+  if (debug_infrun && num_inferiors > 1)
+    fprintf_unfiltered (gdb_stdlog,
+			"infrun: Found %d inferiors, starting at #%d\n",
+			num_inferiors, random_selector);
+
+  /* Select the Nth inferior that was resumed.  */
+
+  inferior *selected = nullptr;
+
+  for (inferior *inf : all_inferiors ())
+    if (inferior_matches (inf))
+      if (random_selector-- == 0)
+	{
+	  selected = inf;
+	  break;
+	}
+
+  /* Now poll for events out of each of the resumed inferior's
+     targets, starting from the selected one.  */
+
+  auto do_wait = [&] (inferior *inf)
+  {
+    switch_to_inferior_no_thread (inf);
+
+    ecs->ptid = do_target_wait_1 (inf, wait_ptid, &ecs->ws, options);
+    ecs->target = inf->process_target ();
+    return (ecs->ws.kind != TARGET_WAITKIND_IGNORE);
+  };
+
+  /* Needed in all-stop+target-non-stop mode, because we end up here
+     spuriously after the target is all stopped and we've already
+     reported the stop to the user, polling for events.  */
+  scoped_restore_current_thread restore_thread;
+
+  int inf_num = selected->num;
+  for (inferior *inf = selected; inf != NULL; inf = inf->next)
+    if (inferior_matches (inf))
+      if (do_wait (inf))
+	return true;
+
+  for (inferior *inf = inferior_list;
+       inf != NULL && inf->num < inf_num;
+       inf = inf->next)
+    if (inferior_matches (inf))
+      if (do_wait (inf))
+	return true;
+
+  ecs->ws.kind = TARGET_WAITKIND_IGNORE;
+  return false;
+}
+
 /* Prepare and stabilize the inferior for detaching it.  E.g.,
    detaching while a thread is displaced stepping is a recipe for
    crashing it, as nothing would readjust the PC out of the scratch
@@ -3490,7 +3680,7 @@ prepare_for_detach (void)
 	 don't get any event.  */
       target_dcache_invalidate ();
 
-      ecs->ptid = do_target_wait (pid_ptid, &ecs->ws, 0);
+      do_target_wait (pid_ptid, ecs, 0);
 
       if (debug_infrun)
 	print_target_wait_results (pid_ptid, ecs->ptid, &ecs->ws);
@@ -3498,7 +3688,8 @@ prepare_for_detach (void)
       /* If an error happens while handling the event, propagate GDB's
 	 knowledge of the executing state to the frontend/user running
 	 state.  */
-      scoped_finish_thread_state finish_state (minus_one_ptid);
+      scoped_finish_thread_state finish_state (inf->process_target (),
+					       minus_one_ptid);
 
       /* Now figure out what to do with the result of the result.  */
       handle_inferior_event (ecs);
@@ -3526,8 +3717,8 @@ prepare_for_detach (void)
    When this function actually returns it means the inferior
    should be left stopped and GDB should read more commands.  */
 
-void
-wait_for_inferior (void)
+static void
+wait_for_inferior (inferior *inf)
 {
   if (debug_infrun)
     fprintf_unfiltered
@@ -3538,13 +3729,13 @@ wait_for_inferior (void)
   /* If an error happens while handling the event, propagate GDB's
      knowledge of the executing state to the frontend/user running
      state.  */
-  scoped_finish_thread_state finish_state (minus_one_ptid);
+  scoped_finish_thread_state finish_state
+    (inf->process_target (), minus_one_ptid);
 
   while (1)
     {
       struct execution_control_state ecss;
       struct execution_control_state *ecs = &ecss;
-      ptid_t waiton_ptid = minus_one_ptid;
 
       memset (ecs, 0, sizeof (*ecs));
 
@@ -3556,10 +3747,11 @@ wait_for_inferior (void)
 	 don't get any event.  */
       target_dcache_invalidate ();
 
-      ecs->ptid = do_target_wait (waiton_ptid, &ecs->ws, 0);
+      ecs->ptid = do_target_wait_1 (inf, minus_one_ptid, &ecs->ws, 0);
+      ecs->target = inf->process_target ();
 
       if (debug_infrun)
-	print_target_wait_results (waiton_ptid, ecs->ptid, &ecs->ws);
+	print_target_wait_results (minus_one_ptid, ecs->ptid, &ecs->ws);
 
       /* Now figure out what to do with the result of the result.  */
       handle_inferior_event (ecs);
@@ -3685,7 +3877,6 @@ fetch_inferior_event (void *client_data)
   struct execution_control_state ecss;
   struct execution_control_state *ecs = &ecss;
   int cmd_done = 0;
-  ptid_t waiton_ptid = minus_one_ptid;
 
   memset (ecs, 0, sizeof (*ecs));
 
@@ -3726,17 +3917,28 @@ fetch_inferior_event (void *client_data)
       = make_scoped_restore (&execution_direction,
 			     target_execution_direction ());
 
-    ecs->ptid = do_target_wait (waiton_ptid, &ecs->ws,
-				target_can_async_p () ? TARGET_WNOHANG : 0);
+    if (!do_target_wait (minus_one_ptid, ecs, TARGET_WNOHANG))
+      return;
+
+    gdb_assert (ecs->ws.kind != TARGET_WAITKIND_IGNORE);
+
+    /* Switch to the target that generated the event, so we can do
+       target calls.  Any inferior bound to the target will do, so we
+       just switch to the first we find.  */
+    for (inferior *inf : all_inferiors (ecs->target))
+      {
+	switch_to_inferior_no_thread (inf);
+	break;
+      }
 
     if (debug_infrun)
-      print_target_wait_results (waiton_ptid, ecs->ptid, &ecs->ws);
+      print_target_wait_results (minus_one_ptid, ecs->ptid, &ecs->ws);
 
     /* If an error happens while handling the event, propagate GDB's
        knowledge of the executing state to the frontend/user running
        state.  */
     ptid_t finish_ptid = !target_is_non_stop_p () ? minus_one_ptid : ecs->ptid;
-    scoped_finish_thread_state finish_state (finish_ptid);
+    scoped_finish_thread_state finish_state (ecs->target, finish_ptid);
 
     /* Get executed before scoped_restore_current_thread above to apply
        still for the thread which has thrown the exception.  */
@@ -3750,7 +3952,7 @@ fetch_inferior_event (void *client_data)
 
     if (!ecs->wait_some_more)
       {
-	struct inferior *inf = find_inferior_ptid (ecs->ptid);
+	struct inferior *inf = find_inferior_ptid (ecs->target, ecs->ptid);
 	int should_stop = 1;
 	struct thread_info *thr = ecs->event_thread;
 
@@ -3855,8 +4057,10 @@ init_thread_stepping_state (struct thread_info *tss)
 /* See infrun.h.  */
 
 void
-set_last_target_status (ptid_t ptid, struct target_waitstatus status)
+set_last_target_status (process_stratum_target *target, ptid_t ptid,
+			target_waitstatus status)
 {
+  target_last_proc_target = target;
   target_last_wait_ptid = ptid;
   target_last_waitstatus = status;
 }
@@ -3864,8 +4068,11 @@ set_last_target_status (ptid_t ptid, struct target_waitstatus status)
 /* See infrun.h.  */
 
 void
-get_last_target_status (ptid_t *ptid, struct target_waitstatus *status)
+get_last_target_status (process_stratum_target **target, ptid_t *ptid,
+			target_waitstatus *status)
 {
+  if (target != nullptr)
+    *target = target_last_proc_target;
   if (ptid != nullptr)
     *ptid = target_last_wait_ptid;
   if (status != nullptr)
@@ -3877,6 +4084,7 @@ get_last_target_status (ptid_t *ptid, struct target_waitstatus *status)
 void
 nullify_last_target_wait_ptid (void)
 {
+  target_last_proc_target = nullptr;
   target_last_wait_ptid = minus_one_ptid;
   target_last_waitstatus = {};
 }
@@ -3888,7 +4096,8 @@ context_switch (execution_control_state *ecs)
 {
   if (debug_infrun
       && ecs->ptid != inferior_ptid
-      && ecs->event_thread != inferior_thread ())
+      && (inferior_ptid == null_ptid
+	  || ecs->event_thread != inferior_thread ()))
     {
       fprintf_unfiltered (gdb_stdlog, "infrun: Switching context from %s ",
 			  target_pid_to_str (inferior_ptid).c_str ());
@@ -4174,20 +4383,19 @@ fill_in_stop_func (struct gdbarch *gdbarch,
 static enum stop_kind
 get_inferior_stop_soon (execution_control_state *ecs)
 {
-  struct inferior *inf = find_inferior_ptid (ecs->ptid);
+  struct inferior *inf = find_inferior_ptid (ecs->target, ecs->ptid);
 
   gdb_assert (inf != NULL);
   return inf->control.stop_soon;
 }
 
-/* Wait for one event.  Store the resulting waitstatus in WS, and
-   return the event ptid.  */
+/* Poll for one event out of the current target.  Store the resulting
+   waitstatus in WS, and return the event ptid.  Does not block.  */
 
 static ptid_t
-wait_one (struct target_waitstatus *ws)
+poll_one_curr_target (struct target_waitstatus *ws)
 {
   ptid_t event_ptid;
-  ptid_t wait_ptid = minus_one_ptid;
 
   overlay_cache_invalid = 1;
 
@@ -4198,16 +4406,101 @@ wait_one (struct target_waitstatus *ws)
   target_dcache_invalidate ();
 
   if (deprecated_target_wait_hook)
-    event_ptid = deprecated_target_wait_hook (wait_ptid, ws, 0);
+    event_ptid = deprecated_target_wait_hook (minus_one_ptid, ws, TARGET_WNOHANG);
   else
-    event_ptid = target_wait (wait_ptid, ws, 0);
+    event_ptid = target_wait (minus_one_ptid, ws, TARGET_WNOHANG);
 
   if (debug_infrun)
-    print_target_wait_results (wait_ptid, event_ptid, ws);
+    print_target_wait_results (minus_one_ptid, event_ptid, ws);
 
   return event_ptid;
 }
 
+/* An event reported by wait_one.  */
+
+struct wait_one_event
+{
+  /* The target the event came out of.  */
+  process_stratum_target *target;
+
+  /* The PTID the event was for.  */
+  ptid_t ptid;
+
+  /* The waitstatus.  */
+  target_waitstatus ws;
+};
+
+/* Wait for one event out of any target.  */
+
+static wait_one_event
+wait_one ()
+{
+  while (1)
+    {
+      for (inferior *inf : all_inferiors ())
+	{
+	  process_stratum_target *target = inf->process_target ();
+	  if (target == NULL
+	      || !target->is_async_p ()
+	      || !target->threads_executing)
+	    continue;
+
+	  switch_to_inferior_no_thread (inf);
+
+	  wait_one_event event;
+	  event.target = target;
+	  event.ptid = poll_one_curr_target (&event.ws);
+
+	  if (event.ws.kind == TARGET_WAITKIND_NO_RESUMED)
+	    {
+	      /* If nothing is resumed, remove the target from the
+		 event loop.  */
+	      target_async (0);
+	    }
+	  else if (event.ws.kind != TARGET_WAITKIND_IGNORE)
+	    return event;
+	}
+
+      /* Block waiting for some event.  */
+
+      fd_set readfds;
+      int nfds = 0;
+
+      FD_ZERO (&readfds);
+
+      for (inferior *inf : all_inferiors ())
+	{
+	  process_stratum_target *target = inf->process_target ();
+	  if (target == NULL
+	      || !target->is_async_p ()
+	      || !target->threads_executing)
+	    continue;
+
+	  int fd = target->async_wait_fd ();
+	  FD_SET (fd, &readfds);
+	  if (nfds <= fd)
+	    nfds = fd + 1;
+	}
+
+      if (nfds == 0)
+	{
+	  /* No waitable targets left.  All must be stopped.  */
+	  return {NULL, minus_one_ptid, {TARGET_WAITKIND_NO_RESUMED}};
+	}
+
+      QUIT;
+
+      int numfds = interruptible_select (nfds, &readfds, 0, NULL, 0);
+      if (numfds < 0)
+	{
+	  if (errno == EINTR)
+	    continue;
+	  else
+	    perror_with_name ("interruptible_select");
+	}
+    }
+}
+
 /* Generate a wrapper for target_stopped_by_REASON that works on PTID
    instead of the current thread.  */
 #define THREAD_STOPPED_BY(REASON)		\
@@ -4230,7 +4523,7 @@ THREAD_STOPPED_BY (hw_breakpoint)
 /* Save the thread's event and stop reason to process it later.  */
 
 static void
-save_waitstatus (struct thread_info *tp, struct target_waitstatus *ws)
+save_waitstatus (struct thread_info *tp, const target_waitstatus *ws)
 {
   if (debug_infrun)
     {
@@ -4330,8 +4623,6 @@ stop_all_threads (void)
 			    "iterations=%d\n", pass, iterations);
       while (1)
 	{
-	  ptid_t event_ptid;
-	  struct target_waitstatus ws;
 	  int need_wait = 0;
 
 	  update_thread_list ();
@@ -4389,28 +4680,29 @@ stop_all_threads (void)
 	  if (pass > 0)
 	    pass = -1;
 
-	  event_ptid = wait_one (&ws);
+	  wait_one_event event = wait_one ();
+
 	  if (debug_infrun)
 	    {
 	      fprintf_unfiltered (gdb_stdlog,
 				  "infrun: stop_all_threads %s %s\n",
-				  target_waitstatus_to_string (&ws).c_str (),
-				  target_pid_to_str (event_ptid).c_str ());
+				  target_waitstatus_to_string (&event.ws).c_str (),
+				  target_pid_to_str (event.ptid).c_str ());
 	    }
 
-	  if (ws.kind == TARGET_WAITKIND_NO_RESUMED
-	      || ws.kind == TARGET_WAITKIND_THREAD_EXITED
-	      || ws.kind == TARGET_WAITKIND_EXITED
-	      || ws.kind == TARGET_WAITKIND_SIGNALLED)
+	  if (event.ws.kind == TARGET_WAITKIND_NO_RESUMED
+	      || event.ws.kind == TARGET_WAITKIND_THREAD_EXITED
+	      || event.ws.kind == TARGET_WAITKIND_EXITED
+	      || event.ws.kind == TARGET_WAITKIND_SIGNALLED)
 	    {
 	      /* All resumed threads exited
 		 or one thread/process exited/signalled.  */
 	    }
 	  else
 	    {
-	      thread_info *t = find_thread_ptid (event_ptid);
+	      thread_info *t = find_thread_ptid (event.target, event.ptid);
 	      if (t == NULL)
-		t = add_thread (event_ptid);
+		t = add_thread (event.target, event.ptid);
 
 	      t->stop_requested = 0;
 	      t->executing = 0;
@@ -4419,15 +4711,15 @@ stop_all_threads (void)
 
 	      /* This may be the first time we see the inferior report
 		 a stop.  */
-	      inferior *inf = find_inferior_ptid (event_ptid);
+	      inferior *inf = find_inferior_ptid (event.target, event.ptid);
 	      if (inf->needs_setup)
 		{
 		  switch_to_thread_no_regs (t);
 		  setup_inferior (0);
 		}
 
-	      if (ws.kind == TARGET_WAITKIND_STOPPED
-		  && ws.value.sig == GDB_SIGNAL_0)
+	      if (event.ws.kind == TARGET_WAITKIND_STOPPED
+		  && event.ws.value.sig == GDB_SIGNAL_0)
 		{
 		  /* We caught the event that we intended to catch, so
 		     there's no event pending.  */
@@ -4456,7 +4748,7 @@ stop_all_threads (void)
 
 		  if (debug_infrun)
 		    {
-		      std::string statstr = target_waitstatus_to_string (&ws);
+		      std::string statstr = target_waitstatus_to_string (&event.ws);
 
 		      fprintf_unfiltered (gdb_stdlog,
 					  "infrun: target_wait %s, saving "
@@ -4468,10 +4760,10 @@ stop_all_threads (void)
 		    }
 
 		  /* Record for later.  */
-		  save_waitstatus (t, &ws);
+		  save_waitstatus (t, &event.ws);
 
-		  sig = (ws.kind == TARGET_WAITKIND_STOPPED
-			 ? ws.value.sig : GDB_SIGNAL_0);
+		  sig = (event.ws.kind == TARGET_WAITKIND_STOPPED
+			 ? event.ws.value.sig : GDB_SIGNAL_0);
 
 		  if (displaced_step_fixup (t, sig) < 0)
 		    {
@@ -4569,7 +4861,7 @@ handle_no_resumed (struct execution_control_state *ecs)
      the synchronous command show "no unwaited-for " to the user.  */
   update_thread_list ();
 
-  for (thread_info *thread : all_non_exited_threads ())
+  for (thread_info *thread : all_non_exited_threads (ecs->target))
     {
       if (thread->executing
 	  || thread->suspend.waitstatus_pending_p)
@@ -4589,7 +4881,7 @@ handle_no_resumed (struct execution_control_state *ecs)
      process exited meanwhile (thus updating the thread list results
      in an empty thread list).  In this case we know we'll be getting
      a process exit event shortly.  */
-  for (inferior *inf : all_non_exited_inferiors ())
+  for (inferior *inf : all_non_exited_inferiors (ecs->target))
     {
       thread_info *thread = any_live_thread_of_inferior (inf);
       if (thread == NULL)
@@ -4659,8 +4951,8 @@ handle_inferior_event (struct execution_control_state *ecs)
       && handle_no_resumed (ecs))
     return;
 
-  /* Cache the last pid/waitstatus.  */
-  set_last_target_status (ecs->ptid, ecs->ws);
+  /* Cache the last target/ptid/waitstatus.  */
+  set_last_target_status (ecs->target, ecs->ptid, ecs->ws);
 
   /* Always clear state belonging to the previous time we stopped.  */
   stop_stack_dummy = STOP_NONE;
@@ -4677,10 +4969,10 @@ handle_inferior_event (struct execution_control_state *ecs)
   if (ecs->ws.kind != TARGET_WAITKIND_EXITED
       && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED)
     {
-      ecs->event_thread = find_thread_ptid (ecs->ptid);
+      ecs->event_thread = find_thread_ptid (ecs->target, ecs->ptid);
       /* If it's a new thread, add it to the thread database.  */
       if (ecs->event_thread == NULL)
-	ecs->event_thread = add_thread (ecs->ptid);
+	ecs->event_thread = add_thread (ecs->target, ecs->ptid);
 
       /* Disable range stepping.  If the next step request could use a
 	 range, this will be end up re-enabled then.  */
@@ -4752,10 +5044,10 @@ handle_inferior_event (struct execution_control_state *ecs)
     else
       mark_ptid = ecs->ptid;
 
-    set_executing (mark_ptid, 0);
+    set_executing (ecs->target, mark_ptid, 0);
 
     /* Likewise the resumed flag.  */
-    set_resumed (mark_ptid, 0);
+    set_resumed (ecs->target, mark_ptid, 0);
   }
 
   switch (ecs->ws.kind)
@@ -4856,7 +5148,7 @@ handle_inferior_event (struct execution_control_state *ecs)
     case TARGET_WAITKIND_EXITED:
     case TARGET_WAITKIND_SIGNALLED:
       inferior_ptid = ecs->ptid;
-      set_current_inferior (find_inferior_ptid (ecs->ptid));
+      set_current_inferior (find_inferior_ptid (ecs->target, ecs->ptid));
       set_current_program_space (current_inferior ()->pspace);
       handle_vfork_child_exec_or_exit (0);
       target_terminal::ours ();	/* Must do this before mourn anyway.  */
@@ -4929,7 +5221,7 @@ Cannot fill $_exitsignal with the correct signal number.\n"));
 	if (displaced_step_in_progress_thread (ecs->event_thread))
 	  {
 	    struct inferior *parent_inf
-	      = find_inferior_ptid (ecs->ptid);
+	      = find_inferior_ptid (ecs->target, ecs->ptid);
 	    struct regcache *child_regcache;
 	    CORE_ADDR parent_pc;
 
@@ -4960,7 +5252,8 @@ Cannot fill $_exitsignal with the correct signal number.\n"));
 	       list yet at this point.  */
 
 	    child_regcache
-	      = get_thread_arch_aspace_regcache (ecs->ws.value.related_pid,
+	      = get_thread_arch_aspace_regcache (parent_inf->process_target (),
+						 ecs->ws.value.related_pid,
 						 gdbarch,
 						 parent_inf->aspace);
 	    /* Read PC value of parent process.  */
@@ -5028,10 +5321,16 @@ Cannot fill $_exitsignal with the correct signal number.\n"));
 
 	  ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
 
+	  process_stratum_target *targ
+	    = ecs->event_thread->inf->process_target ();
+
 	  should_resume = follow_fork ();
 
+	  /* Note that one of these may be an invalid pointer,
+	     depending on detach_fork.  */
 	  thread_info *parent = ecs->event_thread;
-	  thread_info *child = find_thread_ptid (ecs->ws.value.related_pid);
+	  thread_info *child
+	    = find_thread_ptid (targ, ecs->ws.value.related_pid);
 
 	  /* At this point, the parent is marked running, and the
 	     child is marked stopped.  */
@@ -5844,7 +6143,7 @@ handle_signal_stop (struct execution_control_state *ecs)
   if (random_signal)
     {
       /* Signal not for debugging purposes.  */
-      struct inferior *inf = find_inferior_ptid (ecs->ptid);
+      struct inferior *inf = find_inferior_ptid (ecs->target, ecs->ptid);
       enum gdb_signal stop_signal = ecs->event_thread->suspend.stop_signal;
 
       if (debug_infrun)
@@ -6893,7 +7192,8 @@ switch_back_to_stepped_thread (struct execution_control_state *ecs)
 	  /* Ignore threads of processes the caller is not
 	     resuming.  */
 	  if (!sched_multi
-	      && tp->ptid.pid () != ecs->ptid.pid ())
+	      && (tp->inf->process_target () != ecs->target
+		  || tp->inf->pid != ecs->ptid.pid ()))
 	    continue;
 
 	  /* When stepping over a breakpoint, we lock all threads
@@ -7834,7 +8134,7 @@ print_stop_event (struct ui_out *uiout, bool displays)
   struct target_waitstatus last;
   struct thread_info *tp;
 
-  get_last_target_status (nullptr, &last);
+  get_last_target_status (nullptr, nullptr, &last);
 
   {
     scoped_restore save_uiout = make_scoped_restore (&current_uiout, uiout);
@@ -7954,7 +8254,7 @@ normal_stop (void)
 {
   struct target_waitstatus last;
 
-  get_last_target_status (nullptr, &last);
+  get_last_target_status (nullptr, nullptr, &last);
 
   new_stop_id ();
 
@@ -7963,10 +8263,10 @@ normal_stop (void)
      frontend/user running state.  A QUIT is an easy exception to see
      here, so do this before any filtered output.  */
 
-  gdb::optional<scoped_finish_thread_state> maybe_finish_thread_state;
+  ptid_t finish_ptid = null_ptid;
 
   if (!non_stop)
-    maybe_finish_thread_state.emplace (minus_one_ptid);
+    finish_ptid = minus_one_ptid;
   else if (last.kind == TARGET_WAITKIND_SIGNALLED
 	   || last.kind == TARGET_WAITKIND_EXITED)
     {
@@ -7976,10 +8276,17 @@ normal_stop (void)
 	 linux-fork.c automatically switches to another fork from
 	 within target_mourn_inferior.  */
       if (inferior_ptid != null_ptid)
-	maybe_finish_thread_state.emplace (ptid_t (inferior_ptid.pid ()));
+	finish_ptid = ptid_t (inferior_ptid.pid ());
     }
   else if (last.kind != TARGET_WAITKIND_NO_RESUMED)
-    maybe_finish_thread_state.emplace (inferior_ptid);
+    finish_ptid = inferior_ptid;
+
+  gdb::optional<scoped_finish_thread_state> maybe_finish_thread_state;
+  if (finish_ptid != null_ptid)
+    {
+      maybe_finish_thread_state.emplace
+	(user_visible_resume_target (finish_ptid), finish_ptid);
+    }
 
   /* As we're presenting a stop, and potentially removing breakpoints,
      update the thread list so we can tell whether there are threads
diff --git a/gdb/infrun.h b/gdb/infrun.h
index e7cf2959cf..1bf632a229 100644
--- a/gdb/infrun.h
+++ b/gdb/infrun.h
@@ -25,6 +25,7 @@ struct target_waitstatus;
 struct frame_info;
 struct address_space;
 struct return_value_info;
+struct process_stratum_target;
 
 /* True if we are debugging run control.  */
 extern unsigned int debug_infrun;
@@ -93,7 +94,13 @@ extern void proceed (CORE_ADDR, enum gdb_signal);
    resumed.  */
 extern ptid_t user_visible_resume_ptid (int step);
 
-extern void wait_for_inferior (void);
+/* Return the process_stratum target that we will proceed, in the
+   perspective of the user/frontend.  If RESUME_PTID is
+   MINUS_ONE_PTID, then we'll resume all threads of all targets, so
+   the function returns NULL.  Otherwise, we'll be resuming a process
+   or thread of the current process, so we return the current
+   inferior's process stratum target.  */
+extern process_stratum_target *user_visible_resume_target (ptid_t resume_ptid);
 
 /* Return control to GDB when the inferior stops for real.  Print
    appropriate messages, remove breakpoints, give terminal our modes,
@@ -101,15 +108,16 @@ extern void wait_for_inferior (void);
    target, false otherwise.  */
 extern int normal_stop (void);
 
-/* Return the cached copy of the last ptid/waitstatus returned
+/* Return the cached copy of the last target/ptid/waitstatus returned
    by target_wait()/deprecated_target_wait_hook().  The data is
    actually cached by handle_inferior_event(), which gets called
    immediately after target_wait()/deprecated_target_wait_hook().  */
-extern void get_last_target_status (ptid_t *ptid,
+extern void get_last_target_status (process_stratum_target **target,
+				    ptid_t *ptid,
 				    struct target_waitstatus *status);
 
-/* Set the cached copy of the last ptid/waitstatus.  */
-extern void set_last_target_status (ptid_t ptid,
+/* Set the cached copy of the last target/ptid/waitstatus.  */
+extern void set_last_target_status (process_stratum_target *target, ptid_t ptid,
 				    struct target_waitstatus status);
 
 /* Clear the cached copy of the last ptid/waitstatus returned by
diff --git a/gdb/inline-frame.c b/gdb/inline-frame.c
index 5840e3ee31..ab2ec60826 100644
--- a/gdb/inline-frame.c
+++ b/gdb/inline-frame.c
@@ -95,37 +95,54 @@ find_inline_frame_state (thread_info *thread)
   return &state;
 }
 
-/* Forget about any hidden inlined functions in PTID, which is new or
-   about to be resumed.  PTID may be minus_one_ptid (all processes)
-   or a PID (all threads in this process).  */
+/* See inline-frame.h.  */
 
 void
-clear_inline_frame_state (ptid_t ptid)
+clear_inline_frame_state (process_stratum_target *target, ptid_t filter_ptid)
 {
-  if (ptid == minus_one_ptid)
-    {
-      inline_states.clear ();
-      return;
-    }
+  gdb_assert (target != NULL);
 
-  if (ptid.is_pid ())
+  if (filter_ptid == minus_one_ptid || filter_ptid.is_pid ())
     {
-      int pid = ptid.pid ();
+      auto matcher = [target, &filter_ptid] (const inline_state &state)
+	{
+	  thread_info *t = state.thread;
+	  return (t->inf->process_target () == target
+		  && t->ptid.matches (filter_ptid));
+	};
+
       auto it = std::remove_if (inline_states.begin (), inline_states.end (),
-				[pid] (const inline_state &state)
-				  {
-				    return pid == state.thread->inf->pid;
-				  });
+				matcher);
 
       inline_states.erase (it, inline_states.end ());
 
       return;
     }
 
+
+  auto matcher = [target, &filter_ptid] (const inline_state &state)
+    {
+      thread_info *t = state.thread;
+      return (t->inf->process_target () == target
+	      && filter_ptid == t->ptid);
+    };
+
+  auto it = std::find_if (inline_states.begin (), inline_states.end (),
+			  matcher);
+
+  if (it != inline_states.end ())
+    unordered_remove (inline_states, it);
+}
+
+/* See inline-frame.h.  */
+
+void
+clear_inline_frame_state (thread_info *thread)
+{
   auto it = std::find_if (inline_states.begin (), inline_states.end (),
-			  [&ptid] (const inline_state &state)
+			  [thread] (const inline_state &state)
 			    {
-			      return ptid == state.thread->ptid;
+			      return thread == state.thread;
 			    });
 
   if (it != inline_states.end ())
diff --git a/gdb/inline-frame.h b/gdb/inline-frame.h
index e1d98f5ab5..6ba748ee22 100644
--- a/gdb/inline-frame.h
+++ b/gdb/inline-frame.h
@@ -23,6 +23,7 @@
 struct frame_info;
 struct frame_unwind;
 struct bpstats;
+struct process_stratum_target;
 
 /* The inline frame unwinder.  */
 
@@ -39,10 +40,15 @@ extern const struct frame_unwind inline_frame_unwind;
 void skip_inline_frames (thread_info *thread, struct bpstats *stop_chain);
 
 /* Forget about any hidden inlined functions in PTID, which is new or
-   about to be resumed.  If PTID is minus_one_ptid, forget about all
-   hidden inlined functions.  */
+   about to be resumed.  PTID may be minus_one_ptid (all processes of
+   TARGET) or a PID (all threads in this process of TARGET).  */
 
-void clear_inline_frame_state (ptid_t ptid);
+void clear_inline_frame_state (process_stratum_target *target, ptid_t ptid);
+
+/* Forget about any hidden inlined functions in THREAD, which is new
+   or about to be resumed.  */
+
+void clear_inline_frame_state (thread_info *thread);
 
 /* Step into an inlined function by unhiding it.  */
 
diff --git a/gdb/linux-fork.c b/gdb/linux-fork.c
index ab96be2f38..d3e094f530 100644
--- a/gdb/linux-fork.c
+++ b/gdb/linux-fork.c
@@ -519,7 +519,7 @@ Please switch to another checkpoint before deleting the current one"));
      list, waitpid the ptid.
      If fi->parent_ptid is a part of lwp and it is stopped, waitpid the
      ptid.  */
-  thread_info *parent = find_thread_ptid (pptid);
+  thread_info *parent = find_thread_ptid (linux_target, pptid);
   if ((parent == NULL && find_fork_ptid (pptid))
       || (parent != NULL && parent->state == THREAD_STOPPED))
     {
@@ -679,7 +679,7 @@ checkpoint_command (const char *args, int from_tty)
     error (_("checkpoint: call_function_by_hand returned null."));
 
   retpid = value_as_long (ret);
-  get_last_target_status (&last_target_ptid, &last_target_waitstatus);
+  get_last_target_status (nullptr, &last_target_ptid, &last_target_waitstatus);
 
   fp = find_fork_pid (retpid);
 
diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index 35f850270b..9be1ac4471 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -514,9 +514,12 @@ linux_nat_target::follow_fork (int follow_child, int detach_fork)
 	}
       else
 	{
-	  scoped_restore save_inferior_ptid
-	    = make_scoped_restore (&inferior_ptid);
-	  inferior_ptid = child_ptid;
+	  /* Switching inferior_ptid is not enough, because then
+	     inferior_thread () would crash by not finding the thread
+	     in the current inferior.  */
+	  scoped_restore_current_thread restore_current_thread;
+	  thread_info *child = find_thread_ptid (this, child_ptid);
+	  switch_to_thread (child);
 
 	  /* Let the thread_db layer learn about this new process.  */
 	  check_for_thread_db ();
@@ -988,7 +991,7 @@ linux_nat_switch_fork (ptid_t new_ptid)
   /* This changes the thread's ptid while preserving the gdb thread
      num.  Also changes the inferior pid, while preserving the
      inferior num.  */
-  thread_change_ptid (inferior_ptid, new_ptid);
+  thread_change_ptid (linux_target, inferior_ptid, new_ptid);
 
   /* We've just told GDB core that the thread changed target id, but,
      in fact, it really is a different thread, with different register
@@ -1001,7 +1004,7 @@ linux_nat_switch_fork (ptid_t new_ptid)
 static void
 exit_lwp (struct lwp_info *lp)
 {
-  struct thread_info *th = find_thread_ptid (lp->ptid);
+  struct thread_info *th = find_thread_ptid (linux_target, lp->ptid);
 
   if (th)
     {
@@ -1161,9 +1164,9 @@ attach_proc_task_lwp_callback (ptid_t ptid)
 	  /* Also add the LWP to gdb's thread list, in case a
 	     matching libthread_db is not found (or the process uses
 	     raw clone).  */
-	  add_thread (lp->ptid);
-	  set_running (lp->ptid, 1);
-	  set_executing (lp->ptid, 1);
+	  add_thread (linux_target, lp->ptid);
+	  set_running (linux_target, lp->ptid, 1);
+	  set_executing (linux_target, lp->ptid, 1);
 	}
 
       return 1;
@@ -1202,7 +1205,7 @@ linux_nat_target::attach (const char *args, int from_tty)
   ptid = ptid_t (inferior_ptid.pid (),
 		 inferior_ptid.pid (),
 		 0);
-  thread_change_ptid (inferior_ptid, ptid);
+  thread_change_ptid (linux_target, inferior_ptid, ptid);
 
   /* Add the initial process as the first LWP to the list.  */
   lp = add_initial_lwp (ptid);
@@ -1303,7 +1306,7 @@ get_detach_signal (struct lwp_info *lp)
     signo = gdb_signal_from_host (WSTOPSIG (lp->status));
   else
     {
-      struct thread_info *tp = find_thread_ptid (lp->ptid);
+      struct thread_info *tp = find_thread_ptid (linux_target, lp->ptid);
 
       if (target_is_non_stop_p () && !tp->executing)
 	{
@@ -1315,10 +1318,12 @@ get_detach_signal (struct lwp_info *lp)
       else if (!target_is_non_stop_p ())
 	{
 	  ptid_t last_ptid;
+	  process_stratum_target *last_target;
 
-	  get_last_target_status (&last_ptid, nullptr);
+	  get_last_target_status (&last_target, &last_ptid, nullptr);
 
-	  if (lp->ptid.lwp () == last_ptid.lwp ())
+	  if (last_target == linux_target
+	      && lp->ptid.lwp () == last_ptid.lwp ())
 	    signo = tp->suspend.stop_signal;
 	}
     }
@@ -1515,7 +1520,7 @@ linux_resume_one_lwp_throw (struct lwp_info *lp, int step,
      handle the case of stepping a breakpoint instruction).  */
   if (step)
     {
-      struct regcache *regcache = get_thread_regcache (lp->ptid);
+      struct regcache *regcache = get_thread_regcache (linux_target, lp->ptid);
 
       lp->stop_pc = regcache_read_pc (regcache);
     }
@@ -1534,7 +1539,7 @@ linux_resume_one_lwp_throw (struct lwp_info *lp, int step,
   lp->stopped = 0;
   lp->core = -1;
   lp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
-  registers_changed_ptid (lp->ptid);
+  registers_changed_ptid (linux_target, lp->ptid);
 }
 
 /* Called when we try to resume a stopped LWP and that errors out.  If
@@ -1593,7 +1598,7 @@ resume_lwp (struct lwp_info *lp, int step, enum gdb_signal signo)
 {
   if (lp->stopped)
     {
-      struct inferior *inf = find_inferior_ptid (lp->ptid);
+      struct inferior *inf = find_inferior_ptid (linux_target, lp->ptid);
 
       if (inf->vfork_child != NULL)
 	{
@@ -1647,7 +1652,7 @@ linux_nat_resume_callback (struct lwp_info *lp, struct lwp_info *except)
     {
       struct thread_info *thread;
 
-      thread = find_thread_ptid (lp->ptid);
+      thread = find_thread_ptid (linux_target, lp->ptid);
       if (thread != NULL)
 	{
 	  signo = thread->suspend.stop_signal;
@@ -1804,7 +1809,7 @@ linux_handle_syscall_trap (struct lwp_info *lp, int stopping)
 {
   struct target_waitstatus *ourstatus = &lp->waitstatus;
   struct gdbarch *gdbarch = target_thread_architecture (lp->ptid);
-  thread_info *thread = find_thread_ptid (lp->ptid);
+  thread_info *thread = find_thread_ptid (linux_target, lp->ptid);
   int syscall_number = (int) gdbarch_get_syscall_number (gdbarch, thread);
 
   if (stopping)
@@ -2024,15 +2029,15 @@ linux_handle_extended_wait (struct lwp_info *lp, int status)
 	      /* The process is not using thread_db.  Add the LWP to
 		 GDB's list.  */
 	      target_post_attach (new_lp->ptid.lwp ());
-	      add_thread (new_lp->ptid);
+	      add_thread (linux_target, new_lp->ptid);
 	    }
 
 	  /* Even if we're stopping the thread for some reason
 	     internal to this module, from the perspective of infrun
 	     and the user/frontend, this new thread is running until
 	     it next reports a stop.  */
-	  set_running (new_lp->ptid, 1);
-	  set_executing (new_lp->ptid, 1);
+	  set_running (linux_target, new_lp->ptid, 1);
+	  set_executing (linux_target, new_lp->ptid, 1);
 
 	  if (WSTOPSIG (status) != SIGSTOP)
 	    {
@@ -2255,7 +2260,7 @@ wait_lwp (struct lwp_info *lp)
 
   if (lp->must_set_ptrace_flags)
     {
-      struct inferior *inf = find_inferior_pid (lp->ptid.pid ());
+      inferior *inf = find_inferior_pid (linux_target, lp->ptid.pid ());
       int options = linux_nat_ptrace_options (inf->attach_flag);
 
       linux_enable_event_reporting (lp->ptid.lwp (), options);
@@ -2482,7 +2487,7 @@ linux_nat_target::low_status_is_event (int status)
 static int
 stop_wait_callback (struct lwp_info *lp)
 {
-  struct inferior *inf = find_inferior_ptid (lp->ptid);
+  inferior *inf = find_inferior_ptid (linux_target, lp->ptid);
 
   /* If this is a vfork parent, bail out, it is not going to report
      any SIGSTOP until the vfork is done with.  */
@@ -2575,7 +2580,7 @@ status_callback (struct lwp_info *lp)
   if (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
       || lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT)
     {
-      struct regcache *regcache = get_thread_regcache (lp->ptid);
+      struct regcache *regcache = get_thread_regcache (linux_target, lp->ptid);
       CORE_ADDR pc;
       int discard = 0;
 
@@ -2696,7 +2701,7 @@ save_stop_reason (struct lwp_info *lp)
   if (!linux_target->low_status_is_event (lp->status))
     return;
 
-  regcache = get_thread_regcache (lp->ptid);
+  regcache = get_thread_regcache (linux_target, lp->ptid);
   gdbarch = regcache->arch ();
 
   pc = regcache_read_pc (regcache);
@@ -2958,7 +2963,7 @@ linux_nat_filter_event (int lwpid, int status)
       lp = add_lwp (ptid_t (lwpid, lwpid, 0));
       lp->stopped = 1;
       lp->resumed = 1;
-      add_thread (lp->ptid);
+      add_thread (linux_target, lp->ptid);
     }
 
   if (WIFSTOPPED (status) && !lp)
@@ -2984,7 +2989,7 @@ linux_nat_filter_event (int lwpid, int status)
 
   if (WIFSTOPPED (status) && lp->must_set_ptrace_flags)
     {
-      struct inferior *inf = find_inferior_pid (lp->ptid.pid ());
+      inferior *inf = find_inferior_pid (linux_target, lp->ptid.pid ());
       int options = linux_nat_ptrace_options (inf->attach_flag);
 
       linux_enable_event_reporting (lp->ptid.lwp (), options);
@@ -3150,7 +3155,7 @@ linux_nat_filter_event (int lwpid, int status)
       if (!lp->step
 	  && WSTOPSIG (status) && sigismember (&pass_mask, WSTOPSIG (status))
 	  && (WSTOPSIG (status) != SIGSTOP
-	      || !find_thread_ptid (lp->ptid)->stop_requested)
+	      || !find_thread_ptid (linux_target, lp->ptid)->stop_requested)
 	  && !linux_wstatus_maybe_breakpoint (status))
 	{
 	  linux_resume_one_lwp (lp, lp->step, signo);
@@ -3269,7 +3274,7 @@ linux_nat_wait_1 (ptid_t ptid, struct target_waitstatus *ourstatus,
   if (inferior_ptid.is_pid ())
     {
       /* Upgrade the main thread's ptid.  */
-      thread_change_ptid (inferior_ptid,
+      thread_change_ptid (linux_target, inferior_ptid,
 			  ptid_t (inferior_ptid.pid (),
 				  inferior_ptid.pid (), 0));
 
@@ -3414,7 +3419,7 @@ linux_nat_wait_1 (ptid_t ptid, struct target_waitstatus *ourstatus,
   if (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
       && !USE_SIGTRAP_SIGINFO)
     {
-      struct regcache *regcache = get_thread_regcache (lp->ptid);
+      struct regcache *regcache = get_thread_regcache (linux_target, lp->ptid);
       struct gdbarch *gdbarch = regcache->arch ();
       int decr_pc = gdbarch_decr_pc_after_break (gdbarch);
 
@@ -3515,7 +3520,7 @@ resume_stopped_resumed_lwps (struct lwp_info *lp, const ptid_t wait_ptid)
     }
   else
     {
-      struct regcache *regcache = get_thread_regcache (lp->ptid);
+      struct regcache *regcache = get_thread_regcache (linux_target, lp->ptid);
       struct gdbarch *gdbarch = regcache->arch ();
 
       try
@@ -4263,6 +4268,12 @@ linux_async_pipe (int enable)
   return previous;
 }
 
+int
+linux_nat_target::async_wait_fd ()
+{
+  return linux_nat_event_pipe[0];
+}
+
 /* target_async implementation.  */
 
 void
@@ -4320,7 +4331,7 @@ linux_nat_stop_lwp (struct lwp_info *lwp)
 
       if (debug_linux_nat)
 	{
-	  if (find_thread_ptid (lwp->ptid)->stop_requested)
+	  if (find_thread_ptid (linux_target, lwp->ptid)->stop_requested)
 	    fprintf_unfiltered (gdb_stdlog,
 				"LNSL: already stopped/stop_requested %s\n",
 				target_pid_to_str (lwp->ptid).c_str ());
@@ -4377,7 +4388,7 @@ linux_nat_target::thread_address_space (ptid_t ptid)
       pid = ptid.pid ();
     }
 
-  inf = find_inferior_pid (pid);
+  inf = find_inferior_pid (this, pid);
   gdb_assert (inf != NULL);
   return inf->aspace;
 }
diff --git a/gdb/linux-nat.h b/gdb/linux-nat.h
index 0c1695ad10..3c1045f3a4 100644
--- a/gdb/linux-nat.h
+++ b/gdb/linux-nat.h
@@ -88,6 +88,7 @@ public:
   bool supports_non_stop () override;
   bool always_non_stop_p () override;
 
+  int async_wait_fd () override;
   void async (int) override;
 
   void close () override;
diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index 567b01c5d1..304439906e 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -1650,7 +1650,8 @@ linux_corefile_thread (struct thread_info *info,
 {
   struct regcache *regcache;
 
-  regcache = get_thread_arch_regcache (info->ptid, args->gdbarch);
+  regcache = get_thread_arch_regcache (info->inf->process_target (),
+				       info->ptid, args->gdbarch);
 
   target_fetch_registers (regcache, -1);
   gdb::byte_vector siginfo_data = linux_get_siginfo_data (info, args->gdbarch);
diff --git a/gdb/linux-thread-db.c b/gdb/linux-thread-db.c
index 676690b197..e163ece2b6 100644
--- a/gdb/linux-thread-db.c
+++ b/gdb/linux-thread-db.c
@@ -168,6 +168,9 @@ struct thread_db_info
 {
   struct thread_db_info *next;
 
+  /* The target this thread_db_info is bound to.  */
+  process_stratum_target *process_target;
+
   /* Process id this object refers to.  */
   int pid;
 
@@ -228,6 +231,7 @@ add_thread_db_info (void *handle)
 {
   struct thread_db_info *info = XCNEW (struct thread_db_info);
 
+  info->process_target = current_inferior ()->process_target ();
   info->pid = inferior_ptid.pid ();
   info->handle = handle;
 
@@ -246,12 +250,12 @@ add_thread_db_info (void *handle)
    related to process PID, if any; NULL otherwise.  */
 
 static struct thread_db_info *
-get_thread_db_info (int pid)
+get_thread_db_info (process_stratum_target *targ, int pid)
 {
   struct thread_db_info *info;
 
   for (info = thread_db_list; info; info = info->next)
-    if (pid == info->pid)
+    if (targ == info->process_target && pid == info->pid)
       return info;
 
   return NULL;
@@ -265,14 +269,14 @@ static const char *thread_db_err_str (td_err_e err);
    LIBTHREAD_DB_SO's dlopen'ed handle.  */
 
 static void
-delete_thread_db_info (int pid)
+delete_thread_db_info (process_stratum_target *targ, int pid)
 {
   struct thread_db_info *info, *info_prev;
 
   info_prev = NULL;
 
   for (info = thread_db_list; info; info_prev = info, info = info->next)
-    if (pid == info->pid)
+    if (targ == info->process_target && pid == info->pid)
       break;
 
   if (info == NULL)
@@ -406,7 +410,7 @@ thread_from_lwp (thread_info *stopped, ptid_t ptid)
      LWP.  */
   gdb_assert (ptid.lwp () != 0);
 
-  info = get_thread_db_info (ptid.pid ());
+  info = get_thread_db_info (stopped->inf->process_target (), ptid.pid ());
 
   /* Access an lwp we know is stopped.  */
   info->proc_handle.thread = stopped;
@@ -422,7 +426,7 @@ thread_from_lwp (thread_info *stopped, ptid_t ptid)
 	   thread_db_err_str (err));
 
   /* Fill the cache.  */
-  tp = find_thread_ptid (ptid);
+  tp = find_thread_ptid (stopped->inf->process_target (), ptid);
   return record_thread (info, tp, ptid, &th, &ti);
 }
 \f
@@ -434,12 +438,12 @@ thread_db_notice_clone (ptid_t parent, ptid_t child)
 {
   struct thread_db_info *info;
 
-  info = get_thread_db_info (child.pid ());
+  info = get_thread_db_info (linux_target, child.pid ());
 
   if (info == NULL)
     return 0;
 
-  thread_info *stopped = find_thread_ptid (parent);
+  thread_info *stopped = find_thread_ptid (linux_target, parent);
 
   thread_from_lwp (stopped, child);
 
@@ -684,13 +688,13 @@ check_thread_db_callback (const td_thrhandle_t *th, void *arg)
      to how GDB accesses TLS could result in this passing
      without exercising the calls it's supposed to.  */
   ptid_t ptid = ptid_t (tdb_testinfo->info->pid, ti.ti_lid, 0);
-  struct thread_info *thread_info = find_thread_ptid (ptid);
+  thread_info *thread_info = find_thread_ptid (linux_target, ptid);
   if (thread_info != NULL && thread_info->priv != NULL)
     {
       LOG ("; errno");
 
       scoped_restore_current_thread restore_current_thread;
-      switch_to_thread (ptid);
+      switch_to_thread (thread_info);
 
       expression_up expr = parse_expression ("(int) errno");
       struct value *val = evaluate_expression (expr.get ());
@@ -940,10 +944,8 @@ try_thread_db_load_1 (struct thread_db_info *info)
     }
 
   /* The thread library was detected.  Activate the thread_db target
-     if this is the first process using it.  */
-  if (thread_db_list->next == NULL)
-    push_target (&the_thread_db_target);
-
+     for this process.  */
+  push_target (&the_thread_db_target);
   return true;
 }
 
@@ -1013,7 +1015,8 @@ try_thread_db_load (const char *library, bool check_auto_load_safe)
     return true;
 
   /* This library "refused" to work on current inferior.  */
-  delete_thread_db_info (inferior_ptid.pid ());
+  delete_thread_db_info (current_inferior ()->process_target (),
+			 inferior_ptid.pid ());
   return false;
 }
 
@@ -1182,7 +1185,8 @@ thread_db_load (void)
 {
   struct thread_db_info *info;
 
-  info = get_thread_db_info (inferior_ptid.pid ());
+  info = get_thread_db_info (current_inferior ()->process_target (),
+			     inferior_ptid.pid ());
 
   if (info != NULL)
     return true;
@@ -1349,7 +1353,7 @@ record_thread (struct thread_db_info *info,
      thread with this PTID, but it's marked exited, then the kernel
      reused the tid of an old thread.  */
   if (tp == NULL || tp->state == THREAD_EXITED)
-    tp = add_thread_with_info (ptid, priv);
+    tp = add_thread_with_info (info->process_target, ptid, priv);
   else
     tp->priv.reset (priv);
 
@@ -1362,16 +1366,14 @@ record_thread (struct thread_db_info *info,
 void
 thread_db_target::detach (inferior *inf, int from_tty)
 {
-  delete_thread_db_info (inf->pid);
+  delete_thread_db_info (inf->process_target (), inf->pid);
 
   beneath ()->detach (inf, from_tty);
 
   /* NOTE: From this point on, inferior_ptid is null_ptid.  */
 
-  /* If there are no more processes using libpthread, detach the
-     thread_db target ops.  */
-  if (!thread_db_list)
-    unpush_target (this);
+  /* Detach the thread_db target from this inferior.  */
+  unpush_target (this);
 }
 
 ptid_t
@@ -1380,7 +1382,10 @@ thread_db_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
 {
   struct thread_db_info *info;
 
-  ptid = beneath ()->wait (ptid, ourstatus, options);
+  process_stratum_target *beneath
+    = as_process_stratum_target (this->beneath ());
+
+  ptid = beneath->wait (ptid, ourstatus, options);
 
   switch (ourstatus->kind)
     {
@@ -1391,7 +1396,7 @@ thread_db_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
       return ptid;
     }
 
-  info = get_thread_db_info (ptid.pid ());
+  info = get_thread_db_info (beneath, ptid.pid ());
 
   /* If this process isn't using thread_db, we're done.  */
   if (info == NULL)
@@ -1401,15 +1406,14 @@ thread_db_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
     {
       /* New image, it may or may not end up using thread_db.  Assume
 	 not unless we find otherwise.  */
-      delete_thread_db_info (ptid.pid ());
-      if (!thread_db_list)
-	unpush_target (&the_thread_db_target);
+      delete_thread_db_info (beneath, ptid.pid ());
+      unpush_target (this);
 
       return ptid;
     }
 
   /* Fill in the thread's user-level thread id and status.  */
-  thread_from_lwp (find_thread_ptid (ptid), ptid);
+  thread_from_lwp (find_thread_ptid (beneath, ptid), ptid);
 
   return ptid;
 }
@@ -1417,13 +1421,15 @@ thread_db_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
 void
 thread_db_target::mourn_inferior ()
 {
-  delete_thread_db_info (inferior_ptid.pid ());
+  process_stratum_target *target_beneath
+    = as_process_stratum_target (this->beneath ());
+
+  delete_thread_db_info (target_beneath, inferior_ptid.pid ());
 
-  beneath ()->mourn_inferior ();
+  target_beneath->mourn_inferior ();
 
-  /* Detach thread_db target ops.  */
-  if (!thread_db_list)
-    unpush_target (&the_thread_db_target);
+  /* Detach the thread_db target from this inferior.  */
+  unpush_target (this);
 }
 
 struct callback_data
@@ -1488,7 +1494,7 @@ find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
     }
 
   ptid_t ptid (info->pid, ti.ti_lid);
-  tp = find_thread_ptid (ptid);
+  tp = find_thread_ptid (info->process_target, ptid);
   if (tp == NULL || tp->priv == NULL)
     record_thread (info, tp, ptid, th_p, &ti);
 
@@ -1555,7 +1561,8 @@ thread_db_find_new_threads_2 (thread_info *stopped, bool until_no_new)
   struct thread_db_info *info;
   int i, loop;
 
-  info = get_thread_db_info (stopped->ptid.pid ());
+  info = get_thread_db_info (stopped->inf->process_target (),
+			     stopped->ptid.pid ());
 
   /* Access an lwp we know is stopped.  */
   info->proc_handle.thread = stopped;
@@ -1598,16 +1605,14 @@ thread_db_target::update_thread_list ()
 
   for (inferior *inf : all_inferiors ())
     {
-      struct thread_info *thread;
-
       if (inf->pid == 0)
 	continue;
 
-      info = get_thread_db_info (inf->pid);
+      info = get_thread_db_info (inf->process_target (), inf->pid);
       if (info == NULL)
 	continue;
 
-      thread = any_live_thread_of_inferior (inf);
+      thread_info *thread = any_live_thread_of_inferior (inf);
       if (thread == NULL || thread->executing)
 	continue;
 
@@ -1635,7 +1640,7 @@ thread_db_target::update_thread_list ()
 std::string
 thread_db_target::pid_to_str (ptid_t ptid)
 {
-  struct thread_info *thread_info = find_thread_ptid (ptid);
+  thread_info *thread_info = find_thread_ptid (current_inferior (), ptid);
 
   if (thread_info != NULL && thread_info->priv != NULL)
     {
@@ -1728,9 +1733,10 @@ thread_db_target::get_thread_local_address (ptid_t ptid,
 					    CORE_ADDR offset)
 {
   struct thread_info *thread_info;
-
+  process_stratum_target *beneath
+    = as_process_stratum_target (this->beneath ());
   /* Find the matching thread.  */
-  thread_info = find_thread_ptid (ptid);
+  thread_info = find_thread_ptid (beneath, ptid);
 
   /* We may not have discovered the thread yet.  */
   if (thread_info != NULL && thread_info->priv == NULL)
@@ -1740,7 +1746,7 @@ thread_db_target::get_thread_local_address (ptid_t ptid,
     {
       td_err_e err;
       psaddr_t address;
-      thread_db_info *info = get_thread_db_info (ptid.pid ());
+      thread_db_info *info = get_thread_db_info (beneath, ptid.pid ());
       thread_db_thread_info *priv = get_thread_db_thread_info (thread_info);
 
       /* Finally, get the address of the variable.  */
@@ -1799,7 +1805,7 @@ thread_db_target::get_thread_local_address (ptid_t ptid,
 	      : (CORE_ADDR) (uintptr_t) address);
     }
 
-  return beneath ()->get_thread_local_address (ptid, lm, offset);
+  return beneath->get_thread_local_address (ptid, lm, offset);
 }
 
 /* Implement the to_get_ada_task_ptid target method for this target.  */
@@ -1814,12 +1820,13 @@ thread_db_target::get_ada_task_ptid (long lwp, long thread)
 void
 thread_db_target::resume (ptid_t ptid, int step, enum gdb_signal signo)
 {
-  struct thread_db_info *info;
+  process_stratum_target *beneath
+    = as_process_stratum_target (this->beneath ());
 
-  if (ptid == minus_one_ptid)
-    info = get_thread_db_info (inferior_ptid.pid ());
-  else
-    info = get_thread_db_info (ptid.pid ());
+  thread_db_info *info
+    = get_thread_db_info (beneath, (ptid == minus_one_ptid
+				    ? inferior_ptid.pid ()
+				    : ptid.pid ()));
 
   /* This workaround is only needed for child fork lwps stopped in a
      PTRACE_O_TRACEFORK event.  When the inferior is resumed, the
@@ -1827,7 +1834,7 @@ thread_db_target::resume (ptid_t ptid, int step, enum gdb_signal signo)
   if (info)
     info->need_stale_parent_threads_check = 0;
 
-  beneath ()->resume (ptid, step, signo);
+  beneath->resume (ptid, step, signo);
 }
 
 /* std::sort helper function for info_auto_load_libthread_db, sort the
@@ -1953,7 +1960,8 @@ maintenance_check_libthread_db (const char *args, int from_tty)
   if (inferior_pid == 0)
     error (_("No inferior running"));
 
-  info = get_thread_db_info (inferior_pid);
+  info = get_thread_db_info (current_inferior ()->process_target (),
+			     inferior_pid);
   if (info == NULL)
     error (_("No libthread_db loaded"));
 
diff --git a/gdb/mi/mi-interp.c b/gdb/mi/mi-interp.c
index 7659e28038..8b55693d19 100644
--- a/gdb/mi/mi-interp.c
+++ b/gdb/mi/mi-interp.c
@@ -958,7 +958,8 @@ multiple_inferiors_p ()
 }
 
 static void
-mi_on_resume_1 (struct mi_interp *mi, ptid_t ptid)
+mi_on_resume_1 (struct mi_interp *mi,
+		process_stratum_target *targ, ptid_t ptid)
 {
   /* To cater for older frontends, emit ^running, but do it only once
      per each command.  We do it here, since at this point we know
@@ -981,7 +982,7 @@ mi_on_resume_1 (struct mi_interp *mi, ptid_t ptid)
       && !multiple_inferiors_p ())
     fprintf_unfiltered (mi->raw_stdout, "*running,thread-id=\"all\"\n");
   else
-    for (thread_info *tp : all_non_exited_threads (ptid))
+    for (thread_info *tp : all_non_exited_threads (targ, ptid))
       mi_output_running (tp);
 
   if (!running_result_record_printed && mi_proceeded)
@@ -1001,10 +1002,11 @@ mi_on_resume (ptid_t ptid)
 {
   struct thread_info *tp = NULL;
 
+  process_stratum_target *target = current_inferior ()->process_target ();
   if (ptid == minus_one_ptid || ptid.is_pid ())
     tp = inferior_thread ();
   else
-    tp = find_thread_ptid (ptid);
+    tp = find_thread_ptid (target, ptid);
 
   /* Suppress output while calling an inferior function.  */
   if (tp->control.in_infcall)
@@ -1020,7 +1022,7 @@ mi_on_resume (ptid_t ptid)
       target_terminal::scoped_restore_terminal_state term_state;
       target_terminal::ours_for_output ();
 
-      mi_on_resume_1 (mi, ptid);
+      mi_on_resume_1 (mi, target, ptid);
     }
 }
 
diff --git a/gdb/nat/fork-inferior.c b/gdb/nat/fork-inferior.c
index 355e9bef43..3d9f0881c0 100644
--- a/gdb/nat/fork-inferior.c
+++ b/gdb/nat/fork-inferior.c
@@ -450,7 +450,7 @@ fork_inferior (const char *exec_file_arg, const std::string &allargs,
 /* See nat/fork-inferior.h.  */
 
 ptid_t
-startup_inferior (pid_t pid, int ntraps,
+startup_inferior (process_stratum_target *proc_target, pid_t pid, int ntraps,
 		  struct target_waitstatus *last_waitstatus,
 		  ptid_t *last_ptid)
 {
@@ -502,7 +502,7 @@ startup_inferior (pid_t pid, int ntraps,
 	  case TARGET_WAITKIND_SYSCALL_ENTRY:
 	  case TARGET_WAITKIND_SYSCALL_RETURN:
 	    /* Ignore gracefully during startup of the inferior.  */
-	    switch_to_thread (event_ptid);
+	    switch_to_thread (proc_target, event_ptid);
 	    break;
 
 	  case TARGET_WAITKIND_SIGNALLED:
@@ -527,12 +527,12 @@ startup_inferior (pid_t pid, int ntraps,
 	    /* Handle EXEC signals as if they were SIGTRAP signals.  */
 	    xfree (ws.value.execd_pathname);
 	    resume_signal = GDB_SIGNAL_TRAP;
-	    switch_to_thread (event_ptid);
+	    switch_to_thread (proc_target, event_ptid);
 	    break;
 
 	  case TARGET_WAITKIND_STOPPED:
 	    resume_signal = ws.value.sig;
-	    switch_to_thread (event_ptid);
+	    switch_to_thread (proc_target, event_ptid);
 	    break;
 	}
 
diff --git a/gdb/nat/fork-inferior.h b/gdb/nat/fork-inferior.h
index 0a76ff82d3..2efff28485 100644
--- a/gdb/nat/fork-inferior.h
+++ b/gdb/nat/fork-inferior.h
@@ -22,6 +22,8 @@
 
 #include <string>
 
+struct process_stratum_target;
+
 /* Number of traps that happen between exec'ing the shell to run an
    inferior and when we finally get to the inferior code, not counting
    the exec for the shell.  This is 1 on all supported
@@ -50,7 +52,8 @@ extern pid_t fork_inferior (const char *exec_file_arg,
 /* Accept NTRAPS traps from the inferior.
 
    Return the ptid of the inferior being started.  */
-extern ptid_t startup_inferior (pid_t pid, int ntraps,
+extern ptid_t startup_inferior (process_stratum_target *proc_target,
+				pid_t pid, int ntraps,
 				struct target_waitstatus *mystatus,
 				ptid_t *myptid);
 
diff --git a/gdb/nto-procfs.c b/gdb/nto-procfs.c
index 0a199e7224..d00dab5a4d 100644
--- a/gdb/nto-procfs.c
+++ b/gdb/nto-procfs.c
@@ -409,7 +409,7 @@ nto_procfs_target::update_thread_list ()
 	   (e.g. thread exited).  */
 	continue;
       ptid = ptid_t (pid, 0, tid);
-      new_thread = find_thread_ptid (ptid);
+      new_thread = find_thread_ptid (this, ptid);
       if (!new_thread)
 	new_thread = add_thread (ptid);
       update_thread_private_data (new_thread, tid, status.state, 0);
diff --git a/gdb/ppc-fbsd-tdep.c b/gdb/ppc-fbsd-tdep.c
index 290bd1fd88..a3efd860a6 100644
--- a/gdb/ppc-fbsd-tdep.c
+++ b/gdb/ppc-fbsd-tdep.c
@@ -35,6 +35,7 @@
 #include "ppc-fbsd-tdep.h"
 #include "fbsd-tdep.h"
 #include "solib-svr4.h"
+#include "inferior.h"
 
 
 /* 32-bit regset descriptions.  */
@@ -289,7 +290,8 @@ ppcfbsd_get_thread_local_address (struct gdbarch *gdbarch, ptid_t ptid,
   struct regcache *regcache;
   int tp_offset, tp_regnum;
 
-  regcache = get_thread_arch_regcache (ptid, gdbarch);
+  regcache = get_thread_arch_regcache (current_inferior ()->process_target (),
+				       ptid, gdbarch);
 
   if (tdep->wordsize == 4)
     {
diff --git a/gdb/proc-service.c b/gdb/proc-service.c
index b0741318ad..954d150a5e 100644
--- a/gdb/proc-service.c
+++ b/gdb/proc-service.c
@@ -70,17 +70,22 @@ static ps_err_e
 ps_xfer_memory (const struct ps_prochandle *ph, psaddr_t addr,
 		gdb_byte *buf, size_t len, int write)
 {
-  scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
-  int ret;
-  CORE_ADDR core_addr = ps_addr_to_core_addr (addr);
+  scoped_restore_current_inferior restore_inferior;
+  set_current_inferior (ph->thread->inf);
 
+  scoped_restore_current_program_space restore_current_progspace;
+  set_current_program_space (ph->thread->inf->pspace);
+
+  scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
   inferior_ptid = ph->thread->ptid;
 
+  CORE_ADDR core_addr = ps_addr_to_core_addr (addr);
+
+  int ret;
   if (write)
     ret = target_write_memory (core_addr, buf, len);
   else
     ret = target_read_memory (core_addr, buf, len);
-
   return (ret == 0 ? PS_OK : PS_ERR);
 }
 \f
@@ -138,7 +143,9 @@ static struct regcache *
 get_ps_regcache (struct ps_prochandle *ph, lwpid_t lwpid)
 {
   inferior *inf = ph->thread->inf;
-  return get_thread_arch_regcache (ptid_t (inf->pid, lwpid), inf->gdbarch);
+  return get_thread_arch_regcache (inf->process_target (),
+				   ptid_t (inf->pid, lwpid),
+				   inf->gdbarch);
 }
 
 /* Get the general registers of LWP LWPID within the target process PH
diff --git a/gdb/process-stratum-target.c b/gdb/process-stratum-target.c
index 1517088f0f..234a31378b 100644
--- a/gdb/process-stratum-target.c
+++ b/gdb/process-stratum-target.c
@@ -29,7 +29,7 @@ struct address_space *
 process_stratum_target::thread_address_space (ptid_t ptid)
 {
   /* Fall-back to the "main" address space of the inferior.  */
-  inferior *inf = find_inferior_ptid (ptid);
+  inferior *inf = find_inferior_ptid (this, ptid);
 
   if (inf == NULL || inf->aspace == NULL)
     internal_error (__FILE__, __LINE__,
@@ -43,7 +43,7 @@ process_stratum_target::thread_address_space (ptid_t ptid)
 struct gdbarch *
 process_stratum_target::thread_architecture (ptid_t ptid)
 {
-  inferior *inf = find_inferior_ptid (ptid);
+  inferior *inf = find_inferior_ptid (this, ptid);
   gdb_assert (inf != NULL);
   return inf->gdbarch;
 }
diff --git a/gdb/process-stratum-target.h b/gdb/process-stratum-target.h
index a14e4f3a72..6081c0a927 100644
--- a/gdb/process-stratum-target.h
+++ b/gdb/process-stratum-target.h
@@ -51,6 +51,22 @@ public:
   bool has_stack () override;
   bool has_registers () override;
   bool has_execution (inferior *inf) override;
+
+  /* True if any thread is, or may be executing.  We need to track
+     this separately because until we fully sync the thread list, we
+     won't know whether the target is fully stopped, even if we see
+     stop events for all known threads, because any of those threads
+     may have spawned new threads we haven't heard of yet.  */
+  bool threads_executing = false;
 };
 
+/* Downcast TARGET to process_stratum_target.  */
+
+static inline process_stratum_target *
+as_process_stratum_target (target_ops *target)
+{
+  gdb_assert (target->stratum () == process_stratum);
+  return static_cast<process_stratum_target *> (target);
+}
+
 #endif /* !defined (PROCESS_STRATUM_TARGET_H) */
diff --git a/gdb/procfs.c b/gdb/procfs.c
index 848ac7d6e7..f6b9bfbcfc 100644
--- a/gdb/procfs.c
+++ b/gdb/procfs.c
@@ -160,6 +160,8 @@ public:
 
   int can_use_hw_breakpoint (enum bptype, int, int) override;
   bool stopped_data_address (CORE_ADDR *) override;
+
+  void procfs_init_inferior (int pid);
 };
 
 static procfs_target the_procfs_target;
@@ -1315,6 +1317,7 @@ proc_set_current_signal (procinfo *pi, int signo)
     char sinfo[sizeof (siginfo_t)];
   } arg;
   siginfo_t mysinfo;
+  process_stratum_target *wait_target;
   ptid_t wait_ptid;
   struct target_waitstatus wait_status;
 
@@ -1327,8 +1330,9 @@ proc_set_current_signal (procinfo *pi, int signo)
     pi = find_procinfo_or_die (pi->pid, 0);
 
   /* The pointer is just a type alias.  */
-  get_last_target_status (&wait_ptid, &wait_status);
-  if (wait_ptid == inferior_ptid
+  get_last_target_status (&wait_target, &wait_ptid, &wait_status);
+  if (wait_target == &the_procfs_target
+      && wait_ptid == inferior_ptid
       && wait_status.kind == TARGET_WAITKIND_STOPPED
       && wait_status.value.sig == gdb_signal_from_host (signo)
       && proc_get_status (pi)
@@ -1988,7 +1992,7 @@ do_attach (ptid_t ptid)
 
   /* Add it to gdb's thread list.  */
   ptid = ptid_t (pi->pid, lwpid, 0);
-  add_thread (ptid);
+  add_thread (&the_procfs_target, ptid);
 
   return ptid;
 }
@@ -2286,7 +2290,7 @@ wait_again:
 		    if (print_thread_events)
 		      printf_unfiltered (_("[%s exited]\n"),
 					 target_pid_to_str (retval).c_str ());
-		    delete_thread (find_thread_ptid (retval));
+		    delete_thread (find_thread_ptid (this, retval));
 		    status->kind = TARGET_WAITKIND_SPURIOUS;
 		    return retval;
 		  }
@@ -2308,7 +2312,7 @@ wait_again:
 		    if (!proc_run_process (pi, 0, 0))
 		      proc_error (pi, "target_wait, run_process", __LINE__);
 
-		    inf = find_inferior_pid (pi->pid);
+		    inf = find_inferior_pid (this, pi->pid);
 		    if (inf->attach_flag)
 		      {
 			/* Don't call wait: simulate waiting for exit,
@@ -2395,8 +2399,8 @@ wait_again:
 
 		    temp_ptid = ptid_t (pi->pid, temp_tid, 0);
 		    /* If not in GDB's thread list, add it.  */
-		    if (!in_thread_list (temp_ptid))
-		      add_thread (temp_ptid);
+		    if (!in_thread_list (this, temp_ptid))
+		      add_thread (this, temp_ptid);
 
 		    /* Return to WFI, but tell it to immediately resume.  */
 		    status->kind = TARGET_WAITKIND_SPURIOUS;
@@ -2407,7 +2411,7 @@ wait_again:
 		    if (print_thread_events)
 		      printf_unfiltered (_("[%s exited]\n"),
 					 target_pid_to_str (retval).c_str ());
-		    delete_thread (find_thread_ptid (retval));
+		    delete_thread (find_thread_ptid (this, retval));
 		    status->kind = TARGET_WAITKIND_SPURIOUS;
 		    return retval;
 		  }
@@ -2464,8 +2468,8 @@ wait_again:
 
 		    /* If not in GDB's thread list, add it.  */
 		    temp_ptid = ptid_t (pi->pid, temp_tid, 0);
-		    if (!in_thread_list (temp_ptid))
-		      add_thread (temp_ptid);
+		    if (!in_thread_list (this, temp_ptid))
+		      add_thread (this, temp_ptid);
 
 		    status->kind = TARGET_WAITKIND_STOPPED;
 		    status->value.sig = GDB_SIGNAL_0;
@@ -2493,12 +2497,12 @@ wait_again:
 		 threads database, add it.  */
 	      if (retval.pid () > 0
 		  && retval != inferior_ptid
-		  && !in_thread_list (retval))
+		  && !in_thread_list (this, retval))
 		{
 		  /* We have a new thread.  We need to add it both to
 		     GDB's list and to our own.  If we don't create a
 		     procinfo, resume may be unhappy later.  */
-		  add_thread (retval);
+		  add_thread (this, retval);
 		  if (find_procinfo (retval.pid (),
 				     retval.lwp ()) == NULL)
 		    create_procinfo (retval.pid (),
@@ -2851,8 +2855,8 @@ procfs_target::mourn_inferior ()
    whatever is necessary to make the child ready to be debugged, and
    then wait for the child to synchronize.  */
 
-static void
-procfs_init_inferior (struct target_ops *ops, int pid)
+void
+procfs_target::procfs_init_inferior (int pid)
 {
   procinfo *pi;
   int fail;
@@ -2860,8 +2864,8 @@ procfs_init_inferior (struct target_ops *ops, int pid)
 
   /* This routine called on the parent side (GDB side)
      after GDB forks the inferior.  */
-  if (!target_is_pushed (ops))
-    push_target (ops);
+  if (!target_is_pushed (this))
+    push_target (this);
 
   pi = create_procinfo (pid, 0);
   if (pi == NULL)
@@ -2922,8 +2926,7 @@ procfs_init_inferior (struct target_ops *ops, int pid)
   /* We already have a main thread registered in the thread table at
      this point, but it didn't have any lwp info yet.  Notify the core
      about it.  This changes inferior_ptid as well.  */
-  thread_change_ptid (ptid_t (pid),
-		      ptid_t (pid, lwpid, 0));
+  thread_change_ptid (this, ptid_t (pid), ptid_t (pid, lwpid, 0));
 
   gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
 }
@@ -3090,9 +3093,9 @@ procfs_target::create_inferior (const char *exec_file,
   /* We have something that executes now.  We'll be running through
      the shell at this point (if startup-with-shell is true), but the
      pid shouldn't change.  */
-  add_thread_silent (ptid_t (pid));
+  add_thread_silent (this, ptid_t (pid));
 
-  procfs_init_inferior (this, pid);
+  procfs_init_inferior (pid);
 }
 
 /* An observer for the "inferior_created" event.  */
@@ -3109,9 +3112,9 @@ procfs_notice_thread (procinfo *pi, procinfo *thread, void *ptr)
 {
   ptid_t gdb_threadid = ptid_t (pi->pid, thread->tid, 0);
 
-  thread_info *thr = find_thread_ptid (gdb_threadid);
+  thread_info *thr = find_thread_ptid (&the_procfs_target, gdb_threadid);
   if (thr == NULL || thr->state == THREAD_EXITED)
-    add_thread (gdb_threadid);
+    add_thread (&the_procfs_target, gdb_threadid);
 
   return 0;
 }
@@ -3740,7 +3743,7 @@ procfs_do_thread_registers (bfd *obfd, ptid_t ptid,
 			    char *note_data, int *note_size,
 			    enum gdb_signal stop_signal)
 {
-  struct regcache *regcache = get_thread_regcache (ptid);
+  struct regcache *regcache = get_thread_regcache (&the_procfs_target, ptid);
   gdb_gregset_t gregs;
   gdb_fpregset_t fpregs;
   unsigned long merged_pid;
diff --git a/gdb/python/py-threadevent.c b/gdb/python/py-threadevent.c
index c2af2fa3c1..9b2afc4802 100644
--- a/gdb/python/py-threadevent.c
+++ b/gdb/python/py-threadevent.c
@@ -27,7 +27,9 @@ py_get_event_thread (ptid_t ptid)
 {
   if (non_stop)
     {
-      thread_info *thread = find_thread_ptid (ptid);
+      thread_info *thread
+	= find_thread_ptid (current_inferior ()->process_target (),
+			    ptid);
       if (thread != nullptr)
 	return thread_to_thread_object (thread);
       PyErr_SetString (PyExc_RuntimeError, "Could not find event thread");
diff --git a/gdb/ravenscar-thread.c b/gdb/ravenscar-thread.c
index 0cc7e1dc75..5c8d537aae 100644
--- a/gdb/ravenscar-thread.c
+++ b/gdb/ravenscar-thread.c
@@ -219,6 +219,9 @@ get_base_thread_from_ravenscar_task (ptid_t ptid)
 void
 ravenscar_thread_target::update_inferior_ptid ()
 {
+  process_stratum_target *proc_target
+    = as_process_stratum_target (this->beneath ());
+
   int base_cpu;
 
   m_base_ptid = inferior_ptid;
@@ -239,8 +242,8 @@ ravenscar_thread_target::update_inferior_ptid ()
   /* The running thread may not have been added to
      system.tasking.debug's list yet; so ravenscar_update_thread_list
      may not always add it to the thread list.  Add it here.  */
-  if (!find_thread_ptid (inferior_ptid))
-    add_thread (inferior_ptid);
+  if (!find_thread_ptid (proc_target, inferior_ptid))
+    add_thread (proc_target, inferior_ptid);
 }
 
 /* The Ravenscar Runtime exports a symbol which contains the ID of
@@ -336,12 +339,14 @@ ravenscar_thread_target::wait (ptid_t ptid,
 			       struct target_waitstatus *status,
 			       int options)
 {
+  process_stratum_target *beneath
+    = as_process_stratum_target (this->beneath ());
   ptid_t event_ptid;
 
   inferior_ptid = m_base_ptid;
   if (ptid != minus_one_ptid)
     ptid = m_base_ptid;
-  event_ptid = beneath ()->wait (ptid, status, 0);
+  event_ptid = beneath->wait (ptid, status, 0);
   /* Find any new threads that might have been created, and update
      inferior_ptid to the active thread.
 
@@ -367,8 +372,8 @@ ravenscar_thread_target::wait (ptid_t ptid,
 static void
 ravenscar_add_thread (struct ada_task_info *task)
 {
-  if (find_thread_ptid (task->ptid) == NULL)
-    add_thread (task->ptid);
+  if (find_thread_ptid (current_inferior (), task->ptid) == NULL)
+    add_thread (current_inferior ()->process_target (), task->ptid);
 }
 
 void
diff --git a/gdb/record-btrace.c b/gdb/record-btrace.c
index 65dfe70dc1..936facbf1a 100644
--- a/gdb/record-btrace.c
+++ b/gdb/record-btrace.c
@@ -556,10 +556,11 @@ record_btrace_target::info_record ()
 
   DEBUG ("info");
 
-  tp = find_thread_ptid (inferior_ptid);
-  if (tp == NULL)
+  if (inferior_ptid == null_ptid)
     error (_("No thread."));
 
+  tp = inferior_thread ();
+
   validate_registers_access ();
 
   btinfo = &tp->btrace;
@@ -1373,7 +1374,8 @@ record_btrace_target::call_history_from (ULONGEST from, int size,
 enum record_method
 record_btrace_target::record_method (ptid_t ptid)
 {
-  struct thread_info * const tp = find_thread_ptid (ptid);
+  process_stratum_target *proc_target = current_inferior ()->process_target ();
+  thread_info *const tp = find_thread_ptid (proc_target, ptid);
 
   if (tp == NULL)
     error (_("No thread."));
@@ -1389,7 +1391,8 @@ record_btrace_target::record_method (ptid_t ptid)
 bool
 record_btrace_target::record_is_replaying (ptid_t ptid)
 {
-  for (thread_info *tp : all_non_exited_threads (ptid))
+  process_stratum_target *proc_target = current_inferior ()->process_target ();
+  for (thread_info *tp : all_non_exited_threads (proc_target, ptid))
     if (btrace_is_replaying (tp))
       return true;
 
@@ -1519,13 +1522,10 @@ record_btrace_target::remove_breakpoint (struct gdbarch *gdbarch,
 void
 record_btrace_target::fetch_registers (struct regcache *regcache, int regno)
 {
-  struct btrace_insn_iterator *replay;
-  struct thread_info *tp;
-
-  tp = find_thread_ptid (regcache->ptid ());
+  thread_info *tp = find_thread_ptid (regcache->target (), regcache->ptid ());
   gdb_assert (tp != NULL);
 
-  replay = tp->btrace.replay;
+  btrace_insn_iterator *replay = tp->btrace.replay;
   if (replay != NULL && !record_btrace_generating_corefile)
     {
       const struct btrace_insn *insn;
@@ -1966,6 +1966,8 @@ get_thread_current_frame_id (struct thread_info *tp)
 
   switch_to_thread (tp);
 
+  process_stratum_target *proc_target = tp->inf->process_target ();
+
   /* Clear the executing flag to allow changes to the current frame.
      We are not actually running, yet.  We just started a reverse execution
      command or a record goto command.
@@ -1974,7 +1976,7 @@ get_thread_current_frame_id (struct thread_info *tp)
      move the thread.  Since we need to recompute the stack, we temporarily
      set EXECUTING to flase.  */
   executing = tp->executing;
-  set_executing (inferior_ptid, false);
+  set_executing (proc_target, inferior_ptid, false);
 
   id = null_frame_id;
   try
@@ -1984,13 +1986,13 @@ get_thread_current_frame_id (struct thread_info *tp)
   catch (const gdb_exception &except)
     {
       /* Restore the previous execution state.  */
-      set_executing (inferior_ptid, executing);
+      set_executing (proc_target, inferior_ptid, executing);
 
       throw;
     }
 
   /* Restore the previous execution state.  */
-  set_executing (inferior_ptid, executing);
+  set_executing (proc_target, inferior_ptid, executing);
 
   return id;
 }
@@ -2154,11 +2156,14 @@ record_btrace_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
      record_btrace_wait below.
 
      For all-stop targets, we only step INFERIOR_PTID and continue others.  */
+
+  process_stratum_target *proc_target = current_inferior ()->process_target ();
+
   if (!target_is_non_stop_p ())
     {
       gdb_assert (inferior_ptid.matches (ptid));
 
-      for (thread_info *tp : all_non_exited_threads (ptid))
+      for (thread_info *tp : all_non_exited_threads (proc_target, ptid))
 	{
 	  if (tp->ptid.matches (inferior_ptid))
 	    record_btrace_resume_thread (tp, flag);
@@ -2168,7 +2173,7 @@ record_btrace_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
     }
   else
     {
-      for (thread_info *tp : all_non_exited_threads (ptid))
+      for (thread_info *tp : all_non_exited_threads (proc_target, ptid))
 	record_btrace_resume_thread (tp, flag);
     }
 
@@ -2526,7 +2531,8 @@ record_btrace_target::wait (ptid_t ptid, struct target_waitstatus *status,
     }
 
   /* Keep a work list of moving threads.  */
-  for (thread_info *tp : all_non_exited_threads (ptid))
+  process_stratum_target *proc_target = current_inferior ()->process_target ();
+  for (thread_info *tp : all_non_exited_threads (proc_target, ptid))
     if ((tp->btrace.flags & (BTHR_MOVE | BTHR_STOP)) != 0)
       moving.push_back (tp);
 
@@ -2646,7 +2652,10 @@ record_btrace_target::stop (ptid_t ptid)
     }
   else
     {
-      for (thread_info *tp : all_non_exited_threads (ptid))
+      process_stratum_target *proc_target
+	= current_inferior ()->process_target ();
+
+      for (thread_info *tp : all_non_exited_threads (proc_target, ptid))
 	{
 	  tp->btrace.flags &= ~BTHR_MOVE;
 	  tp->btrace.flags |= BTHR_STOP;
diff --git a/gdb/record-full.c b/gdb/record-full.c
index b96642b4f8..2138aa529a 100644
--- a/gdb/record-full.c
+++ b/gdb/record-full.c
@@ -1228,6 +1228,8 @@ record_full_wait_1 (struct target_ops *ops,
 		     interested in the event.  */
 
 		  registers_changed ();
+		  switch_to_thread (current_inferior ()->process_target (),
+				    ret);
 		  regcache = get_current_regcache ();
 		  tmp_pc = regcache_read_pc (regcache);
 		  const struct address_space *aspace = regcache->aspace ();
@@ -1260,14 +1262,17 @@ record_full_wait_1 (struct target_ops *ops,
 
                       if (gdbarch_software_single_step_p (gdbarch))
 			{
+			  process_stratum_target *proc_target
+			    = current_inferior ()->process_target ();
+
 			  /* Try to insert the software single step breakpoint.
 			     If insert success, set step to 0.  */
-			  set_executing (inferior_ptid, 0);
+			  set_executing (proc_target, inferior_ptid, 0);
 			  reinit_frame_cache ();
 
 			  step = !insert_single_step_breakpoints (gdbarch);
 
-			  set_executing (inferior_ptid, 1);
+			  set_executing (proc_target, inferior_ptid, 1);
 			}
 
 		      if (record_debug)
@@ -1290,6 +1295,8 @@ record_full_wait_1 (struct target_ops *ops,
     }
   else
     {
+      switch_to_thread (current_inferior ()->process_target (),
+			record_full_resume_ptid);
       struct regcache *regcache = get_current_regcache ();
       struct gdbarch *gdbarch = regcache->arch ();
       const struct address_space *aspace = regcache->aspace ();
diff --git a/gdb/regcache.c b/gdb/regcache.c
index 2e8b52ee75..ecea0aad0e 100644
--- a/gdb/regcache.c
+++ b/gdb/regcache.c
@@ -196,10 +196,11 @@ reg_buffer::reg_buffer (gdbarch *gdbarch, bool has_pseudo)
     }
 }
 
-regcache::regcache (gdbarch *gdbarch, const address_space *aspace_)
+regcache::regcache (process_stratum_target *target, gdbarch *gdbarch,
+		    const address_space *aspace_)
 /* The register buffers.  A read/write register cache can only hold
    [0 .. gdbarch_num_regs).  */
-  : detached_regcache (gdbarch, false), m_aspace (aspace_)
+  : detached_regcache (gdbarch, false), m_aspace (aspace_), m_target (target)
 {
   m_ptid = minus_one_ptid;
 }
@@ -320,14 +321,19 @@ reg_buffer::assert_regnum (int regnum) const
 std::forward_list<regcache *> regcache::current_regcache;
 
 struct regcache *
-get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch,
+get_thread_arch_aspace_regcache (process_stratum_target *target,
+				 ptid_t ptid, struct gdbarch *gdbarch,
 				 struct address_space *aspace)
 {
+  gdb_assert (target != nullptr);
+
   for (const auto &regcache : regcache::current_regcache)
-    if (regcache->ptid () == ptid && regcache->arch () == gdbarch)
+    if (regcache->target () == target
+	&& regcache->ptid () == ptid
+	&& regcache->arch () == gdbarch)
       return regcache;
 
-  regcache *new_regcache = new regcache (gdbarch, aspace);
+  regcache *new_regcache = new regcache (target, gdbarch, aspace);
 
   regcache::current_regcache.push_front (new_regcache);
   new_regcache->set_ptid (ptid);
@@ -336,26 +342,38 @@ get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch,
 }
 
 struct regcache *
-get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch)
+get_thread_arch_regcache (process_stratum_target *target, ptid_t ptid,
+			  struct gdbarch *gdbarch)
 {
+  scoped_restore_current_inferior restore_current_inferior;
+  set_current_inferior (find_inferior_ptid (target, ptid));
   address_space *aspace = target_thread_address_space (ptid);
 
-  return get_thread_arch_aspace_regcache  (ptid, gdbarch, aspace);
+  return get_thread_arch_aspace_regcache (target, ptid, gdbarch, aspace);
 }
 
+static process_stratum_target *current_thread_target;
 static ptid_t current_thread_ptid;
 static struct gdbarch *current_thread_arch;
 
 struct regcache *
-get_thread_regcache (ptid_t ptid)
+get_thread_regcache (process_stratum_target *target, ptid_t ptid)
 {
-  if (!current_thread_arch || current_thread_ptid != ptid)
+  if (!current_thread_arch
+      || target != current_thread_target
+      || current_thread_ptid != ptid)
     {
+      gdb_assert (ptid != null_ptid);
+
       current_thread_ptid = ptid;
+      current_thread_target = target;
+
+      scoped_restore_current_inferior restore_current_inferior;
+      set_current_inferior (find_inferior_ptid (target, ptid));
       current_thread_arch = target_thread_architecture (ptid);
     }
 
-  return get_thread_arch_regcache (ptid, current_thread_arch);
+  return get_thread_arch_regcache (target, ptid, current_thread_arch);
 }
 
 /* See regcache.h.  */
@@ -363,7 +381,8 @@ get_thread_regcache (ptid_t ptid)
 struct regcache *
 get_thread_regcache (thread_info *thread)
 {
-  return get_thread_regcache (thread->ptid);
+  return get_thread_regcache (thread->inf->process_target (),
+			      thread->ptid);
 }
 
 struct regcache *
@@ -377,7 +396,11 @@ get_current_regcache (void)
 struct regcache *
 get_thread_regcache_for_ptid (ptid_t ptid)
 {
-  return get_thread_regcache (ptid);
+  /* This function doesn't take a process_stratum_target parameter
+     because it's a gdbsupport/ routine implemented by both gdb and
+     gdbserver.  It always refers to a ptid of the current target.  */
+  process_stratum_target *proc_target = current_inferior ()->process_target ();
+  return get_thread_regcache (proc_target, ptid);
 }
 
 /* Observer for the target_changed event.  */
@@ -412,29 +435,34 @@ regcache::regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
    Indicate that registers may have changed, so invalidate the cache.  */
 
 void
-registers_changed_ptid (ptid_t ptid)
+registers_changed_ptid (process_stratum_target *target, ptid_t ptid)
 {
   for (auto oit = regcache::current_regcache.before_begin (),
 	 it = std::next (oit);
        it != regcache::current_regcache.end ();
        )
     {
-      if ((*it)->ptid ().matches (ptid))
+      struct regcache *regcache = *it;
+      if ((target == nullptr || regcache->target () == target)
+	  && regcache->ptid ().matches (ptid))
 	{
-	  delete *it;
+	  delete regcache;
 	  it = regcache::current_regcache.erase_after (oit);
 	}
       else
 	oit = it++;
     }
 
-  if (current_thread_ptid.matches (ptid))
+  if ((target == nullptr || current_thread_target == target)
+      && current_thread_ptid.matches (ptid))
     {
+      current_thread_target = NULL;
       current_thread_ptid = null_ptid;
       current_thread_arch = NULL;
     }
 
-  if (inferior_ptid.matches (ptid))
+  if ((target == nullptr || current_inferior ()->process_target () == target)
+      && inferior_ptid.matches (ptid))
     {
       /* We just deleted the regcache of the current thread.  Need to
 	 forget about any frames we have cached, too.  */
@@ -447,13 +475,13 @@ registers_changed_ptid (ptid_t ptid)
 void
 registers_changed_thread (thread_info *thread)
 {
-  registers_changed_ptid (thread->ptid);
+  registers_changed_ptid (thread->inf->process_target (), thread->ptid);
 }
 
 void
 registers_changed (void)
 {
-  registers_changed_ptid (minus_one_ptid);
+  registers_changed_ptid (nullptr, minus_one_ptid);
 }
 
 void
@@ -1400,6 +1428,21 @@ public:
   }
 };
 
+/* Wrapper around get_thread_arch_aspace_regcache that does some self checks.  */
+
+static void
+test_get_thread_arch_aspace_regcache (process_stratum_target *target,
+				      ptid_t ptid, struct gdbarch *gdbarch,
+				      address_space *aspace)
+{
+  struct regcache *regcache
+    = get_thread_arch_aspace_regcache (target, ptid, gdbarch, aspace);
+  SELF_CHECK (regcache != NULL);
+  SELF_CHECK (regcache->target () == target);
+  SELF_CHECK (regcache->ptid () == ptid);
+  SELF_CHECK (regcache->aspace () == aspace);
+}
+
 static void
 current_regcache_test (void)
 {
@@ -1408,47 +1451,61 @@ current_regcache_test (void)
 
   ptid_t ptid1 (1), ptid2 (2), ptid3 (3);
 
-  /* Get regcache from ptid1, a new regcache is added to
-     current_regcache.  */
-  regcache *regcache = get_thread_arch_aspace_regcache (ptid1,
-							target_gdbarch (),
-							NULL);
+  test_target_ops test_target1;
+  test_target_ops test_target2;
 
-  SELF_CHECK (regcache != NULL);
-  SELF_CHECK (regcache->ptid () == ptid1);
+  /* Get regcache from (target1,ptid1), a new regcache is added to
+     current_regcache.  */
+  test_get_thread_arch_aspace_regcache (&test_target1, ptid1,
+					target_gdbarch (),
+					NULL);
   SELF_CHECK (regcache_access::current_regcache_size () == 1);
 
-  /* Get regcache from ptid2, a new regcache is added to
+  /* Get regcache from (target1,ptid2), a new regcache is added to
      current_regcache.  */
-  regcache = get_thread_arch_aspace_regcache (ptid2,
-					      target_gdbarch (),
-					      NULL);
-  SELF_CHECK (regcache != NULL);
-  SELF_CHECK (regcache->ptid () == ptid2);
+  test_get_thread_arch_aspace_regcache (&test_target1, ptid2,
+					target_gdbarch (),
+					NULL);
   SELF_CHECK (regcache_access::current_regcache_size () == 2);
 
-  /* Get regcache from ptid3, a new regcache is added to
+  /* Get regcache from (target1,ptid3), a new regcache is added to
      current_regcache.  */
-  regcache = get_thread_arch_aspace_regcache (ptid3,
-					      target_gdbarch (),
-					      NULL);
-  SELF_CHECK (regcache != NULL);
-  SELF_CHECK (regcache->ptid () == ptid3);
+  test_get_thread_arch_aspace_regcache (&test_target1, ptid3,
+					target_gdbarch (),
+					NULL);
   SELF_CHECK (regcache_access::current_regcache_size () == 3);
 
-  /* Get regcache from ptid2 again, nothing is added to
+  /* Get regcache from (target1,ptid2) again, nothing is added to
      current_regcache.  */
-  regcache = get_thread_arch_aspace_regcache (ptid2,
-					      target_gdbarch (),
-					      NULL);
-  SELF_CHECK (regcache != NULL);
-  SELF_CHECK (regcache->ptid () == ptid2);
+  test_get_thread_arch_aspace_regcache (&test_target1, ptid2,
+					target_gdbarch (),
+					NULL);
   SELF_CHECK (regcache_access::current_regcache_size () == 3);
 
-  /* Mark ptid2 is changed, so regcache of ptid2 should be removed from
-     current_regcache.  */
-  registers_changed_ptid (ptid2);
-  SELF_CHECK (regcache_access::current_regcache_size () == 2);
+  /* Get regcache from (target2,ptid2), a new regcache is added to
+     current_regcache, since this time we're using a differen
+     target.  */
+  test_get_thread_arch_aspace_regcache (&test_target2, ptid2,
+					target_gdbarch (),
+					NULL);
+  SELF_CHECK (regcache_access::current_regcache_size () == 4);
+
+  /* Mark that (target1,ptid2) changed.  The regcache of (target1,
+     ptid2) should be removed from current_regcache.  */
+  registers_changed_ptid (&test_target1, ptid2);
+  SELF_CHECK (regcache_access::current_regcache_size () == 3);
+
+  /* Get the regcache from (target2,ptid2) again, confirming the
+     registers_changed_ptid call above did not delete it.  */
+  test_get_thread_arch_aspace_regcache (&test_target2, ptid2,
+					target_gdbarch (),
+					NULL);
+  SELF_CHECK (regcache_access::current_regcache_size () == 3);
+
+  /* Confirm that marking all regcaches of all targets as changed
+     clears current_regcache.  */
+  registers_changed_ptid (nullptr, minus_one_ptid);
+  SELF_CHECK (regcache_access::current_regcache_size () == 0);
 }
 
 class target_ops_no_register : public test_target_ops
@@ -1509,8 +1566,9 @@ target_ops_no_register::xfer_partial (enum target_object object,
 class readwrite_regcache : public regcache
 {
 public:
-  readwrite_regcache (struct gdbarch *gdbarch)
-    : regcache (gdbarch, nullptr)
+  readwrite_regcache (process_stratum_target *target,
+		      struct gdbarch *gdbarch)
+    : regcache (target, gdbarch, nullptr)
   {}
 };
 
@@ -1576,7 +1634,7 @@ cooked_read_test (struct gdbarch *gdbarch)
 	break;
     }
 
-  readwrite_regcache readwrite (gdbarch);
+  readwrite_regcache readwrite (&mock_target, gdbarch);
   gdb::def_vector<gdb_byte> buf (register_size (gdbarch, nonzero_regnum));
 
   readwrite.raw_read (nonzero_regnum, buf.data ());
@@ -1709,7 +1767,7 @@ cooked_write_test (struct gdbarch *gdbarch)
     }
   } pop_targets;
 
-  readwrite_regcache readwrite (gdbarch);
+  readwrite_regcache readwrite (&mock_target, gdbarch);
 
   const int num_regs = gdbarch_num_cooked_regs (gdbarch);
 
diff --git a/gdb/regcache.h b/gdb/regcache.h
index f7d4bc60f6..0303a9334a 100644
--- a/gdb/regcache.h
+++ b/gdb/regcache.h
@@ -29,17 +29,20 @@ struct regset;
 struct gdbarch;
 struct address_space;
 class thread_info;
+struct process_stratum_target;
 
 extern struct regcache *get_current_regcache (void);
-extern struct regcache *get_thread_regcache (ptid_t ptid);
+extern struct regcache *get_thread_regcache (process_stratum_target *target,
+					     ptid_t ptid);
 
 /* Get the regcache of THREAD.  */
 extern struct regcache *get_thread_regcache (thread_info *thread);
 
-extern struct regcache *get_thread_arch_regcache (ptid_t, struct gdbarch *);
-extern struct regcache *get_thread_arch_aspace_regcache (ptid_t,
-							 struct gdbarch *,
-							 struct address_space *);
+extern struct regcache *get_thread_arch_regcache
+  (process_stratum_target *targ, ptid_t, struct gdbarch *);
+extern struct regcache *get_thread_arch_aspace_regcache
+  (process_stratum_target *target, ptid_t,
+   struct gdbarch *, struct address_space *);
 
 extern enum register_status
   regcache_raw_read_signed (struct regcache *regcache,
@@ -385,13 +388,19 @@ public:
     this->m_ptid = ptid;
   }
 
+  process_stratum_target *target () const
+  {
+    return m_target;
+  }
+
 /* Dump the contents of a register from the register cache to the target
    debug.  */
   void debug_print_register (const char *func, int regno);
 
   static void regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid);
 protected:
-  regcache (gdbarch *gdbarch, const address_space *aspace_);
+  regcache (process_stratum_target *target, gdbarch *gdbarch,
+	    const address_space *aspace);
 
   static std::forward_list<regcache *> current_regcache;
 
@@ -421,14 +430,16 @@ private:
 
   /* If this is a read-write cache, which thread's registers is
      it connected to?  */
+  process_stratum_target *m_target;
   ptid_t m_ptid;
 
   friend struct regcache *
-  get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch,
+  get_thread_arch_aspace_regcache (process_stratum_target *target, ptid_t ptid,
+				   struct gdbarch *gdbarch,
 				   struct address_space *aspace);
 
   friend void
-  registers_changed_ptid (ptid_t ptid);
+  registers_changed_ptid (process_stratum_target *target, ptid_t ptid);
 };
 
 class readonly_detached_regcache : public readable_regcache
@@ -451,7 +462,8 @@ public:
 };
 
 extern void registers_changed (void);
-extern void registers_changed_ptid (ptid_t);
+extern void registers_changed_ptid (process_stratum_target *target,
+				    ptid_t ptid);
 
 /* Indicate that registers of THREAD may have changed, so invalidate
    the cache.  */
diff --git a/gdb/remote.c b/gdb/remote.c
index ae8720a5f4..773c027bf0 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -277,6 +277,9 @@ public: /* data */
 
   /* The status of the stub support for the various vCont actions.  */
   vCont_action_support supports_vCont;
+  /* Whether vCont support was probed already.  This is a workaround
+     until packet_support is per-connection.  */
+  bool supports_vCont_probed;
 
   /* True if the user has pressed Ctrl-C, but the target hasn't
      responded to that.  */
@@ -536,6 +539,8 @@ public:
 
   void async (int) override;
 
+  int async_wait_fd () override;
+
   void thread_events (int) override;
 
   int can_do_single_step () override;
@@ -1025,7 +1030,7 @@ static void remote_console_output (const char *msg);
 
 static void remote_btrace_reset (remote_state *rs);
 
-static void remote_unpush_and_throw (void);
+static void remote_unpush_and_throw (remote_target *target);
 
 /* For "remote".  */
 
@@ -1395,7 +1400,7 @@ remote_arch_state::remote_arch_state (struct gdbarch *gdbarch)
 static remote_target *
 get_current_remote_target ()
 {
-  target_ops *proc_target = find_target_at (process_stratum);
+  target_ops *proc_target = current_inferior ()->process_target ();
   return dynamic_cast<remote_target *> (proc_target);
 }
 
@@ -1676,6 +1681,7 @@ show_memory_packet_size (struct memory_packet_config *config)
     }
 }
 
+/* FIXME: needs to be per-remote-target.  */
 static struct memory_packet_config memory_write_packet_config =
 {
   "memory-write-packet-size",
@@ -1732,6 +1738,7 @@ remote_target::get_memory_write_packet_size ()
   return get_memory_packet_size (&memory_write_packet_config);
 }
 
+/* FIXME: needs to be per-remote-target.  */
 static struct memory_packet_config memory_read_packet_config =
 {
   "memory-read-packet-size",
@@ -2088,6 +2095,9 @@ enum {
   PACKET_MAX
 };
 
+/* FIXME: needs to be per-remote-target.  Ignoring this for now,
+   assuming all remote targets are the same server (thus all support
+   the same packets).  */
 static struct packet_config remote_protocol_packets[PACKET_MAX];
 
 /* Returns the packet's corresponding "set remote foo-packet" command
@@ -2389,6 +2399,7 @@ remote_target::remote_add_inferior (bool fake_pid_p, int pid, int attached,
 	  inf = add_inferior_with_spaces ();
 	}
       switch_to_inferior_no_thread (inf);
+      push_target (this);
       inferior_appeared (inf, pid);
     }
 
@@ -2404,7 +2415,8 @@ remote_target::remote_add_inferior (bool fake_pid_p, int pid, int attached,
 }
 
 static remote_thread_info *get_remote_thread_info (thread_info *thread);
-static remote_thread_info *get_remote_thread_info (ptid_t ptid);
+static remote_thread_info *get_remote_thread_info (remote_target *target,
+						   ptid_t ptid);
 
 /* Add thread PTID to GDB's thread list.  Tag it as executing/running
    according to RUNNING.  */
@@ -2422,13 +2434,13 @@ remote_target::remote_add_thread (ptid_t ptid, bool running, bool executing)
      might be confusing to the user.  Be silent then, preserving the
      age old behavior.  */
   if (rs->starting_up)
-    thread = add_thread_silent (ptid);
+    thread = add_thread_silent (this, ptid);
   else
-    thread = add_thread (ptid);
+    thread = add_thread (this, ptid);
 
   get_remote_thread_info (thread)->vcont_resumed = executing;
-  set_executing (ptid, executing);
-  set_running (ptid, running);
+  set_executing (this, ptid, executing);
+  set_running (this, ptid, running);
 
   return thread;
 }
@@ -2451,7 +2463,7 @@ remote_target::remote_notice_new_inferior (ptid_t currthread, int executing)
   /* If this is a new thread, add it to GDB's thread list.
      If we leave it up to WFI to do this, bad things will happen.  */
 
-  thread_info *tp = find_thread_ptid (currthread);
+  thread_info *tp = find_thread_ptid (this, currthread);
   if (tp != NULL && tp->state == THREAD_EXITED)
     {
       /* We're seeing an event on a thread id we knew had exited.
@@ -2460,7 +2472,7 @@ remote_target::remote_notice_new_inferior (ptid_t currthread, int executing)
       return;
     }
 
-  if (!in_thread_list (currthread))
+  if (!in_thread_list (this, currthread))
     {
       struct inferior *inf = NULL;
       int pid = currthread.pid ();
@@ -2473,8 +2485,8 @@ remote_target::remote_notice_new_inferior (ptid_t currthread, int executing)
 	     stub doesn't support qC.  This is the first stop reported
 	     after an attach, so this is the main thread.  Update the
 	     ptid in the thread list.  */
-	  if (in_thread_list (ptid_t (pid)))
-	    thread_change_ptid (inferior_ptid, currthread);
+	  if (in_thread_list (this, ptid_t (pid)))
+	    thread_change_ptid (this, inferior_ptid, currthread);
 	  else
 	    {
 	      remote_add_thread (currthread, running, executing);
@@ -2490,7 +2502,7 @@ remote_target::remote_notice_new_inferior (ptid_t currthread, int executing)
 	     doesn't support qC.  This is the first stop reported
 	     after an attach, so this is the main thread.  Update the
 	     ptid in the thread list.  */
-	  thread_change_ptid (inferior_ptid, currthread);
+	  thread_change_ptid (this, inferior_ptid, currthread);
 	  return;
 	}
 
@@ -2498,7 +2510,7 @@ remote_target::remote_notice_new_inferior (ptid_t currthread, int executing)
 	 extended-remote which already was debugging an inferior, we
 	 may not know about it yet.  Add it before adding its child
 	 thread, so notifications are emitted in a sensible order.  */
-      if (find_inferior_pid (currthread.pid ()) == NULL)
+      if (find_inferior_pid (this, currthread.pid ()) == NULL)
 	{
 	  struct remote_state *rs = get_remote_state ();
 	  bool fake_pid_p = !remote_multi_process_p (rs);
@@ -2538,10 +2550,12 @@ get_remote_thread_info (thread_info *thread)
   return static_cast<remote_thread_info *> (thread->priv.get ());
 }
 
+/* Return PTID's private thread data, creating it if necessary.  */
+
 static remote_thread_info *
-get_remote_thread_info (ptid_t ptid)
+get_remote_thread_info (remote_target *target, ptid_t ptid)
 {
-  thread_info *thr = find_thread_ptid (ptid);
+  thread_info *thr = find_thread_ptid (target, ptid);
   return get_remote_thread_info (thr);
 }
 
@@ -3801,6 +3815,9 @@ remote_target::update_thread_list ()
 	 target.  */
       for (thread_info *tp : all_threads_safe ())
 	{
+	  if (tp->inf->process_target () != this)
+	    continue;
+
 	  if (!context.contains_thread (tp->ptid))
 	    {
 	      /* Not found.  */
@@ -3826,7 +3843,7 @@ remote_target::update_thread_list ()
 
 	      remote_notice_new_inferior (item.ptid, executing);
 
-	      thread_info *tp = find_thread_ptid (item.ptid);
+	      thread_info *tp = find_thread_ptid (this, item.ptid);
 	      remote_thread_info *info = get_remote_thread_info (tp);
 	      info->core = item.core;
 	      info->extra = std::move (item.extra);
@@ -4028,13 +4045,6 @@ remote_target::close ()
   /* Make sure we leave stdin registered in the event loop.  */
   terminal_ours ();
 
-  /* We don't have a connection to the remote stub anymore.  Get rid
-     of all the inferiors and their threads we were controlling.
-     Reset inferior_ptid to null_ptid first, as otherwise has_stack_frame
-     will be unable to find the thread corresponding to (pid, 0, 0).  */
-  inferior_ptid = null_ptid;
-  discard_all_inferiors ();
-
   trace_reset_local_state ();
 
   delete this;
@@ -4344,7 +4354,7 @@ remote_target::add_current_inferior_and_thread (char *wait_status)
   /* Add the main thread and switch to it.  Don't try reading
      registers yet, since we haven't fetched the target description
      yet.  */
-  thread_info *tp = add_thread_silent (curr_ptid);
+  thread_info *tp = add_thread_silent (this, curr_ptid);
   switch_to_thread_no_regs (tp);
 }
 
@@ -4420,7 +4430,7 @@ remote_target::process_initial_stop_replies (int from_tty)
       if (ignore_event)
 	continue;
 
-      struct thread_info *evthread = find_thread_ptid (event_ptid);
+      thread_info *evthread = find_thread_ptid (this, event_ptid);
 
       if (ws.kind == TARGET_WAITKIND_STOPPED)
 	{
@@ -4440,14 +4450,14 @@ remote_target::process_initial_stop_replies (int from_tty)
 	  || ws.value.sig != GDB_SIGNAL_0)
 	evthread->suspend.waitstatus_pending_p = 1;
 
-      set_executing (event_ptid, 0);
-      set_running (event_ptid, 0);
+      set_executing (this, event_ptid, 0);
+      set_running (this, event_ptid, 0);
       get_remote_thread_info (evthread)->vcont_resumed = 0;
     }
 
   /* "Notice" the new inferiors before anything related to
      registers/memory.  */
-  for (inferior *inf : all_non_exited_inferiors ())
+  for (inferior *inf : all_non_exited_inferiors (this))
     {
       inf->needs_setup = 1;
 
@@ -4468,7 +4478,7 @@ remote_target::process_initial_stop_replies (int from_tty)
 
       /* If all threads of an inferior were already stopped, we
 	 haven't setup the inferior yet.  */
-      for (inferior *inf : all_non_exited_inferiors ())
+      for (inferior *inf : all_non_exited_inferiors (this))
 	{
 	  if (inf->needs_setup)
 	    {
@@ -4482,7 +4492,7 @@ remote_target::process_initial_stop_replies (int from_tty)
   /* Now go over all threads that are stopped, and print their current
      frame.  If all-stop, then if there's a signalled thread, pick
      that as current.  */
-  for (thread_info *thread : all_non_exited_threads ())
+  for (thread_info *thread : all_non_exited_threads (this))
     {
       if (first == NULL)
 	first = thread;
@@ -4521,7 +4531,7 @@ remote_target::process_initial_stop_replies (int from_tty)
   /* For "info program".  */
   thread_info *thread = inferior_thread ();
   if (thread->state == THREAD_STOPPED)
-    set_last_target_status (inferior_ptid, thread->suspend.waitstatus);
+    set_last_target_status (this, inferior_ptid, thread->suspend.waitstatus);
 }
 
 /* Start the remote connection and sync state.  */
@@ -4694,7 +4704,7 @@ remote_target::start_remote (int from_tty, int extended_p)
       /* Let the stub know that we want it to return the thread.  */
       set_continue_thread (minus_one_ptid);
 
-      if (thread_count () == 0)
+      if (thread_count (this) == 0)
 	{
 	  /* Target has no concept of threads at all.  GDB treats
 	     non-threaded target as single-threaded; add a main
@@ -4720,14 +4730,15 @@ remote_target::start_remote (int from_tty, int extended_p)
 		                    "warning: couldn't determine remote "
 				    "current thread; picking first in list.\n");
 
-	      for (thread_info *tp : all_non_exited_threads ())
+	      for (thread_info *tp : all_non_exited_threads (this,
+							     minus_one_ptid))
 		{
 		  switch_to_thread (tp);
 		  break;
 		}
 	    }
 	  else
-	    switch_to_thread (find_thread_ptid (curr_thread));
+	    switch_to_thread (find_thread_ptid (this, curr_thread));
 	}
 
       /* init_wait_for_inferior should be called before get_offsets in order
@@ -4786,7 +4797,7 @@ remote_target::start_remote (int from_tty, int extended_p)
 	  remote_notif_get_pending_events (notif);
 	}
 
-      if (thread_count () == 0)
+      if (thread_count (this) == 0)
 	{
 	  if (!extended_p)
 	    error (_("The target is not running (try extended-remote?)"));
@@ -5439,7 +5450,7 @@ remote_target::remote_serial_quit_handler ()
 	{
 	  if (query (_("The target is not responding to GDB commands.\n"
 		       "Stop debugging it? ")))
-	    remote_unpush_and_throw ();
+	    remote_unpush_and_throw (this);
 	}
       /* If ^C has already been sent once, offer to disconnect.  */
       else if (!target_terminal::is_ours () && rs->ctrlc_pending_p)
@@ -5463,19 +5474,29 @@ remote_serial_quit_handler ()
   curr_quit_handler_target->remote_serial_quit_handler ();
 }
 
-/* Remove any of the remote.c targets from target stack.  Upper targets depend
-   on it so remove them first.  */
+/* Remove the remote target from the target stack of each inferior
+   that is using it.  Upper targets depend on it so remove them
+   first.  */
 
 static void
-remote_unpush_target (void)
+remote_unpush_target (remote_target *target)
 {
-  pop_all_targets_at_and_above (process_stratum);
+  /* We have to unpush the target from all inferiors, even those that
+     aren't running.  */
+  scoped_restore_current_inferior restore_current_inferior;
+
+  for (inferior *inf : all_inferiors (target))
+    {
+      switch_to_inferior_no_thread (inf);
+      pop_all_targets_at_and_above (process_stratum);
+      generic_mourn_inferior ();
+    }
 }
 
 static void
-remote_unpush_and_throw (void)
+remote_unpush_and_throw (remote_target *target)
 {
-  remote_unpush_target ();
+  remote_unpush_target (target);
   throw_error (TARGET_CLOSE_ERROR, _("Disconnected from target."));
 }
 
@@ -5492,7 +5513,7 @@ remote_target::open_1 (const char *name, int from_tty, int extended_p)
   /* If we're connected to a running target, target_preopen will kill it.
      Ask this question first, before target_preopen has a chance to kill
      anything.  */
-  if (curr_remote != NULL && !have_inferiors ())
+  if (curr_remote != NULL && !target_has_execution)
     {
       if (from_tty
 	  && !query (_("Already connected to a remote target.  Disconnect? ")))
@@ -5621,7 +5642,7 @@ remote_target::open_1 (const char *name, int from_tty, int extended_p)
 	/* Pop the partially set up target - unless something else did
 	   already before throwing the exception.  */
 	if (ex.error != TARGET_CLOSE_ERROR)
-	  remote_unpush_target ();
+	  remote_unpush_target (remote);
 	throw;
       }
   }
@@ -5685,10 +5706,10 @@ remote_target::remote_detach_1 (inferior *inf, int from_tty)
   remote_detach_pid (pid);
 
   /* Exit only if this is the only active inferior.  */
-  if (from_tty && !rs->extended && number_of_live_inferiors () == 1)
+  if (from_tty && !rs->extended && number_of_live_inferiors (this) == 1)
     puts_filtered (_("Ending remote debugging.\n"));
 
-  struct thread_info *tp = find_thread_ptid (inferior_ptid);
+  thread_info *tp = find_thread_ptid (this, inferior_ptid);
 
   /* Check to see if we are detaching a fork parent.  Note that if we
      are detaching a fork child, tp == NULL.  */
@@ -5790,10 +5811,10 @@ remote_target::disconnect (const char *args, int from_tty)
     error (_("Argument given to \"disconnect\" when remotely debugging."));
 
   /* Make sure we unpush even the extended remote targets.  Calling
-     target_mourn_inferior won't unpush, and remote_mourn won't
-     unpush if there is more than one inferior left.  */
-  unpush_target (this);
-  generic_mourn_inferior ();
+     target_mourn_inferior won't unpush, and
+     remote_target::mourn_inferior won't unpush if there is more than
+     one inferior left.  */
+  remote_unpush_target (this);
 
   if (from_tty)
     puts_filtered ("Ending remote debugging.\n");
@@ -5881,10 +5902,10 @@ extended_remote_target::attach (const char *args, int from_tty)
       inferior_ptid = remote_current_thread (inferior_ptid);
 
       /* Add the main thread to the thread list.  */
-      thread_info *thr = add_thread_silent (inferior_ptid);
+      thread_info *thr = add_thread_silent (this, inferior_ptid);
       /* Don't consider the thread stopped until we've processed the
 	 saved stop reply.  */
-      set_executing (thr->ptid, true);
+      set_executing (this, thr->ptid, true);
     }
 
   /* Next, if the target can specify a description, read it.  We do
@@ -5986,6 +6007,7 @@ remote_target::remote_vcont_probe ()
     }
 
   packet_ok (rs->buf, &remote_protocol_packets[PACKET_vCont]);
+  rs->supports_vCont_probed = true;
 }
 
 /* Helper function for building "vCont" resumptions.  Write a
@@ -6023,10 +6045,10 @@ remote_target::append_resumption (char *p, char *endp,
 	{
 	  /* If we don't know about the target thread's tid, then
 	     we're resuming magic_null_ptid (see caller).  */
-	  tp = find_thread_ptid (magic_null_ptid);
+	  tp = find_thread_ptid (this, magic_null_ptid);
 	}
       else
-	tp = find_thread_ptid (ptid);
+	tp = find_thread_ptid (this, ptid);
       gdb_assert (tp != NULL);
 
       if (tp->control.may_range_step)
@@ -6089,7 +6111,7 @@ char *
 remote_target::append_pending_thread_resumptions (char *p, char *endp,
 						  ptid_t ptid)
 {
-  for (thread_info *thread : all_non_exited_threads (ptid))
+  for (thread_info *thread : all_non_exited_threads (this, ptid))
     if (inferior_ptid != thread->ptid
 	&& thread->suspend.stop_signal != GDB_SIGNAL_0)
       {
@@ -6122,7 +6144,7 @@ remote_target::remote_resume_with_hc (ptid_t ptid, int step,
   else
     set_continue_thread (ptid);
 
-  for (thread_info *thread : all_non_exited_threads ())
+  for (thread_info *thread : all_non_exited_threads (this))
     resume_clear_thread_private_info (thread);
 
   buf = rs->buf.data ();
@@ -6259,9 +6281,9 @@ remote_target::resume (ptid_t ptid, int step, enum gdb_signal siggnal)
       remote_thread_info *remote_thr;
 
       if (minus_one_ptid == ptid || ptid.is_pid ())
-	remote_thr = get_remote_thread_info (inferior_ptid);
+	remote_thr = get_remote_thread_info (this, inferior_ptid);
       else
-	remote_thr = get_remote_thread_info (ptid);
+	remote_thr = get_remote_thread_info (this, ptid);
 
       remote_thr->last_resume_step = step;
       remote_thr->last_resume_sig = siggnal;
@@ -6493,7 +6515,7 @@ remote_target::commit_resume ()
   may_global_wildcard_vcont = 1;
 
   /* And assume every process is individually wildcard-able too.  */
-  for (inferior *inf : all_non_exited_inferiors ())
+  for (inferior *inf : all_non_exited_inferiors (this))
     {
       remote_inferior *priv = get_remote_inferior (inf);
 
@@ -6504,7 +6526,7 @@ remote_target::commit_resume ()
      disable process and global wildcard resumes appropriately.  */
   check_pending_events_prevent_wildcard_vcont (&may_global_wildcard_vcont);
 
-  for (thread_info *tp : all_non_exited_threads ())
+  for (thread_info *tp : all_non_exited_threads (this))
     {
       /* If a thread of a process is not meant to be resumed, then we
 	 can't wildcard that process.  */
@@ -6533,7 +6555,7 @@ remote_target::commit_resume ()
   struct vcont_builder vcont_builder (this);
 
   /* Threads first.  */
-  for (thread_info *tp : all_non_exited_threads ())
+  for (thread_info *tp : all_non_exited_threads (this))
     {
       remote_thread_info *remote_thr = get_remote_thread_info (tp);
 
@@ -6562,7 +6584,7 @@ remote_target::commit_resume ()
      supposed to be resumed.  */
   any_process_wildcard = 0;
 
-  for (inferior *inf : all_non_exited_inferiors ())
+  for (inferior *inf : all_non_exited_inferiors (this))
     {
       if (get_remote_inferior (inf)->may_wildcard_vcont)
 	{
@@ -6583,7 +6605,7 @@ remote_target::commit_resume ()
 	}
       else
 	{
-	  for (inferior *inf : all_non_exited_inferiors ())
+	  for (inferior *inf : all_non_exited_inferiors (this))
 	    {
 	      if (get_remote_inferior (inf)->may_wildcard_vcont)
 		{
@@ -6610,7 +6632,10 @@ remote_target::remote_stop_ns (ptid_t ptid)
   char *p = rs->buf.data ();
   char *endp = p + get_remote_packet_size ();
 
-  if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
+  /* FIXME: This supports_vCont_probed check is a workaround until
+     packet_support is per-connection.  */
+  if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN
+      || !rs->supports_vCont_probed)
     remote_vcont_probe ();
 
   if (!rs->supports_vCont.t)
@@ -6767,7 +6792,7 @@ remote_target::interrupt_query ()
       if (query (_("The target is not responding to interrupt requests.\n"
 		   "Stop debugging it? ")))
 	{
-	  remote_unpush_target ();
+	  remote_unpush_target (this);
 	  throw_error (TARGET_CLOSE_ERROR, _("Disconnected from target."));
 	}
     }
@@ -6974,7 +6999,7 @@ remote_target::remove_new_fork_children (threads_listing_context *context)
 
   /* For any threads stopped at a fork event, remove the corresponding
      fork child threads from the CONTEXT list.  */
-  for (thread_info *thread : all_non_exited_threads ())
+  for (thread_info *thread : all_non_exited_threads (this))
     {
       struct target_waitstatus *ws = thread_pending_fork_status (thread);
 
@@ -7016,7 +7041,7 @@ remote_target::check_pending_events_prevent_wildcard_vcont
 	  || event->ws.kind == TARGET_WAITKIND_VFORKED)
 	*may_global_wildcard = 0;
 
-      struct inferior *inf = find_inferior_ptid (event->ptid);
+      struct inferior *inf = find_inferior_ptid (this, event->ptid);
 
       /* This may be the first time we heard about this process.
 	 Regardless, we must not do a global wildcard resume, otherwise
@@ -7382,9 +7407,10 @@ Packet: '%s'\n"),
 
 		  if (rsa == NULL)
 		    {
-		      inferior *inf = (event->ptid == null_ptid
-				       ? NULL
-				       : find_inferior_ptid (event->ptid));
+		      inferior *inf
+			= (event->ptid == null_ptid
+			   ? NULL
+			   : find_inferior_ptid (this, event->ptid));
 		      /* If this is the first time we learn anything
 			 about this process, skip the registers
 			 included in this packet, since we don't yet
@@ -7644,7 +7670,7 @@ remote_target::process_stop_reply (struct stop_reply *stop_reply,
       if (!stop_reply->regcache.empty ())
 	{
 	  struct regcache *regcache
-	    = get_thread_arch_regcache (ptid, stop_reply->arch);
+	    = get_thread_arch_regcache (this, ptid, stop_reply->arch);
 
 	  for (cached_reg_t &reg : stop_reply->regcache)
 	    {
@@ -7656,7 +7682,7 @@ remote_target::process_stop_reply (struct stop_reply *stop_reply,
 	}
 
       remote_notice_new_inferior (ptid, 0);
-      remote_thread_info *remote_thr = get_remote_thread_info (ptid);
+      remote_thread_info *remote_thr = get_remote_thread_info (this, ptid);
       remote_thr->core = stop_reply->core;
       remote_thr->stop_reason = stop_reply->stop_reason;
       remote_thr->watch_data_address = stop_reply->watch_data_address;
@@ -7726,9 +7752,9 @@ remote_target::wait_ns (ptid_t ptid, struct target_waitstatus *status, int optio
 /* Return the first resumed thread.  */
 
 static ptid_t
-first_remote_resumed_thread ()
+first_remote_resumed_thread (remote_target *target)
 {
-  for (thread_info *tp : all_non_exited_threads (minus_one_ptid))
+  for (thread_info *tp : all_non_exited_threads (target, minus_one_ptid))
     if (tp->resumed)
       return tp->ptid;
   return null_ptid;
@@ -7870,7 +7896,7 @@ remote_target::wait_as (ptid_t ptid, target_waitstatus *status, int options)
       if (event_ptid != null_ptid)
 	record_currthread (rs, event_ptid);
       else
-	event_ptid = first_remote_resumed_thread ();
+	event_ptid = first_remote_resumed_thread (this);
     }
   else
     {
@@ -7878,7 +7904,7 @@ remote_target::wait_as (ptid_t ptid, target_waitstatus *status, int options)
       record_currthread (rs, minus_one_ptid);
       /* It's possible that the packet did not include a pid.  */
       if (event_ptid == null_ptid)
-	event_ptid = first_remote_resumed_thread ();
+	event_ptid = first_remote_resumed_thread (this);
       /* EVENT_PTID could still be NULL_PTID.  Double-check.  */
       if (event_ptid == null_ptid)
 	event_ptid = magic_null_ptid;
@@ -8993,11 +9019,11 @@ remote_target::files_info ()
    for output compatibility with throw_perror_with_name.  */
 
 static void
-unpush_and_perror (const char *string)
+unpush_and_perror (remote_target *target, const char *string)
 {
   int saved_errno = errno;
 
-  remote_unpush_target ();
+  remote_unpush_target (target);
   throw_error (TARGET_CLOSE_ERROR, "%s: %s.", string,
 	       safe_strerror (saved_errno));
 }
@@ -9033,12 +9059,12 @@ remote_target::readchar (int timeout)
   switch ((enum serial_rc) ch)
     {
     case SERIAL_EOF:
-      remote_unpush_target ();
+      remote_unpush_target (this);
       throw_error (TARGET_CLOSE_ERROR, _("Remote connection closed"));
       /* no return */
     case SERIAL_ERROR:
-      unpush_and_perror (_("Remote communication error.  "
-			   "Target disconnected."));
+      unpush_and_perror (this, _("Remote communication error.  "
+				 "Target disconnected."));
       /* no return */
     case SERIAL_TIMEOUT:
       break;
@@ -9066,8 +9092,8 @@ remote_target::remote_serial_write (const char *str, int len)
 
   if (serial_write (rs->remote_desc, str, len))
     {
-      unpush_and_perror (_("Remote communication error.  "
-			   "Target disconnected."));
+      unpush_and_perror (this, _("Remote communication error.  "
+				 "Target disconnected."));
     }
 
   if (rs->got_ctrlc_during_io)
@@ -9584,7 +9610,7 @@ remote_target::getpkt_or_notif_sane_1 (gdb::char_vector *buf,
 
 	      if (forever)	/* Watchdog went off?  Kill the target.  */
 		{
-		  remote_unpush_target ();
+		  remote_unpush_target (this);
 		  throw_error (TARGET_CLOSE_ERROR,
 			       _("Watchdog timeout has expired.  "
 				 "Target detached."));
@@ -9694,7 +9720,7 @@ remote_target::kill_new_fork_children (int pid)
 
   /* Kill the fork child threads of any threads in process PID
      that are stopped at a fork event.  */
-  for (thread_info *thread : all_non_exited_threads ())
+  for (thread_info *thread : all_non_exited_threads (this))
     {
       struct target_waitstatus *ws = &thread->pending_follow;
 
@@ -9754,7 +9780,7 @@ remote_target::kill ()
      inferior, then we will tell gdbserver to exit and unpush the
      target.  */
   if (res == -1 && !remote_multi_process_p (rs)
-      && number_of_live_inferiors () == 1)
+      && number_of_live_inferiors (this) == 1)
     {
       remote_kill_k ();
 
@@ -9840,12 +9866,9 @@ remote_target::mourn_inferior ()
   discard_pending_stop_replies (current_inferior ());
 
   /* In 'target remote' mode with one inferior, we close the connection.  */
-  if (!rs->extended && number_of_live_inferiors () <= 1)
+  if (!rs->extended && number_of_live_inferiors (this) <= 1)
     {
-      unpush_target (this);
-
-      /* remote_close takes care of doing most of the clean up.  */
-      generic_mourn_inferior ();
+      remote_unpush_target (this);
       return;
     }
 
@@ -13429,7 +13452,7 @@ remote_target::set_disconnected_tracing (int val)
 int
 remote_target::core_of_thread (ptid_t ptid)
 {
-  struct thread_info *info = find_thread_ptid (ptid);
+  thread_info *info = find_thread_ptid (this, ptid);
 
   if (info != NULL && info->priv != NULL)
     return get_remote_thread_info (info)->core;
@@ -13709,7 +13732,7 @@ remote_target::remote_btrace_maybe_reopen ()
 
   scoped_restore_current_thread restore_thread;
 
-  for (thread_info *tp : all_non_exited_threads ())
+  for (thread_info *tp : all_non_exited_threads (this))
     {
       set_general_thread (tp->ptid);
 
@@ -13925,13 +13948,12 @@ char *
 remote_target::pid_to_exec_file (int pid)
 {
   static gdb::optional<gdb::char_vector> filename;
-  struct inferior *inf;
   char *annex = NULL;
 
   if (packet_support (PACKET_qXfer_exec_file) != PACKET_ENABLE)
     return NULL;
 
-  inf = find_inferior_pid (pid);
+  inferior *inf = find_inferior_pid (this, pid);
   if (inf == NULL)
     internal_error (__FILE__, __LINE__,
 		    _("not currently attached to process %d"), pid);
@@ -13992,7 +14014,7 @@ remote_target::thread_handle_to_thread_info (const gdb_byte *thread_handle,
 					     int handle_len,
 					     inferior *inf)
 {
-  for (thread_info *tp : all_non_exited_threads ())
+  for (thread_info *tp : all_non_exited_threads (this))
     {
       remote_thread_info *priv = get_remote_thread_info (tp);
 
@@ -14064,6 +14086,13 @@ remote_async_inferior_event_handler (gdb_client_data data)
   inferior_event_handler (INF_REG_EVENT, data);
 }
 
+int
+remote_target::async_wait_fd ()
+{
+  struct remote_state *rs = get_remote_state ();
+  return rs->remote_desc->fd;
+}
+
 void
 remote_target::async (int enable)
 {
diff --git a/gdb/riscv-fbsd-tdep.c b/gdb/riscv-fbsd-tdep.c
index a17d55b08b..926fd6b6e6 100644
--- a/gdb/riscv-fbsd-tdep.c
+++ b/gdb/riscv-fbsd-tdep.c
@@ -26,6 +26,7 @@
 #include "trad-frame.h"
 #include "tramp-frame.h"
 #include "gdbarch.h"
+#include "inferior.h"
 
 /* Register maps.  */
 
@@ -183,7 +184,8 @@ riscv_fbsd_get_thread_local_address (struct gdbarch *gdbarch, ptid_t ptid,
 {
   struct regcache *regcache;
 
-  regcache = get_thread_arch_regcache (ptid, gdbarch);
+  regcache = get_thread_arch_regcache (current_inferior ()->process_target (),
+				       ptid, gdbarch);
 
   target_fetch_registers (regcache, RISCV_TP_REGNUM);
 
diff --git a/gdb/sol-thread.c b/gdb/sol-thread.c
index 68fa85130a..869a88acb8 100644
--- a/gdb/sol-thread.c
+++ b/gdb/sol-thread.c
@@ -461,9 +461,13 @@ sol_thread_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
       /* See if we have a new thread.  */
       if (rtnval.tid_p () && rtnval != save_ptid)
 	{
-	  thread_info *thr = find_thread_ptid (rtnval);
+	  thread_info *thr = find_thread_ptid (current_inferior (), rtnval);
 	  if (thr == NULL || thr->state == THREAD_EXITED)
-	    add_thread (rtnval);
+	    {
+	      process_stratum_target *proc_target
+		= current_inferior ()->process_target ();
+	      add_thread (proc_target, rtnval);
+	    }
 	}
     }
 
@@ -853,7 +857,8 @@ ps_lgetregs (struct ps_prochandle *ph, lwpid_t lwpid, prgregset_t gregset)
 {
   ptid_t ptid = ptid_t (inferior_ptid.pid (), lwpid, 0);
   struct regcache *regcache
-    = get_thread_arch_regcache (ptid, target_gdbarch ());
+    = get_thread_arch_regcache (current_inferior ()->process_target (),
+				ptid, target_gdbarch ());
 
   target_fetch_registers (regcache, -1);
   fill_gregset (regcache, (gdb_gregset_t *) gregset, -1);
@@ -869,7 +874,8 @@ ps_lsetregs (struct ps_prochandle *ph, lwpid_t lwpid,
 {
   ptid_t ptid = ptid_t (inferior_ptid.pid (), lwpid, 0);
   struct regcache *regcache
-    = get_thread_arch_regcache (ptid, target_gdbarch ());
+    = get_thread_arch_regcache (current_inferior ()->process_target (),
+				ptid, target_gdbarch ());
 
   supply_gregset (regcache, (const gdb_gregset_t *) gregset);
   target_store_registers (regcache, -1);
@@ -921,7 +927,8 @@ ps_lgetfpregs (struct ps_prochandle *ph, lwpid_t lwpid,
 {
   ptid_t ptid = ptid_t (inferior_ptid.pid (), lwpid, 0);
   struct regcache *regcache
-    = get_thread_arch_regcache (ptid, target_gdbarch ());
+    = get_thread_arch_regcache (current_inferior ()->process_target (),
+				ptid, target_gdbarch ());
 
   target_fetch_registers (regcache, -1);
   fill_fpregset (regcache, (gdb_fpregset_t *) fpregset, -1);
@@ -937,7 +944,8 @@ ps_lsetfpregs (struct ps_prochandle *ph, lwpid_t lwpid,
 {
   ptid_t ptid = ptid_t (inferior_ptid.pid (), lwpid, 0);
   struct regcache *regcache
-    = get_thread_arch_regcache (ptid, target_gdbarch ());
+    = get_thread_arch_regcache (current_inferior ()->process_target (),
+				ptid, target_gdbarch ());
 
   supply_fpregset (regcache, (const gdb_fpregset_t *) fpregset);
   target_store_registers (regcache, -1);
@@ -1037,9 +1045,13 @@ sol_update_thread_list_callback (const td_thrhandle_t *th, void *ignored)
     return -1;
 
   ptid_t ptid = ptid_t (inferior_ptid.pid (), 0, ti.ti_tid);
-  thread_info *thr = find_thread_ptid (ptid);
+  thread_info *thr = find_thread_ptid (current_inferior (), ptid);
   if (thr == NULL || thr->state == THREAD_EXITED)
-    add_thread (ptid);
+    {
+      process_stratum_target *proc_target
+	= current_inferior ()->process_target ();
+      add_thread (proc_target, ptid);
+    }
 
   return 0;
 }
diff --git a/gdb/sol2-tdep.c b/gdb/sol2-tdep.c
index 014b7d79a7..dd6f06ec95 100644
--- a/gdb/sol2-tdep.c
+++ b/gdb/sol2-tdep.c
@@ -58,7 +58,7 @@ sol2_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
   /* GDB didn't use to put a NT_PSTATUS note in Solaris cores.  If
      that's missing, then we're dealing with a fake PID corelow.c made
      up.  */
-  inf = find_inferior_ptid (ptid);
+  inf = find_inferior_ptid (current_inferior ()->process_target (), ptid);
   if (inf == NULL || inf->fake_pid_p)
     return "<core>";
 
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index 486ae1215b..c1d4c49121 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -2375,7 +2375,8 @@ enable_break (struct svr4_info *info, int from_tty)
       if (!load_addr_found)
 	{
 	  struct regcache *regcache
-	    = get_thread_arch_regcache (inferior_ptid, target_gdbarch ());
+	    = get_thread_arch_regcache (current_inferior ()->process_target (),
+					inferior_ptid, target_gdbarch ());
 
 	  load_addr = (regcache_read_pc (regcache)
 		       - exec_entry_point (tmp_bfd.get (), tmp_bfd_target));
diff --git a/gdb/target-delegates.c b/gdb/target-delegates.c
index 52034fe436..5c2142b63f 100644
--- a/gdb/target-delegates.c
+++ b/gdb/target-delegates.c
@@ -83,6 +83,7 @@ struct dummy_target : public target_ops
   bool can_async_p () override;
   bool is_async_p () override;
   void async (int arg0) override;
+  int async_wait_fd () override;
   void thread_events (int arg0) override;
   bool supports_non_stop () override;
   bool always_non_stop_p () override;
@@ -251,6 +252,7 @@ struct debug_target : public target_ops
   bool can_async_p () override;
   bool is_async_p () override;
   void async (int arg0) override;
+  int async_wait_fd () override;
   void thread_events (int arg0) override;
   bool supports_non_stop () override;
   bool always_non_stop_p () override;
@@ -2162,6 +2164,31 @@ debug_target::async (int arg0)
   fputs_unfiltered (")\n", gdb_stdlog);
 }
 
+int
+target_ops::async_wait_fd ()
+{
+  return this->beneath ()->async_wait_fd ();
+}
+
+int
+dummy_target::async_wait_fd ()
+{
+  noprocess ();
+}
+
+int
+debug_target::async_wait_fd ()
+{
+  int result;
+  fprintf_unfiltered (gdb_stdlog, "-> %s->async_wait_fd (...)\n", this->beneath ()->shortname ());
+  result = this->beneath ()->async_wait_fd ();
+  fprintf_unfiltered (gdb_stdlog, "<- %s->async_wait_fd (", this->beneath ()->shortname ());
+  fputs_unfiltered (") = ", gdb_stdlog);
+  target_debug_print_int (result);
+  fputs_unfiltered ("\n", gdb_stdlog);
+  return result;
+}
+
 void
 target_ops::thread_events (int arg0)
 {
diff --git a/gdb/target.c b/gdb/target.c
index df70e67f28..c62a45b2f7 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -110,10 +110,6 @@ static std::unordered_map<const target_info *, target_open_ftype *>
 
 static struct target_ops *the_debug_target;
 
-/* The target stack.  */
-
-static target_stack g_target_stack;
-
 /* Top of target stack.  */
 /* The target structure we are currently using to talk to a process
    or file or whatever "inferior" we have.  */
@@ -121,7 +117,7 @@ static target_stack g_target_stack;
 target_ops *
 current_top_target ()
 {
-  return g_target_stack.top ();
+  return current_inferior ()->top_target ();
 }
 
 /* Command list for target.  */
@@ -226,7 +222,9 @@ target_has_registers_1 (void)
 bool
 target_has_execution_1 (inferior *inf)
 {
-  for (target_ops *t = current_top_target (); t != NULL; t = t->beneath ())
+  for (target_ops *t = inf->top_target ();
+       t != nullptr;
+       t = inf->find_target_beneath (t))
     if (t->has_execution (inf))
       return true;
 
@@ -501,13 +499,16 @@ target_terminal::info (const char *arg, int from_tty)
 bool
 target_supports_terminal_ours (void)
 {
-  /* This can be called before there is any target, so we must check
-     for nullptr here.  */
-  target_ops *top = current_top_target ();
+  /* The current top target is the target at the top of the target
+     stack of the current inferior.  While normally there's always an
+     inferior, we must check for nullptr here because we can get here
+     very early during startup, before the initial inferior is first
+     created.  */
+  inferior *inf = current_inferior ();
 
-  if (top == nullptr)
+  if (inf == nullptr)
     return false;
-  return top->supports_terminal_ours ();
+  return inf->top_target ()->supports_terminal_ours ();
 }
 
 static void
@@ -555,17 +556,30 @@ to_execution_direction must be implemented for reverse async");
 
 /* See target.h.  */
 
+void
+decref_target (target_ops *t)
+{
+  t->decref ();
+  if (t->refcount () == 0)
+    target_close (t);
+}
+
+/* See target.h.  */
+
 void
 target_stack::push (target_ops *t)
 {
-  /* If there's already a target at this stratum, remove it.  */
+  t->incref ();
+
   strata stratum = t->stratum ();
 
+  /* If there's already a target at this stratum, remove it.  */
+
   if (m_stack[stratum] != NULL)
     {
       target_ops *prev = m_stack[stratum];
       m_stack[stratum] = NULL;
-      target_close (prev);
+      decref_target (prev);
     }
 
   /* Now add the new one.  */
@@ -580,15 +594,15 @@ target_stack::push (target_ops *t)
 void
 push_target (struct target_ops *t)
 {
-  g_target_stack.push (t);
+  current_inferior ()->push_target (t);
 }
 
-/* See target.h  */
+/* See target.h.  */
 
 void
 push_target (target_ops_up &&t)
 {
-  g_target_stack.push (t.get ());
+  current_inferior ()->push_target (t.get ());
   t.release ();
 }
 
@@ -597,7 +611,7 @@ push_target (target_ops_up &&t)
 int
 unpush_target (struct target_ops *t)
 {
-  return g_target_stack.unpush (t);
+  return current_inferior ()->unpush_target (t);
 }
 
 /* See target.h.  */
@@ -629,10 +643,13 @@ target_stack::unpush (target_ops *t)
   if (m_top == stratum)
     m_top = t->beneath ()->stratum ();
 
-  /* Finally close the target.  Note we do this after unchaining, so
-     any target method calls from within the target_close
-     implementation don't end up in T anymore.  */
-  target_close (t);
+  /* Finally close the target, if there are no inferiors
+     referencing this target still.  Note we do this after unchaining,
+     so any target method calls from within the target_close
+     implementation don't end up in T anymore.  Do leave the target
+     open if we have are other inferiors referencing this target
+     still.  */
+  decref_target (t);
 
   return true;
 }
@@ -674,12 +691,13 @@ pop_all_targets (void)
   pop_all_targets_above (dummy_stratum);
 }
 
-/* Return 1 if T is now pushed in the target stack.  Return 0 otherwise.  */
+/* Return true if T is now pushed in the current inferior's target
+   stack.  Return false otherwise.  */
 
-int
-target_is_pushed (struct target_ops *t)
+bool
+target_is_pushed (target_ops *t)
 {
-  return g_target_stack.is_pushed (t);
+  return current_inferior ()->target_is_pushed (t);
 }
 
 /* Default implementation of to_get_thread_local_address.  */
@@ -1951,33 +1969,6 @@ target_pre_inferior (int from_tty)
   agent_capability_invalidate ();
 }
 
-/* Callback for iterate_over_inferiors.  Gets rid of the given
-   inferior.  */
-
-static int
-dispose_inferior (struct inferior *inf, void *args)
-{
-  /* Not all killed inferiors can, or will ever be, removed from the
-     inferior list.  Killed inferiors clearly don't need to be killed
-     again, so, we're done.  */
-  if (inf->pid == 0)
-    return 0;
-
-  thread_info *thread = any_thread_of_inferior (inf);
-  if (thread != NULL)
-    {
-      switch_to_thread (thread);
-
-      /* Core inferiors actually should be detached, not killed.  */
-      if (target_has_execution)
-	target_kill ();
-      else
-	target_detach (inf, 0);
-    }
-
-  return 0;
-}
-
 /* This is to be called by the open routine before it does
    anything.  */
 
@@ -1986,12 +1977,19 @@ target_preopen (int from_tty)
 {
   dont_repeat ();
 
-  if (have_inferiors ())
+  if (current_inferior ()->pid != 0)
     {
       if (!from_tty
-	  || !have_live_inferiors ()
+	  || !target_has_execution
 	  || query (_("A program is being debugged already.  Kill it? ")))
-	iterate_over_inferiors (dispose_inferior, NULL);
+	{
+	  /* Core inferiors actually should be detached, not
+	     killed.  */
+	  if (target_has_execution)
+	    target_kill ();
+	  else
+	    target_detach (current_inferior (), 0);
+	}
       else
 	error (_("Program not killed."));
     }
@@ -2033,9 +2031,16 @@ target_detach (inferior *inf, int from_tty)
 
   prepare_for_detach ();
 
+  /* Hold a strong reference because detaching may unpush the
+     target.  */
+  auto proc_target_ref = target_ops_ref::new_reference (inf->process_target ());
+
   current_top_target ()->detach (inf, from_tty);
 
-  registers_changed_ptid (save_pid_ptid);
+  process_stratum_target *proc_target
+    = as_process_stratum_target (proc_target_ref.get ());
+
+  registers_changed_ptid (proc_target, save_pid_ptid);
 
   /* We have to ensure we have no frame cache left.  Normally,
      registers_changed_ptid (save_pid_ptid) calls reinit_frame_cache when
@@ -2083,6 +2088,8 @@ target_pid_to_str (ptid_t ptid)
 const char *
 target_thread_name (struct thread_info *info)
 {
+  gdb_assert (info->inf == current_inferior ());
+
   return current_top_target ()->thread_name (info);
 }
 
@@ -2106,16 +2113,18 @@ target_thread_info_to_thread_handle (struct thread_info *tip)
 void
 target_resume (ptid_t ptid, int step, enum gdb_signal signal)
 {
+  process_stratum_target *curr_target = current_inferior ()->process_target ();
+
   target_dcache_invalidate ();
 
   current_top_target ()->resume (ptid, step, signal);
 
-  registers_changed_ptid (ptid);
+  registers_changed_ptid (curr_target, ptid);
   /* We only set the internal executing state here.  The user/frontend
      running state is set at a higher level.  This also clears the
      thread's stop_pc as side effect.  */
-  set_executing (ptid, 1);
-  clear_inline_frame_state (ptid);
+  set_executing (curr_target, ptid, 1);
+  clear_inline_frame_state (curr_target, ptid);
 }
 
 /* If true, target_commit_resume is a nop.  */
@@ -2540,7 +2549,6 @@ target_get_osdata (const char *type)
   return target_read_stralloc (t, TARGET_OBJECT_OSDATA, type);
 }
 
-
 /* Determine the current address space of thread PTID.  */
 
 struct address_space *
@@ -2559,7 +2567,7 @@ target_thread_address_space (ptid_t ptid)
 target_ops *
 target_ops::beneath () const
 {
-  return g_target_stack.find_beneath (this);
+  return current_inferior ()->find_target_beneath (this);
 }
 
 void
@@ -3155,7 +3163,7 @@ target_stack::find_beneath (const target_ops *t) const
 struct target_ops *
 find_target_at (enum strata stratum)
 {
-  return g_target_stack.at (stratum);
+  return current_inferior ()->target_at (stratum);
 }
 
 \f
@@ -3251,6 +3259,14 @@ dummy_make_corefile_notes (struct target_ops *self,
 
 static dummy_target the_dummy_target;
 
+/* See target.h.  */
+
+target_ops *
+get_dummy_target ()
+{
+  return &the_dummy_target;
+}
+
 static const target_info dummy_target_info = {
   "None",
   N_("None"),
@@ -3337,7 +3353,33 @@ target_interrupt ()
 void
 target_pass_ctrlc (void)
 {
-  current_top_target ()->pass_ctrlc ();
+  /* Pass the Ctrl-C to the first target that has a thread
+     running.  */
+  for (inferior *inf : all_inferiors ())
+    {
+      target_ops *proc_target = inf->process_target ();
+      if (proc_target == NULL)
+	continue;
+
+      for (thread_info *thr : inf->threads ())
+	{
+	  /* A thread can be THREAD_STOPPED and executing, while
+	     running an infcall.  */
+	  if (thr->state == THREAD_RUNNING || thr->executing)
+	    {
+	      /* We can get here quite deep in target layers.  Avoid
+		 switching thread context or anything that would
+		 communicate with the target (e.g., to fetch
+		 registers), or flushing e.g., the frame cache.  We
+		 just switch inferior in order to be able to call
+		 through the target_stack.  */
+	      scoped_restore_current_inferior restore_inferior;
+	      set_current_inferior (inf);
+	      current_top_target ()->pass_ctrlc ();
+	      return;
+	    }
+	}
+    }
 }
 
 /* See target.h.  */
@@ -3985,10 +4027,8 @@ set_write_memory_permission (const char *args, int from_tty,
 }
 
 void
-initialize_targets (void)
+_initialize_target ()
 {
-  push_target (&the_dummy_target);
-
   the_debug_target = new debug_target ();
 
   add_info ("target", info_target_command, targ_desc);
diff --git a/gdb/target.h b/gdb/target.h
index 5939126c3c..d946fc00dd 100644
--- a/gdb/target.h
+++ b/gdb/target.h
@@ -43,6 +43,7 @@ struct inferior;
 #include "infrun.h" /* For enum exec_direction_kind.  */
 #include "breakpoint.h" /* For enum bptype.  */
 #include "gdbsupport/scoped_restore.h"
+#include "gdbsupport/refcounted-object.h"
 
 /* This include file defines the interface between the main part
    of the debugger, and the part which is target-specific, or
@@ -427,6 +428,7 @@ struct target_info
 };
 
 struct target_ops
+  : public refcounted_object
   {
     /* Return this target's stratum.  */
     virtual strata stratum () const = 0;
@@ -445,10 +447,10 @@ struct target_ops
     virtual const target_info &info () const = 0;
 
     /* Name this target type.  */
-    const char *shortname ()
+    const char *shortname () const
     { return info ().shortname; }
 
-    const char *longname ()
+    const char *longname () const
     { return info ().longname; }
 
     /* Close the target.  This is where the target can handle
@@ -694,6 +696,8 @@ struct target_ops
       TARGET_DEFAULT_RETURN (false);
     virtual void async (int)
       TARGET_DEFAULT_NORETURN (tcomplain ());
+    virtual int async_wait_fd ()
+      TARGET_DEFAULT_NORETURN (noprocess ());
     virtual void thread_events (int)
       TARGET_DEFAULT_IGNORE ();
     /* This method must be implemented in some situations.  See the
@@ -1258,6 +1262,27 @@ struct target_ops_deleter
 /* A unique pointer for target_ops.  */
 typedef std::unique_ptr<target_ops, target_ops_deleter> target_ops_up;
 
+/* Decref a target and close if, if there are no references left.  */
+extern void decref_target (target_ops *t);
+
+/* A policy class to interface gdb::ref_ptr with target_ops.  */
+
+struct target_ops_ref_policy
+{
+  static void incref (target_ops *t)
+  {
+    t->incref ();
+  }
+
+  static void decref (target_ops *t)
+  {
+    decref_target (t);
+  }
+};
+
+/* A gdb::ref_ptr pointer to a target_ops.  */
+typedef gdb::ref_ptr<target_ops, target_ops_ref_policy> target_ops_ref;
+
 /* Native target backends call this once at initialization time to
    inform the core about which is the target that can respond to "run"
    or "attach".  Note: native targets are always singletons.  */
@@ -1312,6 +1337,9 @@ private:
 
 extern target_ops *current_top_target ();
 
+/* Return the dummy target.  */
+extern target_ops *get_dummy_target ();
+
 /* Define easy words for doing these operations on our current target.  */
 
 #define	target_shortname	(current_top_target ()->shortname ())
@@ -2360,7 +2388,7 @@ extern void pop_all_targets_at_and_above (enum strata stratum);
    strictly above ABOVE_STRATUM.  */
 extern void pop_all_targets_above (enum strata above_stratum);
 
-extern int target_is_pushed (struct target_ops *t);
+extern bool target_is_pushed (target_ops *t);
 
 extern CORE_ADDR target_translate_tls_address (struct objfile *objfile,
 					       CORE_ADDR offset);
diff --git a/gdb/thread-iter.c b/gdb/thread-iter.c
index 9a41a46aa6..a41f894325 100644
--- a/gdb/thread-iter.c
+++ b/gdb/thread-iter.c
@@ -58,16 +58,22 @@ all_threads_iterator::advance ()
 bool
 all_matching_threads_iterator::m_inf_matches ()
 {
-  return (m_filter_ptid == minus_one_ptid
-	  || m_filter_ptid.pid () == m_inf->pid);
+  return ((m_filter_target == nullptr
+	   || m_filter_target == m_inf->process_target ())
+	  && (m_filter_ptid == minus_one_ptid
+	      || m_filter_ptid.pid () == m_inf->pid));
 }
 
 /* See thread-iter.h.  */
 
 all_matching_threads_iterator::all_matching_threads_iterator
-  (ptid_t filter_ptid)
-  : m_filter_ptid (filter_ptid)
+  (process_stratum_target *filter_target, ptid_t filter_ptid)
+    : m_filter_target (filter_target),
+      m_filter_ptid (filter_ptid)
 {
+  gdb_assert ((filter_target == nullptr && filter_ptid == minus_one_ptid)
+	      || filter_target->stratum () == process_stratum);
+
   m_thr = nullptr;
   for (m_inf = inferior_list; m_inf != NULL; m_inf = m_inf->next)
     if (m_inf_matches ())
diff --git a/gdb/thread-iter.h b/gdb/thread-iter.h
index 72ee9ddcb8..445a349797 100644
--- a/gdb/thread-iter.h
+++ b/gdb/thread-iter.h
@@ -92,12 +92,14 @@ public:
 
   /* Creates an iterator that iterates over all threads that match
      FILTER_PTID.  */
-  explicit all_matching_threads_iterator (ptid_t filter_ptid);
+  all_matching_threads_iterator (process_stratum_target *filter_target,
+				 ptid_t filter_ptid);
 
   /* Create a one-past-end iterator.  */
   all_matching_threads_iterator ()
     : m_inf (nullptr),
       m_thr (nullptr),
+      m_filter_target (nullptr),
       m_filter_ptid (minus_one_ptid)
   {}
 
@@ -131,6 +133,7 @@ private:
   thread_info *m_thr;
 
   /* The filter.  */
+  process_stratum_target *m_filter_target;
   ptid_t m_filter_ptid;
 };
 
@@ -211,20 +214,22 @@ struct all_threads_safe_range
 struct all_matching_threads_range
 {
 public:
-  explicit all_matching_threads_range (ptid_t filter_ptid)
-    : m_filter_ptid (filter_ptid)
+  all_matching_threads_range (process_stratum_target *filter_target,
+			      ptid_t filter_ptid)
+    : m_filter_target (filter_target), m_filter_ptid (filter_ptid)
   {}
   all_matching_threads_range ()
-    : m_filter_ptid (minus_one_ptid)
+    : m_filter_target (nullptr), m_filter_ptid (minus_one_ptid)
   {}
 
   all_matching_threads_iterator begin () const
-  { return all_matching_threads_iterator (m_filter_ptid); }
+  { return all_matching_threads_iterator (m_filter_target, m_filter_ptid); }
   all_matching_threads_iterator end () const
   { return all_matching_threads_iterator (); }
 
 private:
   /* The filter.  */
+  process_stratum_target *m_filter_target;
   ptid_t m_filter_ptid;
 };
 
@@ -236,20 +241,22 @@ private:
 class all_non_exited_threads_range
 {
 public:
-  explicit all_non_exited_threads_range (ptid_t filter_ptid)
-    : m_filter_ptid (filter_ptid)
+  all_non_exited_threads_range (process_stratum_target *filter_target,
+				ptid_t filter_ptid)
+    : m_filter_target (filter_target), m_filter_ptid (filter_ptid)
   {}
 
   all_non_exited_threads_range ()
-    : m_filter_ptid (minus_one_ptid)
+    : m_filter_target (nullptr), m_filter_ptid (minus_one_ptid)
   {}
 
   all_non_exited_threads_iterator begin () const
-  { return all_non_exited_threads_iterator (m_filter_ptid); }
+  { return all_non_exited_threads_iterator (m_filter_target, m_filter_ptid); }
   all_non_exited_threads_iterator end () const
   { return all_non_exited_threads_iterator (); }
 
 private:
+  process_stratum_target *m_filter_target;
   ptid_t m_filter_ptid;
 };
 
diff --git a/gdb/thread.c b/gdb/thread.c
index ae6e6e5205..24ebadd4bc 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -55,13 +55,6 @@
 
 static int highest_thread_num;
 
-/* True if any thread is, or may be executing.  We need to track this
-   separately because until we fully sync the thread list, we won't
-   know whether the target is fully stopped, even if we see stop
-   events for all known threads, because any of those threads may have
-   spawned new threads we haven't heard of yet.  */
-static int threads_executing;
-
 /* RAII type used to increase / decrease the refcount of each thread
    in a given list of threads.  */
 
@@ -89,7 +82,7 @@ private:
 struct thread_info*
 inferior_thread (void)
 {
-  struct thread_info *tp = find_thread_ptid (inferior_ptid);
+  struct thread_info *tp = find_thread_ptid (current_inferior (), inferior_ptid);
   gdb_assert (tp);
   return tp;
 }
@@ -195,7 +188,7 @@ clear_thread_inferior_resources (struct thread_info *tp)
 
   thread_cancel_execution_command (tp);
 
-  clear_inline_frame_state (tp->ptid);
+  clear_inline_frame_state (tp);
 }
 
 /* Set the TP's state as exited.  */
@@ -260,12 +253,11 @@ new_thread (struct inferior *inf, ptid_t ptid)
 }
 
 struct thread_info *
-add_thread_silent (ptid_t ptid)
+add_thread_silent (process_stratum_target *targ, ptid_t ptid)
 {
-  struct inferior *inf = find_inferior_ptid (ptid);
-  gdb_assert (inf != NULL);
+  inferior *inf;
 
-  thread_info *tp = find_thread_ptid (inf, ptid);
+  thread_info *tp = find_thread_ptid (targ, ptid);
   if (tp)
     /* Found an old thread with the same id.  It has to be dead,
        otherwise we wouldn't be adding a new thread with the same id.
@@ -281,7 +273,7 @@ add_thread_silent (ptid_t ptid)
 
       if (inferior_ptid == ptid)
 	{
-	  thread_info *new_thr = new_thread (inf, null_ptid);
+	  thread_info *new_thr = new_thread (tp->inf, null_ptid);
 
 	  /* Make switch_to_thread not read from the thread.  */
 	  new_thr->state = THREAD_EXITED;
@@ -300,10 +292,14 @@ add_thread_silent (ptid_t ptid)
 	  /* All done.  */
 	  return new_thr;
 	}
-      else
-	/* Just go ahead and delete it.  */
-	delete_thread (tp);
+
+      inf = tp->inf;
+
+      /* Just go ahead and delete it.  */
+      delete_thread (tp);
     }
+  else
+    inf = find_inferior_ptid (targ, ptid);
 
   tp = new_thread (inf, ptid);
   gdb::observers::new_thread.notify (tp);
@@ -312,9 +308,10 @@ add_thread_silent (ptid_t ptid)
 }
 
 struct thread_info *
-add_thread_with_info (ptid_t ptid, private_thread_info *priv)
+add_thread_with_info (process_stratum_target *targ, ptid_t ptid,
+		      private_thread_info *priv)
 {
-  struct thread_info *result = add_thread_silent (ptid);
+  thread_info *result = add_thread_silent (targ, ptid);
 
   result->priv.reset (priv);
 
@@ -326,9 +323,9 @@ add_thread_with_info (ptid_t ptid, private_thread_info *priv)
 }
 
 struct thread_info *
-add_thread (ptid_t ptid)
+add_thread (process_stratum_target *targ, ptid_t ptid)
 {
-  return add_thread_with_info (ptid, NULL);
+  return add_thread_with_info (targ, ptid, NULL);
 }
 
 private_thread_info::~private_thread_info () = default;
@@ -352,6 +349,14 @@ thread_info::~thread_info ()
   xfree (this->name);
 }
 
+/* Returns true if THR is the current thread.  */
+
+static bool
+is_current_thread (const thread_info *thr)
+{
+  return thr->inf == current_inferior () && thr->ptid == inferior_ptid;
+}
+
 /* See gdbthread.h.  */
 
 bool
@@ -359,7 +364,7 @@ thread_info::deletable () const
 {
   /* If this is the current thread, or there's code out there that
      relies on it existing (refcount > 0) we can't delete yet.  */
-  return refcount () == 0 && ptid != inferior_ptid;
+  return refcount () == 0 && !is_current_thread (this);
 }
 
 /* Add TP to the end of the step-over chain LIST_P.  */
@@ -514,12 +519,12 @@ find_thread_id (struct inferior *inf, int thr_num)
   return NULL;
 }
 
-/* Find a thread_info by matching PTID.  */
+/* See gdbthread.h.  */
 
 struct thread_info *
-find_thread_ptid (ptid_t ptid)
+find_thread_ptid (process_stratum_target *targ, ptid_t ptid)
 {
-  inferior *inf = find_inferior_ptid (ptid);
+  inferior *inf = find_inferior_ptid (targ, ptid);
   if (inf == NULL)
     return NULL;
   return find_thread_ptid (inf, ptid);
@@ -584,9 +589,9 @@ any_thread_p ()
 }
 
 int
-thread_count (void)
+thread_count (process_stratum_target *proc_target)
 {
-  auto rng = all_threads ();
+  auto rng = all_threads (proc_target);
   return std::distance (rng.begin (), rng.end ());
 }
 
@@ -609,10 +614,10 @@ valid_global_thread_id (int global_id)
   return 0;
 }
 
-int
-in_thread_list (ptid_t ptid)
+bool
+in_thread_list (process_stratum_target *targ, ptid_t ptid)
 {
-  return find_thread_ptid (ptid) != nullptr;
+  return find_thread_ptid (targ, ptid) != nullptr;
 }
 
 /* Finds the first thread of the inferior.  */
@@ -788,7 +793,8 @@ get_last_thread_stack_temporary (thread_info *tp)
 }
 
 void
-thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
+thread_change_ptid (process_stratum_target *targ,
+		    ptid_t old_ptid, ptid_t new_ptid)
 {
   struct inferior *inf;
   struct thread_info *tp;
@@ -796,7 +802,7 @@ thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
   /* It can happen that what we knew as the target inferior id
      changes.  E.g, target remote may only discover the remote process
      pid after adding the inferior to GDB's list.  */
-  inf = find_inferior_ptid (old_ptid);
+  inf = find_inferior_ptid (targ, old_ptid);
   inf->pid = new_ptid.pid ();
 
   tp = find_thread_ptid (inf, old_ptid);
@@ -808,9 +814,9 @@ thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
 /* See gdbthread.h.  */
 
 void
-set_resumed (ptid_t ptid, int resumed)
+set_resumed (process_stratum_target *targ, ptid_t ptid, bool resumed)
 {
-  for (thread_info *tp : all_non_exited_threads (ptid))
+  for (thread_info *tp : all_non_exited_threads (targ, ptid))
     tp->resumed = resumed;
 }
 
@@ -848,7 +854,7 @@ thread_info::set_running (bool running)
 }
 
 void
-set_running (ptid_t ptid, int running)
+set_running (process_stratum_target *targ, ptid_t ptid, bool running)
 {
   /* We try not to notify the observer if no thread has actually
      changed the running state -- merely to reduce the number of
@@ -856,7 +862,7 @@ set_running (ptid_t ptid, int running)
      multiple *running notifications just fine.  */
   bool any_started = false;
 
-  for (thread_info *tp : all_non_exited_threads (ptid))
+  for (thread_info *tp : all_non_exited_threads (targ, ptid))
     if (set_running_thread (tp, running))
       any_started = true;
 
@@ -878,32 +884,32 @@ set_executing_thread (thread_info *thr, bool executing)
 }
 
 void
-set_executing (ptid_t ptid, int executing)
+set_executing (process_stratum_target *targ, ptid_t ptid, bool executing)
 {
-  for (thread_info *tp : all_non_exited_threads (ptid))
+  for (thread_info *tp : all_non_exited_threads (targ, ptid))
     set_executing_thread (tp, executing);
 
   /* It only takes one running thread to spawn more threads.  */
   if (executing)
-    threads_executing = 1;
+    targ->threads_executing = true;
   /* Only clear the flag if the caller is telling us everything is
      stopped.  */
   else if (minus_one_ptid == ptid)
-    threads_executing = 0;
+    targ->threads_executing = false;
 }
 
 /* See gdbthread.h.  */
 
-int
-threads_are_executing (void)
+bool
+threads_are_executing (process_stratum_target *target)
 {
-  return threads_executing;
+  return target->threads_executing;
 }
 
 void
-set_stop_requested (ptid_t ptid, int stop)
+set_stop_requested (process_stratum_target *targ, ptid_t ptid, bool stop)
 {
-  for (thread_info *tp : all_non_exited_threads (ptid))
+  for (thread_info *tp : all_non_exited_threads (targ, ptid))
     tp->stop_requested = stop;
 
   /* Call the stop requested observer so other components of GDB can
@@ -913,11 +919,11 @@ set_stop_requested (ptid_t ptid, int stop)
 }
 
 void
-finish_thread_state (ptid_t ptid)
+finish_thread_state (process_stratum_target *targ, ptid_t ptid)
 {
   bool any_started = false;
 
-  for (thread_info *tp : all_non_exited_threads (ptid))
+  for (thread_info *tp : all_non_exited_threads (targ, ptid))
     if (set_running_thread (tp, tp->executing))
       any_started = true;
 
@@ -1333,7 +1339,7 @@ switch_to_thread (thread_info *thr)
 {
   gdb_assert (thr != NULL);
 
-  if (inferior_ptid == thr->ptid)
+  if (is_current_thread (thr))
     return;
 
   switch_to_thread_no_regs (thr);
@@ -1344,9 +1350,9 @@ switch_to_thread (thread_info *thr)
 /* See gdbsupport/common-gdbthread.h.  */
 
 void
-switch_to_thread (ptid_t ptid)
+switch_to_thread (process_stratum_target *proc_target, ptid_t ptid)
 {
-  thread_info *thr = find_thread_ptid (ptid);
+  thread_info *thr = find_thread_ptid (proc_target, ptid);
   switch_to_thread (thr);
 }
 
@@ -2069,18 +2075,39 @@ print_selected_thread_frame (struct ui_out *uiout,
 }
 
 /* Update the 'threads_executing' global based on the threads we know
-   about right now.  */
+   about right now.  This is used by infrun to tell whether we should
+   pull events out of the current target.  */
 
 static void
 update_threads_executing (void)
 {
-  threads_executing = 0;
-  for (thread_info *tp : all_non_exited_threads ())
+  process_stratum_target *targ = current_inferior ()->process_target ();
+
+  if (targ == NULL)
+    return;
+
+  targ->threads_executing = false;
+
+  for (inferior *inf : all_non_exited_inferiors (targ))
     {
-      if (tp->executing)
+      if (!inf->has_execution ())
+	continue;
+
+      /* If the process has no threads, then it must be we have a
+	 process-exit event pending.  */
+      if (inf->thread_list == NULL)
+	{
+	  targ->threads_executing = true;
+	  return;
+	}
+
+      for (thread_info *tp : inf->non_exited_threads ())
 	{
-	  threads_executing = 1;
-	  break;
+	  if (tp->executing)
+	    {
+	      targ->threads_executing = true;
+	      return;
+	    }
 	}
     }
 }
diff --git a/gdb/top.c b/gdb/top.c
index 15d4fab8be..48e60204f6 100644
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -1679,13 +1679,17 @@ quit_force (int *exit_arg, int from_tty)
 
   /* Give all pushed targets a chance to do minimal cleanup, and pop
      them all out.  */
-  try
+  for (inferior *inf : all_inferiors ())
     {
-      pop_all_targets ();
-    }
-  catch (const gdb_exception &ex)
-    {
-      exception_print (gdb_stderr, ex);
+      switch_to_inferior_no_thread (inf);
+      try
+	{
+	  pop_all_targets ();
+	}
+      catch (const gdb_exception &ex)
+	{
+	  exception_print (gdb_stderr, ex);
+	}
     }
 
   /* Save the history information if it is appropriate to do so.  */
@@ -2231,7 +2235,6 @@ gdb_init (char *argv0)
 #endif
 
   init_cmd_lists ();	    /* This needs to be done first.  */
-  initialize_targets ();    /* Setup target_terminal macros for utils.c.  */
 
   init_page_info ();
 
diff --git a/gdb/tracectf.c b/gdb/tracectf.c
index 758dd5681d..9a8e59186f 100644
--- a/gdb/tracectf.c
+++ b/gdb/tracectf.c
@@ -1169,7 +1169,7 @@ ctf_target_open (const char *dirname, int from_tty)
 
   inferior_appeared (current_inferior (), CTF_PID);
   inferior_ptid = ptid_t (CTF_PID);
-  add_thread_silent (inferior_ptid);
+  add_thread_silent (&ctf_ops, inferior_ptid);
 
   merge_uploaded_trace_state_variables (&uploaded_tsvs);
   merge_uploaded_tracepoints (&uploaded_tps);
diff --git a/gdb/tracefile-tfile.c b/gdb/tracefile-tfile.c
index c5063e66b0..f6ff71104f 100644
--- a/gdb/tracefile-tfile.c
+++ b/gdb/tracefile-tfile.c
@@ -557,7 +557,7 @@ tfile_target_open (const char *arg, int from_tty)
 
   inferior_appeared (current_inferior (), TFILE_PID);
   inferior_ptid = ptid_t (TFILE_PID);
-  add_thread_silent (inferior_ptid);
+  add_thread_silent (&tfile_ops, inferior_ptid);
 
   if (ts->traceframe_count <= 0)
     warning (_("No traceframes present in this file."));
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index a756913cab..7c10f83ddb 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -349,6 +349,8 @@ struct windows_nat_target final : public x86_nat_target<inf_child_target>
   bool get_tib_address (ptid_t ptid, CORE_ADDR *addr) override;
 
   const char *thread_name (struct thread_info *) override;
+
+  int get_windows_debug_event (int pid, struct target_waitstatus *ourstatus);
 };
 
 static windows_nat_target the_windows_nat_target;
@@ -457,9 +459,9 @@ windows_add_thread (ptid_t ptid, HANDLE h, void *tlb, bool main_thread_p)
      the main thread silently (in reality, this thread is really
      more of a process to the user than a thread).  */
   if (main_thread_p)
-    add_thread_silent (ptid);
+    add_thread_silent (&the_windows_nat_target, ptid);
   else
-    add_thread (ptid);
+    add_thread (&the_windows_nat_target, ptid);
 
   /* Set the debug registers for the new thread if they are used.  */
   if (debug_registers_used)
@@ -528,7 +530,7 @@ windows_delete_thread (ptid_t ptid, DWORD exit_code, bool main_thread_p)
 		       target_pid_to_str (ptid).c_str (),
 		       (unsigned) exit_code);
 
-  delete_thread (find_thread_ptid (ptid));
+  delete_thread (find_thread_ptid (&the_windows_nat_target, ptid));
 
   for (th = &thread_head;
        th->next != NULL && th->next->id != id;
@@ -1516,9 +1518,10 @@ ctrl_c_handler (DWORD event_type)
 
 /* Get the next event from the child.  Returns a non-zero thread id if the event
    requires handling by WFI (or whatever).  */
-static int
-get_windows_debug_event (struct target_ops *ops,
-			 int pid, struct target_waitstatus *ourstatus)
+
+int
+windows_nat_target::get_windows_debug_event (int pid,
+					     struct target_waitstatus *ourstatus)
 {
   BOOL debug_event;
   DWORD continue_status, event_code;
@@ -1548,8 +1551,7 @@ get_windows_debug_event (struct target_ops *ops,
 		     "CREATE_THREAD_DEBUG_EVENT"));
       if (saw_create != 1)
 	{
-	  struct inferior *inf;
-	  inf = find_inferior_pid (current_event.dwProcessId);
+	  inferior *inf = find_inferior_pid (this, current_event.dwProcessId);
 	  if (!saw_create && inf->attach_flag)
 	    {
 	      /* Kludge around a Windows bug where first event is a create
@@ -1756,7 +1758,7 @@ windows_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
 	     the user tries to resume the execution in the inferior.
 	     This is a classic race that we should try to fix one day.  */
       SetConsoleCtrlHandler (&ctrl_c_handler, TRUE);
-      retval = get_windows_debug_event (this, pid, ourstatus);
+      retval = get_windows_debug_event (pid, ourstatus);
       SetConsoleCtrlHandler (&ctrl_c_handler, FALSE);
 
       if (retval)
-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [PATCH v2 22/24] Add "info connections" command, "info inferiors" connection number/string
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
                   ` (11 preceding siblings ...)
  2019-10-17 22:50 ` [PATCH v2 15/24] Avoid another inferior_ptid reference in gdb/remote.c Pedro Alves
@ 2019-10-17 22:51 ` Pedro Alves
  2019-10-17 22:51 ` [PATCH v2 04/24] exceptions.c:print_flush: Remove obsolete check Pedro Alves
                   ` (14 subsequent siblings)
  27 siblings, 0 replies; 73+ messages in thread
From: Pedro Alves @ 2019-10-17 22:51 UTC (permalink / raw)
  To: gdb-patches

This commit extends the CLI a bit for multi-target, in three ways.

#1 - New "info connections" command.

This is a new command that lists the open connections (process_stratum
targets).  For example, if you're debugging two remote connections, a
couple local/native processes, and a core dump, all at the same time,
you might see something like this:

 (gdb) info connections
   Num  What                     Description
   1    remote 192.168.0.1:9999  Remote serial target in gdb-specific protocol
   2    remote 192.168.0.2:9998  Remote serial target in gdb-specific protocol
 * 3    native                   Native process
   4    core                     Local core dump file

#2 - New "info inferiors" "Connection" column

You'll also see a new matching "Connection" column in "info
inferiors", showing you which connection an inferior is bound to:

 (gdb) info inferiors
   Num  Description       Connection                   Executable
   1    process 18526     1 (remote 192.168.0.1:9999)  target:/tmp/a.out
   2    process 18531     2 (remote 192.168.0.2:9998)  target:/tmp/a.out
   3    process 19115     3 (native)                   /tmp/prog1
   4    process 6286      4 (core)                     myprogram
 * 5    process 19122     3 (native)                   /bin/hello

#3 - Makes "add-inferior" show the inferior's target connection

"add-inferior" now shows you the connection you've just bound the
inferior to, which is the current process_stratum target:

 (gdb) add-inferior
 [New inferior 2]
 Added inferior 2 on connection 1 (extended-remote localhost:2346)

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

	* Makefile.in (COMMON_SFILES): Add target-connection.c.
	* inferior.c (uiout_field_connection): New function.
	(print_inferior): Add new "connection-id" column.
	(add_inferior_command): Show connection number/string of added
	inferior.
	* process-stratum-target.h
	(process_stratum_target::connection_string): New virtual method.
	(process_stratum_target::connection_number): New field.
	* remote.c (remote_target::connection_string): New override.
	* target-connection.c: New file.
	* target-connection.h: New file.
	* target.c (decref_target): Remove process_stratum targets from
	the connection list.
	(target_stack::push): Add process_stratum targets to the
	connection list.

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

	* gdb.base/kill-detach-inferiors-cmd.exp: Adjust expected output
	of "add-inferior".
	* gdb.base/quit-live.exp: Likewise.
	* gdb.base/remote-exec-file.exp: Likewise.
	* gdb.guile/scm-progspace.exp: Likewise.
	* gdb.linespec/linespec.exp: Likewise.
	* gdb.mi/new-ui-mi-sync.exp: Likewise.
	* gdb.mi/user-selected-context-sync.exp: Likewise.
	* gdb.multi/multi-target.exp (setup): Add "info connection" and
	"info inferiors" tests.
	* gdb.multi/remove-inferiors.exp: Adjust expected output of
	"add-inferior".
	* gdb.multi/watchpoint-multi.exp: Likewise.
	* gdb.python/py-inferior.exp: Likewise.
	* gdb.server/extended-remote-restart.exp: Likewise.
	* gdb.threads/fork-plus-threads.exp: Adjust expected output of
	"info inferiors".
	* gdb.threads/forking-threads-plus-breakpoint.exp: Likewise.
	* gdb.trace/report.exp: Likewise.
---
 gdb/Makefile.in                                    |   1 +
 gdb/inferior.c                                     |  56 ++++++-
 gdb/process-stratum-target.h                       |  13 ++
 gdb/remote.c                                       |  13 ++
 gdb/target-connection.c                            | 162 +++++++++++++++++++++
 gdb/target-connection.h                            |  32 ++++
 gdb/target.c                                       |  10 +-
 .../gdb.base/kill-detach-inferiors-cmd.exp         |   4 +-
 gdb/testsuite/gdb.base/quit-live.exp               |   2 +-
 gdb/testsuite/gdb.base/remote-exec-file.exp        |   2 +-
 gdb/testsuite/gdb.guile/scm-progspace.exp          |   2 +-
 gdb/testsuite/gdb.linespec/linespec.exp            |   2 +-
 gdb/testsuite/gdb.mi/new-ui-mi-sync.exp            |   2 +-
 .../gdb.mi/user-selected-context-sync.exp          |   2 +-
 gdb/testsuite/gdb.multi/multi-target.exp           |  28 +++-
 gdb/testsuite/gdb.multi/remove-inferiors.exp       |   2 +-
 gdb/testsuite/gdb.multi/watchpoint-multi.exp       |   2 +-
 gdb/testsuite/gdb.python/py-inferior.exp           |   4 +-
 .../gdb.server/extended-remote-restart.exp         |  18 ++-
 gdb/testsuite/gdb.threads/fork-plus-threads.exp    |   2 +-
 .../forking-threads-plus-breakpoint.exp            |   2 +-
 gdb/testsuite/gdb.trace/report.exp                 |   2 +-
 22 files changed, 335 insertions(+), 28 deletions(-)
 create mode 100644 gdb/target-connection.c
 create mode 100644 gdb/target-connection.h

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 9d07ac0d97..c3e094a115 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -1119,6 +1119,7 @@ COMMON_SFILES = \
 	symmisc.c \
 	symtab.c \
 	target.c \
+	target-connection.c \
 	target-dcache.c \
 	target-descriptions.c \
 	target-memory.c \
diff --git a/gdb/inferior.c b/gdb/inferior.c
index 577d2e194a..061cf5cebb 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -430,6 +430,31 @@ print_selected_inferior (struct ui_out *uiout)
 		  inf->num, inferior_pid_to_str (inf->pid).c_str (), filename);
 }
 
+/* Helper for print_inferior.  Returns the 'connection-id' string for
+   PROC_TARGET.  */
+
+static std::string
+uiout_field_connection (process_stratum_target *proc_target)
+{
+  if (proc_target == NULL)
+    {
+      return {};
+    }
+  else if (proc_target->connection_string () != NULL)
+    {
+      return string_printf ("%d (%s %s)",
+			    proc_target->connection_number,
+			    proc_target->shortname (),
+			    proc_target->connection_string ());
+    }
+  else
+    {
+      return string_printf ("%d (%s)",
+			    proc_target->connection_number,
+			    proc_target->shortname ());
+    }
+}
+
 /* Prints the list of inferiors and their details on UIOUT.  This is a
    version of 'info_inferior_command' suitable for use from MI.
 
@@ -441,6 +466,7 @@ static void
 print_inferior (struct ui_out *uiout, const char *requested_inferiors)
 {
   int inf_count = 0;
+  size_t connection_id_len = 20;
 
   /* Compute number of inferiors we will print.  */
   for (inferior *inf : all_inferiors ())
@@ -448,6 +474,10 @@ print_inferior (struct ui_out *uiout, const char *requested_inferiors)
       if (!number_is_in_list (requested_inferiors, inf->num))
 	continue;
 
+      std::string conn = uiout_field_connection (inf->process_target ());
+      if (connection_id_len < conn.size ())
+	connection_id_len = conn.size ();
+
       ++inf_count;
     }
 
@@ -457,10 +487,12 @@ print_inferior (struct ui_out *uiout, const char *requested_inferiors)
       return;
     }
 
-  ui_out_emit_table table_emitter (uiout, 4, inf_count, "inferiors");
+  ui_out_emit_table table_emitter (uiout, 5, inf_count, "inferiors");
   uiout->table_header (1, ui_left, "current", "");
   uiout->table_header (4, ui_left, "number", "Num");
   uiout->table_header (17, ui_left, "target-id", "Description");
+  uiout->table_header (connection_id_len, ui_left,
+		       "connection-id", "Connection");
   uiout->table_header (17, ui_left, "exec", "Executable");
 
   uiout->table_body ();
@@ -480,6 +512,9 @@ print_inferior (struct ui_out *uiout, const char *requested_inferiors)
 
       uiout->field_string ("target-id", inferior_pid_to_str (inf->pid));
 
+      std::string conn = uiout_field_connection (inf->process_target ());
+      uiout->field_string ("connection-id", conn.c_str ());
+
       if (inf->pspace->pspace_exec_filename != NULL)
 	uiout->field_string ("exec", inf->pspace->pspace_exec_filename);
       else
@@ -714,9 +749,22 @@ switch_to_inferior_and_push_target (inferior *new_inf,
 
   /* Reuse the target for new inferior.  */
   if (!no_connection && proc_target != NULL)
-    push_target (proc_target);
-
-  printf_filtered (_("Added inferior %d\n"), new_inf->num);
+    {
+      push_target (proc_target);
+      if (proc_target->connection_string () != NULL)
+	printf_filtered (_("Added inferior %d on connection %d (%s %s)\n"),
+			 new_inf->num,
+			 proc_target->connection_number,
+			 proc_target->shortname (),
+			 proc_target->connection_string ());
+      else
+	printf_filtered (_("Added inferior %d on connection %d (%s)\n"),
+			 new_inf->num,
+			 proc_target->connection_number,
+			 proc_target->shortname ());
+    }
+  else
+    printf_filtered (_("Added inferior %d\n"), new_inf->num);
 }
 
 /* add-inferior [-copies N] [-exec FILENAME] [-no-connection] */
diff --git a/gdb/process-stratum-target.h b/gdb/process-stratum-target.h
index 6081c0a927..53e1c6a618 100644
--- a/gdb/process-stratum-target.h
+++ b/gdb/process-stratum-target.h
@@ -31,6 +31,16 @@ public:
 
   strata stratum () const final override { return process_stratum; }
 
+  /* Return a string representation of this target's open connection.
+     This string is used to distinguish different instances of a given
+     target type.  For example, when remote debugging, the target is
+     called "remote", but since we may have more than one remote
+     target open, connection_string() returns the connection serial
+     connection name, e.g., "localhost:10001", "192.168.0.1:20000",
+     etc.  This string is shown in several places, e.g., in "info
+     connections" and "info inferiors".  */
+  virtual const char *connection_string () { return nullptr; }
+
   /* We must default these because they must be implemented by any
      target that can run.  */
   bool can_async_p () override { return false; }
@@ -58,6 +68,9 @@ public:
      stop events for all known threads, because any of those threads
      may have spawned new threads we haven't heard of yet.  */
   bool threads_executing = false;
+
+  /* The connection number.  Visible in "info connections".  */
+  int connection_number = 0;
 };
 
 /* Downcast TARGET to process_stratum_target.  */
diff --git a/gdb/remote.c b/gdb/remote.c
index 773c027bf0..28cf56fbad 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -405,6 +405,8 @@ public:
   const target_info &info () const override
   { return remote_target_info; }
 
+  const char *connection_string () override;
+
   thread_control_capabilities get_thread_control_capabilities () override
   { return tc_schedlock; }
 
@@ -4859,6 +4861,17 @@ remote_target::start_remote (int from_tty, int extended_p)
     insert_breakpoints ();
 }
 
+const char *
+remote_target::connection_string ()
+{
+  remote_state *rs = get_remote_state ();
+
+  if (rs->remote_desc->name != NULL)
+    return rs->remote_desc->name;
+  else
+    return NULL;
+}
+
 /* Open a connection to a remote debugger.
    NAME is the filename used for communication.  */
 
diff --git a/gdb/target-connection.c b/gdb/target-connection.c
new file mode 100644
index 0000000000..a079f218bd
--- /dev/null
+++ b/gdb/target-connection.c
@@ -0,0 +1,162 @@
+/* List of target connections for GDB.
+
+   Copyright (C) 2017-2019 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include "defs.h"
+#include "target-connection.h"
+
+#include <map>
+
+#include "inferior.h"
+#include "target.h"
+
+/* A map between connection number and representative process_stratum
+   target.  */
+static std::map<int, process_stratum_target *> process_targets;
+
+/* The highest connection number ever given to a target.  */
+static int highest_target_connection_num;
+
+/* See target-connection.h.  */
+
+void
+connection_list_add (process_stratum_target *t)
+{
+  if (t->connection_number == 0)
+    {
+      t->connection_number = ++highest_target_connection_num;
+      process_targets[t->connection_number] = t;
+    }
+}
+
+/* See target-connection.h.  */
+
+void
+connection_list_remove (process_stratum_target *t)
+{
+  process_targets.erase (t->connection_number);
+  t->connection_number = 0;
+}
+
+/* Make a target connection string for T.  This is usually T's
+   shortname, but it includes the result of
+   process_stratum_target::connection_string() too if T supports
+   it.  */
+
+static std::string
+make_target_connection_string (process_stratum_target *t)
+{
+  if (t->connection_string () != NULL)
+    return string_printf ("%s %s", t->shortname (),
+			  t->connection_string ());
+  else
+    return t->shortname ();
+}
+
+/* Prints the list of target connections and their details on UIOUT.
+
+   If REQUESTED_CONNECTIONS is not NULL, it's a list of GDB ids of the
+   target connections that should be printed.  Otherwise, all target
+   connections are printed.  */
+
+static void
+print_connection (struct ui_out *uiout, const char *requested_connections)
+{
+  int count = 0;
+  size_t what_len = 0;
+
+  /* Compute number of lines we will print.  */
+  for (const auto &it : process_targets)
+    {
+      if (!number_is_in_list (requested_connections, it.first))
+	continue;
+
+      ++count;
+
+      process_stratum_target *t = it.second;
+
+      size_t l = strlen (t->shortname ());
+      if (t->connection_string () != NULL)
+	l += 1 + strlen (t->connection_string ());
+
+      if (l > what_len)
+	what_len = l;
+    }
+
+  if (count == 0)
+    {
+      uiout->message (_("No connections.\n"));
+      return;
+    }
+
+  ui_out_emit_table table_emitter (uiout, 4, process_targets.size (),
+				   "connections");
+
+  uiout->table_header (1, ui_left, "current", "");
+  uiout->table_header (4, ui_left, "number", "Num");
+  /* The text in the "what" column may include spaces.  Add one extra
+     space to visually separate the What and Description columns a
+     little better.  Compare:
+      "* 1    remote :9999 Remote serial target in gdb-specific protocol"
+      "* 1    remote :9999  Remote serial target in gdb-specific protocol"
+  */
+  uiout->table_header (what_len + 1, ui_left, "what", "What");
+  uiout->table_header (17, ui_left, "description", "Description");
+
+  uiout->table_body ();
+
+  for (const auto &it : process_targets)
+    {
+      process_stratum_target *t = it.second;
+
+      if (!number_is_in_list (requested_connections, t->connection_number))
+	continue;
+
+      ui_out_emit_tuple tuple_emitter (uiout, NULL);
+
+      if (current_inferior ()->process_target () == t)
+	uiout->field_string ("current", "*");
+      else
+	uiout->field_skip ("current");
+
+      uiout->field_signed ("number", t->connection_number);
+
+      uiout->field_string ("what", make_target_connection_string (t).c_str ());
+
+      uiout->field_string ("description", t->longname ());
+
+      uiout->text ("\n");
+    }
+}
+
+/* The "info connections" command.  */
+
+static void
+info_connections_command (const char *args, int from_tty)
+{
+  print_connection (current_uiout, args);
+}
+
+void
+_initialize_target_connection ()
+{
+  add_info ("connections", info_connections_command,
+	    _("\
+Target connections in use.\n\
+Shows the list of target connections currently in use."));
+}
diff --git a/gdb/target-connection.h b/gdb/target-connection.h
new file mode 100644
index 0000000000..0e2dc128d8
--- /dev/null
+++ b/gdb/target-connection.h
@@ -0,0 +1,32 @@
+/* List of target connections for GDB.
+
+   Copyright (C) 2017-2019 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef TARGET_CONNECTION_H
+#define TARGET_CONNECTION_H
+
+struct process_stratum_target;
+
+/* Add a process target to the connection list, if not already
+   added.  */
+void connection_list_add (process_stratum_target *t);
+
+/* Remove a process target from the connection list.  */
+void connection_list_remove (process_stratum_target *t);
+
+#endif /* TARGET_CONNECTION_H */
diff --git a/gdb/target.c b/gdb/target.c
index c62a45b2f7..97a207c85c 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -49,6 +49,7 @@
 #include "gdbsupport/byte-vector.h"
 #include "terminal.h"
 #include <unordered_map>
+#include "target-connection.h"
 
 static void generic_tls_error (void) ATTRIBUTE_NORETURN;
 
@@ -561,7 +562,11 @@ decref_target (target_ops *t)
 {
   t->decref ();
   if (t->refcount () == 0)
-    target_close (t);
+    {
+      if (t->stratum () == process_stratum)
+	connection_list_remove (as_process_stratum_target (t));
+      target_close (t);
+    }
 }
 
 /* See target.h.  */
@@ -573,6 +578,9 @@ target_stack::push (target_ops *t)
 
   strata stratum = t->stratum ();
 
+  if (stratum == process_stratum)
+    connection_list_add (as_process_stratum_target (t));
+
   /* If there's already a target at this stratum, remove it.  */
 
   if (m_stack[stratum] != NULL)
diff --git a/gdb/testsuite/gdb.base/kill-detach-inferiors-cmd.exp b/gdb/testsuite/gdb.base/kill-detach-inferiors-cmd.exp
index 63045cb504..e04a6ee4c4 100644
--- a/gdb/testsuite/gdb.base/kill-detach-inferiors-cmd.exp
+++ b/gdb/testsuite/gdb.base/kill-detach-inferiors-cmd.exp
@@ -32,7 +32,7 @@ if [prepare_for_testing "failed to prepare" $executable] {
 runto_main
 
 # Add another forked inferior process.
-gdb_test "add-inferior" "Added inferior 2" "add inferior 2"
+gdb_test "add-inferior" "Added inferior 2 on connection .*" "add inferior 2"
 gdb_test "inferior 2" "Switching to inferior 2.*"
 gdb_test "file $binfile" "Reading symbols from .*" "load binary"
 gdb_test "start" "Temporary breakpoint.*Starting program.*"
@@ -40,7 +40,7 @@ gdb_test "start" "Temporary breakpoint.*Starting program.*"
 # Add an attached inferior process.
 set test_spawn_id [spawn_wait_for_attach $binfile]
 set test_pid [spawn_id_get_pid $test_spawn_id]
-gdb_test "add-inferior" "Added inferior 3" "add inferior 3"
+gdb_test "add-inferior" "Added inferior 3 on connection .*" "add inferior 3"
 gdb_test "inferior 3" "Switching to inferior 3.*"
 gdb_test "attach $test_pid" "Attaching to process.*" "attach to pid"
 
diff --git a/gdb/testsuite/gdb.base/quit-live.exp b/gdb/testsuite/gdb.base/quit-live.exp
index c0eba44d43..7b23a9c013 100644
--- a/gdb/testsuite/gdb.base/quit-live.exp
+++ b/gdb/testsuite/gdb.base/quit-live.exp
@@ -120,7 +120,7 @@ proc quit_with_live_inferior {appear_how extra_inferior quit_how} {
     }
 
     if {$extra_inferior} {
-	gdb_test "add-inferior" "Added inferior 2*" \
+	gdb_test "add-inferior" "Added inferior 2 on connection .*" \
 	    "add empty inferior 2"
 	gdb_test "inferior 2" "Switching to inferior 2.*" \
 	    "switch to inferior 2"
diff --git a/gdb/testsuite/gdb.base/remote-exec-file.exp b/gdb/testsuite/gdb.base/remote-exec-file.exp
index 3db009b830..6af8afcc5a 100644
--- a/gdb/testsuite/gdb.base/remote-exec-file.exp
+++ b/gdb/testsuite/gdb.base/remote-exec-file.exp
@@ -27,7 +27,7 @@ with_test_prefix "set inf 1" {
 
 # Set remote exec-file in inferior 2.
 with_test_prefix "set inf 2" {
-    gdb_test "add-inferior" "Added inferior 2" "add inferior 2"
+    gdb_test "add-inferior" "Added inferior 2.*" "add inferior 2"
     gdb_test "inferior 2" "Switching to inferior 2.*"
     gdb_test_no_output "set remote exec-file prog2"
 }
diff --git a/gdb/testsuite/gdb.guile/scm-progspace.exp b/gdb/testsuite/gdb.guile/scm-progspace.exp
index cb8384f83d..19520943c0 100644
--- a/gdb/testsuite/gdb.guile/scm-progspace.exp
+++ b/gdb/testsuite/gdb.guile/scm-progspace.exp
@@ -73,7 +73,7 @@ with_test_prefix "program unloaded" {
 # deleted.  We need to, for example, delete an inferior to get the progspace
 # to go away.
 
-gdb_test "add-inferior" "Added inferior 2" "create new inferior"
+gdb_test "add-inferior" "Added inferior 2.*" "create new inferior"
 gdb_test "inferior 2" ".*" "switch to new inferior"
 gdb_test_no_output "remove-inferiors 1" "remove first inferior"
 
diff --git a/gdb/testsuite/gdb.linespec/linespec.exp b/gdb/testsuite/gdb.linespec/linespec.exp
index 45982ddeff..08be8f606f 100644
--- a/gdb/testsuite/gdb.linespec/linespec.exp
+++ b/gdb/testsuite/gdb.linespec/linespec.exp
@@ -201,7 +201,7 @@ gdb_test "break lspec.h:$line" \
 # Multi-inferior tests.
 #
 
-gdb_test "add-inferior" "Added inferior 2" \
+gdb_test "add-inferior" "Added inferior 2.*" \
     "add inferior for linespec tests"
 
 gdb_test "inferior 2" "Switching to inferior 2 .*" \
diff --git a/gdb/testsuite/gdb.mi/new-ui-mi-sync.exp b/gdb/testsuite/gdb.mi/new-ui-mi-sync.exp
index 5560a8be96..7eb4fb3716 100644
--- a/gdb/testsuite/gdb.mi/new-ui-mi-sync.exp
+++ b/gdb/testsuite/gdb.mi/new-ui-mi-sync.exp
@@ -83,7 +83,7 @@ proc do_test {sync_command} {
     # in the separate MI UI.  Note the "run" variant usually triggers
     # =thread-group-started/=thread-created/=library-loaded as well.
     with_spawn_id $gdb_main_spawn_id {
-	gdb_test "add-inferior" "Added inferior 2"
+	gdb_test "add-inferior" "Added inferior 2 on connection .*"
     }
 
     # Interrupt the program.
diff --git a/gdb/testsuite/gdb.mi/user-selected-context-sync.exp b/gdb/testsuite/gdb.mi/user-selected-context-sync.exp
index 621b4c5163..19dd4c9493 100644
--- a/gdb/testsuite/gdb.mi/user-selected-context-sync.exp
+++ b/gdb/testsuite/gdb.mi/user-selected-context-sync.exp
@@ -415,7 +415,7 @@ proc_with_prefix test_setup { mode } {
 	# Add the second inferior now.  While this is not mandatory, it allows
 	# us to assume that per-inferior thread numbering will be used,
 	# simplifying test_continue_to_start a bit (Thread 1.2 and not Thread 2).
-	gdb_test "add-inferior" "Added inferior 2" "add inferior 2"
+	gdb_test "add-inferior" "Added inferior 2 on connection .*" "add inferior 2"
 
 	# Prepare the first inferior for the test.
 	test_continue_to_start $mode 1
diff --git a/gdb/testsuite/gdb.multi/multi-target.exp b/gdb/testsuite/gdb.multi/multi-target.exp
index 3b71e7446b..be466c110b 100644
--- a/gdb/testsuite/gdb.multi/multi-target.exp
+++ b/gdb/testsuite/gdb.multi/multi-target.exp
@@ -137,8 +137,34 @@ proc setup {non-stop} {
 	return 0
     }
 
+    set ws "\[ \t\]+"
+    global decimal
+
+    # Test "info connections" and "info inferior"'s "Connection"
+    # column, while at it.
+
+    gdb_test "info connections" \
+	[multi_line \
+	     "Num${ws}What${ws}Description${ws}" \
+	     "  1${ws}native${ws}Native process${ws}" \
+	     "  2${ws}extended-remote localhost:$decimal${ws}Extended remote serial target in gdb-specific protocol${ws}" \
+	     "  3${ws}core${ws}Local core dump file${ws}" \
+	     "  4${ws}extended-remote localhost:$decimal${ws}Extended remote serial target in gdb-specific protocol${ws}" \
+	   "\\* 5${ws}core${ws}Local core dump file${ws}" \
+	    ]
+
+    gdb_test "info inferiors" \
+	[multi_line \
+	     "Num${ws}Description${ws}Connection${ws}Executable${ws}" \
+	     "  1${ws}process ${decimal}${ws}1 \\(native\\)${ws}${binfile}${ws}" \
+	     "  2${ws}process ${decimal}${ws}2 \\(extended-remote localhost:$decimal\\)${ws}${binfile}${ws}" \
+	     "  3${ws}process ${decimal}${ws}3 \\(core\\)${ws}${binfile}${ws}" \
+	     "  4${ws}process ${decimal}${ws}1 \\(native\\)${ws}${binfile}${ws}" \
+	     "  5${ws}process ${decimal}${ws}4 \\(extended-remote localhost:$decimal\\)${ws}${binfile}${ws}" \
+	   "\\* 6${ws}process ${decimal}${ws}5 \\(core\\)${ws}${binfile}${ws}" \
+	    ]
+
     # For debugging.
-    gdb_test "info inferiors" ".*"
     gdb_test "info threads" ".*"
 
     # Make "continue" resume all inferiors.
diff --git a/gdb/testsuite/gdb.multi/remove-inferiors.exp b/gdb/testsuite/gdb.multi/remove-inferiors.exp
index 18f6c90225..ef3def7b5b 100644
--- a/gdb/testsuite/gdb.multi/remove-inferiors.exp
+++ b/gdb/testsuite/gdb.multi/remove-inferiors.exp
@@ -26,7 +26,7 @@ proc switch_to_inferior { num message } {
 }
 
 proc add_inferior { expected_num message } {
-    gdb_test "add-inferior" "Added inferior ${expected_num}" "${message}"
+    gdb_test "add-inferior" "Added inferior ${expected_num}( on connection .*)?" "${message}"
 }
 
 proc test_remove_inferiors { } {
diff --git a/gdb/testsuite/gdb.multi/watchpoint-multi.exp b/gdb/testsuite/gdb.multi/watchpoint-multi.exp
index b0f47db374..f3f9ca58fc 100644
--- a/gdb/testsuite/gdb.multi/watchpoint-multi.exp
+++ b/gdb/testsuite/gdb.multi/watchpoint-multi.exp
@@ -53,7 +53,7 @@ if [support_displaced_stepping] {
 gdb_breakpoint main {temporary}
 gdb_test "run" "Temporary breakpoint.* main .*" "start to main inferior 1"
 
-gdb_test "add-inferior" "Added inferior 2" "add inferior 2"
+gdb_test "add-inferior" "Added inferior 2 on connection .*" "add inferior 2"
 gdb_test "inferior 2" "witching to inferior 2 .*" "switch to inferior 2, first time"
 gdb_load $binfile
 
diff --git a/gdb/testsuite/gdb.python/py-inferior.exp b/gdb/testsuite/gdb.python/py-inferior.exp
index 72ec1f2186..a4839c83d6 100644
--- a/gdb/testsuite/gdb.python/py-inferior.exp
+++ b/gdb/testsuite/gdb.python/py-inferior.exp
@@ -279,7 +279,7 @@ with_test_prefix "selected_inferior" {
     gdb_test "inferior 1" ".*" "switch to first inferior"
     gdb_test "py print (gdb.selected_inferior().num)" "1" "first inferior selected"
 
-    gdb_test "add-inferior" "Added inferior 3" "create new inferior"
+    gdb_test "add-inferior" "Added inferior 3 on connection .*" "create new inferior"
     gdb_test "inferior 3" ".*" "switch to third inferior"
     gdb_test "py print (gdb.selected_inferior().num)" "3" "third inferior selected"
     gdb_test "inferior 1" ".*" "switch back to first inferior"
@@ -288,7 +288,7 @@ with_test_prefix "selected_inferior" {
 
 # Test repr()/str()
 with_test_prefix "__repr__" {
-    gdb_test "add-inferior" "Added inferior 4" "add inferior 4"
+    gdb_test "add-inferior" "Added inferior 4 on connection .*" "add inferior 4"
     gdb_py_test_silent_cmd "python infs = gdb.inferiors()" "get inferior list" 1
     gdb_test "python print (infs\[0\])" "<gdb.Inferior num=1, pid=$decimal>"
     gdb_test "python print (infs)" \
diff --git a/gdb/testsuite/gdb.server/extended-remote-restart.exp b/gdb/testsuite/gdb.server/extended-remote-restart.exp
index c78342c010..c1010a9358 100644
--- a/gdb/testsuite/gdb.server/extended-remote-restart.exp
+++ b/gdb/testsuite/gdb.server/extended-remote-restart.exp
@@ -88,12 +88,16 @@ proc test_reload { do_kill_p follow_child_p } {
     gdb_breakpoint "breakpt"
     gdb_continue_to_breakpoint "breakpt"
 
-    # Check we have the expected inferiors.
+    set ws "\[ \t\]+"
+    set any_re "\[^\r\n\]+"
+    set connection_re $any_re
+    set executable_re $any_re
+
     gdb_test "info inferiors" \
 	[multi_line \
-	     "  Num  Description       Executable.*" \
-	     "${parent_prefix} 1 +${live_inf_ptn} \[^\r\n\]+" \
-	     "${child_prefix} 2 +${live_inf_ptn} \[^\r\n\]+" ] \
+	     "  Num${ws}Description${ws}Connection${ws}Executable${ws}" \
+	     "${parent_prefix} 1${ws}${live_inf_ptn}${ws}${connection_re}${executable_re}" \
+	     "${child_prefix} 2${ws}${live_inf_ptn}${ws}${connection_re}${executable_re}" ] \
 	"Check inferiors at breakpoint"
 
     if { $do_kill_p } {
@@ -107,9 +111,9 @@ proc test_reload { do_kill_p follow_child_p } {
 	# Check the first inferior really did die.
 	gdb_test "info inferiors" \
 	    [multi_line \
-		 "  Num  Description       Executable.*" \
-		 "${parent_prefix} 1 +${parent_inf_after_kill_ptn} \[^\r\n\]+" \
-		 "${child_prefix} 2 +${child_inf_after_kill_ptn} \[^\r\n\]+" ] \
+		 "  Num${ws}Description${ws}Connection${ws}Executable${ws}" \
+		 "${parent_prefix} 1${ws}${parent_inf_after_kill_ptn}${ws}${connection_re}${executable_re}" \
+		 "${child_prefix} 2${ws}${child_inf_after_kill_ptn}${ws}${connection_re}${executable_re}" ] \
 	    "Check inferior was killed"
     }
 
diff --git a/gdb/testsuite/gdb.threads/fork-plus-threads.exp b/gdb/testsuite/gdb.threads/fork-plus-threads.exp
index 340a8df266..d488bb9d39 100644
--- a/gdb/testsuite/gdb.threads/fork-plus-threads.exp
+++ b/gdb/testsuite/gdb.threads/fork-plus-threads.exp
@@ -109,7 +109,7 @@ proc do_test { detach_on_fork } {
 	"no threads left"
 
     gdb_test "info inferiors" \
-	"Num\[ \t\]+Description\[ \t\]+Executable\[ \t\]+\r\n\\* 1 \[^\r\n\]+" \
+	"Num\[ \t\]+Description\[ \t\]+Connection\[ \t\]+Executable\[ \t\]+\r\n\\* 1 \[^\r\n\]+" \
 	"only inferior 1 left"
 }
 
diff --git a/gdb/testsuite/gdb.threads/forking-threads-plus-breakpoint.exp b/gdb/testsuite/gdb.threads/forking-threads-plus-breakpoint.exp
index 1d1378234e..6d24af401b 100644
--- a/gdb/testsuite/gdb.threads/forking-threads-plus-breakpoint.exp
+++ b/gdb/testsuite/gdb.threads/forking-threads-plus-breakpoint.exp
@@ -142,7 +142,7 @@ proc do_test { cond_bp_target detach_on_fork displaced } {
 	"no threads left"
 
     gdb_test "info inferiors" \
-	"Num\[ \t\]+Description\[ \t\]+Executable\[ \t\]+\r\n\\* 1 \[^\r\n\]+" \
+	"Num\[ \t\]+Description\[ \t\]+Connection\[ \t\]+Executable\[ \t\]+\r\n\\* 1 \[^\r\n\]+" \
 	"only inferior 1 left"
 }
 
diff --git a/gdb/testsuite/gdb.trace/report.exp b/gdb/testsuite/gdb.trace/report.exp
index c847ab0c5b..2949f1bb0d 100644
--- a/gdb/testsuite/gdb.trace/report.exp
+++ b/gdb/testsuite/gdb.trace/report.exp
@@ -391,7 +391,7 @@ proc use_collected_data { data_source } {
 	# There is always a thread of an inferior, either a live one or
 	# a faked one.
 	gdb_test "info threads" "\\* ${decimal}    (process|Thread) \[0-9\.\]+\[ \t\].*"
-	gdb_test "info inferiors" "\\* 1    process ${decimal} \[ \t\]+${binfile}.*"
+	gdb_test "info inferiors" "\\* 1    process ${decimal} \[ \t\]+\[^\r\n\]*\[ \t\]+${binfile}.*"
     }
 }
 
-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [PATCH v2 19/24] Add multi-target tests
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
                   ` (14 preceding siblings ...)
  2019-10-17 22:51 ` [PATCH v2 18/24] Multi-target support Pedro Alves
@ 2019-10-17 22:51 ` Pedro Alves
  2019-10-17 22:57 ` [PATCH v2 05/24] Make target_ops::has_execution take an 'inferior *' instead of a ptid_t Pedro Alves
                   ` (11 subsequent siblings)
  27 siblings, 0 replies; 73+ messages in thread
From: Pedro Alves @ 2019-10-17 22:51 UTC (permalink / raw)
  To: gdb-patches

This adds a testcase exercising multi-target features.  It spawns 6
inferiors, like this:

 inferior 1 -> native
 inferior 2 -> extended-remote 1
 inferior 3 -> core
 inferior 4 -> native
 inferior 5 -> extended-remote 2
 inferior 6 -> core

and then tests various details, including:

 - running to breakpoints

 - interrupting with Ctrl-C and "interrupt -a"

 - "next" bouncing between two breakpoints in two threads running in
   different targets.

 - since we have cores and live inferiors mixed in the same session,
   this makes sure that gdb doesn't try to remove a core dump's
   threads.

 - all-stop and non-stop modes.

This testcase caught a _lot_ of bugs in development.

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

	* gdb.multi/multi-target.c: New file.
	* gdb.multi/multi-target.exp: New file.
	* lib/gdbserver-support.exp (gdb_target_cmd): Handle "Non-stop
	mode requested, but remote does not support non-stop".
---
 gdb/testsuite/gdb.multi/multi-target.c   | 100 +++++++++
 gdb/testsuite/gdb.multi/multi-target.exp | 361 +++++++++++++++++++++++++++++++
 gdb/testsuite/lib/gdbserver-support.exp  |   4 +
 3 files changed, 465 insertions(+)
 create mode 100644 gdb/testsuite/gdb.multi/multi-target.c
 create mode 100644 gdb/testsuite/gdb.multi/multi-target.exp

diff --git a/gdb/testsuite/gdb.multi/multi-target.c b/gdb/testsuite/gdb.multi/multi-target.c
new file mode 100644
index 0000000000..856226e6b9
--- /dev/null
+++ b/gdb/testsuite/gdb.multi/multi-target.c
@@ -0,0 +1,100 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2017-2019 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <limits.h>
+#include <string.h>
+#include <pthread.h>
+
+#define NUM_THREADS 1
+
+static pthread_barrier_t barrier;
+
+static void *
+thread_start (void *arg)
+{
+  pthread_barrier_wait (&barrier);
+
+  while (1)
+    sleep (1);
+  return NULL;
+}
+
+static void
+all_started (void)
+{
+}
+
+int wait_for_gdb;
+
+static void
+function1 (void)
+{
+  while (wait_for_gdb)
+    sleep (1);
+}
+
+static void
+function2 (void)
+{
+  while (wait_for_gdb)
+    sleep (1);
+}
+
+static void
+function3 (void)
+{
+}
+
+static void
+function4 (void)
+{
+}
+
+static void
+function5 (void)
+{
+}
+
+int
+main (int argc, char ** argv)
+{
+  pthread_t thread;
+  int len;
+
+  alarm (360);
+
+  pthread_barrier_init (&barrier, NULL, NUM_THREADS + 1);
+  pthread_create (&thread, NULL, thread_start, NULL);
+
+  pthread_barrier_wait (&barrier);
+  all_started ();
+
+  while (1)
+    {
+      function1 (); /* set break 1 here */
+      function2 (); /* set break 2 here */
+      function3 ();
+      function4 ();
+      function5 ();
+      sleep (1);
+    }
+
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.multi/multi-target.exp b/gdb/testsuite/gdb.multi/multi-target.exp
new file mode 100644
index 0000000000..3b71e7446b
--- /dev/null
+++ b/gdb/testsuite/gdb.multi/multi-target.exp
@@ -0,0 +1,361 @@
+# Copyright 2017-2019 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test multi-target features.
+
+load_lib gdbserver-support.exp
+
+standard_testfile
+
+# The plain remote target can't do multiple inferiors.
+if {[target_info gdb_protocol] != ""} {
+    return
+}
+
+if { [prepare_for_testing "failed to prepare" ${binfile} "${srcfile}" \
+	  {debug pthreads}] } {
+    return
+}
+
+proc connect_target_extended_remote {binfile} {
+    set res [gdbserver_start "--multi" ""]
+    set gdbserver_gdbport [lindex $res 1]
+    return [gdb_target_cmd "extended-remote" $gdbserver_gdbport]
+}
+
+# Add and start inferior number NUM.  Returns true on success, false
+# otherwise.
+proc add_inferior {num target binfile {gcorefile ""}} {
+    # Start another inferior.
+    gdb_test "add-inferior -no-connection" "Added inferior $num" \
+	"add empty inferior $num"
+    gdb_test "inferior $num" "Switching to inferior $num.*" \
+	"switch to inferior $num"
+    gdb_test "file ${binfile}" ".*" "load file in inferior $num"
+    gdb_test_no_output "set remote exec-file ${binfile}" \
+	"set remote-exec file in inferior $num"
+
+    if {$target == "core"} {
+	gdb_test "core $gcorefile" "Core was generated by.*" \
+	    "core [file tail $gcorefile]"
+	return 1
+    }
+
+    if {$target == "extended-remote"} {
+	if {[connect_target_extended_remote $binfile]} {
+	    return 0
+	}
+    }
+    if ![runto "all_started"] then {
+	return 0
+    }
+    delete_breakpoints
+
+    return 1
+}
+
+proc prepare_core {} {
+    global gcorefile gcore_created
+    global binfile
+
+    clean_restart ${binfile}
+
+    if ![runto all_started] then {
+	return -1
+    }
+
+    global testfile
+    set gcorefile [standard_output_file $testfile.gcore]
+    set gcore_created [gdb_gcore_cmd $gcorefile "save a core file"]
+}
+
+proc next_live_inferior {inf} {
+    incr inf
+    if {$inf == 3} {
+	# 3 is a core.
+	return 4
+    }
+    if {$inf > 5} {
+	# 6 is a core.
+	return 1
+    }
+
+    return $inf
+}
+
+# Return true on success, false otherwise.
+
+proc setup {non-stop} {
+    global gcorefile gcore_created
+    global binfile
+
+    clean_restart ${binfile}
+
+    # multi-target depends on target running in non-stop mode.  Force
+    # it on for remote targets, until this is the default.
+    gdb_test_no_output "maint set target-non-stop on"
+
+    gdb_test_no_output "set non-stop ${non-stop}"
+
+    if ![runto all_started] then {
+	return 0
+    }
+
+    delete_breakpoints
+
+    # inferior 1 -> native
+    # inferior 2 -> extended-remote
+    # inferior 3 -> core
+    # inferior 4 -> native
+    # inferior 5 -> extended-remote
+    # inferior 6 -> core
+    if {![add_inferior 2 "extended-remote" $binfile]} {
+	return 0
+    }
+    if {![add_inferior 3 "core" $binfile $gcorefile]} {
+	return 0
+    }
+    if {![add_inferior 4 "native" $binfile]} {
+	return 0
+    }
+    if {![add_inferior 5 "extended-remote" $binfile]} {
+	return 0
+    }
+    if {![add_inferior 6 "core" $binfile $gcorefile]} {
+	return 0
+    }
+
+    # For debugging.
+    gdb_test "info inferiors" ".*"
+    gdb_test "info threads" ".*"
+
+    # Make "continue" resume all inferiors.
+    if {${non-stop} == "off"} {
+	gdb_test_no_output "set schedule-multiple on"
+    }
+
+    return 1
+}
+
+# Test "continue" to breakpoints in different targets.  In non-stop
+# mode, also tests "interrupt -a".
+proc test_continue {non-stop} {
+    if {![setup ${non-stop}]} {
+	untested "setup failed"
+	return
+    }
+
+    proc set_break {inf} {
+	gdb_test "break function${inf} thread ${inf}.1" \
+	"Breakpoint .* function${inf}\\..*"
+    }
+
+    # Select inferior INF, and then run to a breakpoint on inferior
+    # INF+1.
+    proc test_continue_inf {inf} {
+	upvar 1 non-stop non-stop
+
+	global gdb_prompt
+	delete_breakpoints
+
+	set next_inf [next_live_inferior $inf]
+
+	gdb_test "inferior $inf" "Switching to inferior $inf.*"
+	set_break $next_inf
+
+	if {${non-stop} == "off"} {
+	    gdb_test "continue" "hit Breakpoint .* function${next_inf}.*"
+	} else {
+	    set msg "continue"
+	    gdb_test_multiple "continue -a&" $msg {
+		-re "Continuing.*$gdb_prompt " {
+		    pass $msg
+		}
+	    }
+
+	    set msg "hit bp"
+	    gdb_test_multiple "" $msg {
+		-re "hit Breakpoint .* function${next_inf}" {
+		    pass $msg
+		}
+	    }
+
+	    set msg "stop all threads"
+	    gdb_test_multiple "interrupt -a" $msg {
+		-re "$gdb_prompt " {
+		    for {set i 0} {$i < 7} {incr i} {
+			set ok 0
+			gdb_test_multiple "" $msg {
+			    -re "Thread\[^\r\n\]*stopped\\." {
+				set ok 1
+			    }
+			}
+			if {!$ok} {
+			    break
+			}
+		    }
+		    gdb_assert $ok $msg
+		}
+	    }
+	}
+    }
+
+    for {set i 1} {$i <= 5} {incr i} {
+	if {$i == 3} {
+	    # This is a core inferior.
+	    continue
+	}
+
+	with_test_prefix "inf$i" {
+	    test_continue_inf $i
+	}
+    }
+}
+
+# Test interrupting multiple targets with Ctrl-C.
+
+proc test_ctrlc {} {
+    if {![setup "off"]} {
+	untested "setup failed"
+	return
+    }
+
+    delete_breakpoints
+
+    # Select inferior INF, continue all inferiors, and then Ctrl-C.
+    proc test_ctrlc_inf {inf} {
+	global gdb_prompt
+
+	gdb_test "inferior $inf" "Switching to inferior $inf.*"
+
+	set msg "continue"
+	gdb_test_multiple "continue" $msg {
+	    -re "Continuing" {
+		pass $msg
+	    }
+	}
+
+	after 200 { send_gdb "\003" }
+
+	set msg "send_gdb control C"
+	gdb_test_multiple "" $msg {
+	    -re "received signal SIGINT.*$gdb_prompt $" {
+		pass $msg
+	    }
+	}
+
+	set msg "all threads stopped"
+	gdb_test_multiple "info threads" "$msg" {
+	    -re "\\\(running\\\).*$gdb_prompt $" {
+		fail $msg
+	    }
+	    -re "$gdb_prompt $" {
+		pass $msg
+	    }
+	}
+    }
+
+    for {set i 1} {$i <= 5} {incr i} {
+	if {$i == 3} {
+	    # This is a core inferior.
+	    continue
+	}
+
+	with_test_prefix "inf$i" {
+	    test_ctrlc_inf $i
+	}
+    }
+}
+
+# Test "next" bouncing between two breakpoints in two threads running
+# in different targets.
+proc test_ping_pong_next {} {
+    global srcfile
+
+    if {![setup "off"]} {
+	untested "setup failed"
+	return
+    }
+
+    # block/unblock inferiors 1 and 2 according to INF1 and INF2.
+    proc block {inf1 inf2} {
+	gdb_test "thread apply 1.1 p wait_for_gdb = $inf1" " = $inf1"
+	gdb_test "thread apply 2.1 p wait_for_gdb = $inf2" " = $inf2"
+    }
+
+    # We're use inferiors 1 and 2.  Make sure they're really connected
+    # to different targets.
+    gdb_test "thread apply 1.1 maint print target-stack" \
+	"- native.*"
+    gdb_test "thread apply 2.1 maint print target-stack" \
+	"- extended-remote.*"
+
+    # Set two breakpoints, one for each of inferior 1 and 2.  Inferior
+    # 1 is running on the native target, and inferior 2 is running on
+    # extended-gdbserver.  Run to breakpoint 1 to gets things started.
+    set line1 [gdb_get_line_number "set break 1 here"]
+    set line2 [gdb_get_line_number "set break 2 here"]
+
+    gdb_test "thread 1.1" "Switching to thread 1.1 .*"
+
+    gdb_test "break $srcfile:$line1 thread 1.1" \
+	"Breakpoint .*$srcfile:$line1\\..*"
+
+    gdb_test "continue" "hit Breakpoint .*"
+
+    gdb_test "break $srcfile:$line2 thread 2.1" \
+	"Breakpoint .*$srcfile:$line2\\..*"
+
+    # Now block inferior 1 and issue "next".  We should stop at the
+    # breakpoint for inferior 2, given schedlock off.
+    with_test_prefix "next inf 1" {
+	block 1 0
+	gdb_test "next" "Thread 2.1 .*hit Breakpoint .*$srcfile:$line2.*"
+    }
+
+    # Now unblock inferior 2 and block inferior 1.  "next" should run
+    # into the breakpoint in inferior 1.
+    with_test_prefix "next inf 2" {
+	block 0 1
+	gdb_test "next" "Thread 1.1 .*hit Breakpoint .*$srcfile:$line1.*"
+    }
+
+    # Try nexting inferior 1 again.
+    with_test_prefix "next inf 1 again" {
+	block 1 0
+	gdb_test "next" "Thread 2.1 .*hit Breakpoint .*$srcfile:$line2.*"
+    }
+}
+
+# Make a core file with two threads upfront.  Several tests load the
+# same core file.
+prepare_core
+
+# Some basic "continue" + breakpoints tests.
+with_test_prefix "continue" {
+    foreach_with_prefix non-stop {"off" "on"} {
+	test_continue ${non-stop}
+    }
+}
+
+# Some basic all-stop Ctrl-C tests.
+with_test_prefix "interrupt" {
+    test_ctrlc
+}
+
+# Test ping-ponging between two targets with "next".
+with_test_prefix "ping-pong" {
+    test_ping_pong_next
+}
diff --git a/gdb/testsuite/lib/gdbserver-support.exp b/gdb/testsuite/lib/gdbserver-support.exp
index 00f46c8264..a5d88ec314 100644
--- a/gdb/testsuite/lib/gdbserver-support.exp
+++ b/gdb/testsuite/lib/gdbserver-support.exp
@@ -64,6 +64,10 @@ proc gdb_target_cmd_ext { targetname serialport {additional_text ""} } {
 	    -re "Couldn't establish connection to remote.*$gdb_prompt $" {
 		verbose "Connection failed"
 	    }
+	    -re "Non-stop mode requested, but remote does not support non-stop.*$gdb_prompt $" {
+		verbose "remote does not support non-stop"
+		return 1
+	    }
 	    -re "Remote MIPS debugging.*$additional_text.*$gdb_prompt" {
 		verbose "Set target to $targetname"
 		return 0
-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [PATCH v2 04/24] exceptions.c:print_flush: Remove obsolete check
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
                   ` (12 preceding siblings ...)
  2019-10-17 22:51 ` [PATCH v2 22/24] Add "info connections" command, "info inferiors" connection number/string Pedro Alves
@ 2019-10-17 22:51 ` Pedro Alves
  2019-10-17 22:51 ` [PATCH v2 18/24] Multi-target support Pedro Alves
                   ` (13 subsequent siblings)
  27 siblings, 0 replies; 73+ messages in thread
From: Pedro Alves @ 2019-10-17 22:51 UTC (permalink / raw)
  To: gdb-patches

Commit 20f0d60db4fb ("Avoid crash when calling warning too early"),
added a "current_top_target () != NULL" check to
target_supports_terminal_ours, so this check in exceptions.c is now
obsolete.

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

	* exceptions.c (print_flush): Remove current_top_target() check.
---
 gdb/exceptions.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/gdb/exceptions.c b/gdb/exceptions.c
index a405947257..3dccda78e2 100644
--- a/gdb/exceptions.c
+++ b/gdb/exceptions.c
@@ -39,11 +39,7 @@ print_flush (void)
     deprecated_error_begin_hook ();
 
   gdb::optional<target_terminal::scoped_restore_terminal_state> term_state;
-  /* While normally there's always something pushed on the target
-     stack, the NULL check is needed here because we can get here very
-     early during startup, before the target stack is first
-     initialized.  */
-  if (current_top_target () != NULL && target_supports_terminal_ours ())
+  if (target_supports_terminal_ours ())
     {
       term_state.emplace ();
       target_terminal::ours_for_output ();
-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [PATCH v2 23/24] Require always-non-stop for multi-target resumptions
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
                   ` (17 preceding siblings ...)
  2019-10-17 22:57 ` [PATCH v2 07/24] Delete unnecessary code from kill_command Pedro Alves
@ 2019-10-17 22:57 ` Pedro Alves
  2019-11-01 14:51   ` Tom Tromey
  2019-10-17 22:59 ` [PATCH v2 24/24] Multi-target: NEWS and user manual Pedro Alves
                   ` (8 subsequent siblings)
  27 siblings, 1 reply; 73+ messages in thread
From: Pedro Alves @ 2019-10-17 22:57 UTC (permalink / raw)
  To: gdb-patches

Currently, we can only support resuming multiple targets at the same
time if all targets are in non-stop mode (or user-visible all-stop
mode with target backend in non-stop mode).

This patch makes GDB error out if the user tries to resume more than
one target at the same time and one of the resumed targets isn't in
non-stop mode:

 (gdb) info inferiors
   Num  Description       Connection                Executable
   1    process 15303     1 (native)                a.out
 * 2    process 15286     2 (extended-remote :9999) a.out
 (gdb) set schedule-multiple on
 (gdb) c
 Continuing.
 Connection 2 (extended-remote :9999) does not support multi-target resumption.

This is here later in the series instead of in the main multi-target
patch because it depends the previous patch, which added
process_stratum_target::connection_string().

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

	* infrun.c: Include "target-connection.h".
	(check_multi_target_resumption): New.
	(proceed): Call it.
	* target-connection.c (make_target_connection_string): Make
	static.
	* target-connection.h (make_target_connection_string): Declare.
---
 gdb/infrun.c            | 53 +++++++++++++++++++++++++++++++++++++++++++++++++
 gdb/target-connection.c |  7 ++-----
 gdb/target-connection.h |  8 ++++++++
 3 files changed, 63 insertions(+), 5 deletions(-)

diff --git a/gdb/infrun.c b/gdb/infrun.c
index f00761bd54..8e62a6495c 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -30,6 +30,7 @@
 #include "gdbcmd.h"
 #include "cli/cli-script.h"
 #include "target.h"
+#include "target-connection.h"
 #include "gdbthread.h"
 #include "annotate.h"
 #include "symfile.h"
@@ -2888,6 +2889,56 @@ commit_resume_all_targets ()
     }
 }
 
+/* Check that all the targets we're about to resume are in non-stop
+   mode, since that's the only way we support handling events from
+   multiple targets simultaneously.  */
+
+static void
+check_multi_target_resumption (process_stratum_target *resume_target)
+{
+  if (!non_stop && resume_target == nullptr)
+    {
+      scoped_restore_current_thread restore_thread;
+
+      /* This is used to track whether we're resuming more than one
+	 target.  */
+      process_stratum_target *first_connection = nullptr;
+
+      /* The first inferior we see with a target that does not work in
+	 always-non-stop mode.  */
+      inferior *first_not_non_stop = nullptr;
+
+      for (inferior *inf : all_non_exited_inferiors (resume_target))
+	{
+	  switch_to_inferior_no_thread (inf);
+
+	  if (!target_has_execution)
+	    continue;
+
+	  process_stratum_target *proc_target
+	    = current_inferior ()->process_target();
+
+	  if (!target_is_non_stop_p ())
+	    first_not_non_stop = inf;
+
+	  if (first_connection == nullptr)
+	    first_connection = proc_target;
+	  else if (first_connection != proc_target
+		   && first_not_non_stop != nullptr)
+	    {
+	      switch_to_inferior_no_thread (first_not_non_stop);
+
+	      proc_target = current_inferior ()->process_target();
+
+	      error (_("Connection %d (%s) does not support "
+		       "multi-target resumption."),
+		     proc_target->connection_number,
+		     make_target_connection_string (proc_target).c_str ());
+	    }
+	}
+    }
+}
+
 /* Basic routine for continuing the program in various fashions.
 
    ADDR is the address to resume at, or -1 for resume where stopped.
@@ -2938,6 +2989,8 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
   process_stratum_target *resume_target
     = user_visible_resume_target (resume_ptid);
 
+  check_multi_target_resumption (resume_target);
+
   if (addr == (CORE_ADDR) -1)
     {
       if (pc == cur_thr->suspend.stop_pc
diff --git a/gdb/target-connection.c b/gdb/target-connection.c
index a079f218bd..dd0300a82b 100644
--- a/gdb/target-connection.c
+++ b/gdb/target-connection.c
@@ -53,12 +53,9 @@ connection_list_remove (process_stratum_target *t)
   t->connection_number = 0;
 }
 
-/* Make a target connection string for T.  This is usually T's
-   shortname, but it includes the result of
-   process_stratum_target::connection_string() too if T supports
-   it.  */
+/* See target-connection.h.  */
 
-static std::string
+std::string
 make_target_connection_string (process_stratum_target *t)
 {
   if (t->connection_string () != NULL)
diff --git a/gdb/target-connection.h b/gdb/target-connection.h
index 0e2dc128d8..0b31924b99 100644
--- a/gdb/target-connection.h
+++ b/gdb/target-connection.h
@@ -20,6 +20,8 @@
 #ifndef TARGET_CONNECTION_H
 #define TARGET_CONNECTION_H
 
+#include <string>
+
 struct process_stratum_target;
 
 /* Add a process target to the connection list, if not already
@@ -29,4 +31,10 @@ void connection_list_add (process_stratum_target *t);
 /* Remove a process target from the connection list.  */
 void connection_list_remove (process_stratum_target *t);
 
+/* Make a target connection string for T.  This is usually T's
+   shortname, but it includes the result of
+   process_stratum_target::connection_string() too if T supports
+   it.  */
+std::string make_target_connection_string (process_stratum_target *t);
+
 #endif /* TARGET_CONNECTION_H */
-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [PATCH v2 07/24] Delete unnecessary code from kill_command
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
                   ` (16 preceding siblings ...)
  2019-10-17 22:57 ` [PATCH v2 05/24] Make target_ops::has_execution take an 'inferior *' instead of a ptid_t Pedro Alves
@ 2019-10-17 22:57 ` Pedro Alves
  2019-10-17 22:57 ` [PATCH v2 23/24] Require always-non-stop for multi-target resumptions Pedro Alves
                   ` (9 subsequent siblings)
  27 siblings, 0 replies; 73+ messages in thread
From: Pedro Alves @ 2019-10-17 22:57 UTC (permalink / raw)
  To: gdb-patches

I believe this comment:

      /* Killing off the inferior can leave us with a core file.  If
	 so, print the state we are left in.  */

Referred to the fact that a decade ago, by design, GDB would let you
type "run" when debugging a core dump, keeping the core open.  That
"run" would push a process_stratum target on the target stack for the
live process, and, the core would remain open -- we used to have a
core_stratum.  When the live process was killed/detached or exited,
GDB would go back to debugging the core, since the core_stratum target
was now at the top of the stack.  That design had a number of
problems, see here for example:

  https://sourceware.org/ml/gdb-patches/2008-08/msg00290.html

In 2010, core_stratum was finaly eliminated and cores now have
process_stratum too, with commit c0edd9edadfe ("Make core files the
process_stratum.").  Pushing a live process on the stack while you're
debugging a core discards the core completely.

I also thought that this might be in use with checkpoints, but it does
not -- "kill" when you have multiple checkpoints kills all the
checkpoints.

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

	* infcmd.c (kill_command): Remove dead code.
---
 gdb/infcmd.c | 14 --------------
 1 file changed, 14 deletions(-)

diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index 71057748fd..ae1044b95a 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -2501,20 +2501,6 @@ kill_command (const char *arg, int from_tty)
     printf_unfiltered (_("[Inferior %d (%s) killed]\n"),
 		       infnum, pid_str.c_str ());
 
-  /* If we still have other inferiors to debug, then don't mess with
-     with their threads.  */
-  if (!have_inferiors ())
-    {
-      init_thread_list ();		/* Destroy thread info.  */
-
-      /* Killing off the inferior can leave us with a core file.  If
-	 so, print the state we are left in.  */
-      if (target_has_stack)
-	{
-	  printf_filtered (_("In %s,\n"), target_longname);
-	  print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
-	}
-    }
   bfd_cache_close_all ();
 }
 
-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [PATCH v2 05/24] Make target_ops::has_execution take an 'inferior *' instead of a ptid_t
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
                   ` (15 preceding siblings ...)
  2019-10-17 22:51 ` [PATCH v2 19/24] Add multi-target tests Pedro Alves
@ 2019-10-17 22:57 ` Pedro Alves
  2019-10-17 22:57 ` [PATCH v2 07/24] Delete unnecessary code from kill_command Pedro Alves
                   ` (10 subsequent siblings)
  27 siblings, 0 replies; 73+ messages in thread
From: Pedro Alves @ 2019-10-17 22:57 UTC (permalink / raw)
  To: gdb-patches

With the multi-target work, each inferior will have its own target
stack, so to call a target method, we'll need to make sure that the
inferior in question is the current one, otherwise target->beneath()
calls will find the target beneath in the wrong inferior.

In some places, it's much more convenient to be able to check whether
an inferior has execution without having to switch to it in order to
call target_has_execution on the right inferior/target stack, to avoid
side effects with switching inferior/thread/program space.

The current target_ops::has_execution method takes a ptid_t as
parameter, which, in a multi-target world, isn't sufficient to
identify the target.  This patch prepares to address that, by changing
the parameter to an inferior pointer instead.  From the inferior,
we'll be able to query its target stack to tell which target is
beneath.

Also adds a new inferior::has_execution() method to make callers a bit
more natural to read.

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

	* corelow.c (core_target::has_execution): Change parameter type to
	inferior pointer.
	* inferior.c (number_of_live_inferiors): Use
	inferior::has_execution instead of target_has_execution_1.
	* inferior.h (inferior::has_execution): New.
	* linux-thread-db.c (thread_db_target::update_thread_list): Use
	inferior::has_execution instead of target_has_execution_1.
	* process-stratum-target.c
	(process_stratum_target::has_execution): Change parameter type to
	inferior pointer.  Check the inferior's PID instead of
	inferior_ptid.
	* process-stratum-target.h
	(process_stratum_target::has_execution): Change parameter type to
	inferior pointer.
	* record-full.c (record_full_core_target::has_execution): Change
	parameter type to inferior pointer.
	* target.c (target_has_execution_1): Change parameter type to
	inferior pointer.
	(target_has_execution_current): Adjust.
	* target.h (target_ops::has_execution): Change parameter type to
	inferior pointer.
	(target_has_execution_1): Change parameter type to inferior
	pointer.  Change return type to bool.
	* tracefile.h (tracefile_target::has_execution): Change parameter
	type to inferior pointer.
---
 gdb/corelow.c                |  2 +-
 gdb/inferior.c               |  2 +-
 gdb/inferior.h               |  3 +++
 gdb/linux-thread-db.c        |  2 +-
 gdb/process-stratum-target.c |  8 ++++----
 gdb/process-stratum-target.h |  2 +-
 gdb/record-full.c            |  4 ++--
 gdb/target.c                 | 12 ++++++------
 gdb/target.h                 |  7 ++++---
 gdb/tracefile.h              |  2 +-
 10 files changed, 24 insertions(+), 20 deletions(-)

diff --git a/gdb/corelow.c b/gdb/corelow.c
index cea9210a52..a5af5c2742 100644
--- a/gdb/corelow.c
+++ b/gdb/corelow.c
@@ -92,7 +92,7 @@ public:
   bool has_memory () override;
   bool has_stack () override;
   bool has_registers () override;
-  bool has_execution (ptid_t) override { return false; }
+  bool has_execution (inferior *inf) override { return false; }
 
   bool info_proc (const char *, enum info_proc_what) override;
 
diff --git a/gdb/inferior.c b/gdb/inferior.c
index fa1fd3fc10..310b8f5dd3 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -353,7 +353,7 @@ number_of_live_inferiors (void)
   int num_inf = 0;
 
   for (inferior *inf : all_non_exited_inferiors ())
-    if (target_has_execution_1 (ptid_t (inf->pid)))
+    if (inf->has_execution ())
       for (thread_info *tp ATTRIBUTE_UNUSED : inf->non_exited_threads ())
 	{
 	  /* Found a live thread in this inferior, go to the next
diff --git a/gdb/inferior.h b/gdb/inferior.h
index 43f0417e62..720d570fa3 100644
--- a/gdb/inferior.h
+++ b/gdb/inferior.h
@@ -341,6 +341,9 @@ public:
   /* Returns true if we can delete this inferior.  */
   bool deletable () const { return refcount () == 0; }
 
+  bool has_execution ()
+  { return target_has_execution_1 (this); }
+
   /* Pointer to next inferior in singly-linked list of inferiors.  */
   struct inferior *next = NULL;
 
diff --git a/gdb/linux-thread-db.c b/gdb/linux-thread-db.c
index b7c4f245b9..676690b197 100644
--- a/gdb/linux-thread-db.c
+++ b/gdb/linux-thread-db.c
@@ -1622,7 +1622,7 @@ thread_db_target::update_thread_list ()
 	 stop.  That uses thread_db entry points that do not walk
 	 libpthread's thread list, so should be safe, as well as more
 	 efficient.  */
-      if (target_has_execution_1 (thread->ptid))
+      if (thread->inf->has_execution ())
 	continue;
 
       thread_db_find_new_threads_1 (thread);
diff --git a/gdb/process-stratum-target.c b/gdb/process-stratum-target.c
index 2c4b812f86..1517088f0f 100644
--- a/gdb/process-stratum-target.c
+++ b/gdb/process-stratum-target.c
@@ -77,9 +77,9 @@ process_stratum_target::has_registers ()
 }
 
 bool
-process_stratum_target::has_execution (ptid_t the_ptid)
+process_stratum_target::has_execution (inferior *inf)
 {
-  /* If there's no thread selected, then we can't make it run through
-     hoops.  */
-  return the_ptid != null_ptid;
+  /* If there's a process running already, we can't make it run
+     through hoops.  */
+  return inf->pid != 0;
 }
diff --git a/gdb/process-stratum-target.h b/gdb/process-stratum-target.h
index 2e620f82e8..a14e4f3a72 100644
--- a/gdb/process-stratum-target.h
+++ b/gdb/process-stratum-target.h
@@ -50,7 +50,7 @@ public:
   bool has_memory () override;
   bool has_stack () override;
   bool has_registers () override;
-  bool has_execution (ptid_t the_ptid) override;
+  bool has_execution (inferior *inf) override;
 };
 
 #endif /* !defined (PROCESS_STRATUM_TARGET_H) */
diff --git a/gdb/record-full.c b/gdb/record-full.c
index 5168b0b61c..b96642b4f8 100644
--- a/gdb/record-full.c
+++ b/gdb/record-full.c
@@ -319,7 +319,7 @@ public:
 			 struct bp_target_info *,
 			 enum remove_bp_reason) override;
 
-  bool has_execution (ptid_t) override;
+  bool has_execution (inferior *inf) override;
 };
 
 static record_full_target record_full_ops;
@@ -2244,7 +2244,7 @@ record_full_core_target::remove_breakpoint (struct gdbarch *gdbarch,
 /* "has_execution" method for prec over corefile.  */
 
 bool
-record_full_core_target::has_execution (ptid_t the_ptid)
+record_full_core_target::has_execution (inferior *inf)
 {
   return true;
 }
diff --git a/gdb/target.c b/gdb/target.c
index 78bdfeb49a..df70e67f28 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -223,20 +223,20 @@ target_has_registers_1 (void)
   return 0;
 }
 
-int
-target_has_execution_1 (ptid_t the_ptid)
+bool
+target_has_execution_1 (inferior *inf)
 {
   for (target_ops *t = current_top_target (); t != NULL; t = t->beneath ())
-    if (t->has_execution (the_ptid))
-      return 1;
+    if (t->has_execution (inf))
+      return true;
 
-  return 0;
+  return false;
 }
 
 int
 target_has_execution_current (void)
 {
-  return target_has_execution_1 (inferior_ptid);
+  return target_has_execution_1 (current_inferior ());
 }
 
 /* This is used to implement the various target commands.  */
diff --git a/gdb/target.h b/gdb/target.h
index 1bb7276673..5939126c3c 100644
--- a/gdb/target.h
+++ b/gdb/target.h
@@ -679,7 +679,7 @@ struct target_ops
     virtual bool has_memory () { return false; }
     virtual bool has_stack () { return false; }
     virtual bool has_registers () { return false; }
-    virtual bool has_execution (ptid_t) { return false; }
+    virtual bool has_execution (inferior *inf) { return false; }
 
     /* Control thread execution.  */
     virtual thread_control_capabilities get_thread_control_capabilities ()
@@ -1784,9 +1784,10 @@ extern int target_has_registers_1 (void);
    case this will become true after to_create_inferior or
    to_attach.  */
 
-extern int target_has_execution_1 (ptid_t);
+extern bool target_has_execution_1 (inferior *inf);
 
-/* Like target_has_execution_1, but always passes inferior_ptid.  */
+/* Like target_has_execution_1, but always passes
+   current_inferior().  */
 
 extern int target_has_execution_current (void);
 
diff --git a/gdb/tracefile.h b/gdb/tracefile.h
index 8f9dc0e06d..9c7fdea729 100644
--- a/gdb/tracefile.h
+++ b/gdb/tracefile.h
@@ -127,7 +127,7 @@ public:
   bool has_memory () override;
   bool has_stack () override;
   bool has_registers () override;
-  bool has_execution (ptid_t) override { return false; }
+  bool has_execution (inferior *inf) override { return false; }
   bool thread_alive (ptid_t ptid) override;
 };
 
-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [PATCH v2 13/24] Delete exit_inferior_silent(int pid)
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
                   ` (19 preceding siblings ...)
  2019-10-17 22:59 ` [PATCH v2 24/24] Multi-target: NEWS and user manual Pedro Alves
@ 2019-10-17 22:59 ` Pedro Alves
  2019-10-17 22:59 ` [PATCH v2 20/24] gdbarch-selftests.c: No longer error out if debugging something Pedro Alves
                   ` (6 subsequent siblings)
  27 siblings, 0 replies; 73+ messages in thread
From: Pedro Alves @ 2019-10-17 22:59 UTC (permalink / raw)
  To: gdb-patches

In commit 00431a78b28f ("Use thread_info and inferior pointers more
throughout"), I introduced 'exit_inferior_silent(inferior *)' and left
'exit_inferior_silent(int)' behind by mistake.  This overload isn't
used anywhere.  It's not declared in inferior.h either, that commit
deleted its declaration in favor of the new
'exit_inferior_silent(inferior *)'.

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

	* inferior.c (exit_inferior_silent(int)): Delete.
---
 gdb/inferior.c | 8 --------
 1 file changed, 8 deletions(-)

diff --git a/gdb/inferior.c b/gdb/inferior.c
index 619b8caac7..d2354fa749 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -220,14 +220,6 @@ exit_inferior (inferior *inf)
   exit_inferior_1 (inf, 0);
 }
 
-void
-exit_inferior_silent (int pid)
-{
-  struct inferior *inf = find_inferior_pid (pid);
-
-  exit_inferior_1 (inf, 1);
-}
-
 void
 exit_inferior_silent (inferior *inf)
 {
-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [PATCH v2 20/24] gdbarch-selftests.c: No longer error out if debugging something
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
                   ` (20 preceding siblings ...)
  2019-10-17 22:59 ` [PATCH v2 13/24] Delete exit_inferior_silent(int pid) Pedro Alves
@ 2019-10-17 22:59 ` Pedro Alves
  2019-10-17 22:59 ` [PATCH v2 08/24] Introduce switch_to_inferior_no_thread Pedro Alves
                   ` (5 subsequent siblings)
  27 siblings, 0 replies; 73+ messages in thread
From: Pedro Alves @ 2019-10-17 22:59 UTC (permalink / raw)
  To: gdb-patches

Since each inferior has its own target stack, the stratum condition
for the "error out if debugging something" check is always false.

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

	* gdbarch-selftests.c (register_to_value_test): Remove "target
	already pushed" check.
---
 gdb/gdbarch-selftests.c | 5 -----
 1 file changed, 5 deletions(-)

diff --git a/gdb/gdbarch-selftests.c b/gdb/gdbarch-selftests.c
index 0942050479..7fd56b0343 100644
--- a/gdb/gdbarch-selftests.c
+++ b/gdb/gdbarch-selftests.c
@@ -71,11 +71,6 @@ register_to_value_test (struct gdbarch *gdbarch)
       builtin->builtin_char32,
     };
 
-  /* Error out if debugging something, because we're going to push the
-     test target, which would pop any existing target.  */
-  if (current_top_target ()->stratum () >= process_stratum)
-   error (_("target already pushed"));
-
   /* Create a mock environment.  An inferior with a thread, with a
      process_stratum target pushed.  */
 
-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [PATCH v2 24/24] Multi-target: NEWS and user manual
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
                   ` (18 preceding siblings ...)
  2019-10-17 22:57 ` [PATCH v2 23/24] Require always-non-stop for multi-target resumptions Pedro Alves
@ 2019-10-17 22:59 ` Pedro Alves
  2019-10-17 22:59 ` [PATCH v2 13/24] Delete exit_inferior_silent(int pid) Pedro Alves
                   ` (7 subsequent siblings)
  27 siblings, 0 replies; 73+ messages in thread
From: Pedro Alves @ 2019-10-17 22:59 UTC (permalink / raw)
  To: gdb-patches

This commit documents the new multi-target features in both NEWS and
user manual.

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

	* NEWS: Mention multi-target debugging, "info connections", and
	"add-inferior -no-connection".

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

	* gdb.texinfo (Starting): Say "current inferior not connected"
	instead of "GDB not connected".
	(Inferiors and Programs): Rename node to ...
	(Inferiors Connections and Programs): ... this.  Update all
	references.  Talk about multiple target connections.  Update "info
	inferiors" info to mention the connections column.  Describe "info
	connections".  Document "add-inferior -no-connection".
	* guile.texi, python.texi: Update cross references.
---
 gdb/doc/gdb.texinfo | 137 +++++++++++++++++++++++++++++++++++++---------------
 gdb/doc/guile.texi  |   4 +-
 gdb/doc/python.texi |   6 +--
 gdb/NEWS            |  29 +++++++++++
 4 files changed, 132 insertions(+), 44 deletions(-)

diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 1208e4f615..1875dd4135 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -2237,8 +2237,8 @@ kill a child process.
 * Input/Output::                Your program's input and output
 * Attach::                      Debugging an already-running process
 * Kill Process::                Killing the child process
-
-* Inferiors and Programs::      Debugging multiple inferiors and programs
+* Inferiors Connections and Programs:: Debugging multiple inferiors
+					 connections and programs
 * Threads::                     Debugging programs with multiple threads
 * Forks::                       Debugging forks
 * Checkpoint/Restart::          Setting a @emph{bookmark} to return to later
@@ -2493,27 +2493,28 @@ $@file{.zshenv} for the Z shell, or the file specified in the
 @itemx set auto-connect-native-target off
 @itemx show auto-connect-native-target
 
-By default, if not connected to any target yet (e.g., with
-@code{target remote}), the @code{run} command starts your program as a
-native process under @value{GDBN}, on your local machine.  If you're
-sure you don't want to debug programs on your local machine, you can
-tell @value{GDBN} to not connect to the native target automatically
-with the @code{set auto-connect-native-target off} command.
+By default, if the current inferior is not connected to any target yet
+(e.g., with @code{target remote}), the @code{run} command starts your
+program as a native process under @value{GDBN}, on your local machine.
+If you're sure you don't want to debug programs on your local machine,
+you can tell @value{GDBN} to not connect to the native target
+automatically with the @code{set auto-connect-native-target off}
+command.
 
-If @code{on}, which is the default, and if @value{GDBN} is not
+If @code{on}, which is the default, and if the current inferior is not
 connected to a target already, the @code{run} command automaticaly
 connects to the native target, if one is available.
 
-If @code{off}, and if @value{GDBN} is not connected to a target
-already, the @code{run} command fails with an error:
+If @code{off}, and if the current inferior is not connected to a
+target already, the @code{run} command fails with an error:
 
 @smallexample
 (@value{GDBP}) run
 Don't know how to run.  Try "help target".
 @end smallexample
 
-If @value{GDBN} is already connected to a target, @value{GDBN} always
-uses it with the @code{run} command.
+If the current inferior is already connected to a target, @value{GDBN}
+always uses it with the @code{run} command.
 
 In any case, you can explicitly connect to the native target with the
 @code{target native} command.  For example,
@@ -2943,15 +2944,17 @@ next type @code{run}, @value{GDBN} notices that the file has changed, and
 reads the symbol table again (while trying to preserve your current
 breakpoint settings).
 
-@node Inferiors and Programs
-@section Debugging Multiple Inferiors and Programs
+@node Inferiors Connections and Programs
+@section Debugging Multiple Inferiors Connections and Programs
 
 @value{GDBN} lets you run and debug multiple programs in a single
 session.  In addition, @value{GDBN} on some systems may let you run
 several programs simultaneously (otherwise you have to exit from one
-before starting another).  In the most general case, you can have
-multiple threads of execution in each of multiple processes, launched
-from multiple executables.
+before starting another).  On some systems @value{GDBN} may even let
+you debug several programs simultaneously on different remote systems.
+In the most general case, you can have multiple threads of execution
+in each of multiple processes, launched from multiple executables,
+running on different machines.
 
 @cindex inferior
 @value{GDBN} represents the state of each program execution with an
@@ -2985,6 +2988,11 @@ the inferior number assigned by @value{GDBN}
 @item
 the target system's inferior identifier
 
+@item
+the target connection the inferior is bound to, including the unique
+connection number assigned by @value{GDBN}, and the protocol used by
+the connection.
+
 @item
 the name of the executable the inferior is running.
 
@@ -3000,9 +3008,51 @@ For example,
 
 @smallexample
 (@value{GDBP}) info inferiors
-  Num  Description       Executable
-  2    process 2307      hello
-* 1    process 3401      goodbye
+  Num  Description       Connection                      Executable
+* 1    process 3401      1 (native)                      goodbye
+  2    process 2307      2 (extended-remote host:10000)  hello
+@end smallexample
+
+To find out what open target connections exist at any moment, use
+@w{@code{info connections}}:
+
+@table @code
+@kindex info connections [ @var{id}@dots{} ]
+@item info connections
+Print a list of all open target connections currently being managed by
+@value{GDBN}.  By default all connections are printed, but the
+argument @var{id}@dots{} -- a space separated list of connections
+numbers -- can be used to limit the display to just the requested
+connections.
+
+@value{GDBN} displays for each connection (in this order):
+
+@enumerate
+@item
+the connection number assigned by @value{GDBN}.
+
+@item
+the protocol used by the connection.
+
+@item
+a textual description of the protocol used by the connection.
+
+@end enumerate
+
+@noindent
+An asterisk @samp{*} preceding the connection number indicates the
+connection of the current inferior.
+
+For example,
+@end table
+@c end table here to get a little more width for example
+
+@smallexample
+(@value{GDBP}) info connections
+  Num  What                        Description
+* 1    extended-remote host:10000  Extended remote serial target in gdb-specific protocol
+  2    native                      Native process
+  3    core                        Local core dump file
 @end smallexample
 
 To switch focus between inferiors, use the @code{inferior} command:
@@ -3031,13 +3081,22 @@ remove inferiors from the debugging session use the
 
 @table @code
 @kindex add-inferior
-@item add-inferior [ -copies @var{n} ] [ -exec @var{executable} ]
+@item add-inferior [ -copies @var{n} ] [ -exec @var{executable} ] [-no-connection ]
 Adds @var{n} inferiors to be run using @var{executable} as the
 executable; @var{n} defaults to 1.  If no executable is specified,
 the inferiors begins empty, with no program.  You can still assign or
 change the program assigned to the inferior at any time by using the
 @code{file} command with the executable name as its argument.
 
+By default, the new inferior begins connected to the same target
+connection as the current inferior.  For example, if the current
+inferior was connected to @code{gdbserver} with @code{target remote},
+then the new inferior will be connected to the same @code{gdbserver}
+instance.  The @samp{-no-connection} option starts the new inferior
+with no connection yet.  You can then for example use the @code{target
+remote} command to connect to some other @code{gdbserver} instance,
+use @code{run} to spawn a local program, etc.
+
 @kindex clone-inferior
 @item clone-inferior [ -copies @var{n} ] [ @var{infno} ]
 Adds @var{n} inferiors ready to execute the same program as inferior
@@ -3047,15 +3106,15 @@ want to run another instance of the inferior you are debugging.
 
 @smallexample
 (@value{GDBP}) info inferiors
-  Num  Description       Executable
-* 1    process 29964     helloworld
+  Num  Description       Connection   Executable
+* 1    process 29964     1 (native)   helloworld
 (@value{GDBP}) clone-inferior
 Added inferior 2.
 1 inferiors added.
 (@value{GDBP}) info inferiors
-  Num  Description       Executable
-  2    <null>            helloworld
-* 1    process 29964     helloworld
+  Num  Description       Connection   Executable
+* 1    process 29964     1 (native)   helloworld
+  2    <null>            1 (native)   helloworld
 @end smallexample
 
 You can now simply switch focus to inferior 2 and run it.
@@ -3708,14 +3767,14 @@ If you choose to set @samp{detach-on-fork} mode off, then @value{GDBN}
 will retain control of all forked processes (including nested forks).
 You can list the forked processes under the control of @value{GDBN} by
 using the @w{@code{info inferiors}} command, and switch from one fork
-to another by using the @code{inferior} command (@pxref{Inferiors and
-Programs, ,Debugging Multiple Inferiors and Programs}).
+to another by using the @code{inferior} command (@pxref{Inferiors Connections and
+Programs, ,Debugging Multiple Inferiors Connections and Programs}).
 
 To quit debugging one of the forked processes, you can either detach
 from it by using the @w{@code{detach inferiors}} command (allowing it
 to run independently), or kill it using the @w{@code{kill inferiors}}
-command.  @xref{Inferiors and Programs, ,Debugging Multiple Inferiors
-and Programs}.
+command.  @xref{Inferiors Connections and Programs, ,Debugging
+Multiple Inferiors Connections and Programs}.
 
 If you ask to debug a child process and a @code{vfork} is followed by an
 @code{exec}, @value{GDBN} executes the new target up to the first
@@ -11866,8 +11925,8 @@ gdbserver that supports the @code{qGetTIBAddr} request.
 This variable contains the address of the thread information block.
 
 @item $_inferior
-The number of the current inferior.  @xref{Inferiors and
-Programs, ,Debugging Multiple Inferiors and Programs}.
+The number of the current inferior.  @xref{Inferiors Connections and
+Programs, ,Debugging Multiple Inferiors Connections and Programs}.
 
 @item $_thread
 The thread number of the current thread.  @xref{thread numbers}.
@@ -12974,7 +13033,7 @@ character.
 
 @value{GDBN} caches data exchanged between the debugger and a target.
 Each cache is associated with the address space of the inferior.
-@xref{Inferiors and Programs}, about inferior and address space.
+@xref{Inferiors Connections and Programs}, about inferior and address space.
 Such caching generally improves performance in remote debugging
 (@pxref{Remote Debugging}), because it reduces the overhead of the
 remote protocol by bundling memory reads and writes into large chunks.
@@ -28256,7 +28315,7 @@ groups can be obtained using @samp{-list-thread-groups --available}.
 In general, the content of a thread group may be only retrieved only
 after attaching to that thread group.
 
-Thread groups are related to inferiors (@pxref{Inferiors and
+Thread groups are related to inferiors (@pxref{Inferiors Connections and
 Programs}).  Each inferior corresponds to a thread group of a special
 type @samp{process}, and some additional operations are permitted on
 such thread groups.
@@ -35156,7 +35215,7 @@ popup menu, but is needless clutter on the command line, and
 -add-inferior
 @end smallexample
 
-Creates a new inferior (@pxref{Inferiors and Programs}).  The created
+Creates a new inferior (@pxref{Inferiors Connections and Programs}).  The created
 inferior is not associated with any executable.  Such association may
 be established with the @samp{-file-exec-and-symbols} command
 (@pxref{GDB/MI File Commands}).  The command response has a single
@@ -45394,11 +45453,11 @@ command, otherwise you may get an error that looks something like
 @command{gdbserver} can also debug multiple inferiors at once,
 described in
 @ifset man
-the @value{GDBN} manual in node @code{Inferiors and Programs}
--- shell command @code{info -f gdb -n 'Inferiors and Programs'}.
+the @value{GDBN} manual in node @code{Inferiors Connections and Programs}
+-- shell command @code{info -f gdb -n 'Inferiors Connections and Programs'}.
 @end ifset
 @ifclear man
-@ref{Inferiors and Programs}.
+@ref{Inferiors Connections and Programs}.
 @end ifclear
 In such case use the @code{extended-remote} @value{GDBN} command variant:
 
diff --git a/gdb/doc/guile.texi b/gdb/doc/guile.texi
index 699e0975a2..6e1a5cc4c9 100644
--- a/gdb/doc/guile.texi
+++ b/gdb/doc/guile.texi
@@ -2155,7 +2155,7 @@ A program space, or @dfn{progspace}, represents a symbolic view
 of an address space.
 It consists of all of the objfiles of the program.
 @xref{Objfiles In Guile}.
-@xref{Inferiors and Programs, program spaces}, for more details
+@xref{Inferiors Connections and Programs, program spaces}, for more details
 about program spaces.
 
 Each progspace is represented by an instance of the @code{<gdb:progspace>}
@@ -2178,7 +2178,7 @@ if the program it refers to is not loaded in @value{GDBN} any longer.
 @deffn {Scheme Procedure} current-progspace
 This function returns the program space of the currently selected inferior.
 There is always a current progspace, this never returns @code{#f}.
-@xref{Inferiors and Programs}.
+@xref{Inferiors Connections and Programs}.
 @end deffn
 
 @deffn {Scheme Procedure} progspaces
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index 48adad4e97..b7d3b9173e 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -2928,7 +2928,7 @@ itself.
 
 @findex gdb.Inferior
 Programs which are being run under @value{GDBN} are called inferiors
-(@pxref{Inferiors and Programs}).  Python scripts can access
+(@pxref{Inferiors Connections and Programs}).  Python scripts can access
 information about and manipulate inferiors controlled by @value{GDBN}
 via objects of the @code{gdb.Inferior} class.
 
@@ -4158,7 +4158,7 @@ A program space, or @dfn{progspace}, represents a symbolic view
 of an address space.
 It consists of all of the objfiles of the program.
 @xref{Objfiles In Python}.
-@xref{Inferiors and Programs, program spaces}, for more details
+@xref{Inferiors Connections and Programs, program spaces}, for more details
 about program spaces.
 
 The following progspace-related functions are available in the
@@ -4167,7 +4167,7 @@ The following progspace-related functions are available in the
 @findex gdb.current_progspace
 @defun gdb.current_progspace ()
 This function returns the program space of the currently selected inferior.
-@xref{Inferiors and Programs}.  This is identical to
+@xref{Inferiors Connections and Programs}.  This is identical to
 @code{gdb.selected_inferior().progspace} (@pxref{Inferiors In Python}) and is
 included for historical compatibility.
 @end defun
diff --git a/gdb/NEWS b/gdb/NEWS
index 25e67e43c8..a75726fabd 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -46,6 +46,21 @@
   The 'outer_function::' prefix is only needed if 'inner_function' is
   not visible in the current scope.
 
+* Multi-target debugging support
+
+  GDB now supports debugging multiple target connections
+  simultaneously.  For example, you can now have each inferior
+  connected to different remote servers running in different machines,
+  or have one inferior debugging a local native process, an inferior
+  debugging a core dump, etc.
+
+  This support is experimental and comes with some limitations -- you
+  can only resume multiple targets simultaneously if all targets
+  support non-stop mode, and all remote stubs or servers must support
+  the same set of remote protocol features exactly.  See also "info
+  connections" and "add-inferior -no-connection" below, and "maint set
+  target-non-stop" in the user manual.
+
 * Python API
 
   ** The gdb.Value type has a new method 'format_string' which returns a
@@ -147,6 +162,9 @@ show print frame-info
   'frame', 'stepi'.  The python frame filtering also respect this setting.
   The 'backtrace' '-frame-info' option can override this global setting.
 
+info connections
+  Lists the target connections currently in use.
+
 * Changed commands
 
 help
@@ -191,6 +209,17 @@ show print raw-frame-arguments
   old commands are now deprecated and may be removed in a future
   release.
 
+add-inferior [-no-connection]
+  The add-inferior command now supports a "-no-connection" flag that
+  makes the new inferior start with no target connection associated.
+  By default, the new inferior inherits the target connection of the
+  current inferior.  See also "info connections".
+
+info inferior
+  This command's output now includes a new "Connection" column
+  indicating which target connection an inferior is bound to.  See
+  "info connections" above.
+
 maint test-options require-delimiter
 maint test-options unknown-is-error
 maint test-options unknown-is-operand
-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [PATCH v2 08/24] Introduce switch_to_inferior_no_thread
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
                   ` (21 preceding siblings ...)
  2019-10-17 22:59 ` [PATCH v2 20/24] gdbarch-selftests.c: No longer error out if debugging something Pedro Alves
@ 2019-10-17 22:59 ` Pedro Alves
  2019-11-07  9:14   ` Paunovic, Aleksandar
  2019-10-17 23:00 ` [PATCH v2 14/24] Tweak handling of remote errors in response to resumption packet Pedro Alves
                   ` (4 subsequent siblings)
  27 siblings, 1 reply; 73+ messages in thread
From: Pedro Alves @ 2019-10-17 22:59 UTC (permalink / raw)
  To: gdb-patches

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;
-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [PATCH v2 14/24] Tweak handling of remote errors in response to resumption packet
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
                   ` (22 preceding siblings ...)
  2019-10-17 22:59 ` [PATCH v2 08/24] Introduce switch_to_inferior_no_thread Pedro Alves
@ 2019-10-17 23:00 ` Pedro Alves
  2019-10-18 20:23 ` [PATCH v2 00/24] Multi-target support John Baldwin
                   ` (3 subsequent siblings)
  27 siblings, 0 replies; 73+ messages in thread
From: Pedro Alves @ 2019-10-17 23:00 UTC (permalink / raw)
  To: gdb-patches

With current master, on a Fedora 27 machine with a kernel with buggy
watchpoint support, I see:

  (gdb) PASS: gdb.threads/watchpoint-fork.exp: parent: singlethreaded: hardware breakpoints work
  continue
  Continuing.
  warning: Remote failure reply: E01
  Remote communication error.  Target disconnected.: Connection reset by peer.
  (gdb) FAIL: gdb.threads/watchpoint-fork.exp: parent: singlethreaded: watchpoints work
  continue
  The program is not being run.
  (gdb) FAIL: gdb.threads/watchpoint-fork.exp: parent: singlethreaded: breakpoint after the first fork (the program is no longer running)

The FAILs themselves aren't what's interesting here.  What is
interesting is that with the main multi-target patch applied, I was getting this:

  (gdb) PASS: gdb.threads/watchpoint-fork.exp: parent: singlethreaded: hardware breakpoints work
  continue
  Continuing.
  warning: Remote failure reply: E01
  /home/pedro/brno/pedro/gdb/binutils-gdb-2/build/../src/gdb/inferior.c:285: internal-error: inferior* find_inferior_pid(process_stratum_target*, int): Assertion `pid != 0' failed.
  A problem internal to GDB has been detected,
  further debugging may prove unreliable.
  Quit this debugging session? (y or n) FAIL: gdb.threads/watchpoint-fork.exp: parent: singlethreaded: watchpoints work (GDB internal error)

The problem is that in remote_target::wait_as, we're hitting this:

  switch (buf[0])
    {
    case 'E':		/* Error of some sort.	*/
      /* We're out of sync with the target now.  Did it continue or
	 not?  Not is more likely, so report a stop.  */
      rs->waiting_for_stop_reply = 0;

      warning (_("Remote failure reply: %s"), buf);
      status->kind = TARGET_WAITKIND_STOPPED;
      status->value.sig = GDB_SIGNAL_0;
      break;

which leaves event_ptid as null_ptid.  At the end of the function, we then reach:

  else if (status->kind != TARGET_WAITKIND_EXITED
	   && status->kind != TARGET_WAITKIND_SIGNALLED)
    {
      if (event_ptid != null_ptid)
	record_currthread (rs, event_ptid);
      else
	event_ptid = inferior_ptid;                 <<<<< here
    }

and the trouble is that with the multi-target patch, we'll get here
with inferior_ptid as null_ptid too.  That is done exactly to find
these implicit assumptions that inferior_ptid is a good choice for
default thread, which isn't generaly true.

I first thought of fixing this in the "case 'E'" path, but, given that
this "event_ptid = inferior_ptid" path is also taken when the remote
target does not support threads at all, no thread-related packets or
extensions, it's better to fix it in latter path, to handle all
scenarios that miss reporting a thread.

That's what this patch does.

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

	* remote.c (first_remote_resumed_thread): New.
	(remote_target::wait_as): Use it as default event_ptid instead of
	inferior_ptid.
---
 gdb/remote.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/gdb/remote.c b/gdb/remote.c
index f74881a3c1..553f09a6e0 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -7697,6 +7697,17 @@ remote_target::wait_ns (ptid_t ptid, struct target_waitstatus *status, int optio
     }
 }
 
+/* Return the first resumed thread.  */
+
+static ptid_t
+first_remote_resumed_thread ()
+{
+  for (thread_info *tp : all_non_exited_threads (minus_one_ptid))
+    if (tp->resumed)
+      return tp->ptid;
+  return null_ptid;
+}
+
 /* Wait until the remote machine stops, then return, storing status in
    STATUS just as `wait' would.  */
 
@@ -7833,7 +7844,7 @@ remote_target::wait_as (ptid_t ptid, target_waitstatus *status, int options)
       if (event_ptid != null_ptid)
 	record_currthread (rs, event_ptid);
       else
-	event_ptid = inferior_ptid;
+	event_ptid = first_remote_resumed_thread ();
     }
   else
     /* A process exit.  Invalidate our notion of current thread.  */
-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 00/24] Multi-target support
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
                   ` (23 preceding siblings ...)
  2019-10-17 23:00 ` [PATCH v2 14/24] Tweak handling of remote errors in response to resumption packet Pedro Alves
@ 2019-10-18 20:23 ` John Baldwin
  2019-10-29 19:13   ` Pedro Alves
  2019-10-20 11:41 ` [PATCH v2 00/24] Multi-target support Philippe Waroquiers
                   ` (2 subsequent siblings)
  27 siblings, 1 reply; 73+ messages in thread
From: John Baldwin @ 2019-10-18 20:23 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On 10/17/19 3:50 PM, Pedro Alves wrote:
> Here's v2 of the multi-target patchset, which addresses all the review
> comments so far, I believe.  Patch 15 is new, so all following patches
> are shifted by one.
> 
> This time, I've adjusted the host-specific nat files to function API
> changes.  I tried to find spots that would need changes using grep.  I
> built the series on AIX, x86/SPARC Solaris, 64-bit Windows, x86
> GNU/Linux -m64/-m32, and Aarch64 GNU/Linux.  I'm currently running
> this through the buildbot, will have results tomorrow.  I don't expect
> any serious major issue, if any, as so far runs that completed seem
> OK.

I (finally) have a patch to fix the build on FreeBSD/amd64 (and probably
FreeBSD on other platforms) here:

https://github.com/bsdjhb/gdb/commit/e58a36eaef6244d2040ce6f377497ee898978db4

It's a combined patch but has some commentary on bsd-kvm.c which is
kind of special.  That target adds new commands that need to find a
target to operate on.  I opted to have it look at the current inferior
and if (using a dynamic cast) it is a bsd-kvm target the commands
modify the state of that inferior.  I haven't tested it though as I don't
use bsd-kvm.c.

I did try a simple test of creating two inferiors both of which were
running /bin/ls and gdb hung trying to run the second inferior via
'start'.  I suspect this is some kind of bug in the FreeBSD target not
being multi-target ready that I will have to debug.

-- 
John Baldwin

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 00/24] Multi-target support
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
                   ` (24 preceding siblings ...)
  2019-10-18 20:23 ` [PATCH v2 00/24] Multi-target support John Baldwin
@ 2019-10-20 11:41 ` Philippe Waroquiers
  2019-10-29 19:56   ` Pedro Alves
  2019-11-01 14:56 ` Tom Tromey
  2020-01-10 20:13 ` Pedro Alves
  27 siblings, 1 reply; 73+ messages in thread
From: Philippe Waroquiers @ 2019-10-20 11:41 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

I played a little bit with the patch and valgrind
(I had to apply the patch with git am --3way, otherwise it failed to apply).
No problem encountered when playing with the result.
I also re-read the documentation (as I forgot about how this was all
working), and it was all pretty clear.

3 notes:
  * It might be interesting to one day add
     something like 'inferior apply all/list of inferiors SOMMECOMMAND'
  * when having 2 inferiors connected to 2 different valgrind gdbservers,
    I could continue both inferiors by using 'continue&',
    but I had to (artificially) issue 'interrupt' in the second inferior
    to have GDB accepting 'continue&'.  So, this might be the indication
    of a status 'running' which should be maintained per inferior,
    while it might now be maintained globally.
  * I was (again?) confused by add-inferior silently ignoring abbreviations
    (or more generally anything starting with - unless matching exactly
     an option).
    Waiting for option framework to be extended, the add-inferior command
    could use the below that accepts abbreviations and does not ignore
    wrong options.

Thanks

Philippe


diff --git a/gdb/inferior.c b/gdb/inferior.c
index 061cf5cebb..5d72780eb3 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -786,24 +786,23 @@ add_inferior_command (const char *args, int from_tty)
 
       for (char **argv = built_argv.get (); *argv != NULL; argv++)
 	{
-	  if (**argv == '-')
+	  int len = strlen (*argv);
+
+	  if (strncmp (*argv, "-copies", len) == 0)
 	    {
-	      if (strcmp (*argv, "-copies") == 0)
-		{
-		  ++argv;
-		  if (!*argv)
-		    error (_("No argument to -copies"));
-		  copies = parse_and_eval_long (*argv);
-		}
-	      else if (strcmp (*argv, "-no-connection") == 0)
-		no_connection = true;
-	      else if (strcmp (*argv, "-exec") == 0)
-		{
-		  ++argv;
-		  if (!*argv)
-		    error (_("No argument to -exec"));
-		  exec.reset (tilde_expand (*argv));
-		}
+	      ++argv;
+	      if (!*argv)
+		error (_("No argument to -copies"));
+	      copies = parse_and_eval_long (*argv);
+	    }
+	  else if (strncmp (*argv, "-no-connection", len) == 0)
+	    no_connection = true;
+	  else if (strncmp (*argv, "-exec", len) == 0)
+	    {
+	      ++argv;
+	      if (!*argv)
+		error (_("No argument to -exec"));
+	      exec.reset (tilde_expand (*argv));
 	    }
 	  else
 	    error (_("Invalid argument"));



On Thu, 2019-10-17 at 23:50 +0100, Pedro Alves wrote:
> Here's v2 of the multi-target patchset, which addresses all the review
> comments so far, I believe.  Patch 15 is new, so all following patches
> are shifted by one.
> 
> This time, I've adjusted the host-specific nat files to function API
> changes.  I tried to find spots that would need changes using grep.  I
> built the series on AIX, x86/SPARC Solaris, 64-bit Windows, x86
> GNU/Linux -m64/-m32, and Aarch64 GNU/Linux.  I'm currently running
> this through the buildbot, will have results tomorrow.  I don't expect
> any serious major issue, if any, as so far runs that completed seem
> OK.
> 
> Follows the intro blurb:
> 
> This series adds multi-target support to GDB.  What this means is that
> with this series, GDB can now be connected to different targets at the
> same time.  E.g., you can debug a live native process and a core dump
> at the same time, connect to multiple gdbservers, etc.
> 
> Patches 1 to 17 are preparatory patches.
> 
> Patch 18 is the actual multi-target patch.  This is the largest patch
> in the series.  It does a number of things at the same time, but
> they're all intertwined, so I gave up on splitting it further.
> 
> Patch 19 adds tests.  Split out because patch 18 is already too big as
> is.
> 
> Patch 22 does some user-visible changes, including a new command to
> list open target connections.  This is just the bare minimum I could
> think of that is necessary for multi-target work.  I'm sure we'll find
> other tweaks to other commands necessary.
> 
> Documentation is in patch 24.  Eli already reviewed and approved it.
> 
> I've pushed this to users/palves/multi-target-v2 on sourceware.
> 
> Pedro Alves (23):
>   Preserve selected thread in all-stop w/ background execution
>   Don't rely on inferior_ptid in record_full_wait
>   Make "show remote exec-file" inferior-aware
>   exceptions.c:print_flush: Remove obsolete check
>   Make target_ops::has_execution take an 'inferior *' instead of a
>     ptid_t
>   Don't check target is running in remote_target::mourn_inferior
>   Delete unnecessary code from kill_command
>   Introduce switch_to_inferior_no_thread
>   switch inferior/thread before calling target methods
>   Some get_last_target_status tweaks
>   tfile_target::close: trace_fd can't be -1
>   Use all_non_exited_inferiors in infrun.c
>   Delete exit_inferior_silent(int pid)
>   Tweak handling of remote errors in response to resumption packet
>   Fix reconnecting to a gdbserver already debugging multiple processes,
>     I
>   Fix reconnecting to a gdbserver already debugging multiple processes,
>     II
>   Multi-target support
>   Add multi-target tests
>   gdbarch-selftests.c: No longer error out if debugging something
>   Revert 'Remove unused struct serial::name field'
>   Add "info connections" command, "info inferiors" connection
>     number/string
>   Require always-non-stop for multi-target resumptions
>   Multi-target: NEWS and user manual
> 
> Tankut Baris Aktemur (1):
>   Avoid another inferior_ptid reference in gdb/remote.c
> 
>  gdb/doc/gdb.texinfo                                | 137 ++--
>  gdb/doc/guile.texi                                 |   4 +-
>  gdb/doc/python.texi                                |   6 +-
>  gdb/NEWS                                           |  29 +
>  gdb/Makefile.in                                    |   1 +
>  gdb/aarch64-linux-nat.c                            |   2 +-
>  gdb/ada-tasks.c                                    |   4 +-
>  gdb/aix-thread.c                                   |  24 +-
>  gdb/amd64-fbsd-tdep.c                              |   4 +-
>  gdb/amd64-linux-nat.c                              |   2 +-
>  gdb/break-catch-sig.c                              |   3 +-
>  gdb/break-catch-syscall.c                          |   3 +-
>  gdb/breakpoint.c                                   |  25 +-
>  gdb/bsd-uthread.c                                  |  20 +-
>  gdb/btrace.c                                       |   2 +-
>  gdb/corelow.c                                      |  10 +-
>  gdb/event-top.c                                    |  14 +-
>  gdb/exceptions.c                                   |   6 +-
>  gdb/exec.c                                         |  51 +-
>  gdb/exec.h                                         |   7 +
>  gdb/fbsd-tdep.c                                    |   3 +-
>  gdb/fork-child.c                                   |   7 +-
>  gdb/gdbarch-selftests.c                            |   5 -
>  gdb/gdbserver/fork-child.c                         |   3 +-
>  gdb/gdbserver/inferiors.c                          |   2 +-
>  gdb/gdbserver/linux-low.c                          |   2 +-
>  gdb/gdbserver/lynx-low.c                           |   2 +-
>  gdb/gdbserver/nto-low.c                            |   2 +-
>  gdb/gdbserver/remote-utils.c                       |   2 +-
>  gdb/gdbserver/target.c                             |   8 +-
>  gdb/gdbserver/target.h                             |  11 +-
>  gdb/gdbserver/win32-low.c                          |   4 +-
>  gdb/gdbsupport/common-gdbthread.h                  |   5 +-
>  gdb/gdbthread.h                                    | 133 ++--
>  gdb/i386-fbsd-tdep.c                               |   4 +-
>  gdb/i386-linux-nat.c                               |   2 +-
>  gdb/inf-child.c                                    |   2 +-
>  gdb/inf-ptrace.c                                   |   6 +-
>  gdb/infcall.c                                      |   3 +-
>  gdb/infcmd.c                                       | 129 ++--
>  gdb/inferior-iter.h                                |  76 ++-
>  gdb/inferior.c                                     | 156 +++--
>  gdb/inferior.h                                     |  71 +-
>  gdb/infrun.c                                       | 720 ++++++++++++++++-----
>  gdb/infrun.h                                       |  23 +-
>  gdb/inline-frame.c                                 |  51 +-
>  gdb/inline-frame.h                                 |  12 +-
>  gdb/linux-fork.c                                   |   5 +-
>  gdb/linux-nat.c                                    |  76 ++-
>  gdb/linux-nat.h                                    |   1 +
>  gdb/linux-tdep.c                                   |   3 +-
>  gdb/linux-thread-db.c                              | 112 ++--
>  gdb/mi/mi-interp.c                                 |  10 +-
>  gdb/mi/mi-main.c                                   |   6 +-
>  gdb/nat/fork-inferior.c                            |   8 +-
>  gdb/nat/fork-inferior.h                            |   5 +-
>  gdb/nto-procfs.c                                   |   2 +-
>  gdb/ppc-fbsd-tdep.c                                |   4 +-
>  gdb/proc-service.c                                 |  17 +-
>  gdb/process-stratum-target.c                       |  12 +-
>  gdb/process-stratum-target.h                       |  31 +-
>  gdb/procfs.c                                       |  49 +-
>  gdb/python/py-threadevent.c                        |   4 +-
>  gdb/ravenscar-thread.c                             |  15 +-
>  gdb/record-btrace.c                                |  41 +-
>  gdb/record-full.c                                  |  22 +-
>  gdb/regcache.c                                     | 162 +++--
>  gdb/regcache.h                                     |  30 +-
>  gdb/remote.c                                       | 307 +++++----
>  gdb/riscv-fbsd-tdep.c                              |   4 +-
>  gdb/serial.c                                       |   4 +
>  gdb/serial.h                                       |   1 +
>  gdb/sol-thread.c                                   |  28 +-
>  gdb/sol2-tdep.c                                    |   2 +-
>  gdb/solib-svr4.c                                   |   3 +-
>  gdb/target-connection.c                            | 159 +++++
>  gdb/target-connection.h                            |  40 ++
>  gdb/target-delegates.c                             |  27 +
>  gdb/target.c                                       | 192 +++---
>  gdb/target.h                                       |  41 +-
>  gdb/testsuite/gdb.base/fork-running-state.exp      |  17 +-
>  .../gdb.base/kill-detach-inferiors-cmd.exp         |   4 +-
>  gdb/testsuite/gdb.base/quit-live.exp               |   2 +-
>  gdb/testsuite/gdb.base/remote-exec-file.exp        |  46 ++
>  gdb/testsuite/gdb.guile/scm-progspace.exp          |   2 +-
>  gdb/testsuite/gdb.linespec/linespec.exp            |   2 +-
>  gdb/testsuite/gdb.mi/new-ui-mi-sync.exp            |   2 +-
>  .../gdb.mi/user-selected-context-sync.exp          |   2 +-
>  gdb/testsuite/gdb.multi/multi-target.c             | 100 +++
>  gdb/testsuite/gdb.multi/multi-target.exp           | 387 +++++++++++
>  gdb/testsuite/gdb.multi/remove-inferiors.exp       |   2 +-
>  gdb/testsuite/gdb.multi/tids-gid-reset.c           |  22 +
>  gdb/testsuite/gdb.multi/tids-gid-reset.exp         |  96 +++
>  gdb/testsuite/gdb.multi/watchpoint-multi.exp       |   2 +-
>  gdb/testsuite/gdb.python/py-inferior.exp           |   4 +-
>  .../gdb.server/connect-without-multi-process.exp   |   7 +-
>  .../gdb.server/extended-remote-restart.exp         |  22 +-
>  gdb/testsuite/gdb.threads/async.c                  |  70 ++
>  gdb/testsuite/gdb.threads/async.exp                |  98 +++
>  gdb/testsuite/gdb.threads/fork-plus-threads.exp    |   2 +-
>  .../forking-threads-plus-breakpoint.exp            |   2 +-
>  gdb/testsuite/gdb.trace/report.exp                 |   2 +-
>  gdb/testsuite/lib/gdbserver-support.exp            |   4 +
>  gdb/thread-iter.c                                  |  14 +-
>  gdb/thread-iter.h                                  |  25 +-
>  gdb/thread.c                                       | 230 ++++---
>  gdb/top.c                                          |  17 +-
>  gdb/tracectf.c                                     |   2 +-
>  gdb/tracefile-tfile.c                              |   5 +-
>  gdb/tracefile.h                                    |   2 +-
>  gdb/windows-nat.c                                  |  20 +-
>  111 files changed, 3360 insertions(+), 1073 deletions(-)
>  create mode 100644 gdb/target-connection.c
>  create mode 100644 gdb/target-connection.h
>  create mode 100644 gdb/testsuite/gdb.base/remote-exec-file.exp
>  create mode 100644 gdb/testsuite/gdb.multi/multi-target.c
>  create mode 100644 gdb/testsuite/gdb.multi/multi-target.exp
>  create mode 100644 gdb/testsuite/gdb.multi/tids-gid-reset.c
>  create mode 100644 gdb/testsuite/gdb.multi/tids-gid-reset.exp
>  create mode 100644 gdb/testsuite/gdb.threads/async.c
>  create mode 100644 gdb/testsuite/gdb.threads/async.exp
> 

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 00/24] Multi-target support
  2019-10-18 20:23 ` [PATCH v2 00/24] Multi-target support John Baldwin
@ 2019-10-29 19:13   ` Pedro Alves
  2020-01-09 19:32     ` John Baldwin
  0 siblings, 1 reply; 73+ messages in thread
From: Pedro Alves @ 2019-10-29 19:13 UTC (permalink / raw)
  To: John Baldwin, gdb-patches

On 10/18/19 9:22 PM, John Baldwin wrote:
> On 10/17/19 3:50 PM, Pedro Alves wrote:
>> Here's v2 of the multi-target patchset, which addresses all the review
>> comments so far, I believe.  Patch 15 is new, so all following patches
>> are shifted by one.
>>
>> This time, I've adjusted the host-specific nat files to function API
>> changes.  I tried to find spots that would need changes using grep.  I
>> built the series on AIX, x86/SPARC Solaris, 64-bit Windows, x86
>> GNU/Linux -m64/-m32, and Aarch64 GNU/Linux.  I'm currently running
>> this through the buildbot, will have results tomorrow.  I don't expect
>> any serious major issue, if any, as so far runs that completed seem
>> OK.
> 
> I (finally) have a patch to fix the build on FreeBSD/amd64 (and probably
> FreeBSD on other platforms) here:
> 
> https://github.com/bsdjhb/gdb/commit/e58a36eaef6244d2040ce6f377497ee898978db4
> 

Thanks!

> It's a combined patch but has some commentary on bsd-kvm.c which is
> kind of special.  That target adds new commands that need to find a
> target to operate on.  I opted to have it look at the current inferior
> and if (using a dynamic cast) it is a bsd-kvm target the commands
> modify the state of that inferior.  I haven't tested it though as I don't
> use bsd-kvm.c.

I'd rather not merge the bsd-kvm.c parts into my patches as is, for the
reason that it doesn't appear necessary for a minimal keep-working-as-before
change.  I think it would be fine as a follow up patch, though.

A couple comments:

 - I think we should get away from doing this

      inferior_ptid = null_ptid;
      exit_inferior_silent (current_inferior ());

   in 'bsd_kvm_target::close ()'.

 - Class private fields should be named 'm_foo', so m_kd, m_corefile, etc.

For bsd-kvm.c, I _think_ that the only change necessary to keep things
building would be this:

--- a/gdb/bsd-kvm.c
+++ b/gdb/bsd-kvm.c
@@ -136,7 +136,7 @@ bsd_kvm_target_open (const char *arg, int from_tty)
   core_kd = temp_kd;
   push_target (&bsd_kvm_ops);
 
-  add_thread_silent (bsd_kvm_ptid);
+  add_thread_silent (&bsd_kvm_ops, bsd_kvm_ptid);
   inferior_ptid = bsd_kvm_ptid;

Right?  See updated patch below.

> I did try a simple test of creating two inferiors both of which were
> running /bin/ls and gdb hung trying to run the second inferior via
> 'start'.  I suspect this is some kind of bug in the FreeBSD target not
> being multi-target ready that I will have to debug.

Does that work without the multi-target series?  I mean, maybe it's more
a multi-process debugging issue than a multi-target issue?

From 3f879fe9334cbd69f50efd0131c2bed5498cde92 Mon Sep 17 00:00:00 2001
From: John Baldwin <jhb@FreeBSD.org>
Date: Fri, 18 Oct 2019 13:12:11 -0700
Subject: [PATCH] Update FreeBSD native build for multi-target-v2.

---
 gdb/bsd-kvm.c  |  2 +-
 gdb/fbsd-nat.c | 33 +++++++++++++++++----------------
 2 files changed, 18 insertions(+), 17 deletions(-)

diff --git a/gdb/bsd-kvm.c b/gdb/bsd-kvm.c
index 21f978728d..5ffc303f46 100644
--- a/gdb/bsd-kvm.c
+++ b/gdb/bsd-kvm.c
@@ -136,7 +136,7 @@ bsd_kvm_target_open (const char *arg, int from_tty)
   core_kd = temp_kd;
   push_target (&bsd_kvm_ops);
 
-  add_thread_silent (bsd_kvm_ptid);
+  add_thread_silent (&bsd_kvm_ops, bsd_kvm_ptid);
   inferior_ptid = bsd_kvm_ptid;
 
   target_fetch_registers (get_current_regcache (), -1);
diff --git a/gdb/fbsd-nat.c b/gdb/fbsd-nat.c
index 0274ff542e..bb34a9769d 100644
--- a/gdb/fbsd-nat.c
+++ b/gdb/fbsd-nat.c
@@ -991,11 +991,11 @@ fbsd_enable_proc_events (pid_t pid)
    called to discover new threads each time the thread list is updated.  */
 
 static void
-fbsd_add_threads (pid_t pid)
+fbsd_add_threads (fbsd_nat_target *target, pid_t pid)
 {
   int i, nlwps;
 
-  gdb_assert (!in_thread_list (ptid_t (pid)));
+  gdb_assert (!in_thread_list (target, ptid_t (pid)));
   nlwps = ptrace (PT_GETNUMLWPS, pid, NULL, 0);
   if (nlwps == -1)
     perror_with_name (("ptrace"));
@@ -1010,7 +1010,7 @@ fbsd_add_threads (pid_t pid)
     {
       ptid_t ptid = ptid_t (pid, lwps[i], 0);
 
-      if (!in_thread_list (ptid))
+      if (!in_thread_list (target, ptid))
 	{
 #ifdef PT_LWP_EVENTS
 	  struct ptrace_lwpinfo pl;
@@ -1026,7 +1026,7 @@ fbsd_add_threads (pid_t pid)
 	    fprintf_unfiltered (gdb_stdlog,
 				"FLWP: adding thread for LWP %u\n",
 				lwps[i]);
-	  add_thread (ptid);
+	  add_thread (target, ptid);
 	}
     }
 }
@@ -1043,7 +1043,7 @@ fbsd_nat_target::update_thread_list ()
 #else
   prune_threads ();
 
-  fbsd_add_threads (inferior_ptid.pid ());
+  fbsd_add_threads (this, inferior_ptid.pid ());
 #endif
 }
 
@@ -1174,7 +1174,7 @@ fbsd_nat_target::resume (ptid_t ptid, int step, enum gdb_signal signo)
   if (ptid.lwp_p ())
     {
       /* If ptid is a specific LWP, suspend all other LWPs in the process.  */
-      inferior *inf = find_inferior_ptid (ptid);
+      inferior *inf = find_inferior_ptid (this, ptid);
 
       for (thread_info *tp : inf->non_exited_threads ())
         {
@@ -1193,7 +1193,7 @@ fbsd_nat_target::resume (ptid_t ptid, int step, enum gdb_signal signo)
     {
       /* If ptid is a wildcard, resume all matching threads (they won't run
 	 until the process is continued however).  */
-      for (thread_info *tp : all_non_exited_threads (ptid))
+      for (thread_info *tp : all_non_exited_threads (this, ptid))
 	if (ptrace (PT_RESUME, tp->ptid.lwp (), NULL, 0) == -1)
 	  perror_with_name (("ptrace"));
       ptid = inferior_ptid;
@@ -1239,7 +1239,8 @@ fbsd_nat_target::resume (ptid_t ptid, int step, enum gdb_signal signo)
    core, return true.  */
 
 static bool
-fbsd_handle_debug_trap (ptid_t ptid, const struct ptrace_lwpinfo &pl)
+fbsd_handle_debug_trap (fbsd_nat_target *target, ptid_t ptid,
+			const struct ptrace_lwpinfo &pl)
 {
 
   /* Ignore traps without valid siginfo or for signals other than
@@ -1266,7 +1267,7 @@ fbsd_handle_debug_trap (ptid_t ptid, const struct ptrace_lwpinfo &pl)
   if (pl.pl_siginfo.si_code == TRAP_BRKPT)
     {
       /* Fixup PC for the software breakpoint.  */
-      struct regcache *regcache = get_thread_regcache (ptid);
+      struct regcache *regcache = get_thread_regcache (target, ptid);
       struct gdbarch *gdbarch = regcache->arch ();
       int decr_pc = gdbarch_decr_pc_after_break (gdbarch);
 
@@ -1340,7 +1341,7 @@ fbsd_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
 		 threads might be skipped during post_attach that
 		 have not yet reported their PL_FLAG_EXITED event.
 		 Ignore EXITED events for an unknown LWP.  */
-	      thread_info *thr = find_thread_ptid (wptid);
+	      thread_info *thr = find_thread_ptid (this, wptid);
 	      if (thr != nullptr)
 		{
 		  if (debug_fbsd_lwp)
@@ -1364,13 +1365,13 @@ fbsd_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
 	     PL_FLAG_BORN in case the first stop reported after
 	     attaching to an existing process is a PL_FLAG_BORN
 	     event.  */
-	  if (in_thread_list (ptid_t (pid)))
+	  if (in_thread_list (this, ptid_t (pid)))
 	    {
 	      if (debug_fbsd_lwp)
 		fprintf_unfiltered (gdb_stdlog,
 				    "FLWP: using LWP %u for first thread\n",
 				    pl.pl_lwpid);
-	      thread_change_ptid (ptid_t (pid), wptid);
+	      thread_change_ptid (this, ptid_t (pid), wptid);
 	    }
 
 #ifdef PT_LWP_EVENTS
@@ -1380,13 +1381,13 @@ fbsd_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
 		 threads might be added by fbsd_add_threads that have
 		 not yet reported their PL_FLAG_BORN event.  Ignore
 		 BORN events for an already-known LWP.  */
-	      if (!in_thread_list (wptid))
+	      if (!in_thread_list (this, wptid))
 		{
 		  if (debug_fbsd_lwp)
 		    fprintf_unfiltered (gdb_stdlog,
 					"FLWP: adding thread for LWP %u\n",
 					pl.pl_lwpid);
-		  add_thread (wptid);
+		  add_thread (this, wptid);
 		}
 	      ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
 	      return wptid;
@@ -1474,7 +1475,7 @@ fbsd_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
 #endif
 
 #ifdef USE_SIGTRAP_SIGINFO
-	  if (fbsd_handle_debug_trap (wptid, pl))
+	  if (fbsd_handle_debug_trap (this, wptid, pl))
 	    return wptid;
 #endif
 
@@ -1633,7 +1634,7 @@ void
 fbsd_nat_target::post_attach (int pid)
 {
   fbsd_enable_proc_events (pid);
-  fbsd_add_threads (pid);
+  fbsd_add_threads (this, pid);
 }
 
 #ifdef PL_FLAG_EXEC
-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 00/24] Multi-target support
  2019-10-20 11:41 ` [PATCH v2 00/24] Multi-target support Philippe Waroquiers
@ 2019-10-29 19:56   ` Pedro Alves
  0 siblings, 0 replies; 73+ messages in thread
From: Pedro Alves @ 2019-10-29 19:56 UTC (permalink / raw)
  To: Philippe Waroquiers, gdb-patches

On 10/20/19 12:41 PM, Philippe Waroquiers wrote:
> I played a little bit with the patch and valgrind
> (I had to apply the patch with git am --3way, otherwise it failed to apply).

Guess you missed that this was in the users/palves/multi-target-v2 branch.  :-/

> No problem encountered when playing with the result.
> I also re-read the documentation (as I forgot about how this was all
> working), and it was all pretty clear.

That's great to hear.

> 
> 3 notes:
>   * It might be interesting to one day add
>      something like 'inferior apply all/list of inferiors SOMMECOMMAND'

Agreed.

>   * when having 2 inferiors connected to 2 different valgrind gdbservers,
>     I could continue both inferiors by using 'continue&',
>     but I had to (artificially) issue 'interrupt' in the second inferior
>     to have GDB accepting 'continue&'.  So, this might be the indication
>     of a status 'running' which should be maintained per inferior,
>     while it might now be maintained globally.

I've tried to debug this a little, but I'd like to punt for now.  The error
I'm seeing first comes from a breakpoint re-set, where gdb tries to
read memory from the valgrind that is running.  Breakpoint re-sets currently
are too dumb and re-set all locations, instead of only adding/removing
the locations necessary.  And then that is running into the fact that
the remote protocol in the old all-stop mode (which is what valgrind supports)
is synchronous, gdb can't talk to the remote target until the target
reports a stop.

These sorts of issues is why I required that the remote backends
support non-stop mode for this initial pass.  I'd rather not have to fix
all these issues before landing the initial multi-target support, even
if we know there are issues we need to fix to make it cope better with
all-stop-only targets.

>   * I was (again?) confused by add-inferior silently ignoring abbreviations
>     (or more generally anything starting with - unless matching exactly
>      an option).
>     Waiting for option framework to be extended, the add-inferior command
>     could use the below that accepts abbreviations and does not ignore
>     wrong options.

Yes, that is fine with me.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 01/24] Preserve selected thread in all-stop w/ background execution
  2019-10-17 22:50 ` [PATCH v2 01/24] Preserve selected thread in all-stop w/ background execution Pedro Alves
@ 2019-11-01 13:20   ` Tom Tromey
  2019-12-20 17:22     ` Pedro Alves
  0 siblings, 1 reply; 73+ messages in thread
From: Tom Tromey @ 2019-11-01 13:20 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:

Pedro> +	       previously selected thread (now exited).  We chose the
Pedro> +	       later, just because that's what GDB used to do.  After

I think this should say "We choose the latter".

Tom

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 23/24] Require always-non-stop for multi-target resumptions
  2019-10-17 22:57 ` [PATCH v2 23/24] Require always-non-stop for multi-target resumptions Pedro Alves
@ 2019-11-01 14:51   ` Tom Tromey
  2019-12-30 18:30     ` Pedro Alves
  0 siblings, 1 reply; 73+ messages in thread
From: Tom Tromey @ 2019-11-01 14:51 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:

Pedro> Currently, we can only support resuming multiple targets at the same
Pedro> time if all targets are in non-stop mode (or user-visible all-stop
Pedro> mode with target backend in non-stop mode).

I don't understand this, but I would like to.

I would have thought that target-async would be enough here.  As in, the
necessary part is that target events be integrated with the event loop,
so that multiple event sources can be selected on at once.

Why is non-stop needed instead?

Tom

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 02/24] Don't rely on inferior_ptid in record_full_wait
  2019-10-17 22:50 ` [PATCH v2 02/24] Don't rely on inferior_ptid in record_full_wait Pedro Alves
@ 2019-11-01 14:54   ` Tom Tromey
  2019-12-20 17:49     ` Pedro Alves
  0 siblings, 1 reply; 73+ messages in thread
From: Tom Tromey @ 2019-11-01 14:54 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:

Pedro> The multi-target patch sets inferior_ptid to null_ptid before handling
Pedro> a target event, and thus before calling target_wait, in order to catch
Pedro> places in target_ops::wait implementations that are incorrectly
Pedro> relying on inferior_ptid (which could otherwise be a ptid of a
Pedro> different target, for example).

I think it would be good to add a comment before target_ops::wait
explaining what is required from its implementation.  If other
target_ops methods also cannot rely on inferior_ptid, then that
documentation should be updated as well.  This would make it simpler to
know how to update an existing target, or to write a new target.

Tom

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 00/24] Multi-target support
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
                   ` (25 preceding siblings ...)
  2019-10-20 11:41 ` [PATCH v2 00/24] Multi-target support Philippe Waroquiers
@ 2019-11-01 14:56 ` Tom Tromey
  2020-01-10 20:13 ` Pedro Alves
  27 siblings, 0 replies; 73+ messages in thread
From: Tom Tromey @ 2019-11-01 14:56 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:

Pedro> Here's v2 of the multi-target patchset, which addresses all the review
Pedro> comments so far, I believe.  Patch 15 is new, so all following patches
Pedro> are shifted by one.

I read through all of these.  Well, I skimmed patch 18, as it is
probably too big to review (maybe could have been split up a bit more,
but too late now!).

I sent all my comments.  They're basically nits, though there was one
question I'd like to understand.

I think this should all go in.  Congratulations on doing this, it's a
big improvement!

thanks,
Tom

^ permalink raw reply	[flat|nested] 73+ messages in thread

* RE: [PATCH v2 08/24] Introduce switch_to_inferior_no_thread
  2019-10-17 22:59 ` [PATCH v2 08/24] Introduce switch_to_inferior_no_thread Pedro Alves
@ 2019-11-07  9:14   ` Paunovic, Aleksandar
  2019-12-20 18:50     ` Pedro Alves
  0 siblings, 1 reply; 73+ messages in thread
From: Paunovic, Aleksandar @ 2019-11-07  9:14 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

Shouldn't there be this change as well:

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;
--
2.14.5

Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Gary Kershaw
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 01/24] Preserve selected thread in all-stop w/ background execution
  2019-11-01 13:20   ` Tom Tromey
@ 2019-12-20 17:22     ` Pedro Alves
  2019-12-20 18:54       ` Tom Tromey
  0 siblings, 1 reply; 73+ messages in thread
From: Pedro Alves @ 2019-12-20 17:22 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

On 11/1/19 1:20 PM, Tom Tromey wrote:
>>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:
> 
> Pedro> +	       previously selected thread (now exited).  We chose the
> Pedro> +	       later, just because that's what GDB used to do.  After
> 
> I think this should say "We choose the latter".

Curious.  I actually meant the past as written, since it's a choice
that is already made.  But I can change it if it reads more naturally
to a native speaker.  Or I could say "We've chosen".  Let me know.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 02/24] Don't rely on inferior_ptid in record_full_wait
  2019-11-01 14:54   ` Tom Tromey
@ 2019-12-20 17:49     ` Pedro Alves
  2019-12-20 18:57       ` Tom Tromey
  0 siblings, 1 reply; 73+ messages in thread
From: Pedro Alves @ 2019-12-20 17:49 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

On 11/1/19 2:54 PM, Tom Tromey wrote:
>>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:
> 
> Pedro> The multi-target patch sets inferior_ptid to null_ptid before handling
> Pedro> a target event, and thus before calling target_wait, in order to catch
> Pedro> places in target_ops::wait implementations that are incorrectly
> Pedro> relying on inferior_ptid (which could otherwise be a ptid of a
> Pedro> different target, for example).
> 
> I think it would be good to add a comment before target_ops::wait
> explaining what is required from its implementation.  If other
> target_ops methods also cannot rely on inferior_ptid, then that
> documentation should be updated as well.  This would make it simpler to
> know how to update an existing target, or to write a new target.

How does this sound?

diff --git i/gdb/target.h w/gdb/target.h
index 1bb7276673..14a7d3e61f 100644
--- i/gdb/target.h
+++ w/gdb/target.h
@@ -478,6 +478,13 @@ struct target_ops
       TARGET_DEFAULT_NORETURN (noprocess ());
     virtual void commit_resume ()
       TARGET_DEFAULT_IGNORE ();
+    /* See target_wait's description.  Note that implementations of
+       this method must not assume that inferior_ptid on entry is
+       pointing at the thread or inferior that ends up reporting an
+       event.  The reported event could be for some other thread in
+       the current inferior or even for a different process of the
+       current target.  inferior_ptid may also be null_ptid on
+       entry.  */
     virtual ptid_t wait (ptid_t, struct target_waitstatus *,
                         int TARGET_DEBUG_PRINTER (target_debug_print_options))
       TARGET_DEFAULT_FUNC (default_target_wait);


Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 08/24] Introduce switch_to_inferior_no_thread
  2019-11-07  9:14   ` Paunovic, Aleksandar
@ 2019-12-20 18:50     ` Pedro Alves
  2019-12-23 19:30       ` [PATCH] Switch the inferior too in switch_to_program_space_and_thread (Re: [PATCH v2 08/24] Introduce switch_to_inferior_no_thread) Pedro Alves
  0 siblings, 1 reply; 73+ messages in thread
From: Pedro Alves @ 2019-12-20 18:50 UTC (permalink / raw)
  To: Paunovic, Aleksandar; +Cc: gdb-patches

On 11/7/19 9:14 AM, Paunovic, Aleksandar wrote:
> Shouldn't there be this change as well:
> 
> 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.

Oh wow, thanks much for this.  You're right.  I'm working on
converting your example above to a testsuite testcase.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 01/24] Preserve selected thread in all-stop w/ background execution
  2019-12-20 17:22     ` Pedro Alves
@ 2019-12-20 18:54       ` Tom Tromey
  2019-12-20 18:57         ` Pedro Alves
  0 siblings, 1 reply; 73+ messages in thread
From: Tom Tromey @ 2019-12-20 18:54 UTC (permalink / raw)
  To: Pedro Alves; +Cc: Tom Tromey, gdb-patches

>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:

Pedro> On 11/1/19 1:20 PM, Tom Tromey wrote:
>>>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:
>> 
Pedro> +	       previously selected thread (now exited).  We chose the
Pedro> +	       later, just because that's what GDB used to do.  After
>> 
>> I think this should say "We choose the latter".

Pedro> Curious.  I actually meant the past as written, since it's a choice
Pedro> that is already made.  But I can change it if it reads more naturally
Pedro> to a native speaker.  Or I could say "We've chosen".  Let me know.

Can some word be added to make it clear that "later" is intended?
The current text really read as a typo.

Tom

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 01/24] Preserve selected thread in all-stop w/ background execution
  2019-12-20 18:57         ` Pedro Alves
@ 2019-12-20 18:57           ` Tom Tromey
  0 siblings, 0 replies; 73+ messages in thread
From: Tom Tromey @ 2019-12-20 18:57 UTC (permalink / raw)
  To: Pedro Alves; +Cc: Tom Tromey, gdb-patches

>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:

Pedro> Oh.  Woot.  We're talking past each other.  I did not notice the
Pedro> "later" vs "latter" typo until now.  I thought you were pointing
Pedro> strictly at "chose" vs "choose".  The original intention was to say:

Pedro>   "We chose the latter"

Pedro> Is that OK?

Yep!  Thanks :)

Tom

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 02/24] Don't rely on inferior_ptid in record_full_wait
  2019-12-20 17:49     ` Pedro Alves
@ 2019-12-20 18:57       ` Tom Tromey
  0 siblings, 0 replies; 73+ messages in thread
From: Tom Tromey @ 2019-12-20 18:57 UTC (permalink / raw)
  To: Pedro Alves; +Cc: Tom Tromey, gdb-patches

>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:

Pedro> How does this sound?

Pedro> diff --git i/gdb/target.h w/gdb/target.h
Pedro> index 1bb7276673..14a7d3e61f 100644
Pedro> --- i/gdb/target.h
Pedro> +++ w/gdb/target.h
Pedro> @@ -478,6 +478,13 @@ struct target_ops
Pedro>        TARGET_DEFAULT_NORETURN (noprocess ());
Pedro>      virtual void commit_resume ()
Pedro>        TARGET_DEFAULT_IGNORE ();
Pedro> +    /* See target_wait's description.  Note that implementations of
Pedro> +       this method must not assume that inferior_ptid on entry is
Pedro> +       pointing at the thread or inferior that ends up reporting an
Pedro> +       event.  The reported event could be for some other thread in
Pedro> +       the current inferior or even for a different process of the
Pedro> +       current target.  inferior_ptid may also be null_ptid on
Pedro> +       entry.  */
Pedro>      virtual ptid_t wait (ptid_t, struct target_waitstatus *,
Pedro>                          int TARGET_DEBUG_PRINTER (target_debug_print_options))
Pedro>        TARGET_DEFAULT_FUNC (default_target_wait);

Looks good, thanks.

Tom

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 01/24] Preserve selected thread in all-stop w/ background execution
  2019-12-20 18:54       ` Tom Tromey
@ 2019-12-20 18:57         ` Pedro Alves
  2019-12-20 18:57           ` Tom Tromey
  0 siblings, 1 reply; 73+ messages in thread
From: Pedro Alves @ 2019-12-20 18:57 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

On 12/20/19 6:53 PM, Tom Tromey wrote:
>>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:
> 
> Pedro> On 11/1/19 1:20 PM, Tom Tromey wrote:
>>>>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:
>>>
> Pedro> +	       previously selected thread (now exited).  We chose the
> Pedro> +	       later, just because that's what GDB used to do.  After
>>>
>>> I think this should say "We choose the latter".
> 
> Pedro> Curious.  I actually meant the past as written, since it's a choice
> Pedro> that is already made.  But I can change it if it reads more naturally
> Pedro> to a native speaker.  Or I could say "We've chosen".  Let me know.
> 
> Can some word be added to make it clear that "later" is intended?
> The current text really read as a typo.
Oh.  Woot.  We're talking past each other.  I did not notice the
"later" vs "latter" typo until now.  I thought you were pointing
strictly at "chose" vs "choose".  The original intention was to say:

  "We chose the latter"

Is that OK?

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [PATCH] Switch the inferior too in switch_to_program_space_and_thread (Re: [PATCH v2 08/24] Introduce switch_to_inferior_no_thread)
  2019-12-20 18:50     ` Pedro Alves
@ 2019-12-23 19:30       ` Pedro Alves
  2020-01-08 15:48         ` Aktemur, Tankut Baris
  0 siblings, 1 reply; 73+ messages in thread
From: Pedro Alves @ 2019-12-23 19:30 UTC (permalink / raw)
  To: Paunovic, Aleksandar; +Cc: gdb-patches

On 12/20/19 6:50 PM, Pedro Alves wrote:

> Oh wow, thanks much for this.  You're right.  I'm working on
> converting your example above to a testsuite testcase.

Here it is.  I'm putting this at the end of the series, after the
multi-target patches, because before multi-target, the test
fails -- GDB sends a spurious Hg0.0 packet to the remote side.

From 839bb32983d8afb085eeec1ac6ca499f5bbd6244 Mon Sep 17 00:00:00 2001
From: Aleksandar Paunovic <aleksandar.paunovic@intel.com>
Date: Mon, 23 Dec 2019 18:04:32 +0000
Subject: [PATCH] Switch the inferior too in switch_to_program_space_and_thread

With multi-target, each inferior now has its own target connection.
The problem in switch_to_program_space_and_thread is that in the
current state GDB switches to "no thread" and also sets the program
space but because the inferior is not switched, potentially an
incorrect target remains selected.

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.

With this fix in progspace-and-thread.c, we'll get 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 gdbserver.

gdb/ChangeLog:
yyyy-mm-dd  Aleksandar Paunovic  <aleksandar.paunovic@intel.com>
	    Pedro Alves  <palves@redhat.com>

	* progspace-and-thread.c (switch_to_program_space_and_thread):
	Assert there's an inferior for PSPACE.  Use
	switch_to_inferior_no_thread to switch the inferior too.

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

	* gdb.server/bkpt-other-inferior.exp: New file.
---
 gdb/progspace-and-thread.c                       |  6 +-
 gdb/testsuite/gdb.server/bkpt-other-inferior.exp | 93 ++++++++++++++++++++++++
 2 files changed, 96 insertions(+), 3 deletions(-)
 create mode 100644 gdb/testsuite/gdb.server/bkpt-other-inferior.exp

diff --git a/gdb/progspace-and-thread.c b/gdb/progspace-and-thread.c
index 3c92b5c8e0..d51748035e 100644
--- a/gdb/progspace-and-thread.c
+++ b/gdb/progspace-and-thread.c
@@ -25,8 +25,9 @@ void
 switch_to_program_space_and_thread (program_space *pspace)
 {
   inferior *inf = find_inferior_for_program_space (pspace);
+  gdb_assert (inf != nullptr);
 
-  if (inf != NULL && inf->pid != 0)
+  if (inf->pid != 0)
     {
       thread_info *tp = any_live_thread_of_inferior (inf);
 
@@ -39,6 +40,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);
 }
diff --git a/gdb/testsuite/gdb.server/bkpt-other-inferior.exp b/gdb/testsuite/gdb.server/bkpt-other-inferior.exp
new file mode 100644
index 0000000000..e2a7be5cdb
--- /dev/null
+++ b/gdb/testsuite/gdb.server/bkpt-other-inferior.exp
@@ -0,0 +1,93 @@
+# Copyright 2019 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test that GDB does not access the remote target's memory when
+# setting a breakpoint on a function that only exists in an inferior
+# that is not bound to the remote target.
+
+load_lib gdbserver-support.exp
+
+standard_testfile server.c
+
+if { [skip_gdbserver_tests] } {
+    return 0
+}
+
+if { [prepare_for_testing "failed to prepare" ${binfile} "${srcfile}" \
+	  {debug pthreads}] } {
+    return
+}
+
+# Make sure we're disconnected, in case we're testing with an
+# extended-remote board, therefore already connected.
+gdb_test "disconnect" ".*"
+
+# Leave inferior 1 with the exec target, not connected.  Add another
+# inferior, and connect it to gdbserver.
+
+gdb_test "add-inferior" "Added inferior 2" \
+    "add inferior 2"
+gdb_test "inferior 2" "Switching to inferior 2.*" \
+    "switch to inferior 2"
+gdb_test "file ${binfile}" ".*" "load file in inferior 2"
+
+set target_exec [gdbserver_download_current_prog]
+
+# Start GDBserver.
+set res [gdbserver_start "" $target_exec]
+
+# Connect to GDBserver.
+set gdbserver_protocol [lindex $res 0]
+set gdbserver_gdbport [lindex $res 1]
+gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport
+
+# Discard any symbol files that we have opened.
+set test "discard symbol table"
+gdb_test_multiple "file" $test {
+    -re "A program is being debugged already..*Are you sure you want to change the file.*y or n. $" {
+	gdb_test "y" ".*" $test \
+	    {Discard symbol table from `.*'\? \(y or n\) } "y"
+    }
+}
+
+# At this point:
+#
+# - inferior 1 has symbols, and is not connected to any target.
+# - inferior 2 has no symbols, and is connected to gdbserver.
+
+# Setting a breakpoint at some function by name should set a
+# breakpoint on inferior 1, since it has symbols, and should not
+# result in any access to inferior 2's remote target.
+
+gdb_test_no_output "set debug remote 1"
+
+foreach inf_sel {1 2} {
+    with_test_prefix "inf $inf_sel" {
+	gdb_test "inferior $inf_sel" "Switching to inferior $inf_sel.*" \
+	    "switch to inferior"
+
+	set test "set breakpoint"
+	gdb_test_multiple "break main" $test {
+	    -re "Sending packet.*$gdb_prompt $" {
+		fail $test
+	    }
+	    -re "^break main\r\nBreakpoint .* at .*$gdb_prompt $" {
+		pass $test
+	    }
+	}
+
+	delete_breakpoints
+    }
+}

-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 23/24] Require always-non-stop for multi-target resumptions
  2019-11-01 14:51   ` Tom Tromey
@ 2019-12-30 18:30     ` Pedro Alves
  2019-12-31 20:06       ` Tom Tromey
  0 siblings, 1 reply; 73+ messages in thread
From: Pedro Alves @ 2019-12-30 18:30 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

Hello,

On 11/1/19 2:51 PM, Tom Tromey wrote:
>>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:
> 
> Pedro> Currently, we can only support resuming multiple targets at the same
> Pedro> time if all targets are in non-stop mode (or user-visible all-stop
> Pedro> mode with target backend in non-stop mode).
> 
> I don't understand this, but I would like to.
> 
> I would have thought that target-async would be enough here.  As in, the
> necessary part is that target events be integrated with the event loop,
> so that multiple event sources can be selected on at once.
> 
> Why is non-stop needed instead?

The main reason is the remote target -- in all-stop mode, the remote protocol
is synchronous, irrespective of target-async.  I.e., as soon as
you resume the remote target in all-stop (with e.g., vCont), you can't send any
packet to the remote until it reports a stop.  The non-stop variant of the protocol
makes vCont be asynchronous instead.  A vCont results in an immediate "OK" reply,
and then stops are reported via an asynchronous mechanism (%Stop).  So if the
remote backend is in non-stop mode, we can talk to it even if some thread
is running.  This makes things simpler for the common code.  For example, breakpoint
re-set resets every breakpoint and location, and that results in reading memory
from all targets.  If a remote all-stop target is running, then that memory read
will fail, because we can't talk to the remote all-stop target until it fully
stops.  See <https://sourceware.org/ml/gdb-patches/2019-10/msg01052.html>.
Another place that needs more work is where we call stop_all_threads, which
needs to be taught to only explicitly pause all threads of a target one
by one if the target is in non-stop mode.  I ran into things like these
when I last tried to make it work, which made me punt until the
main chunks of multi-target are in.

How about this alternative comment?

+/* Check that all the targets we're about to resume are in non-stop
+   mode.  Ideally, we'd only care whether all targets support
+   target-async, but we're not there yet.  E.g., stop_all_threads
+   doesn't know how to handle all-stop targets.  Also, the remote
+   protocol in all-stop mode is synchronous, irrespective of
+   target-async, which means that things like a breakpoint re-set
+   triggered by one target would try to read memory from all targets
+   and fail.  */
+
+static void
+check_multi_target_resumption (process_stratum_target *resume_target)


From a33b584acec4af5d11dc35ec78afed0bf25742ff Mon Sep 17 00:00:00 2001
From: Pedro Alves <palves@redhat.com>
Date: Wed, 30 Oct 2019 16:51:04 +0000
Subject: [PATCH] Require always-non-stop for multi-target resumptions

Currently, we can only support resuming multiple targets at the same
time if all targets are in non-stop mode (or user-visible all-stop
mode with target backend in non-stop mode).

This patch makes GDB error out if the user tries to resume more than
one target at the same time and one of the resumed targets isn't in
non-stop mode:

 (gdb) info inferiors
   Num  Description       Connection                Executable
   1    process 15303     1 (native)                a.out
 * 2    process 15286     2 (extended-remote :9999) a.out
 (gdb) set schedule-multiple on
 (gdb) c
 Continuing.
 Connection 2 (extended-remote :9999) does not support multi-target resumption.

This is here later in the series instead of in the main multi-target
patch because it depends the previous patch, which added
process_stratum_target::connection_string().

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

	* infrun.c: Include "target-connection.h".
	(check_multi_target_resumption): New.
	(proceed): Call it.
	* target-connection.c (make_target_connection_string): Make
	static.
	* target-connection.h (make_target_connection_string): Declare.
---
 gdb/infrun.c            | 58 +++++++++++++++++++++++++++++++++++++++++++++++++
 gdb/target-connection.c |  7 ++----
 gdb/target-connection.h |  8 +++++++
 3 files changed, 68 insertions(+), 5 deletions(-)

diff --git a/gdb/infrun.c b/gdb/infrun.c
index 688f9e029a..ce2de256df 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -28,6 +28,7 @@
 #include "gdbcore.h"
 #include "gdbcmd.h"
 #include "target.h"
+#include "target-connection.h"
 #include "gdbthread.h"
 #include "annotate.h"
 #include "symfile.h"
@@ -2881,6 +2882,61 @@ commit_resume_all_targets ()
     }
 }
 
+/* Check that all the targets we're about to resume are in non-stop
+   mode.  Ideally, we'd only care whether all targets support
+   target-async, but we're not there yet.  E.g., stop_all_threads
+   doesn't know how to handle all-stop targets.  Also, the remote
+   protocol in all-stop mode is synchronous, irrespective of
+   target-async, which means that things like a breakpoint re-set
+   triggered by one target would try to read memory from all targets
+   and fail.  */
+
+static void
+check_multi_target_resumption (process_stratum_target *resume_target)
+{
+  if (!non_stop && resume_target == nullptr)
+    {
+      scoped_restore_current_thread restore_thread;
+
+      /* This is used to track whether we're resuming more than one
+	 target.  */
+      process_stratum_target *first_connection = nullptr;
+
+      /* The first inferior we see with a target that does not work in
+	 always-non-stop mode.  */
+      inferior *first_not_non_stop = nullptr;
+
+      for (inferior *inf : all_non_exited_inferiors (resume_target))
+	{
+	  switch_to_inferior_no_thread (inf);
+
+	  if (!target_has_execution)
+	    continue;
+
+	  process_stratum_target *proc_target
+	    = current_inferior ()->process_target();
+
+	  if (!target_is_non_stop_p ())
+	    first_not_non_stop = inf;
+
+	  if (first_connection == nullptr)
+	    first_connection = proc_target;
+	  else if (first_connection != proc_target
+		   && first_not_non_stop != nullptr)
+	    {
+	      switch_to_inferior_no_thread (first_not_non_stop);
+
+	      proc_target = current_inferior ()->process_target();
+
+	      error (_("Connection %d (%s) does not support "
+		       "multi-target resumption."),
+		     proc_target->connection_number,
+		     make_target_connection_string (proc_target).c_str ());
+	    }
+	}
+    }
+}
+
 /* Basic routine for continuing the program in various fashions.
 
    ADDR is the address to resume at, or -1 for resume where stopped.
@@ -2931,6 +2987,8 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
   process_stratum_target *resume_target
     = user_visible_resume_target (resume_ptid);
 
+  check_multi_target_resumption (resume_target);
+
   if (addr == (CORE_ADDR) -1)
     {
       if (pc == cur_thr->suspend.stop_pc
diff --git a/gdb/target-connection.c b/gdb/target-connection.c
index a079f218bd..dd0300a82b 100644
--- a/gdb/target-connection.c
+++ b/gdb/target-connection.c
@@ -53,12 +53,9 @@ connection_list_remove (process_stratum_target *t)
   t->connection_number = 0;
 }
 
-/* Make a target connection string for T.  This is usually T's
-   shortname, but it includes the result of
-   process_stratum_target::connection_string() too if T supports
-   it.  */
+/* See target-connection.h.  */
 
-static std::string
+std::string
 make_target_connection_string (process_stratum_target *t)
 {
   if (t->connection_string () != NULL)
diff --git a/gdb/target-connection.h b/gdb/target-connection.h
index 0e2dc128d8..0b31924b99 100644
--- a/gdb/target-connection.h
+++ b/gdb/target-connection.h
@@ -20,6 +20,8 @@
 #ifndef TARGET_CONNECTION_H
 #define TARGET_CONNECTION_H
 
+#include <string>
+
 struct process_stratum_target;
 
 /* Add a process target to the connection list, if not already
@@ -29,4 +31,10 @@ void connection_list_add (process_stratum_target *t);
 /* Remove a process target from the connection list.  */
 void connection_list_remove (process_stratum_target *t);
 
+/* Make a target connection string for T.  This is usually T's
+   shortname, but it includes the result of
+   process_stratum_target::connection_string() too if T supports
+   it.  */
+std::string make_target_connection_string (process_stratum_target *t);
+
 #endif /* TARGET_CONNECTION_H */

-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 23/24] Require always-non-stop for multi-target resumptions
  2019-12-30 18:30     ` Pedro Alves
@ 2019-12-31 20:06       ` Tom Tromey
  0 siblings, 0 replies; 73+ messages in thread
From: Tom Tromey @ 2019-12-31 20:06 UTC (permalink / raw)
  To: Pedro Alves; +Cc: Tom Tromey, gdb-patches

>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:

Pedro> The main reason is the remote target -- in all-stop mode, the
Pedro> remote protocol is synchronous, irrespective of target-async.
[...]

Thanks for the explanation.

Pedro> How about this alternative comment?

Looks good.

Pedro> 	* target-connection.c (make_target_connection_string): Make
Pedro> 	static.

This removes "static" instead.

thanks,
Tom

^ permalink raw reply	[flat|nested] 73+ messages in thread

* RE: [PATCH] Switch the inferior too in switch_to_program_space_and_thread (Re: [PATCH v2 08/24] Introduce switch_to_inferior_no_thread)
  2019-12-23 19:30       ` [PATCH] Switch the inferior too in switch_to_program_space_and_thread (Re: [PATCH v2 08/24] Introduce switch_to_inferior_no_thread) Pedro Alves
@ 2020-01-08 15:48         ` Aktemur, Tankut Baris
  2020-01-10  2:03           ` Pedro Alves
  0 siblings, 1 reply; 73+ messages in thread
From: Aktemur, Tankut Baris @ 2020-01-08 15:48 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches, Paunovic, Aleksandar

On Monday, December 23, 2019 8:30 PM, Pedro Alves wrote:
> 
> On 12/20/19 6:50 PM, Pedro Alves wrote:
> 
> > Oh wow, thanks much for this.  You're right.  I'm working on
> > converting your example above to a testsuite testcase.
> 
> Here it is.  I'm putting this at the end of the series, after the
> multi-target patches, because before multi-target, the test
> fails -- GDB sends a spurious Hg0.0 packet to the remote side.
> 
> From 839bb32983d8afb085eeec1ac6ca499f5bbd6244 Mon Sep 17 00:00:00 2001
> From: Aleksandar Paunovic <aleksandar.paunovic@intel.com>
> Date: Mon, 23 Dec 2019 18:04:32 +0000
> Subject: [PATCH] Switch the inferior too in switch_to_program_space_and_thread
> 
> With multi-target, each inferior now has its own target connection.
> The problem in switch_to_program_space_and_thread is that in the
> current state GDB switches to "no thread" and also sets the program
> space but because the inferior is not switched, potentially an
> incorrect target remains selected.
> 
> 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.
> 
> With this fix in progspace-and-thread.c, we'll get 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 gdbserver.
> 
> gdb/ChangeLog:
> yyyy-mm-dd  Aleksandar Paunovic  <aleksandar.paunovic@intel.com>
> 	    Pedro Alves  <palves@redhat.com>
> 
> 	* progspace-and-thread.c (switch_to_program_space_and_thread):
> 	Assert there's an inferior for PSPACE.  Use
> 	switch_to_inferior_no_thread to switch the inferior too.
> 
> gdb/testsuite/ChangeLog:
> yyyy-mm-dd  Pedro Alves  <palves@redhat.com>
> 
> 	* gdb.server/bkpt-other-inferior.exp: New file.
> ---
>  gdb/progspace-and-thread.c                       |  6 +-
>  gdb/testsuite/gdb.server/bkpt-other-inferior.exp | 93 ++++++++++++++++++++++++
>  2 files changed, 96 insertions(+), 3 deletions(-)
>  create mode 100644 gdb/testsuite/gdb.server/bkpt-other-inferior.exp
> 
> diff --git a/gdb/progspace-and-thread.c b/gdb/progspace-and-thread.c
> index 3c92b5c8e0..d51748035e 100644
> --- a/gdb/progspace-and-thread.c
> +++ b/gdb/progspace-and-thread.c
> @@ -25,8 +25,9 @@ void
>  switch_to_program_space_and_thread (program_space *pspace)
>  {
>    inferior *inf = find_inferior_for_program_space (pspace);
> +  gdb_assert (inf != nullptr);


This creates failure in gdb.base/step-over-exit.exp.  The problem is, a forked
child terminates, and GDB tries to switch to the pspace of that no-longer-existing
inferior through a remaining breakpoint location.  Would it make sense to guard
calls to `switch_to_program_space_and_thread` as below, for example?

diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 080e847de1..30af603c73 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -2879,6 +2879,9 @@ update_inserted_breakpoint_locations (void)
       if (!bl->inserted || !bl->needs_update)
        continue;

+      if (find_inferior_for_program_space (bl->pspace) == nullptr)
+       continue;
+
       switch_to_program_space_and_thread (bl->pspace);

       /* For targets that support global breakpoints, there's no need

There are a handful other cases that can be guarded/skipped similarly.
I'm not sure, though, if the problem is deeper and the calls to
`switch_to_program_space_and_thread` for a non-existing inferior should have
never occurred.  That is, should the breakpoint locations of exited inferiors
be cleaned up much earlier, before we come to this point?

-Baris

> -  if (inf != NULL && inf->pid != 0)
> +  if (inf->pid != 0)
>      {
>        thread_info *tp = any_live_thread_of_inferior (inf);
> 
> @@ -39,6 +40,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);
>  }
Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Gary Kershaw
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 00/24] Multi-target support
  2019-10-29 19:13   ` Pedro Alves
@ 2020-01-09 19:32     ` John Baldwin
  2020-01-09 19:50       ` Pedro Alves
  0 siblings, 1 reply; 73+ messages in thread
From: John Baldwin @ 2020-01-09 19:32 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On 10/29/19 12:13 PM, Pedro Alves wrote:
> On 10/18/19 9:22 PM, John Baldwin wrote:
>> On 10/17/19 3:50 PM, Pedro Alves wrote:
>>> Here's v2 of the multi-target patchset, which addresses all the review
>>> comments so far, I believe.  Patch 15 is new, so all following patches
>>> are shifted by one.
>>>
>>> This time, I've adjusted the host-specific nat files to function API
>>> changes.  I tried to find spots that would need changes using grep.  I
>>> built the series on AIX, x86/SPARC Solaris, 64-bit Windows, x86
>>> GNU/Linux -m64/-m32, and Aarch64 GNU/Linux.  I'm currently running
>>> this through the buildbot, will have results tomorrow.  I don't expect
>>> any serious major issue, if any, as so far runs that completed seem
>>> OK.
>>
>> I (finally) have a patch to fix the build on FreeBSD/amd64 (and probably
>> FreeBSD on other platforms) here:
>>
>> https://github.com/bsdjhb/gdb/commit/e58a36eaef6244d2040ce6f377497ee898978db4
>>
> 
> Thanks!
> 
>> It's a combined patch but has some commentary on bsd-kvm.c which is
>> kind of special.  That target adds new commands that need to find a
>> target to operate on.  I opted to have it look at the current inferior
>> and if (using a dynamic cast) it is a bsd-kvm target the commands
>> modify the state of that inferior.  I haven't tested it though as I don't
>> use bsd-kvm.c.
> 
> I'd rather not merge the bsd-kvm.c parts into my patches as is, for the
> reason that it doesn't appear necessary for a minimal keep-working-as-before
> change.  I think it would be fine as a follow up patch, though.

Ok, that's fine.

> For bsd-kvm.c, I _think_ that the only change necessary to keep things
> building would be this:
> 
> --- a/gdb/bsd-kvm.c
> +++ b/gdb/bsd-kvm.c
> @@ -136,7 +136,7 @@ bsd_kvm_target_open (const char *arg, int from_tty)
>    core_kd = temp_kd;
>    push_target (&bsd_kvm_ops);
>  
> -  add_thread_silent (bsd_kvm_ptid);
> +  add_thread_silent (&bsd_kvm_ops, bsd_kvm_ptid);
>    inferior_ptid = bsd_kvm_ptid;
> 
> Right?  See updated patch below.

Ok, this works for me.

-- 
John Baldwin

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 00/24] Multi-target support
  2020-01-09 19:32     ` John Baldwin
@ 2020-01-09 19:50       ` Pedro Alves
  2020-01-10 13:49         ` Aktemur, Tankut Baris
  0 siblings, 1 reply; 73+ messages in thread
From: Pedro Alves @ 2020-01-09 19:50 UTC (permalink / raw)
  To: John Baldwin, gdb-patches

On 1/9/20 7:31 PM, John Baldwin wrote:
> On 10/29/19 12:13 PM, Pedro Alves wrote:

>> For bsd-kvm.c, I _think_ that the only change necessary to keep things
>> building would be this:
>>
>> --- a/gdb/bsd-kvm.c
>> +++ b/gdb/bsd-kvm.c
>> @@ -136,7 +136,7 @@ bsd_kvm_target_open (const char *arg, int from_tty)
>>    core_kd = temp_kd;
>>    push_target (&bsd_kvm_ops);
>>  
>> -  add_thread_silent (bsd_kvm_ptid);
>> +  add_thread_silent (&bsd_kvm_ops, bsd_kvm_ptid);
>>    inferior_ptid = bsd_kvm_ptid;
>>
>> Right?  See updated patch below.
> 
> Ok, this works for me.

Excellent!  I think I'll be merging this soon, just need to
address Aktemur's latest comments.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH] Switch the inferior too in switch_to_program_space_and_thread (Re: [PATCH v2 08/24] Introduce switch_to_inferior_no_thread)
  2020-01-08 15:48         ` Aktemur, Tankut Baris
@ 2020-01-10  2:03           ` Pedro Alves
  2020-01-10 11:33             ` Aktemur, Tankut Baris
  2020-01-10 14:41             ` Tom Tromey
  0 siblings, 2 replies; 73+ messages in thread
From: Pedro Alves @ 2020-01-10  2:03 UTC (permalink / raw)
  To: Aktemur, Tankut Baris; +Cc: gdb-patches, Paunovic, Aleksandar

On 1/8/20 3:48 PM, Aktemur, Tankut Baris wrote:
> On Monday, December 23, 2019 8:30 PM, Pedro Alves wrote:
>>
>> On 12/20/19 6:50 PM, Pedro Alves wrote:
>>
>>> Oh wow, thanks much for this.  You're right.  I'm working on
>>> converting your example above to a testsuite testcase.
>>
>> Here it is.  I'm putting this at the end of the series, after the
>> multi-target patches, because before multi-target, the test
>> fails -- GDB sends a spurious Hg0.0 packet to the remote side.
>>
>> From 839bb32983d8afb085eeec1ac6ca499f5bbd6244 Mon Sep 17 00:00:00 2001
>> From: Aleksandar Paunovic <aleksandar.paunovic@intel.com>
>> Date: Mon, 23 Dec 2019 18:04:32 +0000
>> Subject: [PATCH] Switch the inferior too in switch_to_program_space_and_thread
>>
>> With multi-target, each inferior now has its own target connection.
>> The problem in switch_to_program_space_and_thread is that in the
>> current state GDB switches to "no thread" and also sets the program
>> space but because the inferior is not switched, potentially an
>> incorrect target remains selected.
>>
>> 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.
>>
>> With this fix in progspace-and-thread.c, we'll get 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 gdbserver.
>>
>> gdb/ChangeLog:
>> yyyy-mm-dd  Aleksandar Paunovic  <aleksandar.paunovic@intel.com>
>> 	    Pedro Alves  <palves@redhat.com>
>>
>> 	* progspace-and-thread.c (switch_to_program_space_and_thread):
>> 	Assert there's an inferior for PSPACE.  Use
>> 	switch_to_inferior_no_thread to switch the inferior too.
>>
>> gdb/testsuite/ChangeLog:
>> yyyy-mm-dd  Pedro Alves  <palves@redhat.com>
>>
>> 	* gdb.server/bkpt-other-inferior.exp: New file.
>> ---
>>  gdb/progspace-and-thread.c                       |  6 +-
>>  gdb/testsuite/gdb.server/bkpt-other-inferior.exp | 93 ++++++++++++++++++++++++
>>  2 files changed, 96 insertions(+), 3 deletions(-)
>>  create mode 100644 gdb/testsuite/gdb.server/bkpt-other-inferior.exp
>>
>> diff --git a/gdb/progspace-and-thread.c b/gdb/progspace-and-thread.c
>> index 3c92b5c8e0..d51748035e 100644
>> --- a/gdb/progspace-and-thread.c
>> +++ b/gdb/progspace-and-thread.c
>> @@ -25,8 +25,9 @@ void
>>  switch_to_program_space_and_thread (program_space *pspace)
>>  {
>>    inferior *inf = find_inferior_for_program_space (pspace);
>> +  gdb_assert (inf != nullptr);
> 
> 
> This creates failure in gdb.base/step-over-exit.exp.  

Thanks for spotting this!

> The problem is, a forked
> child terminates, and GDB tries to switch to the pspace of that no-longer-existing
> inferior through a remaining breakpoint location.  Would it make sense to guard
> calls to `switch_to_program_space_and_thread` as below, for example?

Yeah, I don't think so.  That feels like papering over a problem.

> 
> There are a handful other cases that can be guarded/skipped similarly.
> I'm not sure, though, if the problem is deeper and the calls to
> `switch_to_program_space_and_thread` for a non-existing inferior should have
> never occurred.  That is, should the breakpoint locations of exited inferiors
> be cleaned up much earlier, before we come to this point?

Yes.  Thing is, they have!  But then they're recreated...

Here's an updated patch that fixes that latent problem.  See the
newly expanded description therein.

From c8a096b604c41f37a91a595392a0a5c3266ec09f Mon Sep 17 00:00:00 2001
From: Pedro Alves <palves@redhat.com>
Date: Fri, 10 Jan 2020 01:04:37 +0000
Subject: [PATCH] Switch the inferior too in switch_to_program_space_and_thread

With multi-target, each inferior now has its own target connection.
The problem in switch_to_program_space_and_thread is that in the
current state GDB switches to "no thread" and also sets the program
space but because the inferior is not switched, potentially an
incorrect target remains selected.

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.

With this fix in progspace-and-thread.c, we'll get 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 gdbserver.

The changes around clear_symtab_users calls are necessary because
otherwise we regress gdb.base/step-over-exit.exp, hitting the new
assertion in switch_to_program_space_and_thread.  The problem is, a
forked child terminates, and when GDB decides to auto-purge that
inferior, GDB tries to switch to the pspace of that no-longer-existing
inferior.

The root of the problem is within the program_space destructor:

program_space::~program_space ()
{
...
  set_current_program_space (this);        # (1)
...
  breakpoint_program_space_exit (this);    # (2)
...
  free_all_objfiles ();                    # (3)
...
}

We get here from delete_inferior -> delete_program_space.

So we're deleting an inferior, and the inferior to be
deleted is no longer in the inferior list.

At (2), we've deleted all the breakpoints and locations for the
program space being deleted.

The crash happens while doing a breakpoint re-set, called by
clear_symtab_users at the tail end of (3).  That is, while recreating
breakpoints for the current program space, which is the program space
we're tearing down.  During breakpoint re-set, we try to switch to the
new location's pspace (the current pspace set in (1), so the pspace
we're tearing down) with switch_to_program_space_and_thread, and that
hits the failed assertion.  It's the fact that we recreate breakpoints
in the program_space destructor that is the latent bug here.  Just
don't do that, since we've already taken care of breakpoints with
breakpoint_program_space_exit, and we don't end up in the crash situation.

My first approach to fix this added a symfile_add_flags parameter to
program_space::free_all_objfiles, and then passed that down to
clear_symtab_users.  The program_space dtor would then pass down
SYMFILE_DEFER_BP_RESET to free_all_objfiles.  I couldn't help feeling
that adding that parameter to free_all_objfiles looked a little
awkward, so I settled on something a little different -- hoist the
clear_symtab_users call to the callers.  There are only two callers.
I felt that that didn't look as odd, particularly since
remove_symbol_file_command also does:

  objf->unlink ();
  clear_symtab_users (0);

I.e., objfile deletion is already separate from calling
clear_symtab_users in some places.

I'm not hard set on this approach though; I can go with adding the
symfile_add_flags parameter to program_space::free_all_objfiles if
people prefer that.

gdb/ChangeLog:
yyyy-mm-dd  Aleksandar Paunovic  <aleksandar.paunovic@intel.com>
	    Pedro Alves  <palves@redhat.com>

	* progspace-and-thread.c (switch_to_program_space_and_thread):
	Assert there's an inferior for PSPACE.  Use
	switch_to_inferior_no_thread to switch the inferior too.
	* progspace.c (program_space::~program_space): Call
	clear_symtab_users here, with SYMFILE_DEFER_BP_RESET.
	(program_space::free_all_objfiles): Don't call clear_symtab_users
	here.
	* symfile.c (symbol_file_clear): Call clear_symtab_users here.

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

	* gdb.server/bkpt-other-inferior.exp: New file.
---
 gdb/progspace-and-thread.c                       |  6 +-
 gdb/progspace.c                                  |  5 +-
 gdb/symfile.c                                    |  2 +
 gdb/testsuite/gdb.server/bkpt-other-inferior.exp | 93 ++++++++++++++++++++++++
 4 files changed, 101 insertions(+), 5 deletions(-)
 create mode 100644 gdb/testsuite/gdb.server/bkpt-other-inferior.exp

diff --git a/gdb/progspace-and-thread.c b/gdb/progspace-and-thread.c
index a1824e8935..698661d43f 100644
--- a/gdb/progspace-and-thread.c
+++ b/gdb/progspace-and-thread.c
@@ -25,8 +25,9 @@ void
 switch_to_program_space_and_thread (program_space *pspace)
 {
   inferior *inf = find_inferior_for_program_space (pspace);
+  gdb_assert (inf != nullptr);
 
-  if (inf != NULL && inf->pid != 0)
+  if (inf->pid != 0)
     {
       thread_info *tp = any_live_thread_of_inferior (inf);
 
@@ -39,6 +40,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);
 }
diff --git a/gdb/progspace.c b/gdb/progspace.c
index 96f8acc64c..1361040347 100644
--- a/gdb/progspace.c
+++ b/gdb/progspace.c
@@ -147,6 +147,9 @@ program_space::~program_space ()
   no_shared_libraries (NULL, 0);
   exec_close ();
   free_all_objfiles ();
+  /* Defer breakpoint re-set because we don't want to create new
+     locations for this pspace which we're tearing down.  */
+  clear_symtab_users (SYMFILE_DEFER_BP_RESET);
   if (!gdbarch_has_shared_address_space (target_gdbarch ()))
     free_address_space (this->aspace);
   clear_section_table (&this->target_sections);
@@ -168,8 +171,6 @@ program_space::free_all_objfiles ()
 
   while (!objfiles_list.empty ())
     objfiles_list.front ()->unlink ();
-
-  clear_symtab_users (0);
 }
 
 /* See progspace.h.  */
diff --git a/gdb/symfile.c b/gdb/symfile.c
index 86d6174107..d5f70d7f03 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -1253,6 +1253,8 @@ symbol_file_clear (int from_tty)
 
   current_program_space->free_all_objfiles ();
 
+  clear_symtab_users (0);
+
   gdb_assert (symfile_objfile == NULL);
   if (from_tty)
     printf_filtered (_("No symbol file now.\n"));
diff --git a/gdb/testsuite/gdb.server/bkpt-other-inferior.exp b/gdb/testsuite/gdb.server/bkpt-other-inferior.exp
new file mode 100644
index 0000000000..e2a7be5cdb
--- /dev/null
+++ b/gdb/testsuite/gdb.server/bkpt-other-inferior.exp
@@ -0,0 +1,93 @@
+# Copyright 2019 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test that GDB does not access the remote target's memory when
+# setting a breakpoint on a function that only exists in an inferior
+# that is not bound to the remote target.
+
+load_lib gdbserver-support.exp
+
+standard_testfile server.c
+
+if { [skip_gdbserver_tests] } {
+    return 0
+}
+
+if { [prepare_for_testing "failed to prepare" ${binfile} "${srcfile}" \
+	  {debug pthreads}] } {
+    return
+}
+
+# Make sure we're disconnected, in case we're testing with an
+# extended-remote board, therefore already connected.
+gdb_test "disconnect" ".*"
+
+# Leave inferior 1 with the exec target, not connected.  Add another
+# inferior, and connect it to gdbserver.
+
+gdb_test "add-inferior" "Added inferior 2" \
+    "add inferior 2"
+gdb_test "inferior 2" "Switching to inferior 2.*" \
+    "switch to inferior 2"
+gdb_test "file ${binfile}" ".*" "load file in inferior 2"
+
+set target_exec [gdbserver_download_current_prog]
+
+# Start GDBserver.
+set res [gdbserver_start "" $target_exec]
+
+# Connect to GDBserver.
+set gdbserver_protocol [lindex $res 0]
+set gdbserver_gdbport [lindex $res 1]
+gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport
+
+# Discard any symbol files that we have opened.
+set test "discard symbol table"
+gdb_test_multiple "file" $test {
+    -re "A program is being debugged already..*Are you sure you want to change the file.*y or n. $" {
+	gdb_test "y" ".*" $test \
+	    {Discard symbol table from `.*'\? \(y or n\) } "y"
+    }
+}
+
+# At this point:
+#
+# - inferior 1 has symbols, and is not connected to any target.
+# - inferior 2 has no symbols, and is connected to gdbserver.
+
+# Setting a breakpoint at some function by name should set a
+# breakpoint on inferior 1, since it has symbols, and should not
+# result in any access to inferior 2's remote target.
+
+gdb_test_no_output "set debug remote 1"
+
+foreach inf_sel {1 2} {
+    with_test_prefix "inf $inf_sel" {
+	gdb_test "inferior $inf_sel" "Switching to inferior $inf_sel.*" \
+	    "switch to inferior"
+
+	set test "set breakpoint"
+	gdb_test_multiple "break main" $test {
+	    -re "Sending packet.*$gdb_prompt $" {
+		fail $test
+	    }
+	    -re "^break main\r\nBreakpoint .* at .*$gdb_prompt $" {
+		pass $test
+	    }
+	}
+
+	delete_breakpoints
+    }
+}

-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* RE: [PATCH] Switch the inferior too in switch_to_program_space_and_thread (Re: [PATCH v2 08/24] Introduce switch_to_inferior_no_thread)
  2020-01-10  2:03           ` Pedro Alves
@ 2020-01-10 11:33             ` Aktemur, Tankut Baris
  2020-01-10 12:18               ` Pedro Alves
  2020-01-10 14:41             ` Tom Tromey
  1 sibling, 1 reply; 73+ messages in thread
From: Aktemur, Tankut Baris @ 2020-01-10 11:33 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches, Paunovic, Aleksandar

On Friday, January 10, 2020 3:03 AM, Pedro Alves wrote:
> 
> The changes around clear_symtab_users calls are necessary because
> otherwise we regress gdb.base/step-over-exit.exp, hitting the new
> assertion in switch_to_program_space_and_thread.  The problem is, a
> forked child terminates, and when GDB decides to auto-purge that
> inferior, GDB tries to switch to the pspace of that no-longer-existing
> inferior.
> 
> The root of the problem is within the program_space destructor:
> 
> program_space::~program_space ()
> {
> ...
>   set_current_program_space (this);        # (1)
> ...
>   breakpoint_program_space_exit (this);    # (2)
> ...
>   free_all_objfiles ();                    # (3)
> ...
> }
> 
> We get here from delete_inferior -> delete_program_space.
> 
> So we're deleting an inferior, and the inferior to be
> deleted is no longer in the inferior list.
> 
> At (2), we've deleted all the breakpoints and locations for the
> program space being deleted.
> 
> The crash happens while doing a breakpoint re-set, called by
> clear_symtab_users at the tail end of (3).  That is, while recreating
> breakpoints for the current program space, which is the program space
> we're tearing down.  During breakpoint re-set, we try to switch to the
> new location's pspace (the current pspace set in (1), so the pspace
> we're tearing down) with switch_to_program_space_and_thread, and that
> hits the failed assertion.  It's the fact that we recreate breakpoints
> in the program_space destructor that is the latent bug here.  Just
> don't do that, since we've already taken care of breakpoints with
> breakpoint_program_space_exit, and we don't end up in the crash situation.
> 
> My first approach to fix this added a symfile_add_flags parameter to
> program_space::free_all_objfiles, and then passed that down to
> clear_symtab_users.  The program_space dtor would then pass down
> SYMFILE_DEFER_BP_RESET to free_all_objfiles.  I couldn't help feeling
> that adding that parameter to free_all_objfiles looked a little
> awkward, so I settled on something a little different -- hoist the
> clear_symtab_users call to the callers.  There are only two callers.
> I felt that that didn't look as odd, particularly since
> remove_symbol_file_command also does:
> 
>   objf->unlink ();
>   clear_symtab_users (0);
> 
> I.e., objfile deletion is already separate from calling
> clear_symtab_users in some places.
> 
> I'm not hard set on this approach though; I can go with adding the
> symfile_add_flags parameter to program_space::free_all_objfiles if
> people prefer that.

I personally think that in particular the comment and the change below
have educational value.  So, I'd vote for the approach you sent.

> diff --git a/gdb/progspace.c b/gdb/progspace.c
> index 96f8acc64c..1361040347 100644
> --- a/gdb/progspace.c
> +++ b/gdb/progspace.c
> @@ -147,6 +147,9 @@ program_space::~program_space ()
>    no_shared_libraries (NULL, 0);
>    exec_close ();
>    free_all_objfiles ();
> +  /* Defer breakpoint re-set because we don't want to create new
> +     locations for this pspace which we're tearing down.  */
> +  clear_symtab_users (SYMFILE_DEFER_BP_RESET);
>    if (!gdbarch_has_shared_address_space (target_gdbarch ()))
>      free_address_space (this->aspace);
>    clear_section_table (&this->target_sections);

As far as I can tell, you rebased the series on the current master, correct?
Could you update the users/palves/multi-target-v2 branch?  The new patch does not
apply there cleanly.  I attempted to replicate it on users/palves/multi-target-v2,
but gdb.base/step-over-exit.exp still hits the assertion.

-Baris

Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Gary Kershaw
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH] Switch the inferior too in switch_to_program_space_and_thread (Re: [PATCH v2 08/24] Introduce switch_to_inferior_no_thread)
  2020-01-10 11:33             ` Aktemur, Tankut Baris
@ 2020-01-10 12:18               ` Pedro Alves
  2020-01-10 13:51                 ` Aktemur, Tankut Baris
  0 siblings, 1 reply; 73+ messages in thread
From: Pedro Alves @ 2020-01-10 12:18 UTC (permalink / raw)
  To: Aktemur, Tankut Baris; +Cc: gdb-patches, Paunovic, Aleksandar

On 1/10/20 11:33 AM, Aktemur, Tankut Baris wrote:

> As far as I can tell, you rebased the series on the current master, correct?
> Could you update the users/palves/multi-target-v2 branch?  The new patch does not
> apply there cleanly.  I attempted to replicate it on users/palves/multi-target-v2,
> but gdb.base/step-over-exit.exp still hits the assertion.
Sorry, should have thought to do that.  Done now.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 73+ messages in thread

* RE: [PATCH v2 00/24] Multi-target support
  2020-01-09 19:50       ` Pedro Alves
@ 2020-01-10 13:49         ` Aktemur, Tankut Baris
  2020-01-10 15:40           ` [PATCH] Switch the inferior before outputting its id in "info inferiors" (Re: [PATCH v2 00/24] Multi-target support) Pedro Alves
  0 siblings, 1 reply; 73+ messages in thread
From: Aktemur, Tankut Baris @ 2020-01-10 13:49 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

On Thursday, January 9, 2020 8:50 PM, Pedro Alves wrote:
> 
> Excellent!  I think I'll be merging this soon, just need to
> address Aktemur's latest comments.
> 
> Thanks,
> Pedro Alves

There is one more minor thing, I think.  The "info inferiors" command
iterates over the list of inferiors and prints their info.  One piece of
information is the inferior id, which is printed through the target.
This means that for each inferior, the target of the current inferior is
used, potentially yielding incorrect output.  To fix, inferior can be switched
temporarily before printing its id.  Please see the patches below for details.
The second one attempts to modify the multi-target.exp test to check correct
output.

Thanks
-Baris

From efa9736da4d5545232fd3c3f8c65e297963d556f Mon Sep 17 00:00:00 2001
From: Tankut Baris Aktemur <tankut.baris.aktemur@intel.com>
Date: Fri, 10 Jan 2020 14:27:55 +0100
Subject: [PATCH 1/2] Switch the inferior before outputting its id in "info
 inferiors"

GDB uses the 'current_top_target' when displaying the description of
an inferior.  This leads to same target being used for each inferior
and, in turn, yields incorrect output when the inferior has a target
that is supposed to give a specialized output.  For instance, the
remote target outputs "Remote target" instead of "process XYZ" as the
description if the multi-process feature is not supported or turned
off.

E.g.: Suppose we have a native and a remote target, and the native is
the current inferior.  The remote target does not support multi-process.
For "info inferiors", we would expect to see:

~~~
(gdb) i inferiors
  Num  Description       Connection       Executable
* 1    process 29060     1 (native)       /a/path
  2    Remote target     2 (remote ...)
~~~

but instead we get

~~~
(gdb) i inferiors
  Num  Description       Connection       Executable
* 1    process 29060     1 (native)       /a/path
  2    process 42000     2 (remote ...)
~~~

Similarly, if the current inferior is the remote one, we would expect
to see

~~~
(gdb) i inferiors
  Num  Description       Connection       Executable
  1    process 29060     1 (native)       /a/path
* 2    Remote target     2 (remote ...)
~~~

but we get

~~~
(gdb) i inferiors
  Num  Description       Connection       Executable
* 1    Remote target     1 (native)       /a/path
  2    Remote target     2 (remote ...)
~~~

With this patch, we switch to the inferior when outputting its
description, so that the current_top_target will be aligned to the
inferior we are displaying.

---
 gdb/inferior.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/gdb/inferior.c b/gdb/inferior.c
index 3ce43860f7..ef91e74e89 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -494,6 +494,11 @@ print_inferior (struct ui_out *uiout, const char *requested_inferiors)
   uiout->table_header (17, ui_left, "exec", "Executable");
 
   uiout->table_body ();
+
+  /* Restore the current thread after the loop because we switch the
+     inferior in the loop.  */
+  scoped_restore_current_pspace_and_thread restore_pspace_thread;
+  inferior *current_inf = current_inferior ();
   for (inferior *inf : all_inferiors ())
     {
       if (!number_is_in_list (requested_inferiors, inf->num))
@@ -501,13 +506,17 @@ print_inferior (struct ui_out *uiout, const char *requested_inferiors)
 
       ui_out_emit_tuple tuple_emitter (uiout, NULL);
 
-      if (inf == current_inferior ())
+      if (inf == current_inf)
 	uiout->field_string ("current", "*");
       else
 	uiout->field_skip ("current");
 
       uiout->field_signed ("number", inf->num);
 
+      /* Because the pid_to_str uses current_top_target,
+	 switch the inferior.  */
+      switch_to_inferior_no_thread (inf);
+
       uiout->field_string ("target-id", inferior_pid_to_str (inf->pid));
 
       std::string conn = uiout_field_connection (inf->process_target ());
-- 
2.17.1

From 0779c90c63359abb2574aa02647969296d944791 Mon Sep 17 00:00:00 2001
From: Tankut Baris Aktemur <tankut.baris.aktemur@intel.com>
Date: Fri, 10 Jan 2020 14:27:54 +0100
Subject: [PATCH 2/2] testsuite: enhance "info inferiors" test for multi-target

Expand the "info inferiors" test for the multi-target feature.  The
test was checking for the output of the info commands after setup,
only when the current inferior is the last added inferior.

This patch does the following:

1. The "info inferiors" and "info connections" test is extracted out
   from the "setup" procedure to a separate procedure.

2. The test is enriched to check the output after switching to each
   inferior, not just the last one.

3. The test is performed twice; one for when the multi-process feature
   is turned on, one for off.

---
 gdb/testsuite/gdb.multi/multi-target.exp | 111 +++++++++++++++++------
 1 file changed, 84 insertions(+), 27 deletions(-)

diff --git a/gdb/testsuite/gdb.multi/multi-target.exp b/gdb/testsuite/gdb.multi/multi-target.exp
index be466c110b..63a7a287d1 100644
--- a/gdb/testsuite/gdb.multi/multi-target.exp
+++ b/gdb/testsuite/gdb.multi/multi-target.exp
@@ -137,33 +137,6 @@ proc setup {non-stop} {
 	return 0
     }
 
-    set ws "\[ \t\]+"
-    global decimal
-
-    # Test "info connections" and "info inferior"'s "Connection"
-    # column, while at it.
-
-    gdb_test "info connections" \
-	[multi_line \
-	     "Num${ws}What${ws}Description${ws}" \
-	     "  1${ws}native${ws}Native process${ws}" \
-	     "  2${ws}extended-remote localhost:$decimal${ws}Extended remote serial target in gdb-specific protocol${ws}" \
-	     "  3${ws}core${ws}Local core dump file${ws}" \
-	     "  4${ws}extended-remote localhost:$decimal${ws}Extended remote serial target in gdb-specific protocol${ws}" \
-	   "\\* 5${ws}core${ws}Local core dump file${ws}" \
-	    ]
-
-    gdb_test "info inferiors" \
-	[multi_line \
-	     "Num${ws}Description${ws}Connection${ws}Executable${ws}" \
-	     "  1${ws}process ${decimal}${ws}1 \\(native\\)${ws}${binfile}${ws}" \
-	     "  2${ws}process ${decimal}${ws}2 \\(extended-remote localhost:$decimal\\)${ws}${binfile}${ws}" \
-	     "  3${ws}process ${decimal}${ws}3 \\(core\\)${ws}${binfile}${ws}" \
-	     "  4${ws}process ${decimal}${ws}1 \\(native\\)${ws}${binfile}${ws}" \
-	     "  5${ws}process ${decimal}${ws}4 \\(extended-remote localhost:$decimal\\)${ws}${binfile}${ws}" \
-	   "\\* 6${ws}process ${decimal}${ws}5 \\(core\\)${ws}${binfile}${ws}" \
-	    ]
-
     # For debugging.
     gdb_test "info threads" ".*"
 
@@ -365,6 +338,83 @@ proc test_ping_pong_next {} {
     }
 }
 
+# Test "info inferiors" and "info connections" where the multi-process
+# feature of remote targets is turned off or on.
+proc test_info_inferiors {multi_process} {
+    setup "off"
+
+    gdb_test_no_output \
+	"set remote multiprocess-feature-packet $multi_process"
+
+    # Get the description for inferior INF for when the current
+    # inferior id is CURRENT.
+    proc inf_desc {inf current} {
+	set ws "\[ \t\]+"
+	global decimal
+	upvar multi_process multi_process
+
+	if {($multi_process == "off") && ($inf == 2 || $inf == 5)} {
+	    set desc "Remote target"
+	} else {
+	    set desc "process ${decimal}"
+	}
+
+	set desc "${inf}${ws}${desc}${ws}"
+	if {$inf == $current} {
+	    return "\\* $desc"
+	} else {
+	    return "  $desc"
+	}
+    }
+
+    # Get the "Num" column for CONNECTION for when the current
+    # inferior id is CURRENT_INF
+    proc connection_num {connection current_inf} {
+	switch $current_inf {
+	    "4" { set current_connection "1"}
+	    "5" { set current_connection "4"}
+	    "6" { set current_connection "5"}
+	    default { set current_connection $current_inf}
+	}
+	if {$connection == $current_connection} {
+	    return "\\* $connection"
+	} else {
+	    return "  $connection"
+	}
+    }
+
+    set ws "\[ \t\]+"
+    global decimal binfile
+
+    # Test "info connections" and "info inferior" by switching to each
+    # inferior one by one.
+    for {set inf 1} {$inf <= 6} {incr inf} {
+	gdb_test "inferior $inf" "Switching to inferior $inf.*" \
+	    "inferior switch ($inf)"
+
+	gdb_test "info connections" \
+	    [multi_line \
+		 "Num${ws}What${ws}Description${ws}" \
+		 "[connection_num 1 $inf]${ws}native${ws}Native process${ws}" \
+		 "[connection_num 2 $inf]${ws}extended-remote localhost:$decimal${ws}Extended remote serial target in gdb-specific protocol${ws}" \
+		 "[connection_num 3 $inf]${ws}core${ws}Local core dump file${ws}" \
+		 "[connection_num 4 $inf]${ws}extended-remote localhost:$decimal${ws}Extended remote serial target in gdb-specific protocol${ws}" \
+		 "[connection_num 5 $inf]${ws}core${ws}Local core dump file${ws}" \
+		] "info connections ($inf)"
+
+	gdb_test "info inferiors" \
+	    [multi_line \
+		 "Num${ws}Description${ws}Connection${ws}Executable${ws}" \
+		 "[inf_desc 1 $inf]1 \\(native\\)${ws}${binfile}${ws}" \
+		 "[inf_desc 2 $inf]2 \\(extended-remote localhost:$decimal\\)${ws}${binfile}${ws}" \
+		 "[inf_desc 3 $inf]3 \\(core\\)${ws}${binfile}${ws}" \
+		 "[inf_desc 4 $inf]1 \\(native\\)${ws}${binfile}${ws}" \
+		 "[inf_desc 5 $inf]4 \\(extended-remote localhost:$decimal\\)${ws}${binfile}${ws}" \
+		 "[inf_desc 6 $inf]5 \\(core\\)${ws}${binfile}${ws}" \
+		] "info inferiors ($inf)"
+    }
+}
+
 # Make a core file with two threads upfront.  Several tests load the
 # same core file.
 prepare_core
@@ -385,3 +435,10 @@ with_test_prefix "interrupt" {
 with_test_prefix "ping-pong" {
     test_ping_pong_next
 }
+
+# Test "info inferiors" and "info connections" commands
+with_test_prefix "info-inferiors" {
+    foreach_with_prefix multi_process {"on" "off"} {
+	test_info_inferiors $multi_process
+    }
+}
-- 
2.17.1



Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Gary Kershaw
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928

^ permalink raw reply	[flat|nested] 73+ messages in thread

* RE: [PATCH] Switch the inferior too in switch_to_program_space_and_thread (Re: [PATCH v2 08/24] Introduce switch_to_inferior_no_thread)
  2020-01-10 12:18               ` Pedro Alves
@ 2020-01-10 13:51                 ` Aktemur, Tankut Baris
  0 siblings, 0 replies; 73+ messages in thread
From: Aktemur, Tankut Baris @ 2020-01-10 13:51 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches, Paunovic, Aleksandar

On Friday, January 10, 2020 1:18 PM, Pedro Alves wrote:
> 
> > As far as I can tell, you rebased the series on the current master, correct?
> > Could you update the users/palves/multi-target-v2 branch?  The new patch does not
> > apply there cleanly.  I attempted to replicate it on users/palves/multi-target-v2,
> > but gdb.base/step-over-exit.exp still hits the assertion.
> Sorry, should have thought to do that.  Done now.

Got it, and ran the test without problems.  Thank you.

-Baris

Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Gary Kershaw
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH] Switch the inferior too in switch_to_program_space_and_thread (Re: [PATCH v2 08/24] Introduce switch_to_inferior_no_thread)
  2020-01-10  2:03           ` Pedro Alves
  2020-01-10 11:33             ` Aktemur, Tankut Baris
@ 2020-01-10 14:41             ` Tom Tromey
  2020-01-10 20:03               ` Pedro Alves
  1 sibling, 1 reply; 73+ messages in thread
From: Tom Tromey @ 2020-01-10 14:41 UTC (permalink / raw)
  To: Pedro Alves; +Cc: Aktemur, Tankut Baris, gdb-patches, Paunovic, Aleksandar

>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:

Pedro> The crash happens while doing a breakpoint re-set, called by
Pedro> clear_symtab_users at the tail end of (3).  That is, while recreating
Pedro> breakpoints for the current program space, which is the program space
Pedro> we're tearing down.  During breakpoint re-set, we try to switch to the
Pedro> new location's pspace (the current pspace set in (1), so the pspace
Pedro> we're tearing down) with switch_to_program_space_and_thread, and that
Pedro> hits the failed assertion.

Whatever happened to fine-grained breakpoint resetting?
Among other things maybe that would help with this sort of problem.

Pedro> I'm not hard set on this approach though; I can go with adding the
Pedro> symfile_add_flags parameter to program_space::free_all_objfiles if
Pedro> people prefer that.

This approach seems fine.
Longer term I would like to get rid of clear_symtab_users.  Maybe it
could be replaced with more precisely targeted observers instead.

thanks,
Tom

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [PATCH] Switch the inferior before outputting its id in "info inferiors" (Re: [PATCH v2 00/24] Multi-target support)
  2020-01-10 13:49         ` Aktemur, Tankut Baris
@ 2020-01-10 15:40           ` Pedro Alves
  0 siblings, 0 replies; 73+ messages in thread
From: Pedro Alves @ 2020-01-10 15:40 UTC (permalink / raw)
  To: Aktemur, Tankut Baris; +Cc: gdb-patches

On 1/10/20 1:49 PM, Aktemur, Tankut Baris wrote:

> There is one more minor thing, I think.  The "info inferiors" command
> iterates over the list of inferiors and prints their info.  One piece of
> information is the inferior id, which is printed through the target.
> This means that for each inferior, the target of the current inferior is
> used, potentially yielding incorrect output.  To fix, inferior can be switched
> temporarily before printing its id.  Please see the patches below for details.
> The second one attempts to modify the multi-target.exp test to check correct
> output.

Excellent, thanks!  These look great.  I squashed them into a single patch,
added ChangeLog entries, and put it on the multi-target branch.  I'll take care
of pushing it to master along the rest so it all goes together.

Small comment, which I've addressed:

> +	gdb_test "info connections" \...

> +		] "info connections ($inf)"
> +
> +	gdb_test "info inferiors" \
...

> +		] "info inferiors ($inf)"
> +    }

See:
 https://sourceware.org/gdb/wiki/GDBTestcaseCookbook#Do_not_use_.22tail_parentheses.22_on_test_messages

And:
 https://sourceware.org/gdb/wiki/GDBTestcaseCookbook#Make_sure_test_messages_are_unique

I fixed it using with_test_prefix.  Actually, the multi-target.exp testcase had another
case of non-unique messages, which I've fixed too.

Here's the version of your patch that I put in the branch.

From 2168664e216d9637b89fcc0e3c577d0aad89edcf Mon Sep 17 00:00:00 2001
From: Tankut Baris Aktemur <tankut.baris.aktemur@intel.com>
Date: Fri, 10 Jan 2020 15:15:36 +0000
Subject: [PATCH] Switch the inferior before outputting its id in "info
 inferiors"

GDB uses the 'current_top_target' when displaying the description of
an inferior.  This leads to same target being used for each inferior
and, in turn, yields incorrect output when the inferior has a target
that is supposed to give a specialized output.  For instance, the
remote target outputs "Remote target" instead of "process XYZ" as the
description if the multi-process feature is not supported or turned
off.

E.g.: Suppose we have a native and a remote target, and the native is
the current inferior.  The remote target does not support multi-process.
For "info inferiors", we would expect to see:

~~~
(gdb) i inferiors
  Num  Description       Connection       Executable
* 1    process 29060     1 (native)       /a/path
  2    Remote target     2 (remote ...)
~~~

but instead we get

~~~
(gdb) i inferiors
  Num  Description       Connection       Executable
* 1    process 29060     1 (native)       /a/path
  2    process 42000     2 (remote ...)
~~~

Similarly, if the current inferior is the remote one, we would expect
to see

~~~
(gdb) i inferiors
  Num  Description       Connection       Executable
  1    process 29060     1 (native)       /a/path
* 2    Remote target     2 (remote ...)
~~~

but we get

~~~
(gdb) i inferiors
  Num  Description       Connection       Executable
* 1    Remote target     1 (native)       /a/path
  2    Remote target     2 (remote ...)
~~~

With this patch, we switch to the inferior when outputting its
description, so that the current_top_target will be aligned to the
inferior we are displaying.

For testing, this patch expands the "info inferiors" test for the
multi-target feature.  The test was checking for the output of the
info commands after setup, only when the current inferior is the last
added inferior.

This patch does the following to the testcase:

1. The "info inferiors" and "info connections" test is extracted out
   from the "setup" procedure to a separate procedure.

2. The test is enriched to check the output after switching to each
   inferior, not just the last one.

3. The test is performed twice; one for when the multi-process feature
   is turned on, one for off.

gdb/ChangeLog:
yyyy-mm-dd  Tankut Baris Aktemur  <tankut.baris.aktemur@intel.com>

	* inferior.c (print_inferior): Switch inferior before printing it.

gdb/testsuite/ChangeLog:
yyyy-mm-dd  Tankut Baris Aktemur  <tankut.baris.aktemur@intel.com>

	* gdb.multi/multi-target.exp (setup): Factor out "info
	connections" and "info inferiors" tests to ...
	(test_info_inferiors): ... this new procedure.
	(top level): Run new "info-inferiors" tests.
---
 gdb/inferior.c                           |  11 ++-
 gdb/testsuite/gdb.multi/multi-target.exp | 113 +++++++++++++++++++++++--------
 2 files changed, 96 insertions(+), 28 deletions(-)

diff --git a/gdb/inferior.c b/gdb/inferior.c
index 3ce43860f7..eb090dfde1 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -494,6 +494,11 @@ print_inferior (struct ui_out *uiout, const char *requested_inferiors)
   uiout->table_header (17, ui_left, "exec", "Executable");
 
   uiout->table_body ();
+
+  /* Restore the current thread after the loop because we switch the
+     inferior in the loop.  */
+  scoped_restore_current_pspace_and_thread restore_pspace_thread;
+  inferior *current_inf = current_inferior ();
   for (inferior *inf : all_inferiors ())
     {
       if (!number_is_in_list (requested_inferiors, inf->num))
@@ -501,13 +506,17 @@ print_inferior (struct ui_out *uiout, const char *requested_inferiors)
 
       ui_out_emit_tuple tuple_emitter (uiout, NULL);
 
-      if (inf == current_inferior ())
+      if (inf == current_inf)
 	uiout->field_string ("current", "*");
       else
 	uiout->field_skip ("current");
 
       uiout->field_signed ("number", inf->num);
 
+      /* Because target_pid_to_str uses current_top_target,
+	 switch the inferior.  */
+      switch_to_inferior_no_thread (inf);
+
       uiout->field_string ("target-id", inferior_pid_to_str (inf->pid));
 
       std::string conn = uiout_field_connection (inf->process_target ());
diff --git a/gdb/testsuite/gdb.multi/multi-target.exp b/gdb/testsuite/gdb.multi/multi-target.exp
index 257b9c62ad..602a734342 100644
--- a/gdb/testsuite/gdb.multi/multi-target.exp
+++ b/gdb/testsuite/gdb.multi/multi-target.exp
@@ -137,33 +137,6 @@ proc setup {non-stop} {
 	return 0
     }
 
-    set ws "\[ \t\]+"
-    global decimal
-
-    # Test "info connections" and "info inferior"'s "Connection"
-    # column, while at it.
-
-    gdb_test "info connections" \
-	[multi_line \
-	     "Num${ws}What${ws}Description${ws}" \
-	     "  1${ws}native${ws}Native process${ws}" \
-	     "  2${ws}extended-remote localhost:$decimal${ws}Extended remote serial target in gdb-specific protocol${ws}" \
-	     "  3${ws}core${ws}Local core dump file${ws}" \
-	     "  4${ws}extended-remote localhost:$decimal${ws}Extended remote serial target in gdb-specific protocol${ws}" \
-	   "\\* 5${ws}core${ws}Local core dump file${ws}" \
-	    ]
-
-    gdb_test "info inferiors" \
-	[multi_line \
-	     "Num${ws}Description${ws}Connection${ws}Executable${ws}" \
-	     "  1${ws}process ${decimal}${ws}1 \\(native\\)${ws}${binfile}${ws}" \
-	     "  2${ws}process ${decimal}${ws}2 \\(extended-remote localhost:$decimal\\)${ws}${binfile}${ws}" \
-	     "  3${ws}process ${decimal}${ws}3 \\(core\\)${ws}${binfile}${ws}" \
-	     "  4${ws}process ${decimal}${ws}1 \\(native\\)${ws}${binfile}${ws}" \
-	     "  5${ws}process ${decimal}${ws}4 \\(extended-remote localhost:$decimal\\)${ws}${binfile}${ws}" \
-	   "\\* 6${ws}process ${decimal}${ws}5 \\(core\\)${ws}${binfile}${ws}" \
-	    ]
-
     # For debugging.
     gdb_test "info threads" ".*"
 
@@ -365,6 +338,85 @@ proc test_ping_pong_next {} {
     }
 }
 
+# Test "info inferiors" and "info connections".  MULTI_PROCESS
+# indicates whether the multi-process feature of remote targets is
+# turned off or on.
+proc test_info_inferiors {multi_process} {
+    setup "off"
+
+    gdb_test_no_output \
+	"set remote multiprocess-feature-packet $multi_process"
+
+    # Get the description for inferior INF for when the current
+    # inferior id is CURRENT.
+    proc inf_desc {inf current} {
+	set ws "\[ \t\]+"
+	global decimal
+	upvar multi_process multi_process
+
+	if {($multi_process == "off") && ($inf == 2 || $inf == 5)} {
+	    set desc "Remote target"
+	} else {
+	    set desc "process ${decimal}"
+	}
+
+	set desc "${inf}${ws}${desc}${ws}"
+	if {$inf == $current} {
+	    return "\\* $desc"
+	} else {
+	    return "  $desc"
+	}
+    }
+
+    # Get the "Num" column for CONNECTION for when the current
+    # inferior id is CURRENT_INF.
+    proc connection_num {connection current_inf} {
+	switch $current_inf {
+	    "4" { set current_connection "1"}
+	    "5" { set current_connection "4"}
+	    "6" { set current_connection "5"}
+	    default { set current_connection $current_inf}
+	}
+	if {$connection == $current_connection} {
+	    return "\\* $connection"
+	} else {
+	    return "  $connection"
+	}
+    }
+
+    set ws "\[ \t\]+"
+    global decimal binfile
+
+    # Test "info connections" and "info inferior" by switching to each
+    # inferior one by one.
+    for {set inf 1} {$inf <= 6} {incr inf} {
+	with_test_prefix "inferior $inf" {
+	    gdb_test "inferior $inf" "Switching to inferior $inf.*"
+
+	    gdb_test "info connections" \
+		[multi_line \
+		     "Num${ws}What${ws}Description${ws}" \
+		     "[connection_num 1 $inf]${ws}native${ws}Native process${ws}" \
+		     "[connection_num 2 $inf]${ws}extended-remote localhost:$decimal${ws}Extended remote serial target in gdb-specific protocol${ws}" \
+		     "[connection_num 3 $inf]${ws}core${ws}Local core dump file${ws}" \
+		     "[connection_num 4 $inf]${ws}extended-remote localhost:$decimal${ws}Extended remote serial target in gdb-specific protocol${ws}" \
+		     "[connection_num 5 $inf]${ws}core${ws}Local core dump file${ws}" \
+		    ]
+
+	    gdb_test "info inferiors" \
+		[multi_line \
+		     "Num${ws}Description${ws}Connection${ws}Executable${ws}" \
+		     "[inf_desc 1 $inf]1 \\(native\\)${ws}${binfile}${ws}" \
+		     "[inf_desc 2 $inf]2 \\(extended-remote localhost:$decimal\\)${ws}${binfile}${ws}" \
+		     "[inf_desc 3 $inf]3 \\(core\\)${ws}${binfile}${ws}" \
+		     "[inf_desc 4 $inf]1 \\(native\\)${ws}${binfile}${ws}" \
+		     "[inf_desc 5 $inf]4 \\(extended-remote localhost:$decimal\\)${ws}${binfile}${ws}" \
+		     "[inf_desc 6 $inf]5 \\(core\\)${ws}${binfile}${ws}" \
+		    ]
+	}
+    }
+}
+
 # Make a core file with two threads upfront.  Several tests load the
 # same core file.
 prepare_core
@@ -385,3 +437,10 @@ with_test_prefix "interrupt" {
 with_test_prefix "ping-pong" {
     test_ping_pong_next
 }
+
+# Test "info inferiors" and "info connections" commands
+with_test_prefix "info-inferiors" {
+    foreach_with_prefix multi_process {"on" "off"} {
+	test_info_inferiors $multi_process
+    }
+}

-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH] Switch the inferior too in switch_to_program_space_and_thread (Re: [PATCH v2 08/24] Introduce switch_to_inferior_no_thread)
  2020-01-10 14:41             ` Tom Tromey
@ 2020-01-10 20:03               ` Pedro Alves
  0 siblings, 0 replies; 73+ messages in thread
From: Pedro Alves @ 2020-01-10 20:03 UTC (permalink / raw)
  To: Tom Tromey; +Cc: Aktemur, Tankut Baris, gdb-patches, Paunovic, Aleksandar

On 1/10/20 2:34 PM, Tom Tromey wrote:
>>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:
> 
> Pedro> The crash happens while doing a breakpoint re-set, called by
> Pedro> clear_symtab_users at the tail end of (3).  That is, while recreating
> Pedro> breakpoints for the current program space, which is the program space
> Pedro> we're tearing down.  During breakpoint re-set, we try to switch to the
> Pedro> new location's pspace (the current pspace set in (1), so the pspace
> Pedro> we're tearing down) with switch_to_program_space_and_thread, and that
> Pedro> hits the failed assertion.
> 
> Whatever happened to fine-grained breakpoint resetting?
> Among other things maybe that would help with this sort of problem.

Yeah...  Many years ago Keith gave it a try, and then he had to go do
something else.  And then few years later, I gave it another go.
I got to somewhere quite close to useful, but never managed to get back
to finishing it.  The branch doesn't look like is on my github, I'd
have to dig it out.  This was pre-C++...  Maybe starting to scratch
is better...

> 
> Pedro> I'm not hard set on this approach though; I can go with adding the
> Pedro> symfile_add_flags parameter to program_space::free_all_objfiles if
> Pedro> people prefer that.
> 
> This approach seems fine.
> Longer term I would like to get rid of clear_symtab_users.  Maybe it
> could be replaced with more precisely targeted observers instead.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 00/24] Multi-target support
  2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
                   ` (26 preceding siblings ...)
  2019-11-01 14:56 ` Tom Tromey
@ 2020-01-10 20:13 ` Pedro Alves
  2020-08-04  3:30   ` Kevin Buettner
  27 siblings, 1 reply; 73+ messages in thread
From: Pedro Alves @ 2020-01-10 20:13 UTC (permalink / raw)
  To: gdb-patches

I've now merged the multi-target work to master, including
the couple follow up patches developed and discussed on this
thread.

Massive thank you to everyone involved, for reviews,
discussions, groundwork, patches, and whatnot!

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 18/24] Multi-target support
  2019-10-17 22:51 ` [PATCH v2 18/24] Multi-target support Pedro Alves
@ 2020-01-11  3:12   ` Simon Marchi
  2020-01-12  1:58     ` [pushed] Remove last traces of discard_all_inferiors (Re: [PATCH v2 18/24] Multi-target support) Pedro Alves
  2020-01-12 20:17   ` [PATCH v2 18/24] Multi-target support Simon Marchi
                     ` (4 subsequent siblings)
  5 siblings, 1 reply; 73+ messages in thread
From: Simon Marchi @ 2020-01-11  3:12 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On 2019-10-17 6:50 p.m., Pedro Alves wrote:
> @@ -539,14 +574,14 @@ extern void exit_inferior_num_silent (int num);
>  
>  extern void inferior_appeared (struct inferior *inf, int pid);
>  
> -/* Get rid of all inferiors.  */
> -extern void discard_all_inferiors (void);

I'm rebasing my -Wmissing-declarations patch series and see this:

  CXX    inferior.o
/home/simark/src/binutils-gdb/gdb/inferior.c:264:1: error: no previous declaration for ‘void discard_all_inferiors()’ [-Werror=missing-declarations]
  264 | discard_all_inferiors (void)
      | ^~~~~~~~~~~~~~~~~~~~~

The declaration of discard_all_inferiors was removed, but not the definition, I
guess the intent was to remove the definition as well?

Also grepping for it finds one remaining use:

  bsd-kvm.c:159:  discard_all_inferiors ();

I presume that this call should just be removed too?

Simon

^ permalink raw reply	[flat|nested] 73+ messages in thread

* [pushed] Remove last traces of discard_all_inferiors (Re: [PATCH v2 18/24] Multi-target support)
  2020-01-11  3:12   ` Simon Marchi
@ 2020-01-12  1:58     ` Pedro Alves
  0 siblings, 0 replies; 73+ messages in thread
From: Pedro Alves @ 2020-01-12  1:58 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 1/11/20 3:12 AM, Simon Marchi wrote:
> On 2019-10-17 6:50 p.m., Pedro Alves wrote:
>> @@ -539,14 +574,14 @@ extern void exit_inferior_num_silent (int num);
>>  
>>  extern void inferior_appeared (struct inferior *inf, int pid);
>>  
>> -/* Get rid of all inferiors.  */
>> -extern void discard_all_inferiors (void);
> 
> I'm rebasing my -Wmissing-declarations patch series and see this:
> 
>   CXX    inferior.o
> /home/simark/src/binutils-gdb/gdb/inferior.c:264:1: error: no previous declaration for ‘void discard_all_inferiors()’ [-Werror=missing-declarations]
>   264 | discard_all_inferiors (void)
>       | ^~~~~~~~~~~~~~~~~~~~~
> 
> The declaration of discard_all_inferiors was removed, but not the definition, I
> guess the intent was to remove the definition as well?

Whoops, yes, indeed.

> 
> Also grepping for it finds one remaining use:
> 
>   bsd-kvm.c:159:  discard_all_inferiors ();
> 
> I presume that this call should just be removed too?

I think we still need the exit_inferiors call.  I suspect direct
calls to exit_inferior shouldn't happen -- that we should use
detach/kill/disconnect instead, but that's another story.

Here's the minimal & obvious patch, which I've just now pushed.

From 4ec89149dd83efecea15300bf425c9988f4cd5c0 Mon Sep 17 00:00:00 2001
From: Pedro Alves <palves@redhat.com>
Date: Sun, 12 Jan 2020 00:40:02 +0000
Subject: [PATCH] Remove last traces of discard_all_inferiors

The multi-target patch should have removed all traces of
discard_all_inferiors, but somehow one use stayed behind along with
the definition of the function.

discard_all_inferiors is bad now because it blindly exits inferiors of
all target connections.  It's best to remove it.

gdb/ChangeLog:
2020-01-12  Pedro Alves  <palves@redhat.com>

	* bsd-kvm.c (bsd_kvm_target::close): Call exit_inferior_silent
	directly for the current inferior instead of
	discard_all_inferiors.
	(discard_all_inferiors): Delete.
---
 gdb/ChangeLog  | 7 +++++++
 gdb/bsd-kvm.c  | 2 +-
 gdb/inferior.c | 7 -------
 3 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 735c46bf70..980114919d 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,10 @@
+2020-01-12  Pedro Alves  <palves@redhat.com>
+
+	* bsd-kvm.c (bsd_kvm_target::close): Call exit_inferior_silent
+	directly for the current inferior instead of
+	discard_all_inferiors.
+	(discard_all_inferiors): Delete.
+
 2020-01-11  Tom Tromey  <tom@tromey.com>
 
 	* tui/tui-wingeneral.c (box_win): Check cli_styling.
diff --git a/gdb/bsd-kvm.c b/gdb/bsd-kvm.c
index f864ba8b41..b1b1fee5f4 100644
--- a/gdb/bsd-kvm.c
+++ b/gdb/bsd-kvm.c
@@ -156,7 +156,7 @@ bsd_kvm_target::close ()
     }
 
   inferior_ptid = null_ptid;
-  discard_all_inferiors ();
+  exit_inferior_silent (current_inferior ());
 }
 
 static LONGEST
diff --git a/gdb/inferior.c b/gdb/inferior.c
index eb090dfde1..c2e9da3d3d 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -260,13 +260,6 @@ inferior_appeared (struct inferior *inf, int pid)
   gdb::observers::inferior_appeared.notify (inf);
 }
 
-void
-discard_all_inferiors (void)
-{
-  for (inferior *inf : all_non_exited_inferiors ())
-    exit_inferior_silent (inf);
-}
-
 struct inferior *
 find_inferior_id (int num)
 {

base-commit: 57d87c09a33acdce280f4c9ae4f55b885a5cee99
-- 
2.14.5

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 18/24] Multi-target support
  2019-10-17 22:51 ` [PATCH v2 18/24] Multi-target support Pedro Alves
  2020-01-11  3:12   ` Simon Marchi
@ 2020-01-12 20:17   ` Simon Marchi
  2020-01-13 15:19     ` Pedro Alves
  2020-01-12 22:30   ` Simon Marchi
                     ` (3 subsequent siblings)
  5 siblings, 1 reply; 73+ messages in thread
From: Simon Marchi @ 2020-01-12 20:17 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches, Andrew Burgess

On 2019-10-17 6:50 p.m., Pedro Alves wrote:
> This commit adds multi-target support to GDB.  What this means is that
> with this commit, GDB can now be connected to different targets at the
> same time.  E.g., you can debug a live native process and a core dump
> at the same time, connect to multiple gdbservers, etc.
>
> ...

remote-sim.c needs to be updated to.  The patch below makes it build, although
I have not tried it (I don't have time right now to re-figure out again how do
do it :)).

From 3342486f68499477f38d90c775f496846cb0d65e Mon Sep 17 00:00:00 2001
From: Simon Marchi <simon.marchi@polymtl.ca>
Date: Sun, 12 Jan 2020 14:30:18 -0500
Subject: [PATCH] gdb: adjust remote-sim.c to multi-target

The remote-sim.c file doesn't build since the main multi-target patch
(5b6d1e4f, "Multi-target support"), this patch is an attempt to fix it.
I have only build-tested it, so I'm not sure it runs fine, but it should
get us close at least.

I made these functions methods of the gdbsim_target, because they need
to pass the target down to some GDB core functions, like
find_inferior_ptid:

 - get_sim_inferior_data_by_ptid (renamed to get_inferior_data_by_ptid)
 - gdbsim_resume_inferior (renamed to resume_one_inferior)
 - gdbsim_close_inferior (renamed to close_one_inferior)

In the last two, I changed iterate_over_inferiors to a range-based for,
since that gives simpler code (no need to pass data through the void
pointer).

The next_pid variable, INITIAL_PID macro and sim_inferior_data structure
are simply moved up in the file, above gdbsim_target.

gdb/ChangeLog:

	* remote-sim.c (next_pid, INITIAL_PID, sim_inferior_data): Move
	up.
	(gdbsim_target) <get_inferior_data_by_ptid, resume_one_inferior,
	close_one_inferior>: New methods.
	(get_sim_inferior_data_by_ptid): Move to gdbsim_target,
	pass down target to find_inferior_pid.
	(gdbsim_target::fetch_registers, gdbsim_target::store_registers):
	Pass down target to find_inferior_ptid.
	(gdbsim_target::create_inferior): Pass down target to
	add_thread_silent.
	(gdbsim_close_inferior): Move to gdbsim_close_inferior, pass
	target down to find_inferior_ptid and switch_to_thread.
	(gdbsim_target::close): Update to call close_one_inferior.
	(struct resume_data): Remove.
	(gdbsim_resume_inferior): Move to gdbsim_target.  Take arguments
	directly, rather than through a void pointer.
	(gdbsim_target::resume): Update to call resume_one_inferior.
---
 gdb/remote-sim.c | 146 +++++++++++++++++++++++------------------------
 1 file changed, 70 insertions(+), 76 deletions(-)

diff --git a/gdb/remote-sim.c b/gdb/remote-sim.c
index 582d206d60c7..533c0d6a9e12 100644
--- a/gdb/remote-sim.c
+++ b/gdb/remote-sim.c
@@ -74,6 +74,43 @@ static void gdb_os_error (host_callback *, const char *, ...)
    sim_* are the interface to the simulator (see remote-sim.h).
    gdbsim_* are stuff which is internal to gdb.  */

+/* Value of the next pid to allocate for an inferior.  As indicated
+   elsewhere, its initial value is somewhat arbitrary; it's critical
+   though that it's not zero or negative.  */
+static int next_pid;
+#define INITIAL_PID 42000
+
+/* Simulator-specific, per-inferior state.  */
+struct sim_inferior_data {
+  explicit sim_inferior_data (SIM_DESC desc)
+    : gdbsim_desc (desc),
+      remote_sim_ptid (next_pid, 0, next_pid)
+  {
+    ++next_pid;
+  }
+
+  ~sim_inferior_data ();
+
+  /* Flag which indicates whether or not the program has been loaded.  */
+  int program_loaded = 0;
+
+  /* Simulator descriptor for this inferior.  */
+  SIM_DESC gdbsim_desc;
+
+  /* This is the ptid we use for this particular simulator instance.  Its
+     value is somewhat arbitrary, as the simulator target don't have a
+     notion of tasks or threads, but we need something non-null to place
+     in inferior_ptid.  For simulators which permit multiple instances,
+     we also need a unique identifier to use for each inferior.  */
+  ptid_t remote_sim_ptid;
+
+  /* Signal with which to resume.  */
+  enum gdb_signal resume_siggnal = GDB_SIGNAL_0;
+
+  /* Flag which indicates whether resume should step or not.  */
+  int resume_step = 0;
+};
+
 static const target_info gdbsim_target_info = {
   "sim",
   N_("simulator"),
@@ -126,47 +163,16 @@ struct gdbsim_target final

   bool has_all_memory ()  override;
   bool has_memory ()  override;
+
+private:
+  sim_inferior_data *get_inferior_data_by_ptid (ptid_t ptid,
+						int sim_instance_needed);
+  void resume_one_inferior (inferior *inf, bool step, gdb_signal siggnal);
+  void close_one_inferior (inferior *inf);
 };

 static struct gdbsim_target gdbsim_ops;

-/* Value of the next pid to allocate for an inferior.  As indicated
-   elsewhere, its initial value is somewhat arbitrary; it's critical
-   though that it's not zero or negative.  */
-static int next_pid;
-#define INITIAL_PID 42000
-
-/* Simulator-specific, per-inferior state.  */
-struct sim_inferior_data {
-  explicit sim_inferior_data (SIM_DESC desc)
-    : gdbsim_desc (desc),
-      remote_sim_ptid (next_pid, 0, next_pid)
-  {
-    ++next_pid;
-  }
-
-  ~sim_inferior_data ();
-
-  /* Flag which indicates whether or not the program has been loaded.  */
-  int program_loaded = 0;
-
-  /* Simulator descriptor for this inferior.  */
-  SIM_DESC gdbsim_desc;
-
-  /* This is the ptid we use for this particular simulator instance.  Its
-     value is somewhat arbitrary, as the simulator target don't have a
-     notion of tasks or threads, but we need something non-null to place
-     in inferior_ptid.  For simulators which permit multiple instances,
-     we also need a unique identifier to use for each inferior.  */
-  ptid_t remote_sim_ptid;
-
-  /* Signal with which to resume.  */
-  enum gdb_signal resume_siggnal = GDB_SIGNAL_0;
-
-  /* Flag which indicates whether resume should step or not.  */
-  int resume_step = 0;
-};
-
 static inferior_key<sim_inferior_data> sim_inferior_data_key;

 /* Flag indicating the "open" status of this module.  It's set to 1
@@ -262,8 +268,9 @@ get_sim_inferior_data (struct inferior *inf, int sim_instance_needed)
    inferior in question.  Return NULL when no inferior is found or
    when ptid has a zero or negative pid component.  */

-static struct sim_inferior_data *
-get_sim_inferior_data_by_ptid (ptid_t ptid, int sim_instance_needed)
+sim_inferior_data *
+gdbsim_target::get_inferior_data_by_ptid (ptid_t ptid,
+					  int sim_instance_needed)
 {
   struct inferior *inf;
   int pid = ptid.pid ();
@@ -271,7 +278,7 @@ get_sim_inferior_data_by_ptid (ptid_t ptid, int sim_instance_needed)
   if (pid <= 0)
     return NULL;

-  inf = find_inferior_pid (pid);
+  inf = find_inferior_pid (this, pid);

   if (inf)
     return get_sim_inferior_data (inf, sim_instance_needed);
@@ -441,7 +448,7 @@ void
 gdbsim_target::fetch_registers (struct regcache *regcache, int regno)
 {
   struct gdbarch *gdbarch = regcache->arch ();
-  struct inferior *inf = find_inferior_ptid (regcache->ptid ());
+  struct inferior *inf = find_inferior_ptid (this, regcache->ptid ());
   struct sim_inferior_data *sim_data
     = get_sim_inferior_data (inf, SIM_INSTANCE_NEEDED);

@@ -510,7 +517,7 @@ void
 gdbsim_target::store_registers (struct regcache *regcache, int regno)
 {
   struct gdbarch *gdbarch = regcache->arch ();
-  struct inferior *inf = find_inferior_ptid (regcache->ptid ());
+  struct inferior *inf = find_inferior_ptid (this, regcache->ptid ());
   struct sim_inferior_data *sim_data
     = get_sim_inferior_data (inf, SIM_INSTANCE_NEEDED);

@@ -656,7 +663,7 @@ gdbsim_target::create_inferior (const char *exec_file,

   inferior_ptid = sim_data->remote_sim_ptid;
   inferior_appeared (current_inferior (), inferior_ptid.pid ());
-  add_thread_silent (inferior_ptid);
+  add_thread_silent (this, inferior_ptid);

   insert_breakpoints ();	/* Needed to get correct instruction
 				   in cache.  */
@@ -770,8 +777,8 @@ gdbsim_target_open (const char *args, int from_tty)
 /* Callback for iterate_over_inferiors.  Called (indirectly) by
    gdbsim_close().  */

-static int
-gdbsim_close_inferior (struct inferior *inf, void *arg)
+void
+gdbsim_target::close_one_inferior (inferior *inf)
 {
   struct sim_inferior_data *sim_data = sim_inferior_data_key.get (inf);
   if (sim_data != NULL)
@@ -785,14 +792,12 @@ gdbsim_close_inferior (struct inferior *inf, void *arg)
 	 Thus we need to verify the existence of an inferior using the
 	 pid in question before setting inferior_ptid via
 	 switch_to_thread() or mourning the inferior.  */
-      if (find_inferior_ptid (ptid) != NULL)
+      if (find_inferior_ptid (this, ptid) != NULL)
 	{
-	  switch_to_thread (ptid);
+	  switch_to_thread (this, ptid);
 	  generic_mourn_inferior ();
 	}
     }
-
-  return 0;
 }

 /* Close out all files and local state before this target loses control.  */
@@ -803,7 +808,8 @@ gdbsim_target::close ()
   if (remote_debug)
     fprintf_unfiltered (gdb_stdlog, "gdbsim_close\n");

-  iterate_over_inferiors (gdbsim_close_inferior, NULL);
+  for (inferior *inf : all_inferiors (this))
+    close_one_inferior (inf);

   if (sim_argv != NULL)
     {
@@ -839,45 +845,30 @@ gdbsim_target::detach (inferior *inf, int from_tty)
    or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given
    to the target, or zero for no signal.  */

-struct resume_data
-{
-  enum gdb_signal siggnal;
-  int step;
-};
-
-static int
-gdbsim_resume_inferior (struct inferior *inf, void *arg)
+void
+gdbsim_target::resume_one_inferior (inferior *inf, bool step,
+				    gdb_signal siggnal)
 {
   struct sim_inferior_data *sim_data
     = get_sim_inferior_data (inf, SIM_INSTANCE_NOT_NEEDED);
-  struct resume_data *rd = (struct resume_data *) arg;

   if (sim_data)
     {
-      sim_data->resume_siggnal = rd->siggnal;
-      sim_data->resume_step = rd->step;
+      sim_data->resume_siggnal = siggnal;
+      sim_data->resume_step = step;

       if (remote_debug)
 	fprintf_unfiltered (gdb_stdlog,
 			    _("gdbsim_resume: pid %d, step %d, signal %d\n"),
-			    inf->pid, rd->step, rd->siggnal);
+			    inf->pid, step, siggnal);
     }
-
-  /* When called from iterate_over_inferiors, a zero return causes the
-     iteration process to proceed until there are no more inferiors to
-     consider.  */
-  return 0;
 }

 void
 gdbsim_target::resume (ptid_t ptid, int step, enum gdb_signal siggnal)
 {
-  struct resume_data rd;
   struct sim_inferior_data *sim_data
-    = get_sim_inferior_data_by_ptid (ptid, SIM_INSTANCE_NOT_NEEDED);
-
-  rd.siggnal = siggnal;
-  rd.step = step;
+    = get_inferior_data_by_ptid (ptid, SIM_INSTANCE_NOT_NEEDED);

   /* We don't access any sim_data members within this function.
      What's of interest is whether or not the call to
@@ -887,9 +878,12 @@ gdbsim_target::resume (ptid_t ptid, int step, enum gdb_signal siggnal)
      either have multiple inferiors to resume or an error condition.  */

   if (sim_data)
-    gdbsim_resume_inferior (find_inferior_ptid (ptid), &rd);
+    resume_one_inferior (find_inferior_ptid (this, ptid), step, siggnal);
   else if (ptid == minus_one_ptid)
-    iterate_over_inferiors (gdbsim_resume_inferior, &rd);
+    {
+      for (inferior *inf : all_inferiors (this))
+	resume_one_inferior (inf, step, siggnal);
+    }
   else
     error (_("The program is not being run."));
 }
@@ -969,7 +963,7 @@ gdbsim_target::wait (ptid_t ptid, struct target_waitstatus *status, int options)
 				      SIM_INSTANCE_NEEDED);
   else
     {
-      sim_data = get_sim_inferior_data_by_ptid (ptid, SIM_INSTANCE_NEEDED);
+      sim_data = get_inferior_data_by_ptid (ptid, SIM_INSTANCE_NEEDED);
       if (sim_data == NULL)
 	error (_("Unable to wait for pid %d.  Inferior not found."),
 	       ptid.pid ());
@@ -1248,7 +1242,7 @@ bool
 gdbsim_target::thread_alive (ptid_t ptid)
 {
   struct sim_inferior_data *sim_data
-    = get_sim_inferior_data_by_ptid (ptid, SIM_INSTANCE_NOT_NEEDED);
+    = get_inferior_data_by_ptid (ptid, SIM_INSTANCE_NOT_NEEDED);

   if (sim_data == NULL)
     return false;
-- 
2.24.1



^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 18/24] Multi-target support
  2019-10-17 22:51 ` [PATCH v2 18/24] Multi-target support Pedro Alves
  2020-01-11  3:12   ` Simon Marchi
  2020-01-12 20:17   ` [PATCH v2 18/24] Multi-target support Simon Marchi
@ 2020-01-12 22:30   ` Simon Marchi
  2020-01-13 15:59     ` Pedro Alves
  2020-01-17  4:03   ` Simon Marchi
                     ` (2 subsequent siblings)
  5 siblings, 1 reply; 73+ messages in thread
From: Simon Marchi @ 2020-01-12 22:30 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On 2019-10-17 6:50 p.m., Pedro Alves wrote:
> This commit adds multi-target support to GDB.  What this means is that
> with this commit, GDB can now be connected to different targets at the
> same time.  E.g., you can debug a live native process and a core dump
> at the same time, connect to multiple gdbservers, etc.
>
> ...

The darwin-nat target also needs to be updated, I have a patch that builds
here on this branch on my Github:

  https://github.com/simark/binutils-gdb/tree/darwin-nat-multi-target

But it is not cleaned up yet, and I will need to go over it again to give it
a second look.

Simon

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 18/24] Multi-target support
  2020-01-12 20:17   ` [PATCH v2 18/24] Multi-target support Simon Marchi
@ 2020-01-13 15:19     ` Pedro Alves
  2020-01-13 16:37       ` Simon Marchi
  0 siblings, 1 reply; 73+ messages in thread
From: Pedro Alves @ 2020-01-13 15:19 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches, Andrew Burgess

On 1/12/20 7:57 PM, Simon Marchi wrote:
> On 2019-10-17 6:50 p.m., Pedro Alves wrote:
>> This commit adds multi-target support to GDB.  What this means is that
>> with this commit, GDB can now be connected to different targets at the
>> same time.  E.g., you can debug a live native process and a core dump
>> at the same time, connect to multiple gdbservers, etc.
>>
>> ...
> 
> remote-sim.c needs to be updated to.  The patch below makes it build, although
> I have not tried it (I don't have time right now to re-figure out again how do
> do it :)).
> 

Sorry about this.  I don't know how I missed it.

The patch look good to me.  One nit:

>  /* Callback for iterate_over_inferiors.  Called (indirectly) by
>     gdbsim_close().  */

This comment is now stale.

> 
> -static int
> -gdbsim_close_inferior (struct inferior *inf, void *arg)
> +void
> +gdbsim_target::close_one_inferior (inferior *inf)
>  {
Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 18/24] Multi-target support
  2020-01-12 22:30   ` Simon Marchi
@ 2020-01-13 15:59     ` Pedro Alves
  0 siblings, 0 replies; 73+ messages in thread
From: Pedro Alves @ 2020-01-13 15:59 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 1/12/20 9:45 PM, Simon Marchi wrote:
> On 2019-10-17 6:50 p.m., Pedro Alves wrote:
>> This commit adds multi-target support to GDB.  What this means is that
>> with this commit, GDB can now be connected to different targets at the
>> same time.  E.g., you can debug a live native process and a core dump
>> at the same time, connect to multiple gdbservers, etc.
>>
>> ...
> 
> The darwin-nat target also needs to be updated, I have a patch that builds
> here on this branch on my Github:
> 
>   https://github.com/simark/binutils-gdb/tree/darwin-nat-multi-target
> 
> But it is not cleaned up yet, and I will need to go over it again to give it
> a second look.

Thanks for handling this!

Pedro Alves

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 18/24] Multi-target support
  2020-01-13 15:19     ` Pedro Alves
@ 2020-01-13 16:37       ` Simon Marchi
  0 siblings, 0 replies; 73+ messages in thread
From: Simon Marchi @ 2020-01-13 16:37 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches, Andrew Burgess

On 2020-01-13 10:10 a.m., Pedro Alves wrote:
> On 1/12/20 7:57 PM, Simon Marchi wrote:
>> On 2019-10-17 6:50 p.m., Pedro Alves wrote:
>>> This commit adds multi-target support to GDB.  What this means is that
>>> with this commit, GDB can now be connected to different targets at the
>>> same time.  E.g., you can debug a live native process and a core dump
>>> at the same time, connect to multiple gdbservers, etc.
>>>
>>> ...
>>
>> remote-sim.c needs to be updated to.  The patch below makes it build, although
>> I have not tried it (I don't have time right now to re-figure out again how do
>> do it :)).
>>
> 
> Sorry about this.  I don't know how I missed it.
> 
> The patch look good to me.  One nit:
> 
>>  /* Callback for iterate_over_inferiors.  Called (indirectly) by
>>     gdbsim_close().  */
> 
> This comment is now stale.

I changed it to:

  /* Helper for gdbsim_target::close.  */

The comment on gdbsim_target::close already explains what is done when closing.

Pushed with that fixed, thanks.

Simon

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 18/24] Multi-target support
  2019-10-17 22:51 ` [PATCH v2 18/24] Multi-target support Pedro Alves
                     ` (2 preceding siblings ...)
  2020-01-12 22:30   ` Simon Marchi
@ 2020-01-17  4:03   ` Simon Marchi
  2020-01-17 16:19     ` Simon Marchi
  2020-01-17 15:18   ` Simon Marchi
  2020-05-16  8:16   ` Andreas Schwab
  5 siblings, 1 reply; 73+ messages in thread
From: Simon Marchi @ 2020-01-17  4:03 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On 2019-10-17 6:50 p.m., Pedro Alves wrote:
> This commit adds multi-target support to GDB.  What this means is that
> with this commit, GDB can now be connected to different targets at the
> same time.  E.g., you can debug a live native process and a core dump
> at the same time, connect to multiple gdbservers, etc.

Hi Pedro,

I think I found something odd happening starting with this commit.  When
the test gdb.threads/tid-reuse.exp runs, we are adding a thread_info to the
inferior's thread list when a thread_info object with the same ptid is
already present.  I don't think this is ever supposed to happen, right?

This can be observed by applying the following diff and running
gdb.threads/tid-reuse.exp.  Note that some distributions are starting to bump
pid_max to a really large value, so make sure it's not that on your system.

diff --git a/gdb/thread.c b/gdb/thread.c
index 4959f938c7f3..8909e371e2ce 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -254,7 +254,10 @@ new_thread (struct inferior *inf, ptid_t ptid)
       struct thread_info *last;

       for (last = inf->thread_list; last->next != NULL; last = last->next)
-	;
+	gdb_assert (ptid != last->ptid);
+
+      gdb_assert (ptid != last->ptid);
+
       last->next = tp;
     }


What I have found so far, by breaking at thread.c:274 and stepping into
the "delete_thread" call, is that `inferior_ptid == ptid` is false
(inferior_ptid is (0,0,0)).  In delete_thread, we are actually not
deleting the thread because ->deletable () returns false, because
the refcount is 2.

Before this patch, we would enter the `if (inferior_ptid == ptid)`.

If it can help, here is the command line I'm using to debug this, which is
mimicking the test:

  ./gdb -nx --data-directory=data-directory testsuite/outputs/gdb.threads/tid-reuse/tid-reuse -iex "set confirm off" -iex "set pagination off" -ex start -ex "set print thread-events off" -ex "b after_count" -ex c -ex "b do_nothing_thread_func" -ex c -ex d -ex "b after_reuse_time"  -ex c

Simon

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 18/24] Multi-target support
  2019-10-17 22:51 ` [PATCH v2 18/24] Multi-target support Pedro Alves
                     ` (3 preceding siblings ...)
  2020-01-17  4:03   ` Simon Marchi
@ 2020-01-17 15:18   ` Simon Marchi
  2020-05-16  8:16   ` Andreas Schwab
  5 siblings, 0 replies; 73+ messages in thread
From: Simon Marchi @ 2020-01-17 15:18 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches; +Cc: Morichetti, Laurent

On 2019-10-17 6:50 p.m., Pedro Alves wrote:
> This commit adds multi-target support to GDB.  What this means is that
> with this commit, GDB can now be connected to different targets at the
> same time.  E.g., you can debug a live native process and a core dump
> at the same time, connect to multiple gdbservers, etc.

I have reported another bug introduced by this patch, originally reported
to me by Laurent, in CC.

Essentially, starting an inferior, then trying to run a second inferior twice,
gives us a:

  warning: td_ta_new failed: generic error

and the second run does not successfully execute the program.

I filed it in bugzilla: https://sourceware.org/bugzilla/show_bug.cgi?id=25410

Simon

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 18/24] Multi-target support
  2020-01-17  4:03   ` Simon Marchi
@ 2020-01-17 16:19     ` Simon Marchi
  0 siblings, 0 replies; 73+ messages in thread
From: Simon Marchi @ 2020-01-17 16:19 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On 2020-01-16 10:47 p.m., Simon Marchi wrote:
> On 2019-10-17 6:50 p.m., Pedro Alves wrote:
>> This commit adds multi-target support to GDB.  What this means is that
>> with this commit, GDB can now be connected to different targets at the
>> same time.  E.g., you can debug a live native process and a core dump
>> at the same time, connect to multiple gdbservers, etc.
> 
> Hi Pedro,
> 
> I think I found something odd happening starting with this commit.  When
> the test gdb.threads/tid-reuse.exp runs, we are adding a thread_info to the
> inferior's thread list when a thread_info object with the same ptid is
> already present.  I don't think this is ever supposed to happen, right?
> 
> This can be observed by applying the following diff and running
> gdb.threads/tid-reuse.exp.  Note that some distributions are starting to bump
> pid_max to a really large value, so make sure it's not that on your system.
> 
> diff --git a/gdb/thread.c b/gdb/thread.c
> index 4959f938c7f3..8909e371e2ce 100644
> --- a/gdb/thread.c
> +++ b/gdb/thread.c
> @@ -254,7 +254,10 @@ new_thread (struct inferior *inf, ptid_t ptid)
>        struct thread_info *last;
> 
>        for (last = inf->thread_list; last->next != NULL; last = last->next)
> -	;
> +	gdb_assert (ptid != last->ptid);
> +
> +      gdb_assert (ptid != last->ptid);
> +
>        last->next = tp;
>      }
> 
> 
> What I have found so far, by breaking at thread.c:274 and stepping into
> the "delete_thread" call, is that `inferior_ptid == ptid` is false
> (inferior_ptid is (0,0,0)).  In delete_thread, we are actually not
> deleting the thread because ->deletable () returns false, because
> the refcount is 2.
> 
> Before this patch, we would enter the `if (inferior_ptid == ptid)`.
> 
> If it can help, here is the command line I'm using to debug this, which is
> mimicking the test:
> 
>   ./gdb -nx --data-directory=data-directory testsuite/outputs/gdb.threads/tid-reuse/tid-reuse -iex "set confirm off" -iex "set pagination off" -ex start -ex "set print thread-events off" -ex "b after_count" -ex c -ex "b do_nothing_thread_func" -ex c -ex d -ex "b after_reuse_time"  -ex c
> 
> Simon
> 

I filed this in bugzilla, so we can track it better:

https://sourceware.org/bugzilla/show_bug.cgi?id=25412

Simon

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 18/24] Multi-target support
  2019-10-17 22:51 ` [PATCH v2 18/24] Multi-target support Pedro Alves
                     ` (4 preceding siblings ...)
  2020-01-17 15:18   ` Simon Marchi
@ 2020-05-16  8:16   ` Andreas Schwab
  2020-05-16 11:33     ` Fix IA-64 GNU/Linux build (Re: [PATCH v2 18/24] Multi-target support) Pedro Alves
  5 siblings, 1 reply; 73+ messages in thread
From: Andreas Schwab @ 2020-05-16  8:16 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

../../gdb/ia64-linux-nat.c: In function ‘void enable_watchpoints_in_psr(ptid_t)’:
../../gdb/ia64-linux-nat.c:535:56: error: no matching function for call to ‘get_thread_regcache(ptid_t&)’
   struct regcache *regcache = get_thread_regcache (ptid);
                                                        ^
In file included from ../../gdb/ia64-linux-nat.c:25:0:
../../gdb/regcache.h:35:25: note: candidate: regcache* get_thread_regcache(process_stratum_target*, ptid_t)
 extern struct regcache *get_thread_regcache (process_stratum_target *target,
                         ^
../../gdb/regcache.h:35:25: note:   candidate expects 2 arguments, 1 provided
../../gdb/regcache.h:39:25: note: candidate: regcache* get_thread_regcache(thread_info*)
 extern struct regcache *get_thread_regcache (thread_info *thread);
                         ^
../../gdb/regcache.h:39:25: note:   no known conversion for argument 1 from ‘ptid_t’ to ‘thread_info*’

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Fix IA-64 GNU/Linux build (Re: [PATCH v2 18/24] Multi-target support)
  2020-05-16  8:16   ` Andreas Schwab
@ 2020-05-16 11:33     ` Pedro Alves
  0 siblings, 0 replies; 73+ messages in thread
From: Pedro Alves @ 2020-05-16 11:33 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: gdb-patches

On 5/16/20 9:16 AM, Andreas Schwab wrote:
> ../../gdb/ia64-linux-nat.c: In function ‘void enable_watchpoints_in_psr(ptid_t)’:
> ../../gdb/ia64-linux-nat.c:535:56: error: no matching function for call to ‘get_thread_regcache(ptid_t&)’
>    struct regcache *regcache = get_thread_regcache (ptid);
>                                                         ^
> In file included from ../../gdb/ia64-linux-nat.c:25:0:
> ../../gdb/regcache.h:35:25: note: candidate: regcache* get_thread_regcache(process_stratum_target*, ptid_t)
>  extern struct regcache *get_thread_regcache (process_stratum_target *target,
>                          ^
> ../../gdb/regcache.h:35:25: note:   candidate expects 2 arguments, 1 provided
> ../../gdb/regcache.h:39:25: note: candidate: regcache* get_thread_regcache(thread_info*)
>  extern struct regcache *get_thread_regcache (thread_info *thread);
>                          ^
> ../../gdb/regcache.h:39:25: note:   no known conversion for argument 1 from ‘ptid_t’ to ‘thread_info*’

I don't have an IA-64 cross compiler handy, but I believe the below
should fix it.  I went ahead and merged it.

From 9bf058f09457de5efd094b87081b7d031ce96cbc Mon Sep 17 00:00:00 2001
From: Pedro Alves <palves@redhat.com>
Date: Sat, 16 May 2020 12:26:56 +0100
Subject: [PATCH] Fix IA64 GNU/Linux build
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This commit should fix:

 ../../gdb/ia64-linux-nat.c: In function ‘void enable_watchpoints_in_psr(ptid_t)’:
 ../../gdb/ia64-linux-nat.c:535:56: error: no matching function for call to ‘get_thread_regcache(ptid_t&)’
    struct regcache *regcache = get_thread_regcache (ptid);
							 ^
 In file included from ../../gdb/ia64-linux-nat.c:25:0:
 ../../gdb/regcache.h:35:25: note: candidate: regcache* get_thread_regcache(process_stratum_target*, ptid_t)
  extern struct regcache *get_thread_regcache (process_stratum_target *target,
			  ^
 ../../gdb/regcache.h:35:25: note:   candidate expects 2 arguments, 1 provided
 ../../gdb/regcache.h:39:25: note: candidate: regcache* get_thread_regcache(thread_info*)
  extern struct regcache *get_thread_regcache (thread_info *thread);
			  ^
 ../../gdb/regcache.h:39:25: note:   no known conversion for argument 1 from ‘ptid_t’ to ‘thread_info*’

gdb/ChangeLog:
2020-05-16  Pedro Alves  <palves@redhat.com>

	* ia64-linux-nat.c
	(ia64_linux_nat_target) <enable_watchpoints_in_psr(ptid_t)>:
	Declare method.
	(enable_watchpoints_in_psr): Now a method of ia64_linux_nat_target.
---
 gdb/ChangeLog        | 7 +++++++
 gdb/ia64-linux-nat.c | 8 +++++---
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 0e8e47d8d22..1c4dc5c94c2 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,10 @@
+2020-05-16  Pedro Alves  <palves@redhat.com>
+
+	* ia64-linux-nat.c
+	(ia64_linux_nat_target) <enable_watchpoints_in_psr(ptid_t)>:
+	Declare method.
+	(enable_watchpoints_in_psr): Now a method of ia64_linux_nat_target.
+
 2020-05-15  Simon Marchi  <simon.marchi@efficios.com>
 
 	* sparc64-tdep.c (adi_stat_t): Remove typedef (leaving struct).
diff --git a/gdb/ia64-linux-nat.c b/gdb/ia64-linux-nat.c
index 01cfa0decd5..8f36ea78e76 100644
--- a/gdb/ia64-linux-nat.c
+++ b/gdb/ia64-linux-nat.c
@@ -80,6 +80,8 @@ class ia64_linux_nat_target final : public linux_nat_target
   /* Override linux_nat_target low methods.  */
   void low_new_thread (struct lwp_info *lp) override;
   bool low_status_is_event (int status) override;
+
+  void enable_watchpoints_in_psr (ptid_t ptid);
 };
 
 static ia64_linux_nat_target the_ia64_linux_nat_target;
@@ -529,10 +531,10 @@ fill_fpregset (const struct regcache *regcache,
 #define IA64_PSR_DB (1UL << 24)
 #define IA64_PSR_DD (1UL << 39)
 
-static void
-enable_watchpoints_in_psr (ptid_t ptid)
+void
+ia64_linux_nat_target::enable_watchpoints_in_psr (ptid_t ptid)
 {
-  struct regcache *regcache = get_thread_regcache (ptid);
+  struct regcache *regcache = get_thread_regcache (this, ptid);
   ULONGEST psr;
 
   regcache_cooked_read_unsigned (regcache, IA64_PSR_REGNUM, &psr);

base-commit: 8bbf03947dd594262e672c1fbc3462a81c811b6f
-- 
2.14.5


^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 00/24] Multi-target support
  2020-01-10 20:13 ` Pedro Alves
@ 2020-08-04  3:30   ` Kevin Buettner
  2020-08-06 15:16     ` Pedro Alves
  0 siblings, 1 reply; 73+ messages in thread
From: Kevin Buettner @ 2020-08-04  3:30 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

On Fri, 10 Jan 2020 20:13:22 +0000
Pedro Alves <palves@redhat.com> wrote:

> I've now merged the multi-target work to master, including
> the couple follow up patches developed and discussed on this
> thread.

I've run into a regression stemming from this commit:

5b6d1e4fa4 (HEAD, refs/bisect/bad) Multi-target support

More info in Bug 26336:

https://sourceware.org/bugzilla/show_bug.cgi?id=26336

(I thought at first that I introduced this problem with my recent
core file work, but realized that was not the case after I disabled
it.  I then did a bisection starting from late last year.)

Kevin


^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 00/24] Multi-target support
  2020-08-04  3:30   ` Kevin Buettner
@ 2020-08-06 15:16     ` Pedro Alves
  2020-08-06 17:49       ` Tom Tromey
  2020-08-07 22:43       ` Kevin Buettner
  0 siblings, 2 replies; 73+ messages in thread
From: Pedro Alves @ 2020-08-06 15:16 UTC (permalink / raw)
  To: Kevin Buettner; +Cc: gdb-patches

On 8/4/20 4:30 AM, Kevin Buettner via Gdb-patches wrote:
> On Fri, 10 Jan 2020 20:13:22 +0000
> Pedro Alves <palves@redhat.com> wrote:
> 
>> I've now merged the multi-target work to master, including
>> the couple follow up patches developed and discussed on this
>> thread.
> 
> I've run into a regression stemming from this commit:
> 
> 5b6d1e4fa4 (HEAD, refs/bisect/bad) Multi-target support
> 
> More info in Bug 26336:
> 
> https://sourceware.org/bugzilla/show_bug.cgi?id=26336
> 
> (I thought at first that I introduced this problem with my recent
> core file work, but realized that was not the case after I disabled
> it.  I then did a bisection starting from late last year.)

Thanks for the bisect and the initial analysis.  This patch fixes
it for me.  No regressions for me on either -m32 nor -m64, and
the corefile.exp regression is fixed.  Let me know what you think.

From a82de99c678920ba3f7a296419a8aaa376c92d98 Mon Sep 17 00:00:00 2001
From: Pedro Alves <pedro@palves.net>
Date: Thu, 6 Aug 2020 13:05:02 +0100
Subject: [PATCH] gdb.base/corefile.exp regression for unix/-m32 on x86_64 (PR
 26336)

gdb.base/corefile.exp is showing an unexpected failure and an
unresolved testcase when testing against unix/-m32:

 (gdb) PASS: gdb.base/corefile.exp: attach: sanity check we see the core file
 attach 15741
 gdb/dwarf2-frame.c:1009: internal-error: dwarf2_frame_cache* dwarf2_frame_cache(frame_info*, void**): Assertion `fde != NULL' failed.
 A problem internal to GDB has been detected,
 further debugging may prove unreliable.
 Quit this debugging session? (y or n) FAIL: gdb.base/corefile.exp: attach: with core (GDB internal error)
 Resyncing due to internal error.

This regressed with:

 From 5b6d1e4fa4fc6827c7b3f0e99ff120dfa14d65d2 Mon Sep 17 00:00:00 2001
 From: Pedro Alves <palves@redhat.com>
 Date: Fri, 10 Jan 2020 20:06:08 +0000
 Subject: [PATCH] Multi-target support

The assertion is here:

 #0  internal_error (file=0xbffffccb0 <error: Cannot access memory at address 0xbffffccb0>, line=0, fmt=0x555556327320 "en_US.UTF-8") at sr
 c/gdbsupport/errors.cc:51
 #1  0x00005555557d4e45 in dwarf2_frame_cache (this_frame=0x55555672f950, this_cache=0x55555672f968) at src/gdb/dwarf2/frame.c:1013
 #2  0x00005555557d5886 in dwarf2_frame_this_id (this_frame=0x55555672f950, this_cache=0x55555672f968, this_id=0x55555672f9b0) at src/gdb/d
 warf2/frame.c:1226
 #3  0x00005555558b184e in compute_frame_id (fi=0x55555672f950) at src/gdb/frame.c:558
 #4  0x00005555558b19b2 in get_frame_id (fi=0x55555672f950) at src/gdb/frame.c:588
 #5  0x0000555555bda338 in scoped_restore_current_thread::scoped_restore_current_thread (this=0x7fffffffd0d8) at src/gdb/thread.c:1458
 #6  0x00005555556ce41f in scoped_restore_current_pspace_and_thread::scoped_restore_current_pspace_and_thread (During symbol reading: .debug_line address at offset 0x1db2d3
 is 0 [in module /home/pedro/gdb/cascais-builds/binutils-gdb/gdb/gdb]
 this=0x7fffffffd0d0) at src/gdb/progspace-and-thread.h:29
 #7  0x0000555555898ea6 in remove_target_sections (owner=0x555556935550) at src/gdb/exec.c:798
 #8  0x0000555555b700b6 in symfile_free_objfile (objfile=0x555556935550) at src/gdb/symfile.c:3742
 #9  0x000055555565050e in std::_Function_handler<void (objfile*), void (*)(objfile*)>::_M_invoke(std::_Any_data const&, objfile*&&) (__functor=..., __args#0=@0x7fffffffd190
 : 0x555556935550) at /usr/include/c++/9/bits/std_function.h:300
 #10 0x0000555555a3053d in std::function<void (objfile*)>::operator()(objfile*) const (this=0x555556752a20, __args#0=0x555556935550) at /usr/include/c++/9/bits/std_function.
 h:688
 #11 0x0000555555a2ff01 in gdb::observers::observable<objfile*>::notify (this=0x5555562eaa80 <gdb::observers::free_objfile>, args#0=0x555556935550) at /net/cascais.nfs/gdb/b
 inutils-gdb/src/gdb/../gdbsupport/observable.h:106
 #12 0x0000555555a2c56a in objfile::~objfile (this=0x555556935550, __in_chrg=<optimized out>) at src/gdb/objfiles.c:521
 #13 0x0000555555a31d46 in std::_Sp_counted_ptr<objfile*, (__gnu_cxx::_Lock_policy)2>::_M_dispose (this=0x555556c1f6f0) at /usr/include/c++/9/bits/shared_ptr_base.h:377
 #14 0x00005555556d3444 in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release (this=0x555556c1f6f0) at /usr/include/c++/9/bits/shared_ptr_base.h:155
 #15 0x00005555556cec77 in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count (this=0x555556b99ee8, __in_chrg=<optimized out>) at /usr/include/c++/9/bits/shared_ptr_base.h:730
 #16 0x0000555555a2f8da in std::__shared_ptr<objfile, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr (this=0x555556b99ee0, __in_chrg=<optimized out>) at /usr/include/c++/9/bits/shared_ptr_base.h:1169
 #17 0x0000555555a2f8fa in std::shared_ptr<objfile>::~shared_ptr (this=0x555556b99ee0, __in_chrg=<optimized out>) at /usr/include/c++/9/bits/shared_ptr.h:103
 #18 0x0000555555a63fba in __gnu_cxx::new_allocator<std::_List_node<std::shared_ptr<objfile> > >::destroy<std::shared_ptr<objfile> > (this=0x55555679f0c0, __p=0x555556b99ee0) at /usr/include/c++/9/ext/new_allocator.h:153
 #19 0x0000555555a638fb in std::allocator_traits<std::allocator<std::_List_node<std::shared_ptr<objfile> > > >::destroy<std::shared_ptr<objfile> > (__a=..., __p=0x555556b99ee0) at /usr/include/c++/9/bits/alloc_traits.h:497
 #20 0x0000555555a6351c in std::__cxx11::list<std::shared_ptr<objfile>, std::allocator<std::shared_ptr<objfile> > >::_M_erase (this=0x55555679f0c0, __position=std::shared_ptr<objfile> (expired, weak count 1) = {get() = 0x555556935550}) at /usr/include/c++/9/bits/stl_list.h:1921
 #21 0x0000555555a62dab in std::__cxx11::list<std::shared_ptr<objfile>, std::allocator<std::shared_ptr<objfile> > >::erase (this=0x55555679f0c0, __position=std::shared_ptr<objfile> (expired, weak count 1) = {get() = 0x555556935550}) at /usr/include/c++/9/bits/list.tcc:158
 #22 0x0000555555a614dd in program_space::remove_objfile (this=0x55555679f080, objfile=0x555556935550) at src/gdb/progspace.c:207
 #23 0x0000555555a2c4dc in objfile::unlink (this=0x555556935550) at src/gdb/objfiles.c:497
 #24 0x0000555555a2da65 in objfile_purge_solibs () at src/gdb/objfiles.c:904
 #25 0x0000555555b3af74 in no_shared_libraries (ignored=0x0, from_tty=1) at src/gdb/solib.c:1236
 #26 0x0000555555bbafc7 in target_pre_inferior (from_tty=1) at src/gdb/target.c:1900
 #27 0x0000555555940afb in attach_command (args=0x5555563277c7 "15741", from_tty=1) at src/gdb/infcmd.c:2582
 ...


The problem is that the multi-target commit added a
scoped_restore_current_thread to remove_target_sections (frame #7
above).  scoped_restore_current_thread's ctor fetches the selected
frame's frame id.  If the frame had not had its frame id computed yet,
it is computed then (frame #4 above).  Because it has been determined
earlier that the frame's unwinder is the DWARF unwinder, we end up
here:

 static struct dwarf2_frame_cache *
 dwarf2_frame_cache (struct frame_info *this_frame, void **this_cache)
 {
 ...
   /* Find the correct FDE.  */
   fde = dwarf2_frame_find_fde (&pc1, &cache->per_objfile);
   gdb_assert (fde != NULL);

And, that assertion fails.  The assertion is reasonable, because the
DWARF unwinder only claims the frame if it managed to find the FDE
earlier (in dwarf2_frame_sniffer).

(unix/-m32 is thus really a red herring here -- it's just that on
x86_64 -m64, the frame is not claimed by the DWARF unwinder.)

The reason the assertion is failing, is because the objfile that
contains the FDE has been removed from the objfiles list already when
we get here (frame #22 above).  This suggests that the fix should be
to invalidate DWARF frames when their objfile is removed.  Or to keep
it simple and safe, invalidate the frame cache when an objfile is
removed.  That is what this commit does.

OOC, I checked why is it that when you unload a file with plain "(gdb)
file", we don't hit the assertion.  It must be because we're already
flushing the frame cache somewhere else in that case.  And indeed, we
flush the frame cache here:

 (gdb) bt
 #0  reinit_frame_cache () at src/gdb/frame.c:1857
 #1  0x0000555555ad1ad6 in registers_changed_ptid (target=0x0, ptid=...) at src/gdb/regcache.c:470
 #2  0x0000555555ad1b58 in registers_changed () at src/gdb/regcache.c:485
 #3  0x00005555558d095e in set_target_gdbarch (new_gdbarch=0x555556d5f5b0) at src/gdb/gdbarch.c:5528
 #4  0x0000555555677175 in set_gdbarch_from_file (abfd=0x0) at src/gdb/arch-utils.c:601
 #5  0x0000555555897c6b in exec_file_attach (filename=0x0, from_tty=1) at src/gdb/exec.c:409
 #6  0x000055555589852d in exec_file_command (args=0x0, from_tty=1) at src/gdb/exec.c:571
 #7  0x00005555558985a1 in file_command (arg=0x0, from_tty=1) at src/gdb/exec.c:583
 #8  0x000055555572b55f in do_const_cfunc (c=0x55555672e200, args=0x0, from_tty=1) at src/gdb/cli/cli-decode.c:95
 #9  0x000055555572f3d3 in cmd_func (cmd=0x55555672e200, args=0x0, from_tty=1) at src/gdb/cli/cli-decode.c:2181
 #10 0x0000555555be1ecc in execute_command (p=0x555556327804 "", from_tty=1) at src/gdb/top.c:668
 #11 0x0000555555895427 in command_handler (command=0x555556327800 "file") at src/gdb/event-top.c:588
 #12 0x00005555558958af in command_line_handler (rl=...) at src/gdb/event-top.c:773
 #13 0x0000555555894b3e in gdb_rl_callback_handler (rl=0x55555a09e240 "file") at src/gdb/event-top.c:219
 #14 0x0000555555ccfeec in rl_callback_read_char () at src/readline/readline/callback.c:281
 #15 0x000055555589495a in gdb_rl_callback_read_char_wrapper_noexcept () at src/gdb/event-top.c:177
 #16 0x0000555555894a08 in gdb_rl_callback_read_char_wrapper (client_data=0x555556327520) at src/gdb/event-top.c:194
 #17 0x00005555558952a5 in stdin_event_handler (error=0, client_data=0x555556327520) at src/gdb/event-top.c:516
 #18 0x0000555555e027d6 in handle_file_event (file_ptr=0x555558d20840, ready_mask=1) at src/gdbsupport/event-loop.cc:548
 #19 0x0000555555e02d88 in gdb_wait_for_event (block=1) at src/gdbsupport/event-loop.cc:673
 #20 0x0000555555e01c42 in gdb_do_one_event () at src/gdbsupport/event-loop.cc:215
 #21 0x00005555559c47c2 in start_event_loop () at src/gdb/main.c:356
 #22 0x00005555559c490d in captured_command_loop () at src/gdb/main.c:416
 #23 0x00005555559c6217 in captured_main (data=0x7fffffffdc00) at src/gdb/main.c:1253
 #24 0x00005555559c6289 in gdb_main (args=0x7fffffffdc00) at src/gdb/main.c:1268
 #25 0x0000555555621756 in main (argc=3, argv=0x7fffffffdd18) at src/gdb/gdb.c:32

gdb/ChangeLog:

	* progspace.c (program_space::remove_objfile): Invalidate the
	frame cache.
---
 gdb/progspace.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/gdb/progspace.c b/gdb/progspace.c
index a0b14a6d2eb..462083ce1f6 100644
--- a/gdb/progspace.c
+++ b/gdb/progspace.c
@@ -198,6 +198,12 @@ program_space::add_objfile (std::shared_ptr<objfile> &&objfile,
 void
 program_space::remove_objfile (struct objfile *objfile)
 {
+  /* Removing an objfile from the objfile list invalidates any frame
+     that was built using frame info found in the objfile.  Reinit the
+     frame cache to get rid of any frame that might otherwise
+     reference stale info.  */
+  reinit_frame_cache ();
+
   auto iter = std::find_if (objfiles_list.begin (), objfiles_list.end (),
 			    [=] (const std::shared_ptr<::objfile> &objf)
 			    {

base-commit: 1a9f72a7a8f445b8d665eb36b053a18e758e63e6
-- 
2.14.5


^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 00/24] Multi-target support
  2020-08-06 15:16     ` Pedro Alves
@ 2020-08-06 17:49       ` Tom Tromey
  2020-08-07 22:43       ` Kevin Buettner
  1 sibling, 0 replies; 73+ messages in thread
From: Tom Tromey @ 2020-08-06 17:49 UTC (permalink / raw)
  To: Pedro Alves; +Cc: Kevin Buettner, gdb-patches

>>>>> "Pedro" == Pedro Alves <pedro@palves.net> writes:

Pedro> 	* progspace.c (program_space::remove_objfile): Invalidate the
Pedro> 	frame cache.

Looks good to me.

Tom

^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 00/24] Multi-target support
  2020-08-06 15:16     ` Pedro Alves
  2020-08-06 17:49       ` Tom Tromey
@ 2020-08-07 22:43       ` Kevin Buettner
  2020-08-12 18:45         ` Pedro Alves
  1 sibling, 1 reply; 73+ messages in thread
From: Kevin Buettner @ 2020-08-07 22:43 UTC (permalink / raw)
  To: gdb-patches

On Thu, 6 Aug 2020 16:16:10 +0100
Pedro Alves <pedro@palves.net> wrote:

> On 8/4/20 4:30 AM, Kevin Buettner via Gdb-patches wrote:
> > On Fri, 10 Jan 2020 20:13:22 +0000
> > Pedro Alves <palves@redhat.com> wrote:
> >   
> >> I've now merged the multi-target work to master, including
> >> the couple follow up patches developed and discussed on this
> >> thread.  
> > 
> > I've run into a regression stemming from this commit:
> > 
> > 5b6d1e4fa4 (HEAD, refs/bisect/bad) Multi-target support
> > 
> > More info in Bug 26336:
> > 
> > https://sourceware.org/bugzilla/show_bug.cgi?id=26336
> > 
> > (I thought at first that I introduced this problem with my recent
> > core file work, but realized that was not the case after I disabled
> > it.  I then did a bisection starting from late last year.)  
> 
> Thanks for the bisect and the initial analysis.  This patch fixes
> it for me.  No regressions for me on either -m32 nor -m64, and
> the corefile.exp regression is fixed.  Let me know what you think.

Your analysis make sense to me and the patch looks good.

I've tested your patch locally and no longer see the corefile.exp
regression when testing w/ x86_64/-m32 (or with just x86_64).

Kevin


^ permalink raw reply	[flat|nested] 73+ messages in thread

* Re: [PATCH v2 00/24] Multi-target support
  2020-08-07 22:43       ` Kevin Buettner
@ 2020-08-12 18:45         ` Pedro Alves
  0 siblings, 0 replies; 73+ messages in thread
From: Pedro Alves @ 2020-08-12 18:45 UTC (permalink / raw)
  To: Kevin Buettner, gdb-patches

On 8/7/20 11:43 PM, Kevin Buettner wrote:
> On Thu, 6 Aug 2020 16:16:10 +0100
> Pedro Alves <pedro@palves.net> wrote:
> 
>> On 8/4/20 4:30 AM, Kevin Buettner via Gdb-patches wrote:
>>> On Fri, 10 Jan 2020 20:13:22 +0000
>>> Pedro Alves <palves@redhat.com> wrote:
>>>   
>>>> I've now merged the multi-target work to master, including
>>>> the couple follow up patches developed and discussed on this
>>>> thread.  
>>>
>>> I've run into a regression stemming from this commit:
>>>
>>> 5b6d1e4fa4 (HEAD, refs/bisect/bad) Multi-target support
>>>
>>> More info in Bug 26336:
>>>
>>> https://sourceware.org/bugzilla/show_bug.cgi?id=26336
>>>
>>> (I thought at first that I introduced this problem with my recent
>>> core file work, but realized that was not the case after I disabled
>>> it.  I then did a bisection starting from late last year.)  
>>
>> Thanks for the bisect and the initial analysis.  This patch fixes
>> it for me.  No regressions for me on either -m32 nor -m64, and
>> the corefile.exp regression is fixed.  Let me know what you think.
> 
> Your analysis make sense to me and the patch looks good.
> 
> I've tested your patch locally and no longer see the corefile.exp
> regression when testing w/ x86_64/-m32 (or with just x86_64).
> 

Thanks guys.  I've merged it now.

Pedro Alves

^ permalink raw reply	[flat|nested] 73+ messages in thread

end of thread, other threads:[~2020-08-12 18:45 UTC | newest]

Thread overview: 73+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-17 22:50 [PATCH v2 00/24] Multi-target support Pedro Alves
2019-10-17 22:50 ` [PATCH v2 12/24] Use all_non_exited_inferiors in infrun.c Pedro Alves
2019-10-17 22:50 ` [PATCH v2 16/24] Fix reconnecting to a gdbserver already debugging multiple processes, I Pedro Alves
2019-10-17 22:50 ` [PATCH v2 21/24] Revert 'Remove unused struct serial::name field' Pedro Alves
2019-10-17 22:50 ` [PATCH v2 17/24] Fix reconnecting to a gdbserver already debugging multiple processes, II Pedro Alves
2019-10-17 22:50 ` [PATCH v2 11/24] tfile_target::close: trace_fd can't be -1 Pedro Alves
2019-10-17 22:50 ` [PATCH v2 02/24] Don't rely on inferior_ptid in record_full_wait Pedro Alves
2019-11-01 14:54   ` Tom Tromey
2019-12-20 17:49     ` Pedro Alves
2019-12-20 18:57       ` Tom Tromey
2019-10-17 22:50 ` [PATCH v2 06/24] Don't check target is running in remote_target::mourn_inferior Pedro Alves
2019-10-17 22:50 ` [PATCH v2 03/24] Make "show remote exec-file" inferior-aware Pedro Alves
2019-10-17 22:50 ` [PATCH v2 10/24] Some get_last_target_status tweaks Pedro Alves
2019-10-17 22:50 ` [PATCH v2 01/24] Preserve selected thread in all-stop w/ background execution Pedro Alves
2019-11-01 13:20   ` Tom Tromey
2019-12-20 17:22     ` Pedro Alves
2019-12-20 18:54       ` Tom Tromey
2019-12-20 18:57         ` Pedro Alves
2019-12-20 18:57           ` Tom Tromey
2019-10-17 22:50 ` [PATCH v2 09/24] switch inferior/thread before calling target methods Pedro Alves
2019-10-17 22:50 ` [PATCH v2 15/24] Avoid another inferior_ptid reference in gdb/remote.c Pedro Alves
2019-10-17 22:51 ` [PATCH v2 22/24] Add "info connections" command, "info inferiors" connection number/string Pedro Alves
2019-10-17 22:51 ` [PATCH v2 04/24] exceptions.c:print_flush: Remove obsolete check Pedro Alves
2019-10-17 22:51 ` [PATCH v2 18/24] Multi-target support Pedro Alves
2020-01-11  3:12   ` Simon Marchi
2020-01-12  1:58     ` [pushed] Remove last traces of discard_all_inferiors (Re: [PATCH v2 18/24] Multi-target support) Pedro Alves
2020-01-12 20:17   ` [PATCH v2 18/24] Multi-target support Simon Marchi
2020-01-13 15:19     ` Pedro Alves
2020-01-13 16:37       ` Simon Marchi
2020-01-12 22:30   ` Simon Marchi
2020-01-13 15:59     ` Pedro Alves
2020-01-17  4:03   ` Simon Marchi
2020-01-17 16:19     ` Simon Marchi
2020-01-17 15:18   ` Simon Marchi
2020-05-16  8:16   ` Andreas Schwab
2020-05-16 11:33     ` Fix IA-64 GNU/Linux build (Re: [PATCH v2 18/24] Multi-target support) Pedro Alves
2019-10-17 22:51 ` [PATCH v2 19/24] Add multi-target tests Pedro Alves
2019-10-17 22:57 ` [PATCH v2 05/24] Make target_ops::has_execution take an 'inferior *' instead of a ptid_t Pedro Alves
2019-10-17 22:57 ` [PATCH v2 07/24] Delete unnecessary code from kill_command Pedro Alves
2019-10-17 22:57 ` [PATCH v2 23/24] Require always-non-stop for multi-target resumptions Pedro Alves
2019-11-01 14:51   ` Tom Tromey
2019-12-30 18:30     ` Pedro Alves
2019-12-31 20:06       ` Tom Tromey
2019-10-17 22:59 ` [PATCH v2 24/24] Multi-target: NEWS and user manual Pedro Alves
2019-10-17 22:59 ` [PATCH v2 13/24] Delete exit_inferior_silent(int pid) Pedro Alves
2019-10-17 22:59 ` [PATCH v2 20/24] gdbarch-selftests.c: No longer error out if debugging something Pedro Alves
2019-10-17 22:59 ` [PATCH v2 08/24] Introduce switch_to_inferior_no_thread Pedro Alves
2019-11-07  9:14   ` Paunovic, Aleksandar
2019-12-20 18:50     ` Pedro Alves
2019-12-23 19:30       ` [PATCH] Switch the inferior too in switch_to_program_space_and_thread (Re: [PATCH v2 08/24] Introduce switch_to_inferior_no_thread) Pedro Alves
2020-01-08 15:48         ` Aktemur, Tankut Baris
2020-01-10  2:03           ` Pedro Alves
2020-01-10 11:33             ` Aktemur, Tankut Baris
2020-01-10 12:18               ` Pedro Alves
2020-01-10 13:51                 ` Aktemur, Tankut Baris
2020-01-10 14:41             ` Tom Tromey
2020-01-10 20:03               ` Pedro Alves
2019-10-17 23:00 ` [PATCH v2 14/24] Tweak handling of remote errors in response to resumption packet Pedro Alves
2019-10-18 20:23 ` [PATCH v2 00/24] Multi-target support John Baldwin
2019-10-29 19:13   ` Pedro Alves
2020-01-09 19:32     ` John Baldwin
2020-01-09 19:50       ` Pedro Alves
2020-01-10 13:49         ` Aktemur, Tankut Baris
2020-01-10 15:40           ` [PATCH] Switch the inferior before outputting its id in "info inferiors" (Re: [PATCH v2 00/24] Multi-target support) Pedro Alves
2019-10-20 11:41 ` [PATCH v2 00/24] Multi-target support Philippe Waroquiers
2019-10-29 19:56   ` Pedro Alves
2019-11-01 14:56 ` Tom Tromey
2020-01-10 20:13 ` Pedro Alves
2020-08-04  3:30   ` Kevin Buettner
2020-08-06 15:16     ` Pedro Alves
2020-08-06 17:49       ` Tom Tromey
2020-08-07 22:43       ` Kevin Buettner
2020-08-12 18:45         ` Pedro Alves

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).