[v3,3/3] gdb: follow-fork: push target and add thread in target_follow_fork

Message ID 20210610143305.2018627-4-simon.marchi@polymtl.ca
State New
Headers show
Series
  • Follow fork improvements
Related show

Commit Message

Mike Frysinger via Gdb-patches June 10, 2021, 2:33 p.m.
In the context of ROCm-gdb [1], the ROCm target sits on top of the
linux-nat target.  when a process forks, it needs to carry over some
data from the forking inferior to the fork child inferior.  Ideally, the
ROCm target would implement the follow_fork target_ops method, but there
are some small problems.  This patch fixes these, which helps the ROCm
target, but also makes things more consistent and a bit nicer in
general, I believe.

The main problem is: when follow-fork-mode is "parent",
target_follow_fork is called with the parent as the current inferior.
When it's "child", target_follow_fork is called with the child as the
current inferior.  This means that target_follow_fork is sometimes
called on the parent's target stack and sometimes on the child's target
stack.

The parent's target stack may contain targets above the process target,
such as the ROCm target.  So if follow-fork-child is "parent", the ROCm
target would get notified of the fork and do whatever is needed.  But
the child's target stack, at that moment, only contains the exec and
process target copied over from the parent.  The child's target stack is
set up by follow_fork_inferior, before calling target_follow_fork.  In
that case, the ROCm target wouldn't get notified of the fork.

For consistency, I think it would be good to always call
target_follow_fork on the parent inferior's target stack.  I think it
makes sense as a way to indicate "this inferior has called fork, do
whatever is needed".  The desired outcome of the fork (whether an
inferior is created for the child, do we need to detach from the child)
can be indicated by passed parameter.

I therefore propose these changes:

 - make follow_fork_inferior always call target_follow_fork with the
   parent as the current inferior.  That lets all targets present on the
   parent's target stack do some fork-related handling and push
   themselves on the fork child's target stack if needed.

   For this purpose, pass the child inferior down to target_follow_fork
   and follow_fork implementations.  This is nullptr if no inferior is
   created for the child, because we want to detach from it.

 - as a result, in follow_fork_inferior, detach from the parent inferior
   (if needed) only after the target_follow_fork call.  This is needed
   because we want to call target_follow_fork before the parent's
   starget stack is torn down.

 - hand over to the targets in the parent's target stack (including the
   process target) the responsibility to push themselves, if needed, to
   the child's target stack.  Also hand over the responsibility to the
   process target, at the same time, to create the child's initial
   thread (just like we do for follow_exec).

 - pass the child inferior to exec_on_vfork, so we don't need to swap
   the current inferior between parent and child.  Nothing in
   exec_on_vfork depends on the current inferior, after this change.

   Although this could perhaps be replaced with just having the exec
   target implement follow_fork and push itself in the child's target
   stack, like the process target does... We would just need to make
   sure the process target calls beneath()->follow_fork(...).  I'm not
   sure about this one.

gdb/ChangeLog:

	* target.h (struct target_ops) <follow_fork>: Add inferior*
	parameter.
	(target_follow_fork): Likewise.
	* target.c (default_follow_fork): Likewise.
	(target_follow_fork): Likewise.
	* fbsd-nat.h (class fbsd_nat_target) <follow_fork>: Likewise.
	(fbsd_nat_target::follow_fork): Likewise, and call
	inf_ptrace_target::follow_fork.
	* linux-nat.h (class linux_nat_target) <follow_fork>: Likewise.
	* linux-nat.c (linux_nat_target::follow_fork): Likewise, and
	call inf_ptrace_target::follow_fork.
	* obsd-nat.h (obsd_nat_target) <follow_fork>: Likewise.
	* obsd-nat.c (obsd_nat_target::follow_fork): Likewise, and call
	inf_ptrace_target::follow_fork.
	* remote.c (class remote_target) <follow_fork>: Likewise.
	(remote_target::follow_fork): Likewise, and call
	process_stratum_target::follow_fork.
	* process-stratum-target.h (class process_stratum_target)
	<follow_fork>: New.
	* process-stratum-target.c
	(process_stratum_target::follow_fork): New.
	* target-delegates.c: Re-generate.

[1] https://github.com/ROCm-Developer-Tools/ROCgdb

Change-Id: I460bd0af850f0485e8aed4b24c6d8262a4c69929
---
 gdb/exec.c                   |   6 +-
 gdb/exec.h                   |   9 +-
 gdb/fbsd-nat.c               |   8 +-
 gdb/fbsd-nat.h               |   2 +-
 gdb/infrun.c                 | 166 +++++++++++++++--------------------
 gdb/linux-nat.c              |  15 ++--
 gdb/linux-nat.h              |   2 +-
 gdb/obsd-nat.c               |   9 +-
 gdb/obsd-nat.h               |   2 +-
 gdb/process-stratum-target.c |  15 ++++
 gdb/process-stratum-target.h |   9 ++
 gdb/remote.c                 |  10 ++-
 gdb/target-delegates.c       |  24 ++---
 gdb/target.c                 |  24 +++--
 gdb/target.h                 |   7 +-
 15 files changed, 166 insertions(+), 142 deletions(-)

-- 
2.32.0

Patch

diff --git a/gdb/exec.c b/gdb/exec.c
index ff0db82a69b9..07c8f3d42fda 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -679,10 +679,10 @@  program_space::remove_target_sections (void *owner)
 /* See exec.h.  */
 
 void
-exec_on_vfork ()
+exec_on_vfork (inferior *vfork_child)
 {
-  if (!current_program_space->target_sections ().empty ())
-    current_inferior ()->push_target (&exec_ops);
+  if (!vfork_child->pspace->target_sections ().empty ())
+    vfork_child->push_target (&exec_ops);
 }
 
 
diff --git a/gdb/exec.h b/gdb/exec.h
index 1119953dc8fb..c237f831d234 100644
--- a/gdb/exec.h
+++ b/gdb/exec.h
@@ -34,12 +34,11 @@  struct objfile;
 
 extern target_section_table build_section_table (struct bfd *);
 
-/* 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.  */
+/* VFORK_CHILD is a child vforked and its program space is shared with its
+   parent.  This pushes the exec target on that inferior's target stack if
+   there are sections in the program space's section table.  */
 
-extern void exec_on_vfork ();
+extern void exec_on_vfork (inferior *vfork_child);
 
 /* Read from mappable read-only sections of BFD executable files.
    Return TARGET_XFER_OK, if read is successful.  Return
diff --git a/gdb/fbsd-nat.c b/gdb/fbsd-nat.c
index 42c95834e50d..148b49497af4 100644
--- a/gdb/fbsd-nat.c
+++ b/gdb/fbsd-nat.c
@@ -1406,9 +1406,13 @@  fbsd_nat_target::supports_stopped_by_sw_breakpoint ()
    the ptid of the followed inferior.  */
 
 void
-fbsd_nat_target::follow_fork (ptid_t child_ptid, target_waitkind fork_kind,
-			      bool follow_child, bool detach_fork)
+fbsd_nat_target::follow_fork (inferior *child_inf, ptid_t child_ptid,
+			      target_waitkind fork_kind, bool follow_child,
+			      bool detach_fork)
 {
+  inf_ptrace_target::follow_fork (child_inf, child_ptid, fork_kind,
+				  follow_child, detach_fork);
+
   if (!follow_child && detach_fork)
     {
       pid_t child_pid = child_ptid.pid ();
diff --git a/gdb/fbsd-nat.h b/gdb/fbsd-nat.h
index 0f8b30df225b..0c67b24c29f2 100644
--- a/gdb/fbsd-nat.h
+++ b/gdb/fbsd-nat.h
@@ -80,7 +80,7 @@  class fbsd_nat_target : public inf_ptrace_target
 #endif
 
 #ifdef TDP_RFPPWAIT
-  void follow_fork (ptid_t, target_waitkind, bool, bool) override;
+  void follow_fork (inferior *, ptid_t, target_waitkind, bool, bool) override;
 
   int insert_fork_catchpoint (int) override;
   int remove_fork_catchpoint (int) override;
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 608fc29c078c..16b3d9028f57 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -428,7 +428,8 @@  holding the child stopped.  Try \"set detach-on-fork\" or \
       return true;
     }
 
-  thread_info *child_thr = nullptr;
+  inferior *parent_inf = current_inferior ();
+  inferior *child_inf = nullptr;
 
   if (!follow_child)
     {
@@ -462,25 +463,15 @@  holding the child stopped.  Try \"set detach-on-fork\" or \
 	}
       else
 	{
-	  struct inferior *parent_inf, *child_inf;
-
 	  /* Add process to GDB's tables.  */
 	  child_inf = add_inferior (child_ptid.pid ());
 
-	  parent_inf = current_inferior ();
 	  child_inf->attach_flag = parent_inf->attach_flag;
 	  copy_terminal_info (child_inf, parent_inf);
 	  child_inf->gdbarch = parent_inf->gdbarch;
 	  copy_inferior_target_desc_info (child_inf, parent_inf);
 
-	  scoped_restore_current_pspace_and_thread restore_pspace_thread;
-
-	  set_current_inferior (child_inf);
-	  switch_to_no_thread ();
 	  child_inf->symfile_flags = SYMFILE_NO_READ;
-	  child_inf->push_target (parent_inf->process_target ());
-	  child_thr = add_thread_silent (child_inf->process_target (),
-					 child_ptid);
 
 	  /* If this is a vfork child, then the address-space is
 	     shared with the parent.  */
@@ -489,7 +480,7 @@  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 ();
+	      exec_on_vfork (child_inf);
 
 	      /* The parent will be frozen until the child is done
 		 with the shared region.  Keep track of the
@@ -498,32 +489,18 @@  holding the child stopped.  Try \"set detach-on-fork\" or \
 	      child_inf->pending_detach = 0;
 	      parent_inf->vfork_child = child_inf;
 	      parent_inf->pending_detach = 0;
-
-	      /* Now that the inferiors and program spaces are all
-		 wired up, we can switch to the child thread (which
-		 switches inferior and program space too).  */
-	      switch_to_thread (child_thr);
 	    }
 	  else
 	    {
 	      child_inf->aspace = new_address_space ();
 	      child_inf->pspace = new program_space (child_inf->aspace);
 	      child_inf->removable = 1;
-	      set_current_program_space (child_inf->pspace);
 	      clone_program_space (child_inf->pspace, parent_inf->pspace);
-
-	      /* solib_create_inferior_hook relies on the current
-		 thread.  */
-	      switch_to_thread (child_thr);
 	    }
 	}
 
       if (has_vforked)
 	{
-	  struct inferior *parent_inf;
-
-	  parent_inf = current_inferior ();
-
 	  /* If we detached from the child, then we have to be careful
 	     to not insert breakpoints in the parent until the child
 	     is done with the shared memory region.  However, if we're
@@ -538,8 +515,6 @@  holding the child stopped.  Try \"set detach-on-fork\" or \
   else
     {
       /* Follow the child.  */
-      struct inferior *parent_inf, *child_inf;
-      struct program_space *parent_pspace;
 
       if (print_inferior_events)
 	{
@@ -559,71 +534,12 @@  holding the child stopped.  Try \"set detach-on-fork\" or \
 
       child_inf = add_inferior (child_ptid.pid ());
 
-      parent_inf = current_inferior ();
       child_inf->attach_flag = parent_inf->attach_flag;
       copy_terminal_info (child_inf, parent_inf);
       child_inf->gdbarch = parent_inf->gdbarch;
       copy_inferior_target_desc_info (child_inf, parent_inf);
 
-      parent_pspace = parent_inf->pspace;
-
-      process_stratum_target *target = parent_inf->process_target ();
-
-      {
-	/* 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_detach (parent_inf, 0);
-	    parent_inf = NULL;
-	  }
-
-	/* 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.  */
-
-	set_current_inferior (child_inf);
-	child_inf->push_target (target);
-      }
-
-      child_thr = add_thread_silent (target, child_ptid);
+      program_space *parent_pspace = parent_inf->pspace;
 
       /* If this is a vfork child, then the address-space is shared
 	 with the parent.  If we detached from the parent, then we can
@@ -633,7 +549,7 @@  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 ();
+	  exec_on_vfork (child_inf);
 	}
       else
 	{
@@ -641,22 +557,80 @@  holding the child stopped.  Try \"set detach-on-fork\" or \
 	  child_inf->pspace = new program_space (child_inf->aspace);
 	  child_inf->removable = 1;
 	  child_inf->symfile_flags = SYMFILE_NO_READ;
-	  set_current_program_space (child_inf->pspace);
 	  clone_program_space (child_inf->pspace, parent_pspace);
 	}
-
-      switch_to_thread (child_thr);
     }
 
-  target_follow_fork (child_ptid, fork_kind, follow_child, detach_fork);
+  gdb_assert (current_inferior () == parent_inf);
+
+  /* target_follow_fork is responsible for pushing the appropriate targets on
+     the new inferior's target stack and adding the initial thread (with ptid
+     CHILD_PTID).  */
+  target_follow_fork (child_inf, child_ptid, fork_kind, follow_child,
+		      detach_fork);
+
+  /* target_follow_fork must leave the parent as the current inferior.  If we
+     want to follow the child, we make it the current one below.  */
+  gdb_assert (current_inferior () == parent_inf);
+
+  /* If there is a child inferior, target_follow_fork must have created a thread
+     for it.  */
+  if (child_inf != nullptr)
+    gdb_assert (child_inf->thread_list != nullptr);
+
+  /* Detach the parent if needed.  */
+  if (follow_child)
+    {
+      /* 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_detach (parent_inf, 0);
+	}
+    }
 
   /* If we ended up creating a new inferior, call post_create_inferior to inform
      the various subcomponents.  */
-  if (child_thr != nullptr)
+  if (child_inf != nullptr)
     {
-      scoped_restore_current_thread restore;
-      switch_to_thread (child_thr);
+      /* If FOLLOW_CHILD, we leave CHILD_INF as the current inferior
+         (do not restore the parent as the current inferior).  */
+      gdb::optional<scoped_restore_current_thread> maybe_restore;
+
+      if (!follow_child)
+	maybe_restore.emplace ();
 
+      switch_to_thread (*child_inf->threads ().begin ());
       post_create_inferior (0);
     }
 
diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index b6fb2fa4a4b6..6c009b6a2970 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -442,19 +442,20 @@  struct lwp_deleter
 
 typedef std::unique_ptr<struct lwp_info, lwp_deleter> lwp_info_up;
 
-/* Target hook for follow_fork.  On entry inferior_ptid must be the
-   ptid of the followed inferior.  At return, inferior_ptid will be
-   unchanged.  */
+/* Target hook for follow_fork.  */
 
 void
-linux_nat_target::follow_fork (ptid_t child_ptid, target_waitkind fork_kind,
-			       bool follow_child, bool detach_fork)
+linux_nat_target::follow_fork (inferior *child_inf, ptid_t child_ptid,
+			       target_waitkind fork_kind, bool follow_child,
+			       bool detach_fork)
 {
+  inf_ptrace_target::follow_fork (child_inf, child_ptid, fork_kind,
+				  follow_child, detach_fork);
+
   if (!follow_child)
     {
       bool has_vforked = fork_kind == TARGET_WAITKIND_VFORKED;
       ptid_t parent_ptid = inferior_ptid;
-      child_ptid = inferior_thread ()->pending_follow.value.related_pid;
       int parent_pid = parent_ptid.lwp ();
       int child_pid = child_ptid.lwp ();
 
@@ -587,7 +588,7 @@  linux_nat_target::follow_fork (ptid_t child_ptid, target_waitkind fork_kind,
     {
       struct lwp_info *child_lp;
 
-      child_lp = add_lwp (inferior_ptid);
+      child_lp = add_lwp (child_ptid);
       child_lp->stopped = 1;
       child_lp->last_resume_kind = resume_stop;
     }
diff --git a/gdb/linux-nat.h b/gdb/linux-nat.h
index ee36c56519b2..83bd6d4a6786 100644
--- a/gdb/linux-nat.h
+++ b/gdb/linux-nat.h
@@ -133,7 +133,7 @@  class linux_nat_target : public inf_ptrace_target
 
   void post_attach (int) override;
 
-  void follow_fork (ptid_t, target_waitkind, bool, bool) override;
+  void follow_fork (inferior *, ptid_t, target_waitkind, bool, bool) override;
 
   std::vector<static_tracepoint_marker>
     static_tracepoint_markers_by_strid (const char *id) override;
diff --git a/gdb/obsd-nat.c b/gdb/obsd-nat.c
index 46fdc0676eab..ab433524dadf 100644
--- a/gdb/obsd-nat.c
+++ b/gdb/obsd-nat.c
@@ -190,13 +190,16 @@  obsd_nat_target::post_startup_inferior (ptid_t pid)
     perror_with_name (("ptrace"));
 }
 
-/* Target hook for follow_fork.  On entry and at return inferior_ptid is
-   the ptid of the followed inferior.  */
+/* Target hook for follow_fork.  */
 
 void
-obsd_nat_target::follow_fork (ptid_t child_ptid, target_waitkind fork_kind,
+obsd_nat_target::follow_fork (inferior *child_inf, ptid_t child_ptid,
+			      target_waitkind fork_kind,
 			      bool follow_child, bool detach_fork)
 {
+  inf_ptrace_target::follow_fork (child_inf, child_ptid, fork_kind,
+				  follow_child, detach_fork);
+
   if (!follow_child)
     {
       /* Breakpoints have already been detached from the child by
diff --git a/gdb/obsd-nat.h b/gdb/obsd-nat.h
index ddd4baf77614..bb9f214278ac 100644
--- a/gdb/obsd-nat.h
+++ b/gdb/obsd-nat.h
@@ -30,7 +30,7 @@  class obsd_nat_target : public inf_ptrace_target
   ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
 
 #ifdef PT_GET_PROCESS_STATE
-  void follow_fork (ptid_t, target_waitkind, bool, bool) override;
+  void follow_fork (inferior *inf, ptid_t, target_waitkind, bool, bool) override;
 
   int insert_fork_catchpoint (int) override;
 
diff --git a/gdb/process-stratum-target.c b/gdb/process-stratum-target.c
index c851090a7f2e..9933debde4c3 100644
--- a/gdb/process-stratum-target.c
+++ b/gdb/process-stratum-target.c
@@ -106,6 +106,21 @@  process_stratum_target::follow_exec (inferior *follow_inf, ptid_t ptid,
 
 /* See process-stratum-target.h.  */
 
+void
+process_stratum_target::follow_fork (inferior *child_inf, ptid_t child_ptid,
+				     target_waitkind fork_kind,
+				     bool follow_child,
+				     bool detach_on_fork)
+{
+  if (child_inf != nullptr)
+    {
+      child_inf->push_target (this);
+      add_thread_silent (this, child_ptid);
+    }
+}
+
+/* See process-stratum-target.h.  */
+
 std::set<process_stratum_target *>
 all_non_exited_process_targets ()
 {
diff --git a/gdb/process-stratum-target.h b/gdb/process-stratum-target.h
index 31a97753db9c..22f42c3c4ef6 100644
--- a/gdb/process-stratum-target.h
+++ b/gdb/process-stratum-target.h
@@ -71,6 +71,15 @@  class process_stratum_target : public target_ops
   void follow_exec (inferior *follow_inf, ptid_t ptid,
 		    const char *execd_pathname) override;
 
+  /* Default implementation of follow_fork.
+
+     If a child inferior was created by infrun while following the fork
+     (CHILD_INF is non-nullptr), push this target on CHILD_INF's target stack
+     and add an initial thread with ptid CHILD_PTID.  */
+  void follow_fork (inferior *child_inf, ptid_t child_ptid,
+		    target_waitkind fork_kind, bool follow_child,
+		    bool detach_on_fork) 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
diff --git a/gdb/remote.c b/gdb/remote.c
index ecdf834093be..fce99d42bdde 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -682,7 +682,7 @@  class remote_target : public process_stratum_target
 
   const struct btrace_config *btrace_conf (const struct btrace_target_info *) override;
   bool augmented_libraries_svr4_read () override;
-  void follow_fork (ptid_t, target_waitkind, bool, bool) override;
+  void follow_fork (inferior *, ptid_t, target_waitkind, bool, bool) override;
   void follow_exec (inferior *, ptid_t, const char *) override;
   int insert_fork_catchpoint (int) override;
   int remove_fork_catchpoint (int) override;
@@ -5899,9 +5899,13 @@  extended_remote_target::detach (inferior *inf, int from_tty)
    remote target as well.  */
 
 void
-remote_target::follow_fork (ptid_t child_ptid, target_waitkind fork_kind,
-			    bool follow_child, bool detach_fork)
+remote_target::follow_fork (inferior *child_inf, ptid_t child_ptid,
+			    target_waitkind fork_kind, bool follow_child,
+			    bool detach_fork)
 {
+  process_stratum_target::follow_fork (child_inf, child_ptid,
+				       fork_kind, follow_child, detach_fork);
+
   struct remote_state *rs = get_remote_state ();
 
   if ((fork_kind == TARGET_WAITKIND_FORKED && remote_fork_event_p (rs))
diff --git a/gdb/target-delegates.c b/gdb/target-delegates.c
index fa4cc5bb2dfc..3fd2854955fe 100644
--- a/gdb/target-delegates.c
+++ b/gdb/target-delegates.c
@@ -56,7 +56,7 @@  struct dummy_target : public target_ops
   int remove_fork_catchpoint (int arg0) override;
   int insert_vfork_catchpoint (int arg0) override;
   int remove_vfork_catchpoint (int arg0) override;
-  void follow_fork (ptid_t arg0, target_waitkind arg1, bool arg2, bool arg3) override;
+  void follow_fork (inferior *arg0, ptid_t arg1, target_waitkind arg2, bool arg3, bool arg4) override;
   int insert_exec_catchpoint (int arg0) override;
   int remove_exec_catchpoint (int arg0) override;
   void follow_exec (inferior *arg0, ptid_t arg1, const char *arg2) override;
@@ -231,7 +231,7 @@  struct debug_target : public target_ops
   int remove_fork_catchpoint (int arg0) override;
   int insert_vfork_catchpoint (int arg0) override;
   int remove_vfork_catchpoint (int arg0) override;
-  void follow_fork (ptid_t arg0, target_waitkind arg1, bool arg2, bool arg3) override;
+  void follow_fork (inferior *arg0, ptid_t arg1, target_waitkind arg2, bool arg3, bool arg4) override;
   int insert_exec_catchpoint (int arg0) override;
   int remove_exec_catchpoint (int arg0) override;
   void follow_exec (inferior *arg0, ptid_t arg1, const char *arg2) override;
@@ -1519,30 +1519,32 @@  debug_target::remove_vfork_catchpoint (int arg0)
 }
 
 void
-target_ops::follow_fork (ptid_t arg0, target_waitkind arg1, bool arg2, bool arg3)
+target_ops::follow_fork (inferior *arg0, ptid_t arg1, target_waitkind arg2, bool arg3, bool arg4)
 {
-  this->beneath ()->follow_fork (arg0, arg1, arg2, arg3);
+  this->beneath ()->follow_fork (arg0, arg1, arg2, arg3, arg4);
 }
 
 void
-dummy_target::follow_fork (ptid_t arg0, target_waitkind arg1, bool arg2, bool arg3)
+dummy_target::follow_fork (inferior *arg0, ptid_t arg1, target_waitkind arg2, bool arg3, bool arg4)
 {
-  default_follow_fork (this, arg0, arg1, arg2, arg3);
+  default_follow_fork (this, arg0, arg1, arg2, arg3, arg4);
 }
 
 void
-debug_target::follow_fork (ptid_t arg0, target_waitkind arg1, bool arg2, bool arg3)
+debug_target::follow_fork (inferior *arg0, ptid_t arg1, target_waitkind arg2, bool arg3, bool arg4)
 {
   fprintf_unfiltered (gdb_stdlog, "-> %s->follow_fork (...)\n", this->beneath ()->shortname ());
-  this->beneath ()->follow_fork (arg0, arg1, arg2, arg3);
+  this->beneath ()->follow_fork (arg0, arg1, arg2, arg3, arg4);
   fprintf_unfiltered (gdb_stdlog, "<- %s->follow_fork (", this->beneath ()->shortname ());
-  target_debug_print_ptid_t (arg0);
+  target_debug_print_inferior_p (arg0);
   fputs_unfiltered (", ", gdb_stdlog);
-  target_debug_print_target_waitkind (arg1);
+  target_debug_print_ptid_t (arg1);
   fputs_unfiltered (", ", gdb_stdlog);
-  target_debug_print_bool (arg2);
+  target_debug_print_target_waitkind (arg2);
   fputs_unfiltered (", ", gdb_stdlog);
   target_debug_print_bool (arg3);
+  fputs_unfiltered (", ", gdb_stdlog);
+  target_debug_print_bool (arg4);
   fputs_unfiltered (")\n", gdb_stdlog);
 }
 
diff --git a/gdb/target.c b/gdb/target.c
index c5468d332ec2..fdbb422fd866 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -2700,9 +2700,9 @@  target_program_signals (gdb::array_view<const unsigned char> program_signals)
 }
 
 static void
-default_follow_fork (struct target_ops *self, ptid_t child_ptid,
-		     target_waitkind fork_kind, bool follow_child,
-		     bool detach_fork)
+default_follow_fork (struct target_ops *self, inferior *child_inf,
+		     ptid_t child_ptid, target_waitkind fork_kind,
+		     bool follow_child, bool detach_fork)
 {
   /* Some target returned a fork event, but did not know how to follow it.  */
   internal_error (__FILE__, __LINE__,
@@ -2712,12 +2712,24 @@  default_follow_fork (struct target_ops *self, ptid_t child_ptid,
 /* See target.h.  */
 
 void
-target_follow_fork (ptid_t child_ptid, target_waitkind fork_kind,
-		    bool follow_child, bool detach_fork)
+target_follow_fork (inferior *child_inf, ptid_t child_ptid,
+		    target_waitkind fork_kind, bool follow_child,
+		    bool detach_fork)
 {
   target_ops *target = current_inferior ()->top_target ();
 
-  return target->follow_fork (child_ptid, fork_kind, follow_child, detach_fork);
+  /* Check consistency between CHILD_INF, CHILD_PTID, FOLLOW_CHILD and
+     DETACH_FORK.  */
+  if (child_inf != nullptr)
+    {
+      gdb_assert (follow_child || !detach_fork);
+      gdb_assert (child_inf->pid == child_ptid.pid ());
+    }
+  else
+    gdb_assert (!follow_child && detach_fork);
+
+  return target->follow_fork (child_inf, child_ptid, fork_kind, follow_child,
+			      detach_fork);
 }
 
 /* See target.h.  */
diff --git a/gdb/target.h b/gdb/target.h
index 3395ac333169..88bbf9e8aefe 100644
--- a/gdb/target.h
+++ b/gdb/target.h
@@ -636,7 +636,7 @@  struct target_ops
       TARGET_DEFAULT_RETURN (1);
     virtual int remove_vfork_catchpoint (int)
       TARGET_DEFAULT_RETURN (1);
-    virtual void follow_fork (ptid_t, target_waitkind, bool, bool)
+    virtual void follow_fork (inferior *, ptid_t, target_waitkind, bool, bool)
       TARGET_DEFAULT_FUNC (default_follow_fork);
     virtual int insert_exec_catchpoint (int)
       TARGET_DEFAULT_RETURN (1);
@@ -1719,8 +1719,9 @@  extern int target_remove_vfork_catchpoint (int pid);
    bookkeeping and fiddling necessary to continue debugging either the parent,
    the child or both.  */
 
-void target_follow_fork (ptid_t child_ptid, target_waitkind fork_kind,
-			 bool follow_child, bool detach_fork);
+void target_follow_fork (inferior *inf, ptid_t child_ptid,
+			 target_waitkind fork_kind, bool follow_child,
+			 bool detach_fork);
 
 /* Handle the target-specific bookkeeping required when the inferior makes an
    exec call.