[v3,2/3] gdb: pass child_ptid and fork kind to target_ops::follow_fork

Message ID 20210610143305.2018627-3-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.
This is a small cleanup I think would be nice, that I spotted while
doing the following patch.

gdb/ChangeLog:

	* target.h (struct target_ops) <follow_fork>: Add ptid and
	target_waitkind parameters.
	(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.c (fbsd_nat_target::follow_fork): Likewise.
	* linux-nat.h (class linux_nat_target) <follow_fork>: Likewise.
	* linux-nat.c (linux_nat_target::follow_fork): Likewise.
	* obsd-nat.h (class obsd_nat_target) <follow_fork>: Likewise.
	* obsd-nat.c (obsd_nat_target::follow_fork): Likewise.
	* remote.c (class remote_target) <follow_fork>: Likewise.
	* target-debug.h (target_debug_print_target_waitkind): New.
	* target-delegates.c: Re-generate.

Change-Id: I5421a542f2e19100a22b74cc333d2b235d0de3c8
---
 gdb/fbsd-nat.c         |  8 ++++----
 gdb/fbsd-nat.h         |  2 +-
 gdb/infrun.c           | 15 +++++++--------
 gdb/linux-nat.c        | 19 +++++++------------
 gdb/linux-nat.h        |  2 +-
 gdb/obsd-nat.c         |  8 +++-----
 gdb/obsd-nat.h         |  2 +-
 gdb/remote.c           | 18 ++++++------------
 gdb/target-debug.h     |  2 ++
 gdb/target-delegates.c | 24 ++++++++++++++----------
 gdb/target.c           |  8 +++++---
 gdb/target.h           | 15 ++++++++-------
 12 files changed, 59 insertions(+), 64 deletions(-)

-- 
2.32.0

Patch

diff --git a/gdb/fbsd-nat.c b/gdb/fbsd-nat.c
index 581c04d5f83d..42c95834e50d 100644
--- a/gdb/fbsd-nat.c
+++ b/gdb/fbsd-nat.c
@@ -1406,12 +1406,12 @@  fbsd_nat_target::supports_stopped_by_sw_breakpoint ()
    the ptid of the followed inferior.  */
 
 void
-fbsd_nat_target::follow_fork (bool follow_child, bool detach_fork)
+fbsd_nat_target::follow_fork (ptid_t child_ptid, target_waitkind fork_kind,
+			      bool follow_child, bool detach_fork)
 {
   if (!follow_child && detach_fork)
     {
-      struct thread_info *tp = inferior_thread ();
-      pid_t child_pid = tp->pending_follow.value.related_pid.pid ();
+      pid_t child_pid = child_ptid.pid ();
 
       /* Breakpoints have already been detached from the child by
 	 infrun.c.  */
@@ -1420,7 +1420,7 @@  fbsd_nat_target::follow_fork (bool follow_child, bool detach_fork)
 	perror_with_name (("ptrace"));
 
 #ifndef PTRACE_VFORK
-      if (tp->pending_follow.kind == TARGET_WAITKIND_VFORKED)
+      if (fork_kind == TARGET_WAITKIND_VFORKED)
 	{
 	  /* We can't insert breakpoints until the child process has
 	     finished with the shared memory region.  The parent
diff --git a/gdb/fbsd-nat.h b/gdb/fbsd-nat.h
index 772655d320e6..0f8b30df225b 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 (bool, bool) override;
+  void follow_fork (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 791ccee6ea0b..608fc29c078c 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -404,13 +404,12 @@  show_follow_fork_mode_string (struct ui_file *file, int from_tty,
 static bool
 follow_fork_inferior (bool follow_child, bool detach_fork)
 {
-  int has_vforked;
-  ptid_t parent_ptid, child_ptid;
-
-  has_vforked = (inferior_thread ()->pending_follow.kind
-		 == TARGET_WAITKIND_VFORKED);
-  parent_ptid = inferior_ptid;
-  child_ptid = inferior_thread ()->pending_follow.value.related_pid;
+  target_waitkind fork_kind = inferior_thread ()->pending_follow.kind;
+  gdb_assert (fork_kind == TARGET_WAITKIND_FORKED
+	      || fork_kind == TARGET_WAITKIND_VFORKED);
+  bool has_vforked = fork_kind == TARGET_WAITKIND_VFORKED;
+  ptid_t parent_ptid = inferior_ptid;
+  ptid_t child_ptid = inferior_thread ()->pending_follow.value.related_pid;
 
   if (has_vforked
       && !non_stop /* Non-stop always resumes both branches.  */
@@ -649,7 +648,7 @@  holding the child stopped.  Try \"set detach-on-fork\" or \
       switch_to_thread (child_thr);
     }
 
-  target_follow_fork (follow_child, detach_fork);
+  target_follow_fork (child_ptid, fork_kind, follow_child, detach_fork);
 
   /* If we ended up creating a new inferior, call post_create_inferior to inform
      the various subcomponents.  */
diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index d394262c2af3..b6fb2fa4a4b6 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -447,24 +447,19 @@  typedef std::unique_ptr<struct lwp_info, lwp_deleter> lwp_info_up;
    unchanged.  */
 
 void
-linux_nat_target::follow_fork (bool follow_child, bool detach_fork)
+linux_nat_target::follow_fork (ptid_t child_ptid, target_waitkind fork_kind,
+			       bool follow_child, bool detach_fork)
 {
   if (!follow_child)
     {
-      struct lwp_info *child_lp = NULL;
-      int has_vforked;
-      ptid_t parent_ptid, child_ptid;
-      int parent_pid, child_pid;
-
-      has_vforked = (inferior_thread ()->pending_follow.kind
-		     == TARGET_WAITKIND_VFORKED);
-      parent_ptid = inferior_ptid;
+      bool has_vforked = fork_kind == TARGET_WAITKIND_VFORKED;
+      ptid_t parent_ptid = inferior_ptid;
       child_ptid = inferior_thread ()->pending_follow.value.related_pid;
-      parent_pid = parent_ptid.lwp ();
-      child_pid = child_ptid.lwp ();
+      int parent_pid = parent_ptid.lwp ();
+      int child_pid = child_ptid.lwp ();
 
       /* We're already attached to the parent, by default.  */
-      child_lp = add_lwp (child_ptid);
+      lwp_info *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 feeaddafe991..ee36c56519b2 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 (bool, bool) override;
+  void follow_fork (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 a8164ddbad15..46fdc0676eab 100644
--- a/gdb/obsd-nat.c
+++ b/gdb/obsd-nat.c
@@ -194,17 +194,15 @@  obsd_nat_target::post_startup_inferior (ptid_t pid)
    the ptid of the followed inferior.  */
 
 void
-obsd_nat_target::follow_fork (bool follow_child, bool detach_fork)
+obsd_nat_target::follow_fork (ptid_t child_ptid, target_waitkind fork_kind,
+			      bool follow_child, bool detach_fork)
 {
   if (!follow_child)
     {
-      struct thread_info *tp = inferior_thread ();
-      pid_t child_pid = tp->pending_follow.value.related_pid.pid ();
-
       /* Breakpoints have already been detached from the child by
 	 infrun.c.  */
 
-      if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
+      if (ptrace (PT_DETACH, child_ptid.pid (), (PTRACE_TYPE_ARG3)1, 0) == -1)
 	perror_with_name (("ptrace"));
     }
 }
diff --git a/gdb/obsd-nat.h b/gdb/obsd-nat.h
index 60b078fd0d30..ddd4baf77614 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 (bool, bool) override;
+  void follow_fork (ptid_t, target_waitkind, bool, bool) override;
 
   int insert_fork_catchpoint (int) override;
 
diff --git a/gdb/remote.c b/gdb/remote.c
index 39bdd2e4ab08..ecdf834093be 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 (bool, bool) override;
+  void follow_fork (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,13 +5899,13 @@  extended_remote_target::detach (inferior *inf, int from_tty)
    remote target as well.  */
 
 void
-remote_target::follow_fork (bool follow_child, bool detach_fork)
+remote_target::follow_fork (ptid_t child_ptid, target_waitkind fork_kind,
+			    bool follow_child, bool detach_fork)
 {
   struct remote_state *rs = get_remote_state ();
-  enum target_waitkind kind = inferior_thread ()->pending_follow.kind;
 
-  if ((kind == TARGET_WAITKIND_FORKED && remote_fork_event_p (rs))
-      || (kind == TARGET_WAITKIND_VFORKED && remote_vfork_event_p (rs)))
+  if ((fork_kind == TARGET_WAITKIND_FORKED && remote_fork_event_p (rs))
+      || (fork_kind == TARGET_WAITKIND_VFORKED && remote_vfork_event_p (rs)))
     {
       /* When following the parent and detaching the child, we detach
 	 the child here.  For the case of following the child and
@@ -5916,13 +5916,7 @@  remote_target::follow_fork (bool follow_child, bool detach_fork)
       if (detach_fork && !follow_child)
 	{
 	  /* Detach the fork child.  */
-	  ptid_t child_ptid;
-	  pid_t child_pid;
-
-	  child_ptid = inferior_thread ()->pending_follow.value.related_pid;
-	  child_pid = child_ptid.pid ();
-
-	  remote_detach_pid (child_pid);
+	  remote_detach_pid (child_ptid.pid ());
 	}
     }
 }
diff --git a/gdb/target-debug.h b/gdb/target-debug.h
index c3004c2c229b..5949441bc66c 100644
--- a/gdb/target-debug.h
+++ b/gdb/target-debug.h
@@ -174,6 +174,8 @@ 
   target_debug_do_print (host_address_to_string (X.data ()))
 #define target_debug_print_gdb_unique_xmalloc_ptr_char(X) \
   target_debug_do_print (X.get ())
+#define target_debug_print_target_waitkind(X) \
+  target_debug_do_print (pulongest (X))
 
 static void
 target_debug_print_struct_target_waitstatus_p (struct target_waitstatus *status)
diff --git a/gdb/target-delegates.c b/gdb/target-delegates.c
index 3e759a2f80ea..fa4cc5bb2dfc 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 (bool arg0, bool arg1) override;
+  void follow_fork (ptid_t arg0, target_waitkind arg1, bool arg2, bool arg3) 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 (bool arg0, bool arg1) override;
+  void follow_fork (ptid_t arg0, target_waitkind arg1, bool arg2, bool arg3) 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,26 +1519,30 @@  debug_target::remove_vfork_catchpoint (int arg0)
 }
 
 void
-target_ops::follow_fork (bool arg0, bool arg1)
+target_ops::follow_fork (ptid_t arg0, target_waitkind arg1, bool arg2, bool arg3)
 {
-  this->beneath ()->follow_fork (arg0, arg1);
+  this->beneath ()->follow_fork (arg0, arg1, arg2, arg3);
 }
 
 void
-dummy_target::follow_fork (bool arg0, bool arg1)
+dummy_target::follow_fork (ptid_t arg0, target_waitkind arg1, bool arg2, bool arg3)
 {
-  default_follow_fork (this, arg0, arg1);
+  default_follow_fork (this, arg0, arg1, arg2, arg3);
 }
 
 void
-debug_target::follow_fork (bool arg0, bool arg1)
+debug_target::follow_fork (ptid_t arg0, target_waitkind arg1, bool arg2, bool arg3)
 {
   fprintf_unfiltered (gdb_stdlog, "-> %s->follow_fork (...)\n", this->beneath ()->shortname ());
-  this->beneath ()->follow_fork (arg0, arg1);
+  this->beneath ()->follow_fork (arg0, arg1, arg2, arg3);
   fprintf_unfiltered (gdb_stdlog, "<- %s->follow_fork (", this->beneath ()->shortname ());
-  target_debug_print_bool (arg0);
+  target_debug_print_ptid_t (arg0);
   fputs_unfiltered (", ", gdb_stdlog);
-  target_debug_print_bool (arg1);
+  target_debug_print_target_waitkind (arg1);
+  fputs_unfiltered (", ", gdb_stdlog);
+  target_debug_print_bool (arg2);
+  fputs_unfiltered (", ", gdb_stdlog);
+  target_debug_print_bool (arg3);
   fputs_unfiltered (")\n", gdb_stdlog);
 }
 
diff --git a/gdb/target.c b/gdb/target.c
index 6babfc562563..c5468d332ec2 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -2700,7 +2700,8 @@  target_program_signals (gdb::array_view<const unsigned char> program_signals)
 }
 
 static void
-default_follow_fork (struct target_ops *self, bool follow_child,
+default_follow_fork (struct target_ops *self, 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.  */
@@ -2711,11 +2712,12 @@  default_follow_fork (struct target_ops *self, bool follow_child,
 /* See target.h.  */
 
 void
-target_follow_fork (bool follow_child, bool detach_fork)
+target_follow_fork (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 (follow_child, detach_fork);
+  return target->follow_fork (child_ptid, fork_kind, follow_child, detach_fork);
 }
 
 /* See target.h.  */
diff --git a/gdb/target.h b/gdb/target.h
index e22f9038197c..3395ac333169 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 (bool, bool)
+    virtual void follow_fork (ptid_t, target_waitkind, bool, bool)
       TARGET_DEFAULT_FUNC (default_follow_fork);
     virtual int insert_exec_catchpoint (int)
       TARGET_DEFAULT_RETURN (1);
@@ -1713,13 +1713,14 @@  extern int target_insert_vfork_catchpoint (int pid);
 
 extern int target_remove_vfork_catchpoint (int pid);
 
-/* If the inferior forks or vforks, this function will be called at
-   the next resume in order to perform any bookkeeping and fiddling
-   necessary to continue debugging either the parent or child, as
-   requested, and releasing the other.  Information about the fork
-   or vfork event is available via get_last_target_status ().  */
+/* Call the follow_fork method on the current target stack.
 
-void target_follow_fork (bool follow_child, bool detach_fork);
+   This function is called when the inferior or vforks, to perform any
+   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);
 
 /* Handle the target-specific bookkeeping required when the inferior makes an
    exec call.