public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/3] Refactor target_ops::post_startup_inferior
@ 2021-12-03 10:28 Andrew Burgess
  2021-12-03 10:28 ` [PATCH 1/3] gdb: have mips_nbsd_nat_target inherit from nbsd_nat_target Andrew Burgess
                   ` (3 more replies)
  0 siblings, 4 replies; 12+ messages in thread
From: Andrew Burgess @ 2021-12-03 10:28 UTC (permalink / raw)
  To: gdb-patches; +Cc: Andrew Burgess

While working on another patch series I needed to understand how
post_startup_inferior is used.

I found the current setup confusing, so here's a small series which I
think makes things clearer.

All feedback welcome,

Thanks,
Andrew

---

Andrew Burgess (3):
  gdb: have mips_nbsd_nat_target inherit from nbsd_nat_target
  gdb: rename gdb_startup_inferior to inf_child_target::startup_inferior
  gdb: make post_startup_inferior a virtual method on inf_ptrace_target

 gdb/aarch64-linux-nat.c |  2 +-
 gdb/darwin-nat.c        |  2 +-
 gdb/fbsd-nat.c          |  2 +-
 gdb/fbsd-nat.h          |  5 ++++-
 gdb/fork-child.c        | 17 +----------------
 gdb/gnu-nat.c           |  2 +-
 gdb/inf-child.c         | 24 +++++++++++++++++-------
 gdb/inf-child.h         | 12 ++++++++++--
 gdb/inf-ptrace.c        |  4 ++--
 gdb/inf-ptrace.h        | 12 ++++++++++++
 gdb/inferior.h          |  7 -------
 gdb/linux-nat.c         |  2 ++
 gdb/linux-nat.h         |  6 ++++--
 gdb/mips-netbsd-nat.c   |  3 ++-
 gdb/netbsd-nat.c        |  2 +-
 gdb/netbsd-nat.h        |  4 +++-
 gdb/obsd-nat.c          |  2 ++
 gdb/obsd-nat.h          |  5 +++--
 gdb/procfs.c            |  2 +-
 gdb/rs6000-aix-nat.c    |  5 +++++
 gdb/target-delegates.c  | 23 -----------------------
 gdb/target.c            |  8 --------
 gdb/target.h            | 14 --------------
 gdb/x86-linux-nat.c     |  2 ++
 gdb/x86-linux-nat.h     |  7 ++++---
 25 files changed, 79 insertions(+), 95 deletions(-)

-- 
2.25.4


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

* [PATCH 1/3] gdb: have mips_nbsd_nat_target inherit from nbsd_nat_target
  2021-12-03 10:28 [PATCH 0/3] Refactor target_ops::post_startup_inferior Andrew Burgess
@ 2021-12-03 10:28 ` Andrew Burgess
  2021-12-03 16:23   ` John Baldwin
  2021-12-03 10:28 ` [PATCH 2/3] gdb: rename gdb_startup_inferior to inf_child_target::startup_inferior Andrew Burgess
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 12+ messages in thread
From: Andrew Burgess @ 2021-12-03 10:28 UTC (permalink / raw)
  To: gdb-patches; +Cc: Andrew Burgess

While working on another patch I had reason to look at
mips-netbsd-nat.c, and noticed that the class mips_nbsd_nat_target
inherits directly from inf_ptrace_target.

This is a little strange as alpha_bsd_nat_target,
arm_netbsd_nat_target, hppa_nbsd_nat_target, m68k_bsd_nat_target,
ppc_nbsd_nat_target, sh_nbsd_nat_target, and vax_bsd_nat_target all
inherit from nbsd_nat_target.

Originally, in this commit:

  commit f6ac5f3d63e03a81c4ff3749aba234961cc9090e
  Date:   Thu May 3 00:37:22 2018 +0100

      Convert struct target_ops to C++

When the target tree was converted to C++, all of the above classes
inherited from inf_ptrace_target except for hppa_nbsd_nat_target,
which was the only class that originally inherited from
nbsd_nat_target.

Later on all the remaining targets (except mips) were converted to
inherit from nbsd_nat_target, these are the commits:

  commit 4fed520be264b60893aa674071947890f8172915
  Date:   Sat Mar 14 16:05:24 2020 +0100

      Inherit alpha_netbsd_nat_target from nbsd_nat_target

  commit 6018d381a00515933016c539d2fdc18ad0d304b8
  Date:   Sat Mar 14 14:50:51 2020 +0100

      Inherit arm_netbsd_nat_target from nbsd_nat_target

  commit 01a801176ea15ddfc988cade2e3d84c3b0abfec3
  Date:   Sat Mar 14 16:54:42 2020 +0100

      Inherit m68k_bsd_nat_target from nbsd_nat_target

  commit 9faa006d11a5e08264a007463435f84b77864c9c
  Date:   Thu Mar 19 14:52:57 2020 +0100

      Inherit ppc_nbsd_nat_target from nbsd_nat_target

  commit 9809762324491b851332ce600ae9bde8dd34f601
  Date:   Tue Mar 17 15:07:39 2020 +0100

      Inherit sh_nbsd_nat_target from nbsd_nat_target

  commit d5be5fa4207da00d039a1d5a040ba316e7092cbd
  Date:   Sat Mar 14 13:21:58 2020 +0100

      Inherit vax_bsd_nat_target from nbsd_nat_target

I could only find mailing list threads for ppc and sh in the archive ,
and unfortunately, none of the commits has any real detail that might
explain why mips was missed out, the only extra context I could find
was this message:

  https://sourceware.org/pipermail/gdb-patches/2020-March/166853.html

Which says that "proper" OS support is going to be added to
nbsd_nat_target, hence the need to inherit from that class.

My guess is that leaving mips_nbsd_nat_target unchanged was an
oversight, so, in this commit, I propose changing mips_nbsd_nat_target
to inherit from nbsd_nat_target just like all the other nbsd targets.

My motivation for this patch relates to the post_startup_inferior
target method.  In a future commit I want to change how this method is
handled.  Currently the mips_nbsd_nat_target will pick up the empty
implementation of inf_child_target::post_startup_inferior rather than
the version in netbsd-nat.c.  This feels like a bug to me, as surely,
enabling of proc events is something that would need to be done for
all netbsd targets, regardless of architecture.

In my future patch I have a choice then, either (a) add a new, empty
implementation of post_startup_inferior to mips_nbsd_nat_target,
or (b) this commit, have mips_nbsd_nat_target inherit from
nbsd_nat_target.  Option (b) seems like the right way to go, hence,
this commit.

I've done absolutely no testing for this change, not even building it,
as that would require at least an environment in which I can x-build
mips-netbsd applications, which I have no idea how to set up.
---
 gdb/mips-netbsd-nat.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/gdb/mips-netbsd-nat.c b/gdb/mips-netbsd-nat.c
index 349ab55b7dc..887163cccd4 100644
--- a/gdb/mips-netbsd-nat.c
+++ b/gdb/mips-netbsd-nat.c
@@ -30,9 +30,10 @@
 
 #include "mips-tdep.h"
 #include "mips-netbsd-tdep.h"
+#include "netbsd-nat.h"
 #include "inf-ptrace.h"
 
-class mips_nbsd_nat_target final : public inf_ptrace_target
+class mips_nbsd_nat_target final : public nbsd_nat_target
 {
   void fetch_registers (struct regcache *, int) override;
   void store_registers (struct regcache *, int) override;
-- 
2.25.4


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

* [PATCH 2/3] gdb: rename gdb_startup_inferior to inf_child_target::startup_inferior
  2021-12-03 10:28 [PATCH 0/3] Refactor target_ops::post_startup_inferior Andrew Burgess
  2021-12-03 10:28 ` [PATCH 1/3] gdb: have mips_nbsd_nat_target inherit from nbsd_nat_target Andrew Burgess
@ 2021-12-03 10:28 ` Andrew Burgess
  2021-12-07 18:58   ` Tom Tromey
  2021-12-09 18:45   ` Andrew Burgess
  2021-12-03 10:28 ` [PATCH 3/3] gdb: make post_startup_inferior a virtual method on inf_ptrace_target Andrew Burgess
  2021-12-13 11:21 ` [PATCH 0/3] Refactor target_ops::post_startup_inferior Andrew Burgess
  3 siblings, 2 replies; 12+ messages in thread
From: Andrew Burgess @ 2021-12-03 10:28 UTC (permalink / raw)
  To: gdb-patches; +Cc: Andrew Burgess

I notice that gdb_startup_inferior is only ever called from within
classes derived from inf_child_target, which makes sense given what
the function does.

This commit makes the function a protected method within
inf_child_target, and updates all the callers.

There should be no user visible changes after this commit.
---
 gdb/darwin-nat.c |  2 +-
 gdb/fork-child.c | 17 +----------------
 gdb/gnu-nat.c    |  2 +-
 gdb/inf-child.c  | 17 +++++++++++++++++
 gdb/inf-child.h  | 10 ++++++++++
 gdb/inf-ptrace.c |  2 +-
 gdb/inferior.h   |  7 -------
 gdb/procfs.c     |  2 +-
 8 files changed, 32 insertions(+), 27 deletions(-)

diff --git a/gdb/darwin-nat.c b/gdb/darwin-nat.c
index e1aeb69e404..aa7eac1d7fb 100644
--- a/gdb/darwin-nat.c
+++ b/gdb/darwin-nat.c
@@ -1753,7 +1753,7 @@ darwin_nat_target::ptrace_him (int pid)
 
   init_thread_list (inf);
 
-  gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
+  startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
 }
 
 static void
diff --git a/gdb/fork-child.c b/gdb/fork-child.c
index 3e995ed6f65..af8dca9ae26 100644
--- a/gdb/fork-child.c
+++ b/gdb/fork-child.c
@@ -29,6 +29,7 @@
 #include "gdbsupport/filestuff.h"
 #include "nat/fork-inferior.h"
 #include "gdbsupport/common-inferior.h"
+#include "inf-child.h"
 
 /* The exec-wrapper, if any, that will be used when starting the
    inferior.  */
@@ -118,22 +119,6 @@ postfork_child_hook ()
   new_tty ();
 }
 
-/* See inferior.h.  */
-
-ptid_t
-gdb_startup_inferior (pid_t pid, int num_traps)
-{
-  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 (proc_target, ptid, false);
-
-  return ptid;
-}
-
 /* Implement the "unset exec-wrapper" command.  */
 
 static void
diff --git a/gdb/gnu-nat.c b/gdb/gnu-nat.c
index 13127cd8246..bd09fa94ecc 100644
--- a/gdb/gnu-nat.c
+++ b/gdb/gnu-nat.c
@@ -2137,7 +2137,7 @@ gnu_nat_target::create_inferior (const char *exec_file,
   thread_change_ptid (this, inferior_ptid,
 		      ptid_t (inf->pid, inf_pick_first_thread (), 0));
 
-  gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
+  startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
 
   inf->pending_execs = 0;
   /* Get rid of the old shell threads.  */
diff --git a/gdb/inf-child.c b/gdb/inf-child.c
index 5e821f45598..780de98e267 100644
--- a/gdb/inf-child.c
+++ b/gdb/inf-child.c
@@ -34,6 +34,7 @@
 #include "gdbsupport/agent.h"
 #include "gdbsupport/gdb_wait.h"
 #include "gdbsupport/filestuff.h"
+#include "nat/fork-inferior.h"
 
 #include <sys/types.h>
 #include <fcntl.h>
@@ -420,6 +421,22 @@ inf_child_target::follow_exec (inferior *follow_inf, ptid_t ptid,
 
 /* See inf-child.h.  */
 
+ptid_t
+inf_child_target::startup_inferior (pid_t pid, int num_traps)
+{
+  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 (proc_target, ptid, false);
+
+  return ptid;
+}
+
+/* See inf-child.h.  */
+
 void
 add_inf_child_target (inf_child_target *target)
 {
diff --git a/gdb/inf-child.h b/gdb/inf-child.h
index aa33c538138..0d13d6bc6e8 100644
--- a/gdb/inf-child.h
+++ b/gdb/inf-child.h
@@ -102,6 +102,16 @@ class inf_child_target
      done by calling either generic_mourn_inferior or
      detach_inferior.  */
   void maybe_unpush_target ();
+
+  /* Helper function to call the global STARTUP_INFERIOR with PID and
+     NUM_TRAPS.  PID should correspond to the current inferior.  No threads
+     in the current inferior should be marked as resumed when calling this
+     method.
+
+     Return the ptid_t from STARTUP_INFERIOR, all the threads in the
+     process matching the returned ptid_t are marked as non-executing, and
+     non-resumed.  */
+  ptid_t startup_inferior (pid_t pid, int num_traps);
 };
 
 /* Functions for helping to write a native target.  */
diff --git a/gdb/inf-ptrace.c b/gdb/inf-ptrace.c
index 852636ba646..e6fe2d04522 100644
--- a/gdb/inf-ptrace.c
+++ b/gdb/inf-ptrace.c
@@ -99,7 +99,7 @@ inf_ptrace_target::create_inferior (const char *exec_file,
 
   unpusher.release ();
 
-  gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
+  startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
 
   /* On some targets, there must be some explicit actions taken after
      the inferior has been started up.  */
diff --git a/gdb/inferior.h b/gdb/inferior.h
index e9210b1258d..a99d4afdc17 100644
--- a/gdb/inferior.h
+++ b/gdb/inferior.h
@@ -185,13 +185,6 @@ extern void child_pass_ctrlc (struct target_ops *self);
 
 extern void child_interrupt (struct target_ops *self);
 
-/* From fork-child.c */
-
-/* Helper function to call STARTUP_INFERIOR with PID and NUM_TRAPS.
-   This function already calls set_executing.  Return the ptid_t from
-   STARTUP_INFERIOR.  */
-extern ptid_t gdb_startup_inferior (pid_t pid, int num_traps);
-
 /* From infcmd.c */
 
 /* Initial inferior setup.  Determines the exec file is not yet known,
diff --git a/gdb/procfs.c b/gdb/procfs.c
index 2c96919dceb..c4b011da6ce 100644
--- a/gdb/procfs.c
+++ b/gdb/procfs.c
@@ -2704,7 +2704,7 @@ procfs_target::procfs_init_inferior (int pid)
      about it.  This changes inferior_ptid as well.  */
   thread_change_ptid (this, ptid_t (pid), ptid_t (pid, lwpid, 0));
 
-  gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
+  startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
 }
 
 /* When GDB forks to create a new process, this function is called on
-- 
2.25.4


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

* [PATCH 3/3] gdb: make post_startup_inferior a virtual method on inf_ptrace_target
  2021-12-03 10:28 [PATCH 0/3] Refactor target_ops::post_startup_inferior Andrew Burgess
  2021-12-03 10:28 ` [PATCH 1/3] gdb: have mips_nbsd_nat_target inherit from nbsd_nat_target Andrew Burgess
  2021-12-03 10:28 ` [PATCH 2/3] gdb: rename gdb_startup_inferior to inf_child_target::startup_inferior Andrew Burgess
@ 2021-12-03 10:28 ` Andrew Burgess
  2021-12-03 16:39   ` John Baldwin
  2021-12-13 11:21 ` [PATCH 0/3] Refactor target_ops::post_startup_inferior Andrew Burgess
  3 siblings, 1 reply; 12+ messages in thread
From: Andrew Burgess @ 2021-12-03 10:28 UTC (permalink / raw)
  To: gdb-patches; +Cc: Andrew Burgess

While working on a later patch that required me to understand how GDB
starts up inferiors, I was confused by the
target_ops::post_startup_inferior method.

The post_startup_inferior target function is only called from
inf_ptrace_target::create_inferior.

Part of the target class hierarchy looks like this:

  inf_child_target
     |
     '-- inf_ptrace_target
            |
            |-- linux_nat_target
            |
            |-- fbsd_nat_target
            |
            |-- nbsd_nat_target
            |
            |-- obsd_nat_target
            |
            '-- rs6000_nat_target

Every sub-class of inf_ptrace_target, except rs6000_nat_target,
implements ::post_startup_inferior.  The rs6000_nat_target picks up
the implementation of ::post_startup_inferior not from
inf_ptrace_target, but from inf_child_target.

No descendent of inf_child_target, outside the inf_ptrace_target
sub-tree, implements ::post_startup_inferior, which isn't really
surprising, as they would never see the method called (remember, the
method is only called from inf_ptrace_target::create_inferior).

What I find confusing is the role inf_child_target plays in
implementing, what is really a helper function for just one of its
descendents.

In this commit I propose that we formally make ::post_startup_inferior
a helper function of inf_ptrace_target.  To do this I will remove the
::post_startup_inferior from the target_ops API, and instead make this
a protected, pure virtual function on inf_ptrace_target.

I'll remove the empty implementation of ::post_startup_inferior from
the inf_child_target class, and add a new empty implementation to the
rs6000_nat_target class.

All the other descendents of inf_ptrace_target already provide an
implementation of this method and so don't need to change beyond
making the method protected within their class declarations.

To me, this makes much more sense now.  The helper function, which is
only called from within the inf_ptrace_target class, is now a part of
the inf_ptrace_target class.

The only way in which this change is visible to a user is if the user
turns on 'set debug target 1'.  With this debug flag on, prior to this
patch the user would see something like:

  -> native->post_startup_inferior (...)
  <- native->post_startup_inferior (2588939)

After this patch these lines are no longer present, as the
post_startup_inferior is no longer a top level target method.  For me,
this is an acceptable change.
---
 gdb/aarch64-linux-nat.c |  2 +-
 gdb/fbsd-nat.c          |  2 +-
 gdb/fbsd-nat.h          |  5 ++++-
 gdb/inf-child.c         |  7 -------
 gdb/inf-child.h         |  2 --
 gdb/inf-ptrace.c        |  2 +-
 gdb/inf-ptrace.h        | 12 ++++++++++++
 gdb/linux-nat.c         |  2 ++
 gdb/linux-nat.h         |  6 ++++--
 gdb/netbsd-nat.c        |  2 +-
 gdb/netbsd-nat.h        |  4 +++-
 gdb/obsd-nat.c          |  2 ++
 gdb/obsd-nat.h          |  5 +++--
 gdb/rs6000-aix-nat.c    |  5 +++++
 gdb/target-delegates.c  | 23 -----------------------
 gdb/target.c            |  8 --------
 gdb/target.h            | 14 --------------
 gdb/x86-linux-nat.c     |  2 ++
 gdb/x86-linux-nat.h     |  7 ++++---
 19 files changed, 45 insertions(+), 67 deletions(-)

diff --git a/gdb/aarch64-linux-nat.c b/gdb/aarch64-linux-nat.c
index aa42365d25d..0ca386331e5 100644
--- a/gdb/aarch64-linux-nat.c
+++ b/gdb/aarch64-linux-nat.c
@@ -694,7 +694,7 @@ ps_get_thread_area (struct ps_prochandle *ph,
 }
 \f
 
-/* Implement the "post_startup_inferior" target_ops method.  */
+/* Implement the virtual inf_ptrace_target::post_startup_inferior method.  */
 
 void
 aarch64_linux_nat_target::post_startup_inferior (ptid_t ptid)
diff --git a/gdb/fbsd-nat.c b/gdb/fbsd-nat.c
index e90aa12e442..e84dafdad9d 100644
--- a/gdb/fbsd-nat.c
+++ b/gdb/fbsd-nat.c
@@ -1548,7 +1548,7 @@ fbsd_nat_target::remove_vfork_catchpoint (int pid)
 }
 #endif
 
-/* Implement the "post_startup_inferior" target_ops method.  */
+/* Implement the virtual inf_ptrace_target::post_startup_inferior method.  */
 
 void
 fbsd_nat_target::post_startup_inferior (ptid_t pid)
diff --git a/gdb/fbsd-nat.h b/gdb/fbsd-nat.h
index 34d7e6d25ba..863c0a4e190 100644
--- a/gdb/fbsd-nat.h
+++ b/gdb/fbsd-nat.h
@@ -76,7 +76,6 @@ class fbsd_nat_target : public inf_ptrace_target
 
   ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
 
-  void post_startup_inferior (ptid_t) override;
   void post_attach (int) override;
 
 #ifdef USE_SIGTRAP_SIGINFO
@@ -106,6 +105,10 @@ class fbsd_nat_target : public inf_ptrace_target
 
   bool supports_disable_randomization () override;
 
+protected:
+
+  void post_startup_inferior (ptid_t) override;
+
 private:
   /* Helper routines for use in fetch_registers and store_registers in
      subclasses.  These routines fetch and store a single set of
diff --git a/gdb/inf-child.c b/gdb/inf-child.c
index 780de98e267..bd9b692ecde 100644
--- a/gdb/inf-child.c
+++ b/gdb/inf-child.c
@@ -202,13 +202,6 @@ inf_child_target::maybe_unpush_target ()
     current_inferior ()->unpush_target (this);
 }
 
-void
-inf_child_target::post_startup_inferior (ptid_t ptid)
-{
-  /* This target doesn't require a meaningful "post startup inferior"
-     operation by a debugger.  */
-}
-
 bool
 inf_child_target::can_run ()
 {
diff --git a/gdb/inf-child.h b/gdb/inf-child.h
index 0d13d6bc6e8..c4a3c955734 100644
--- a/gdb/inf-child.h
+++ b/gdb/inf-child.h
@@ -55,8 +55,6 @@ class inf_child_target
   void interrupt () override;
   void pass_ctrlc () override;
 
-  void post_startup_inferior (ptid_t) override;
-
   void follow_exec (inferior *follow_inf, ptid_t ptid,
 		    const char *execd_pathname) override;
 
diff --git a/gdb/inf-ptrace.c b/gdb/inf-ptrace.c
index e6fe2d04522..e5f88d7654e 100644
--- a/gdb/inf-ptrace.c
+++ b/gdb/inf-ptrace.c
@@ -103,7 +103,7 @@ inf_ptrace_target::create_inferior (const char *exec_file,
 
   /* On some targets, there must be some explicit actions taken after
      the inferior has been started up.  */
-  target_post_startup_inferior (ptid);
+  post_startup_inferior (ptid);
 }
 
 /* Clean up a rotting corpse of an inferior after it died.  */
diff --git a/gdb/inf-ptrace.h b/gdb/inf-ptrace.h
index 8aded9b60db..af61d829fc8 100644
--- a/gdb/inf-ptrace.h
+++ b/gdb/inf-ptrace.h
@@ -60,6 +60,18 @@ struct inf_ptrace_target : public inf_child_target
 protected:
   /* Cleanup the inferior after a successful ptrace detach.  */
   void detach_success (inferior *inf);
+
+  /* Some targets (such as ttrace-based HPUX) don't allow us to request
+     notification of inferior events such as fork and vork immediately
+     after the inferior is created.  (This because of how gdb gets an
+     inferior created via invoking a shell to do it.  In such a scenario,
+     if the shell init file has commands in it, the shell will fork and
+     exec for each of those commands, and we will see each such fork
+     event.  Very bad.)
+
+     Such targets will supply an appropriate definition for this
+     function.  */
+  virtual void post_startup_inferior (ptid_t ptid) = 0;
 };
 
 #ifndef __NetBSD__
diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index fbb60a398b0..6f5031adacf 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -411,6 +411,8 @@ linux_nat_target::post_attach (int pid)
   linux_init_ptrace_procfs (pid, 1);
 }
 
+/* Implement the virtual inf_ptrace_target::post_startup_inferior method.  */
+
 void
 linux_nat_target::post_startup_inferior (ptid_t ptid)
 {
diff --git a/gdb/linux-nat.h b/gdb/linux-nat.h
index 95e26b7ee46..116eb7ed51b 100644
--- a/gdb/linux-nat.h
+++ b/gdb/linux-nat.h
@@ -129,8 +129,6 @@ class linux_nat_target : public inf_ptrace_target
 
   char *pid_to_exec_file (int pid) override;
 
-  void post_startup_inferior (ptid_t) override;
-
   void post_attach (int) override;
 
   void follow_fork (inferior *, ptid_t, target_waitkind, bool, bool) override;
@@ -189,6 +187,10 @@ class linux_nat_target : public inf_ptrace_target
   /* SIGTRAP-like breakpoint status events recognizer.  The default
      recognizes SIGTRAP only.  */
   virtual bool low_status_is_event (int status);
+
+protected:
+
+    void post_startup_inferior (ptid_t) override;
 };
 
 /* The final/concrete instance.  */
diff --git a/gdb/netbsd-nat.c b/gdb/netbsd-nat.c
index e06c036fcdd..09720761d1c 100644
--- a/gdb/netbsd-nat.c
+++ b/gdb/netbsd-nat.c
@@ -132,7 +132,7 @@ nbsd_add_threads (nbsd_nat_target *target, pid_t pid)
   netbsd_nat::for_each_thread (pid, fn);
 }
 
-/* Implement the "post_startup_inferior" target_ops method.  */
+/* Implement the virtual inf_ptrace_target::post_startup_inferior method.  */
 
 void
 nbsd_nat_target::post_startup_inferior (ptid_t ptid)
diff --git a/gdb/netbsd-nat.h b/gdb/netbsd-nat.h
index 43d60eb3415..ad83f2036d4 100644
--- a/gdb/netbsd-nat.h
+++ b/gdb/netbsd-nat.h
@@ -32,7 +32,6 @@ struct nbsd_nat_target : public inf_ptrace_target
 
   bool thread_alive (ptid_t ptid) override;
   const char *thread_name (struct thread_info *thr) override;
-  void post_startup_inferior (ptid_t ptid) override;
   void post_attach (int pid) override;
   void update_thread_list () override;
   std::string pid_to_str (ptid_t ptid) override;
@@ -57,6 +56,9 @@ struct nbsd_nat_target : public inf_ptrace_target
 					ULONGEST *xfered_len) override;
   bool supports_dumpcore () override;
   void dumpcore (const char *filename) override;
+
+protected:
+  void post_startup_inferior (ptid_t ptid) override;
 };
 
 #endif /* netbsd-nat.h */
diff --git a/gdb/obsd-nat.c b/gdb/obsd-nat.c
index c70820d60d0..2340dfed1c3 100644
--- a/gdb/obsd-nat.c
+++ b/gdb/obsd-nat.c
@@ -145,6 +145,8 @@ obsd_nat_target::post_attach (int pid)
   obsd_enable_proc_events (pid);
 }
 
+/* Implement the virtual inf_ptrace_target::post_startup_inferior method.  */
+
 void
 obsd_nat_target::post_startup_inferior (ptid_t pid)
 {
diff --git a/gdb/obsd-nat.h b/gdb/obsd-nat.h
index 1f74b9984c5..5ebbf61db40 100644
--- a/gdb/obsd-nat.h
+++ b/gdb/obsd-nat.h
@@ -35,9 +35,10 @@ class obsd_nat_target : public inf_ptrace_target
 
   int remove_fork_catchpoint (int) override;
 
-  void post_startup_inferior (ptid_t) override;
-
   void post_attach (int) override;
+
+protected:
+  void post_startup_inferior (ptid_t) override;
 };
 
 #endif /* obsd-nat.h */
diff --git a/gdb/rs6000-aix-nat.c b/gdb/rs6000-aix-nat.c
index 72e59e5e484..4da06be1113 100644
--- a/gdb/rs6000-aix-nat.c
+++ b/gdb/rs6000-aix-nat.c
@@ -91,6 +91,11 @@ class rs6000_nat_target final : public inf_ptrace_target
 
   ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
 
+protected:
+
+  void post_startup_inferior (ptid_t ptid) override
+  { /* Nothing.  */ }
+
 private:
   enum target_xfer_status
     xfer_shared_libraries (enum target_object object,
diff --git a/gdb/target-delegates.c b/gdb/target-delegates.c
index fb9c78a5f79..9636e3212bc 100644
--- a/gdb/target-delegates.c
+++ b/gdb/target-delegates.c
@@ -51,7 +51,6 @@ struct dummy_target : public target_ops
   void terminal_info (const char *arg0, int arg1) override;
   void kill () override;
   void load (const char *arg0, int arg1) override;
-  void post_startup_inferior (ptid_t arg0) override;
   int insert_fork_catchpoint (int arg0) override;
   int remove_fork_catchpoint (int arg0) override;
   int insert_vfork_catchpoint (int arg0) override;
@@ -226,7 +225,6 @@ struct debug_target : public target_ops
   void terminal_info (const char *arg0, int arg1) override;
   void kill () override;
   void load (const char *arg0, int arg1) override;
-  void post_startup_inferior (ptid_t arg0) override;
   int insert_fork_catchpoint (int arg0) override;
   int remove_fork_catchpoint (int arg0) override;
   int insert_vfork_catchpoint (int arg0) override;
@@ -1393,27 +1391,6 @@ debug_target::load (const char *arg0, int arg1)
   fputs_unfiltered (")\n", gdb_stdlog);
 }
 
-void
-target_ops::post_startup_inferior (ptid_t arg0)
-{
-  this->beneath ()->post_startup_inferior (arg0);
-}
-
-void
-dummy_target::post_startup_inferior (ptid_t arg0)
-{
-}
-
-void
-debug_target::post_startup_inferior (ptid_t arg0)
-{
-  fprintf_unfiltered (gdb_stdlog, "-> %s->post_startup_inferior (...)\n", this->beneath ()->shortname ());
-  this->beneath ()->post_startup_inferior (arg0);
-  fprintf_unfiltered (gdb_stdlog, "<- %s->post_startup_inferior (", this->beneath ()->shortname ());
-  target_debug_print_ptid_t (arg0);
-  fputs_unfiltered (")\n", gdb_stdlog);
-}
-
 int
 target_ops::insert_fork_catchpoint (int arg0)
 {
diff --git a/gdb/target.c b/gdb/target.c
index 06a21c46a19..65d2a1ca514 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -302,14 +302,6 @@ target_files_info ()
 
 /* See target.h.  */
 
-void
-target_post_startup_inferior (ptid_t ptid)
-{
-  return current_inferior ()->top_target ()->post_startup_inferior (ptid);
-}
-
-/* See target.h.  */
-
 int
 target_insert_fork_catchpoint (int pid)
 {
diff --git a/gdb/target.h b/gdb/target.h
index e709b7d7cfd..4736d32e78c 100644
--- a/gdb/target.h
+++ b/gdb/target.h
@@ -626,8 +626,6 @@ struct target_ops
     virtual bool can_create_inferior ();
     virtual void create_inferior (const char *, const std::string &,
 				  char **, int);
-    virtual void post_startup_inferior (ptid_t)
-      TARGET_DEFAULT_IGNORE ();
     virtual int insert_fork_catchpoint (int)
       TARGET_DEFAULT_RETURN (1);
     virtual int remove_fork_catchpoint (int)
@@ -1688,18 +1686,6 @@ extern void target_kill (void);
 
 extern void target_load (const char *arg, int from_tty);
 
-/* Some targets (such as ttrace-based HPUX) don't allow us to request
-   notification of inferior events such as fork and vork immediately
-   after the inferior is created.  (This because of how gdb gets an
-   inferior created via invoking a shell to do it.  In such a scenario,
-   if the shell init file has commands in it, the shell will fork and
-   exec for each of those commands, and we will see each such fork
-   event.  Very bad.)
-
-   Such targets will supply an appropriate definition for this function.  */
-
-extern void target_post_startup_inferior (ptid_t ptid);
-
 /* On some targets, we can catch an inferior fork or vfork event when
    it occurs.  These functions insert/remove an already-created
    catchpoint for such events.  They return  0 for success, 1 if the
diff --git a/gdb/x86-linux-nat.c b/gdb/x86-linux-nat.c
index adea1ad0092..71769085493 100644
--- a/gdb/x86-linux-nat.c
+++ b/gdb/x86-linux-nat.c
@@ -81,6 +81,8 @@ x86_linux_nat_target::~x86_linux_nat_target ()
 {
 }
 
+/* Implement the virtual inf_ptrace_target::post_startup_inferior method.  */
+
 void
 x86_linux_nat_target::post_startup_inferior (ptid_t ptid)
 {
diff --git a/gdb/x86-linux-nat.h b/gdb/x86-linux-nat.h
index a8123f76491..e1f232adae8 100644
--- a/gdb/x86-linux-nat.h
+++ b/gdb/x86-linux-nat.h
@@ -29,9 +29,6 @@ struct x86_linux_nat_target : public x86_nat_target<linux_nat_target>
 {
   virtual ~x86_linux_nat_target () override = 0;
 
-  /* Override the GNU/Linux inferior startup hook.  */
-  void post_startup_inferior (ptid_t) override;
-
   /* Add the description reader.  */
   const struct target_desc *read_description () override;
 
@@ -73,6 +70,10 @@ struct x86_linux_nat_target : public x86_nat_target<linux_nat_target>
 
   void low_delete_thread (struct arch_lwp_info *lwp) override
   { x86_linux_delete_thread (lwp); }
+
+protected:
+  /* Override the GNU/Linux inferior startup hook.  */
+  void post_startup_inferior (ptid_t) override;
 };
 
 \f
-- 
2.25.4


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

* Re: [PATCH 1/3] gdb: have mips_nbsd_nat_target inherit from nbsd_nat_target
  2021-12-03 10:28 ` [PATCH 1/3] gdb: have mips_nbsd_nat_target inherit from nbsd_nat_target Andrew Burgess
@ 2021-12-03 16:23   ` John Baldwin
  0 siblings, 0 replies; 12+ messages in thread
From: John Baldwin @ 2021-12-03 16:23 UTC (permalink / raw)
  To: Andrew Burgess, gdb-patches

On 12/3/21 2:28 AM, Andrew Burgess via Gdb-patches wrote:
> While working on another patch I had reason to look at
> mips-netbsd-nat.c, and noticed that the class mips_nbsd_nat_target
> inherits directly from inf_ptrace_target.
> 
> This is a little strange as alpha_bsd_nat_target,
> arm_netbsd_nat_target, hppa_nbsd_nat_target, m68k_bsd_nat_target,
> ppc_nbsd_nat_target, sh_nbsd_nat_target, and vax_bsd_nat_target all
> inherit from nbsd_nat_target.
> 
> Originally, in this commit:
> 
>    commit f6ac5f3d63e03a81c4ff3749aba234961cc9090e
>    Date:   Thu May 3 00:37:22 2018 +0100
> 
>        Convert struct target_ops to C++
> 
> When the target tree was converted to C++, all of the above classes
> inherited from inf_ptrace_target except for hppa_nbsd_nat_target,
> which was the only class that originally inherited from
> nbsd_nat_target.
> 
> Later on all the remaining targets (except mips) were converted to
> inherit from nbsd_nat_target, these are the commits:

I think your change is likely correct and that missing MIPS was just
an oversight.  I've cc'd Kamil who did all the recent NetBSD changes
in case he wants to chime in.

-- 
John Baldwin

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

* Re: [PATCH 3/3] gdb: make post_startup_inferior a virtual method on inf_ptrace_target
  2021-12-03 10:28 ` [PATCH 3/3] gdb: make post_startup_inferior a virtual method on inf_ptrace_target Andrew Burgess
@ 2021-12-03 16:39   ` John Baldwin
  2021-12-07 19:10     ` Tom Tromey
  2021-12-08 11:46     ` Andrew Burgess
  0 siblings, 2 replies; 12+ messages in thread
From: John Baldwin @ 2021-12-03 16:39 UTC (permalink / raw)
  To: Andrew Burgess, gdb-patches

On 12/3/21 2:28 AM, Andrew Burgess via Gdb-patches wrote:
> While working on a later patch that required me to understand how GDB
> starts up inferiors, I was confused by the
> target_ops::post_startup_inferior method.
> 
> The post_startup_inferior target function is only called from
> inf_ptrace_target::create_inferior.
> 
> Part of the target class hierarchy looks like this:
> 
>    inf_child_target
>       |
>       '-- inf_ptrace_target
>              |
>              |-- linux_nat_target
>              |
>              |-- fbsd_nat_target
>              |
>              |-- nbsd_nat_target
>              |
>              |-- obsd_nat_target
>              |
>              '-- rs6000_nat_target
> 
> Every sub-class of inf_ptrace_target, except rs6000_nat_target,
> implements ::post_startup_inferior.  The rs6000_nat_target picks up
> the implementation of ::post_startup_inferior not from
> inf_ptrace_target, but from inf_child_target.
> 
> No descendent of inf_child_target, outside the inf_ptrace_target
> sub-tree, implements ::post_startup_inferior, which isn't really
> surprising, as they would never see the method called (remember, the
> method is only called from inf_ptrace_target::create_inferior).
> 
> What I find confusing is the role inf_child_target plays in
> implementing, what is really a helper function for just one of its
> descendents.
> 
> In this commit I propose that we formally make ::post_startup_inferior
> a helper function of inf_ptrace_target.  To do this I will remove the
> ::post_startup_inferior from the target_ops API, and instead make this
> a protected, pure virtual function on inf_ptrace_target.
> 
> I'll remove the empty implementation of ::post_startup_inferior from
> the inf_child_target class, and add a new empty implementation to the
> rs6000_nat_target class.
> 
> All the other descendents of inf_ptrace_target already provide an
> implementation of this method and so don't need to change beyond
> making the method protected within their class declarations.
> 
> To me, this makes much more sense now.  The helper function, which is
> only called from within the inf_ptrace_target class, is now a part of
> the inf_ptrace_target class.
> 
> The only way in which this change is visible to a user is if the user
> turns on 'set debug target 1'.  With this debug flag on, prior to this
> patch the user would see something like:
> 
>    -> native->post_startup_inferior (...)
>    <- native->post_startup_inferior (2588939)
> 
> After this patch these lines are no longer present, as the
> post_startup_inferior is no longer a top level target method.  For me,
> this is an acceptable change.

The idea sounds fine.

> diff --git a/gdb/inf-ptrace.h b/gdb/inf-ptrace.h
> index 8aded9b60db..af61d829fc8 100644
> --- a/gdb/inf-ptrace.h
> +++ b/gdb/inf-ptrace.h
> @@ -60,6 +60,18 @@ struct inf_ptrace_target : public inf_child_target
>   protected:
>     /* Cleanup the inferior after a successful ptrace detach.  */
>     void detach_success (inferior *inf);
> +
> +  /* Some targets (such as ttrace-based HPUX) don't allow us to request
> +     notification of inferior events such as fork and vork immediately
> +     after the inferior is created.  (This because of how gdb gets an
> +     inferior created via invoking a shell to do it.  In such a scenario,
> +     if the shell init file has commands in it, the shell will fork and
> +     exec for each of those commands, and we will see each such fork
> +     event.  Very bad.)
> +
> +     Such targets will supply an appropriate definition for this
> +     function.  */
> +  virtual void post_startup_inferior (ptid_t ptid) = 0;
>   };

I realize this is a cut and paste of the existing comment, but does this
warrant some updating?  I'm not sure if GDB even supports HPUX anymore as
a target?  (All the *hpux* triples are obsolete in configure.tgt)  Also,
perhaps s/vork/vfork/ while here?

Alternatively, a more invasive patch (but perhaps one a bit more OOP-like)
would be to have the various nat targets override create_inferior rather
than having a separate target method.  For example, in fbsd-nat.c it would
be something like this:

void
fbsd_nat_target::create_inferior (const char *exec_file,
                                   const std::string &allargs,
                                   char **env, int from_tty)
{
    inf_ptrace_target::create_inferior (exec_file, allargs, env, from_tty);

    fbsd_enable_proc_events (inferior_ptid.pid ());
}

That removes the need for the comment above as each child class can comment
if needed.

-- 
John Baldwin

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

* Re: [PATCH 2/3] gdb: rename gdb_startup_inferior to inf_child_target::startup_inferior
  2021-12-03 10:28 ` [PATCH 2/3] gdb: rename gdb_startup_inferior to inf_child_target::startup_inferior Andrew Burgess
@ 2021-12-07 18:58   ` Tom Tromey
  2021-12-09 18:45   ` Andrew Burgess
  1 sibling, 0 replies; 12+ messages in thread
From: Tom Tromey @ 2021-12-07 18:58 UTC (permalink / raw)
  To: Andrew Burgess via Gdb-patches; +Cc: Andrew Burgess

>>>>> "Andrew" == Andrew Burgess via Gdb-patches <gdb-patches@sourceware.org> writes:

Andrew> I notice that gdb_startup_inferior is only ever called from within
Andrew> classes derived from inf_child_target, which makes sense given what
Andrew> the function does.

Andrew> This commit makes the function a protected method within
Andrew> inf_child_target, and updates all the callers.

Looks reasonable to me.

Tom

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

* Re: [PATCH 3/3] gdb: make post_startup_inferior a virtual method on inf_ptrace_target
  2021-12-03 16:39   ` John Baldwin
@ 2021-12-07 19:10     ` Tom Tromey
  2021-12-08 11:46     ` Andrew Burgess
  1 sibling, 0 replies; 12+ messages in thread
From: Tom Tromey @ 2021-12-07 19:10 UTC (permalink / raw)
  To: John Baldwin; +Cc: Andrew Burgess, gdb-patches

>>>>> "John" == John Baldwin <jhb@FreeBSD.org> writes:

John> Alternatively, a more invasive patch (but perhaps one a bit more OOP-like)
John> would be to have the various nat targets override create_inferior rather
John> than having a separate target method.  For example, in fbsd-nat.c it would
John> be something like this:

This makes sense to me, though I'm also fine with the existing patch.

Tom

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

* Re: [PATCH 3/3] gdb: make post_startup_inferior a virtual method on inf_ptrace_target
  2021-12-03 16:39   ` John Baldwin
  2021-12-07 19:10     ` Tom Tromey
@ 2021-12-08 11:46     ` Andrew Burgess
  2021-12-08 18:11       ` John Baldwin
  1 sibling, 1 reply; 12+ messages in thread
From: Andrew Burgess @ 2021-12-08 11:46 UTC (permalink / raw)
  To: John Baldwin; +Cc: gdb-patches

* John Baldwin <jhb@FreeBSD.org> [2021-12-03 08:39:52 -0800]:

> On 12/3/21 2:28 AM, Andrew Burgess via Gdb-patches wrote:
> > While working on a later patch that required me to understand how GDB
> > starts up inferiors, I was confused by the
> > target_ops::post_startup_inferior method.
> > 
> > The post_startup_inferior target function is only called from
> > inf_ptrace_target::create_inferior.
> > 
> > Part of the target class hierarchy looks like this:
> > 
> >    inf_child_target
> >       |
> >       '-- inf_ptrace_target
> >              |
> >              |-- linux_nat_target
> >              |
> >              |-- fbsd_nat_target
> >              |
> >              |-- nbsd_nat_target
> >              |
> >              |-- obsd_nat_target
> >              |
> >              '-- rs6000_nat_target
> > 
> > Every sub-class of inf_ptrace_target, except rs6000_nat_target,
> > implements ::post_startup_inferior.  The rs6000_nat_target picks up
> > the implementation of ::post_startup_inferior not from
> > inf_ptrace_target, but from inf_child_target.
> > 
> > No descendent of inf_child_target, outside the inf_ptrace_target
> > sub-tree, implements ::post_startup_inferior, which isn't really
> > surprising, as they would never see the method called (remember, the
> > method is only called from inf_ptrace_target::create_inferior).
> > 
> > What I find confusing is the role inf_child_target plays in
> > implementing, what is really a helper function for just one of its
> > descendents.
> > 
> > In this commit I propose that we formally make ::post_startup_inferior
> > a helper function of inf_ptrace_target.  To do this I will remove the
> > ::post_startup_inferior from the target_ops API, and instead make this
> > a protected, pure virtual function on inf_ptrace_target.
> > 
> > I'll remove the empty implementation of ::post_startup_inferior from
> > the inf_child_target class, and add a new empty implementation to the
> > rs6000_nat_target class.
> > 
> > All the other descendents of inf_ptrace_target already provide an
> > implementation of this method and so don't need to change beyond
> > making the method protected within their class declarations.
> > 
> > To me, this makes much more sense now.  The helper function, which is
> > only called from within the inf_ptrace_target class, is now a part of
> > the inf_ptrace_target class.
> > 
> > The only way in which this change is visible to a user is if the user
> > turns on 'set debug target 1'.  With this debug flag on, prior to this
> > patch the user would see something like:
> > 
> >    -> native->post_startup_inferior (...)
> >    <- native->post_startup_inferior (2588939)
> > 
> > After this patch these lines are no longer present, as the
> > post_startup_inferior is no longer a top level target method.  For me,
> > this is an acceptable change.
> 
> The idea sounds fine.
> 
> > diff --git a/gdb/inf-ptrace.h b/gdb/inf-ptrace.h
> > index 8aded9b60db..af61d829fc8 100644
> > --- a/gdb/inf-ptrace.h
> > +++ b/gdb/inf-ptrace.h
> > @@ -60,6 +60,18 @@ struct inf_ptrace_target : public inf_child_target
> >   protected:
> >     /* Cleanup the inferior after a successful ptrace detach.  */
> >     void detach_success (inferior *inf);
> > +
> > +  /* Some targets (such as ttrace-based HPUX) don't allow us to request
> > +     notification of inferior events such as fork and vork immediately
> > +     after the inferior is created.  (This because of how gdb gets an
> > +     inferior created via invoking a shell to do it.  In such a scenario,
> > +     if the shell init file has commands in it, the shell will fork and
> > +     exec for each of those commands, and we will see each such fork
> > +     event.  Very bad.)
> > +
> > +     Such targets will supply an appropriate definition for this
> > +     function.  */
> > +  virtual void post_startup_inferior (ptid_t ptid) = 0;
> >   };
> 
> I realize this is a cut and paste of the existing comment, but does this
> warrant some updating?  I'm not sure if GDB even supports HPUX anymore as
> a target?  (All the *hpux* triples are obsolete in configure.tgt)  Also,
> perhaps s/vork/vfork/ while here?
> 
> Alternatively, a more invasive patch (but perhaps one a bit more OOP-like)
> would be to have the various nat targets override create_inferior rather
> than having a separate target method.  For example, in fbsd-nat.c it would
> be something like this:
> 
> void
> fbsd_nat_target::create_inferior (const char *exec_file,
>                                   const std::string &allargs,
>                                   char **env, int from_tty)
> {
>    inf_ptrace_target::create_inferior (exec_file, allargs, env, from_tty);
> 
>    fbsd_enable_proc_events (inferior_ptid.pid ());
> }
> 
> That removes the need for the comment above as each child class can comment
> if needed.

Thanks for the suggestions.  I've updated the comment, but have not
restructured the code as part of this commit.  That doesn't mean it
couldn't be done in the future though.

My reasoning was that, though the restructure you suggest works well
for the fbsd_nat_target, I don't think the change is as compelling for
other targets, for example: linux_nat_target implements
create_inferior and post_startup_inferior, however,
post_startup_inferior is overridden by sub-classes of
linux_nat_target, so we'd still want some kind of virtual method
system.  And its similar with inf_process_target and
nbst_nat_target/obsd_nat_target, but in this case the virtual method
would need to live on inf_ptrace_target anyway, so you'd end up having
to add an empty implementation to fbsd_nat_target ... then you've
basically back to where we are now.

Thanks,
Andrew


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

* Re: [PATCH 3/3] gdb: make post_startup_inferior a virtual method on inf_ptrace_target
  2021-12-08 11:46     ` Andrew Burgess
@ 2021-12-08 18:11       ` John Baldwin
  0 siblings, 0 replies; 12+ messages in thread
From: John Baldwin @ 2021-12-08 18:11 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: gdb-patches

On 12/8/21 3:46 AM, Andrew Burgess wrote:
> * John Baldwin <jhb@FreeBSD.org> [2021-12-03 08:39:52 -0800]:
> 
> Thanks for the suggestions.  I've updated the comment, but have not
> restructured the code as part of this commit.  That doesn't mean it
> couldn't be done in the future though.
> 
> My reasoning was that, though the restructure you suggest works well
> for the fbsd_nat_target, I don't think the change is as compelling for
> other targets, for example: linux_nat_target implements
> create_inferior and post_startup_inferior, however,
> post_startup_inferior is overridden by sub-classes of
> linux_nat_target, so we'd still want some kind of virtual method
> system.  And its similar with inf_process_target and
> nbst_nat_target/obsd_nat_target, but in this case the virtual method
> would need to live on inf_ptrace_target anyway, so you'd end up having
> to add an empty implementation to fbsd_nat_target ... then you've
> basically back to where we are now.

Ah, that's my fault for not looking more widely.  I think just updating
the comment is a fine.  The restructuring was more just a suggestion.

-- 
John Baldwin

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

* Re: [PATCH 2/3] gdb: rename gdb_startup_inferior to inf_child_target::startup_inferior
  2021-12-03 10:28 ` [PATCH 2/3] gdb: rename gdb_startup_inferior to inf_child_target::startup_inferior Andrew Burgess
  2021-12-07 18:58   ` Tom Tromey
@ 2021-12-09 18:45   ` Andrew Burgess
  1 sibling, 0 replies; 12+ messages in thread
From: Andrew Burgess @ 2021-12-09 18:45 UTC (permalink / raw)
  To: gdb-patches

* Andrew Burgess <aburgess@redhat.com> [2021-12-03 10:28:44 +0000]:

> I notice that gdb_startup_inferior is only ever called from within
> classes derived from inf_child_target, which makes sense given what
> the function does.
> 
> This commit makes the function a protected method within
> inf_child_target, and updates all the callers.

I'm dropping this patch from the series.  This patch breaks builds of
targets that don't use fork/exec to start native processes, for
example, configuring with `--enable-targets=all
--host=i686-w64-mingw32` will break with this patch.

To understand what happens, with the above configure options, the
gdb/windows-nat.c file is built into GDB and the files
gdb/fork-child.c and gdb/nat/fork-inferior.c are not built in to GDB.

As windows-nat.c doesn't call gdb_startup_inferior this was fine.

After this patch windows-nat.c still doesn't call
gdb_startup_inferior, but now, the content of that function has moved
into inf-child.c, which is built, and this function does call to a
function in gdb/nat/fork-inferior.c, which still isn't being built.

And so, we now have a missing symbol, and a link failure.

As neither of the other two patches in this series depend on this one,
I plan to push this series without this patch.

Thanks,
Andrew


> 
> There should be no user visible changes after this commit.
> ---
>  gdb/darwin-nat.c |  2 +-
>  gdb/fork-child.c | 17 +----------------
>  gdb/gnu-nat.c    |  2 +-
>  gdb/inf-child.c  | 17 +++++++++++++++++
>  gdb/inf-child.h  | 10 ++++++++++
>  gdb/inf-ptrace.c |  2 +-
>  gdb/inferior.h   |  7 -------
>  gdb/procfs.c     |  2 +-
>  8 files changed, 32 insertions(+), 27 deletions(-)
> 
> diff --git a/gdb/darwin-nat.c b/gdb/darwin-nat.c
> index e1aeb69e404..aa7eac1d7fb 100644
> --- a/gdb/darwin-nat.c
> +++ b/gdb/darwin-nat.c
> @@ -1753,7 +1753,7 @@ darwin_nat_target::ptrace_him (int pid)
>  
>    init_thread_list (inf);
>  
> -  gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
> +  startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
>  }
>  
>  static void
> diff --git a/gdb/fork-child.c b/gdb/fork-child.c
> index 3e995ed6f65..af8dca9ae26 100644
> --- a/gdb/fork-child.c
> +++ b/gdb/fork-child.c
> @@ -29,6 +29,7 @@
>  #include "gdbsupport/filestuff.h"
>  #include "nat/fork-inferior.h"
>  #include "gdbsupport/common-inferior.h"
> +#include "inf-child.h"
>  
>  /* The exec-wrapper, if any, that will be used when starting the
>     inferior.  */
> @@ -118,22 +119,6 @@ postfork_child_hook ()
>    new_tty ();
>  }
>  
> -/* See inferior.h.  */
> -
> -ptid_t
> -gdb_startup_inferior (pid_t pid, int num_traps)
> -{
> -  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 (proc_target, ptid, false);
> -
> -  return ptid;
> -}
> -
>  /* Implement the "unset exec-wrapper" command.  */
>  
>  static void
> diff --git a/gdb/gnu-nat.c b/gdb/gnu-nat.c
> index 13127cd8246..bd09fa94ecc 100644
> --- a/gdb/gnu-nat.c
> +++ b/gdb/gnu-nat.c
> @@ -2137,7 +2137,7 @@ gnu_nat_target::create_inferior (const char *exec_file,
>    thread_change_ptid (this, inferior_ptid,
>  		      ptid_t (inf->pid, inf_pick_first_thread (), 0));
>  
> -  gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
> +  startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
>  
>    inf->pending_execs = 0;
>    /* Get rid of the old shell threads.  */
> diff --git a/gdb/inf-child.c b/gdb/inf-child.c
> index 5e821f45598..780de98e267 100644
> --- a/gdb/inf-child.c
> +++ b/gdb/inf-child.c
> @@ -34,6 +34,7 @@
>  #include "gdbsupport/agent.h"
>  #include "gdbsupport/gdb_wait.h"
>  #include "gdbsupport/filestuff.h"
> +#include "nat/fork-inferior.h"
>  
>  #include <sys/types.h>
>  #include <fcntl.h>
> @@ -420,6 +421,22 @@ inf_child_target::follow_exec (inferior *follow_inf, ptid_t ptid,
>  
>  /* See inf-child.h.  */
>  
> +ptid_t
> +inf_child_target::startup_inferior (pid_t pid, int num_traps)
> +{
> +  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 (proc_target, ptid, false);
> +
> +  return ptid;
> +}
> +
> +/* See inf-child.h.  */
> +
>  void
>  add_inf_child_target (inf_child_target *target)
>  {
> diff --git a/gdb/inf-child.h b/gdb/inf-child.h
> index aa33c538138..0d13d6bc6e8 100644
> --- a/gdb/inf-child.h
> +++ b/gdb/inf-child.h
> @@ -102,6 +102,16 @@ class inf_child_target
>       done by calling either generic_mourn_inferior or
>       detach_inferior.  */
>    void maybe_unpush_target ();
> +
> +  /* Helper function to call the global STARTUP_INFERIOR with PID and
> +     NUM_TRAPS.  PID should correspond to the current inferior.  No threads
> +     in the current inferior should be marked as resumed when calling this
> +     method.
> +
> +     Return the ptid_t from STARTUP_INFERIOR, all the threads in the
> +     process matching the returned ptid_t are marked as non-executing, and
> +     non-resumed.  */
> +  ptid_t startup_inferior (pid_t pid, int num_traps);
>  };
>  
>  /* Functions for helping to write a native target.  */
> diff --git a/gdb/inf-ptrace.c b/gdb/inf-ptrace.c
> index 852636ba646..e6fe2d04522 100644
> --- a/gdb/inf-ptrace.c
> +++ b/gdb/inf-ptrace.c
> @@ -99,7 +99,7 @@ inf_ptrace_target::create_inferior (const char *exec_file,
>  
>    unpusher.release ();
>  
> -  gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
> +  startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
>  
>    /* On some targets, there must be some explicit actions taken after
>       the inferior has been started up.  */
> diff --git a/gdb/inferior.h b/gdb/inferior.h
> index e9210b1258d..a99d4afdc17 100644
> --- a/gdb/inferior.h
> +++ b/gdb/inferior.h
> @@ -185,13 +185,6 @@ extern void child_pass_ctrlc (struct target_ops *self);
>  
>  extern void child_interrupt (struct target_ops *self);
>  
> -/* From fork-child.c */
> -
> -/* Helper function to call STARTUP_INFERIOR with PID and NUM_TRAPS.
> -   This function already calls set_executing.  Return the ptid_t from
> -   STARTUP_INFERIOR.  */
> -extern ptid_t gdb_startup_inferior (pid_t pid, int num_traps);
> -
>  /* From infcmd.c */
>  
>  /* Initial inferior setup.  Determines the exec file is not yet known,
> diff --git a/gdb/procfs.c b/gdb/procfs.c
> index 2c96919dceb..c4b011da6ce 100644
> --- a/gdb/procfs.c
> +++ b/gdb/procfs.c
> @@ -2704,7 +2704,7 @@ procfs_target::procfs_init_inferior (int pid)
>       about it.  This changes inferior_ptid as well.  */
>    thread_change_ptid (this, ptid_t (pid), ptid_t (pid, lwpid, 0));
>  
> -  gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
> +  startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
>  }
>  
>  /* When GDB forks to create a new process, this function is called on
> -- 
> 2.25.4
> 


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

* Re: [PATCH 0/3] Refactor target_ops::post_startup_inferior
  2021-12-03 10:28 [PATCH 0/3] Refactor target_ops::post_startup_inferior Andrew Burgess
                   ` (2 preceding siblings ...)
  2021-12-03 10:28 ` [PATCH 3/3] gdb: make post_startup_inferior a virtual method on inf_ptrace_target Andrew Burgess
@ 2021-12-13 11:21 ` Andrew Burgess
  3 siblings, 0 replies; 12+ messages in thread
From: Andrew Burgess @ 2021-12-13 11:21 UTC (permalink / raw)
  To: gdb-patches

I've now pushed patches #1 and #3 from this series.  Patch #2 is
dropped as I discussed in the reply to that patch.

If Kamil (or anyone else) gets back to me w.r.t. patch #1, indicating
that this was an incorrect change, then I'm more than happy to revert
this patch, just let me know.

Thanks,
Andrew


* Andrew Burgess <aburgess@redhat.com> [2021-12-03 10:28:42 +0000]:

> While working on another patch series I needed to understand how
> post_startup_inferior is used.
> 
> I found the current setup confusing, so here's a small series which I
> think makes things clearer.
> 
> All feedback welcome,
> 
> Thanks,
> Andrew
> 
> ---
> 
> Andrew Burgess (3):
>   gdb: have mips_nbsd_nat_target inherit from nbsd_nat_target
>   gdb: rename gdb_startup_inferior to inf_child_target::startup_inferior
>   gdb: make post_startup_inferior a virtual method on inf_ptrace_target
> 
>  gdb/aarch64-linux-nat.c |  2 +-
>  gdb/darwin-nat.c        |  2 +-
>  gdb/fbsd-nat.c          |  2 +-
>  gdb/fbsd-nat.h          |  5 ++++-
>  gdb/fork-child.c        | 17 +----------------
>  gdb/gnu-nat.c           |  2 +-
>  gdb/inf-child.c         | 24 +++++++++++++++++-------
>  gdb/inf-child.h         | 12 ++++++++++--
>  gdb/inf-ptrace.c        |  4 ++--
>  gdb/inf-ptrace.h        | 12 ++++++++++++
>  gdb/inferior.h          |  7 -------
>  gdb/linux-nat.c         |  2 ++
>  gdb/linux-nat.h         |  6 ++++--
>  gdb/mips-netbsd-nat.c   |  3 ++-
>  gdb/netbsd-nat.c        |  2 +-
>  gdb/netbsd-nat.h        |  4 +++-
>  gdb/obsd-nat.c          |  2 ++
>  gdb/obsd-nat.h          |  5 +++--
>  gdb/procfs.c            |  2 +-
>  gdb/rs6000-aix-nat.c    |  5 +++++
>  gdb/target-delegates.c  | 23 -----------------------
>  gdb/target.c            |  8 --------
>  gdb/target.h            | 14 --------------
>  gdb/x86-linux-nat.c     |  2 ++
>  gdb/x86-linux-nat.h     |  7 ++++---
>  25 files changed, 79 insertions(+), 95 deletions(-)
> 
> -- 
> 2.25.4
> 


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

end of thread, other threads:[~2021-12-13 11:21 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-03 10:28 [PATCH 0/3] Refactor target_ops::post_startup_inferior Andrew Burgess
2021-12-03 10:28 ` [PATCH 1/3] gdb: have mips_nbsd_nat_target inherit from nbsd_nat_target Andrew Burgess
2021-12-03 16:23   ` John Baldwin
2021-12-03 10:28 ` [PATCH 2/3] gdb: rename gdb_startup_inferior to inf_child_target::startup_inferior Andrew Burgess
2021-12-07 18:58   ` Tom Tromey
2021-12-09 18:45   ` Andrew Burgess
2021-12-03 10:28 ` [PATCH 3/3] gdb: make post_startup_inferior a virtual method on inf_ptrace_target Andrew Burgess
2021-12-03 16:39   ` John Baldwin
2021-12-07 19:10     ` Tom Tromey
2021-12-08 11:46     ` Andrew Burgess
2021-12-08 18:11       ` John Baldwin
2021-12-13 11:21 ` [PATCH 0/3] Refactor target_ops::post_startup_inferior Andrew Burgess

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).