public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@efficios.com>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@efficios.com>, Pedro Alves <pedro@palves.net>
Subject: [PATCH 1/7] gdb: pass execing and following inferior to inferior_execd observers
Date: Mon,  3 Apr 2023 14:52:02 -0400	[thread overview]
Message-ID: <20230403185208.197965-2-simon.marchi@efficios.com> (raw)
In-Reply-To: <20230403185208.197965-1-simon.marchi@efficios.com>

The upcoming patch to support exec in the amd-dbgapi target needs to
detach amd-dbgapi from the inferior doing the exec and attach amd-dbgapi
to the inferior continuing the execution.  They may or may not be the
same, depending on the `set follow-exec-mode` setting.  But even if they
are the same, we need to do the detach / attach dance.

With the current observable signature, the observers only receive the
inferior in which execution continues (the "following" inferior).

Change the signature to pass both inferiors, and update all existing
observers.

Change-Id: I259d1ea09f70f43be739378d6023796f2fce2659
Reviewed-By: Pedro Alves <pedro@palves.net>
---
 gdb/infrun.c     | 39 +++++++++++++++++++++------------------
 gdb/jit.c        | 20 ++++++++++++++------
 gdb/linux-tdep.c | 10 +++++++++-
 gdb/observable.h |  8 ++++++--
 gdb/solib.c      |  3 ++-
 5 files changed, 52 insertions(+), 28 deletions(-)

diff --git a/gdb/infrun.c b/gdb/infrun.c
index 87141117dfe3..c95a8e7ee1da 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1293,7 +1293,8 @@ follow_exec (ptid_t ptid, const char *exec_file_target)
      previous incarnation of this process.  */
   no_shared_libraries (nullptr, 0);
 
-  struct inferior *inf = current_inferior ();
+  inferior *execing_inferior = current_inferior ();
+  inferior *following_inferior;
 
   if (follow_exec_mode_string == follow_exec_mode_new)
     {
@@ -1304,19 +1305,19 @@ follow_exec (ptid_t ptid, const char *exec_file_target)
 	 inferior's pid.  Having two inferiors with the same pid would confuse
 	 find_inferior_p(t)id.  Transfer the terminal state and info from the
 	  old to the new inferior.  */
-      inferior *new_inferior = add_inferior_with_spaces ();
-
-      swap_terminal_info (new_inferior, inf);
-      exit_inferior_silent (inf);
+      following_inferior = add_inferior_with_spaces ();
 
-      new_inferior->pid = pid;
-      target_follow_exec (new_inferior, ptid, exec_file_target);
+      swap_terminal_info (following_inferior, execing_inferior);
+      exit_inferior_silent (execing_inferior);
 
-      /* We continue with the new inferior.  */
-      inf = new_inferior;
+      following_inferior->pid = pid;
     }
   else
     {
+      /* follow-exec-mode is "same", we continue execution in the execing
+	 inferior.  */
+      following_inferior = execing_inferior;
+
       /* The old description may no longer be fit for the new image.
 	 E.g, a 64-bit process exec'ed a 32-bit process.  Clear the
 	 old description; we'll read a new one below.  No need to do
@@ -1324,18 +1325,20 @@ follow_exec (ptid_t ptid, const char *exec_file_target)
 	 around (its description is later cleared/refetched on
 	 restart).  */
       target_clear_description ();
-      target_follow_exec (inf, ptid, exec_file_target);
     }
 
-  gdb_assert (current_inferior () == inf);
-  gdb_assert (current_program_space == inf->pspace);
+  target_follow_exec (following_inferior, ptid, exec_file_target);
+
+  gdb_assert (current_inferior () == following_inferior);
+  gdb_assert (current_program_space == following_inferior->pspace);
 
   /* Attempt to open the exec file.  SYMFILE_DEFER_BP_RESET is used
      because the proper displacement for a PIE (Position Independent
      Executable) main symbol file will only be computed by
      solib_create_inferior_hook below.  breakpoint_re_set would fail
      to insert the breakpoints with the zero displacement.  */
-  try_open_exec_file (exec_file_host.get (), inf, SYMFILE_DEFER_BP_RESET);
+  try_open_exec_file (exec_file_host.get (), following_inferior,
+		      SYMFILE_DEFER_BP_RESET);
 
   /* If the target can specify a description, read it.  Must do this
      after flipping to the new executable (because the target supplied
@@ -1345,7 +1348,7 @@ follow_exec (ptid_t ptid, const char *exec_file_target)
      registers.  */
   target_find_description ();
 
-  gdb::observers::inferior_execd.notify (inf);
+  gdb::observers::inferior_execd.notify (execing_inferior, following_inferior);
 
   breakpoint_re_set ();
 
@@ -1622,15 +1625,15 @@ infrun_inferior_exit (struct inferior *inf)
 }
 
 static void
-infrun_inferior_execd (inferior *inf)
+infrun_inferior_execd (inferior *exec_inf, inferior *follow_inf)
 {
   /* If some threads where was doing a displaced step in this inferior at the
      moment of the exec, they no longer exist.  Even if the exec'ing thread
      doing a displaced step, we don't want to to any fixup nor restore displaced
      stepping buffer bytes.  */
-  inf->displaced_step_state.reset ();
+  follow_inf->displaced_step_state.reset ();
 
-  for (thread_info *thread : inf->threads ())
+  for (thread_info *thread : follow_inf->threads ())
     thread->displaced_step_state.reset ();
 
   /* Since an in-line step is done with everything else stopped, if there was
@@ -1638,7 +1641,7 @@ infrun_inferior_execd (inferior *inf)
      thread.  */
   clear_step_over_info ();
 
-  inf->thread_waiting_for_vfork_done = nullptr;
+  follow_inf->thread_waiting_for_vfork_done = nullptr;
 }
 
 /* If ON, and the architecture supports it, GDB will use displaced
diff --git a/gdb/jit.c b/gdb/jit.c
index e276b3417a04..e085d5623336 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -1147,7 +1147,10 @@ jit_prepend_unwinder (struct gdbarch *gdbarch)
     }
 }
 
-/* Register any already created translations.  */
+/* Looks for the descriptor and registration symbols and breakpoints
+   the registration function.  If it finds both, it registers all the
+   already JITed code.  If it has already found the symbols, then it
+   doesn't try again.  */
 
 static void
 jit_inferior_init (inferior *inf)
@@ -1203,10 +1206,7 @@ jit_inferior_init (inferior *inf)
     }
 }
 
-/* Looks for the descriptor and registration symbols and breakpoints
-   the registration function.  If it finds both, it registers all the
-   already JITed code.  If it has already found the symbols, then it
-   doesn't try again.  */
+/* inferior_created observer.  */
 
 static void
 jit_inferior_created_hook (inferior *inf)
@@ -1214,6 +1214,14 @@ jit_inferior_created_hook (inferior *inf)
   jit_inferior_init (inf);
 }
 
+/* inferior_execd observer.  */
+
+static void
+jit_inferior_execd_hook (inferior *exec_inf, inferior *follow_inf)
+{
+  jit_inferior_init (follow_inf);
+}
+
 /* Exported routine to call to re-set the jit breakpoints,
    e.g. when a program is rerun.  */
 
@@ -1304,7 +1312,7 @@ _initialize_jit ()
 	   &maintenanceinfolist);
 
   gdb::observers::inferior_created.attach (jit_inferior_created_hook, "jit");
-  gdb::observers::inferior_execd.attach (jit_inferior_created_hook, "jit");
+  gdb::observers::inferior_execd.attach (jit_inferior_execd_hook, "jit");
   gdb::observers::inferior_exit.attach (jit_inferior_exit_hook, "jit");
   gdb::observers::breakpoint_deleted.attach (jit_breakpoint_deleted, "jit");
 
diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index 1fc9cb6faee9..b5eee5e108ce 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -244,6 +244,14 @@ invalidate_linux_cache_inf (struct inferior *inf)
   linux_inferior_data.clear (inf);
 }
 
+/* inferior_execd observer.  */
+
+static void
+linux_inferior_execd (inferior *exec_inf, inferior *follow_inf)
+{
+  invalidate_linux_cache_inf (follow_inf);
+}
+
 /* Fetch the linux cache info for INF.  This function always returns a
    valid INFO pointer.  */
 
@@ -2789,7 +2797,7 @@ _initialize_linux_tdep ()
 					"linux-tdep");
   gdb::observers::inferior_appeared.attach (invalidate_linux_cache_inf,
 					    "linux-tdep");
-  gdb::observers::inferior_execd.attach (invalidate_linux_cache_inf,
+  gdb::observers::inferior_execd.attach (linux_inferior_execd,
 					 "linux-tdep");
 
   add_setshow_boolean_cmd ("use-coredump-filter", class_files,
diff --git a/gdb/observable.h b/gdb/observable.h
index efd0446e1689..00955cbc876b 100644
--- a/gdb/observable.h
+++ b/gdb/observable.h
@@ -90,8 +90,12 @@ extern observable<> executable_changed;
    information on the inferior has been printed.  */
 extern observable<inferior */* inferior */> inferior_created;
 
-/* The inferior INF has exec'ed a new executable file.  */
-extern observable<struct inferior */* inf */> inferior_execd;
+/* The inferior EXEC_INF has exec'ed a new executable file.
+
+   Execution continues in FOLLOW_INF, which may or may not be the same as
+   EXEC_INF, depending on "set follow-exec-mode".  */
+extern observable<inferior */* exec_inf */, inferior */* follow_inf */>
+    inferior_execd;
 
 /* The status of process record for inferior inferior in gdb has
    changed.  The process record is started if STARTED is true, and
diff --git a/gdb/solib.c b/gdb/solib.c
index 09bee497fd6e..16147830ef2c 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -1744,7 +1744,8 @@ _initialize_solib ()
 {
   gdb::observers::free_objfile.attach (remove_user_added_objfile,
 				       "solib");
-  gdb::observers::inferior_execd.attach ([] (inferior *inf)
+  gdb::observers::inferior_execd.attach ([] (inferior *exec_inf,
+					     inferior *follow_inf)
     {
       solib_create_inferior_hook (0);
     }, "solib");
-- 
2.40.0


  reply	other threads:[~2023-04-03 18:52 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-03 18:52 [PATCH 0/7] amdgpu: handle fork and exec Simon Marchi
2023-04-03 18:52 ` Simon Marchi [this message]
2023-04-03 18:52 ` [PATCH 2/7] gdb: add inferior_forked observable Simon Marchi
2023-04-03 18:52 ` [PATCH 3/7] gdb: remove regcache::target Simon Marchi
2023-04-03 18:52 ` [PATCH 4/7] gdb: add maybe_switch_inferior function Simon Marchi
2023-04-03 18:52 ` [PATCH 5/7] gdb: make regcache::raw_update switch to right inferior Simon Marchi
2023-04-03 18:52 ` [PATCH 6/7] gdb: switch to right inferior in fetch_inferior_event Simon Marchi
2023-04-03 18:52 ` [PATCH 7/7] gdb/amdgpu: add follow fork and exec support Simon Marchi
2023-04-13 15:54 ` [PATCH 0/7] amdgpu: handle fork and exec Simon Marchi
2023-04-17 17:57   ` Simon Marchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230403185208.197965-2-simon.marchi@efficios.com \
    --to=simon.marchi@efficios.com \
    --cc=gdb-patches@sourceware.org \
    --cc=pedro@palves.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).