[v3,1/3] gdb: call post_create_inferior at end of follow_fork_inferior

Message ID 20210610143305.2018627-2-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.
GDB doesn't handle well the case of an inferior using the JIT interface
to register JIT-ed objiles and forking.  If an inferior registers a code
object using the JIT interface and then forks, the child process
conceptually has the same code object loaded, so GDB should look it up
and learn about it (it currently doesn't).

To achieve this, I think it would make sense to have the
inferior_created observable called when an inferior is created due to a
fork in follow_fork_inferior.  The inferior_created observable is
currently called both after starting a new inferior and after attaching
to an inferior, allowing various sub-components to learn about that new
executing inferior.  We can see handling a fork child just like
attaching to it, so any work done when attaching should also be done in
the case of a fork child.

Instead of just calling the inferior_created observable, this patch
makes follow_fork_inferior call the whole post_create_inferior function.
This way, the attach and follow-fork code code paths are more alike.

Given that post_create_inferior calls solib_create_inferior_hook,
follow_fork_inferior doesn't need to do it itself, so those calls to
solib_create_inferior_hook are removed.

One question you may have: why not just call post_create_inferior at the
places where solib_create_inferior_hook is currently called, instead of
after target_follow_fork?

 - there's something fishy for the second solib_create_inferior_hook
   call site: at this point we have switched the current program space
   to the child's, but not the current inferior nor the current thread.
   So solib_create_inferior_hook (and everything under, including
   check_for_thread_db, for example) is called with inferior 1 as the
   current inferior and inferior 2's program space as the current
   program space.  I think that's wrong, because at this point we are
   setting up inferior 2, and all that code relies on the current
   inferior.  We could just add a switch_to_thread call before it to
   make inferior 2 the current one, but there are other problems (see
   below).

 - solib_create_inferior_hook is currently not called on the
   `follow_child && detach_fork` path.  I think we need to call it,
   because we still get a new inferior in that case (even though we
   detach the parent).  If we only call post_create_inferior where
   solib_create_inferior_hook used to be called, then the JIT
   subcomponent doesn't get informed about the new inferior, and that
   introduces a failure in the new gdb.base/jit-elf-fork.exp test.

 - if we try to put the post_create_inferior just after the
   switch_to_thread that was originally at line 662, or just before the
   call to target_follow_fork, we introduce a subtle failure in
   gdb.threads/fork-thread-pending.exp.  What happens then is that
   libthread_db gets loaded (somewhere under post_create_inferior)
   before the linux-nat target learns about the LWPs (which happens in
   linux_nat_target::follow_fork).  As a result, the ALL_LWPS loop in
   try_thread_db_load_1 doesn't see the child LWP, and the thread-db
   target doesn't have the chance to fill in thread_info::priv.  A bit
   later, when the test does "info threads", and
   thread_db_target::pid_to_str is called, the thread-db target doesn't
   recognize the thread as one of its own, and delegates the request to
   the target below.  Because the pid_to_str output is not the expected
   one, the test fails.

   This tells me that we need to call the process target's follow_fork
   first, to make the process target create the necessary LWP and thread
   structures.  Then, we can call post_create_inferior to let the other
   components of GDB do their thing.

   But then you may ask: check_for_thread_db is already called today,
   somewhere under solib_create_inferior_hook, and that is before
   target_follow_fork, why don't we see this ordering problem!?  Well,
   because of the first bullet point: when check_for_thread_db /
   thread_db_load are called, the current inferior is (erroneously)
   inferior 1, the parent.  Because libthread_db is already loaded for
   the parent, thread_db_load early returns.  check_for_thread_db later
   gets called by linux_nat_target::follow_fork.  At this point, the
   current inferior is the correct one and the child's LWP exists, so
   all is well.

Since we now call post_create_inferior after target_follow_fork, which
calls the inferior_created observable, which calls check_for_thread_db,
I don't think linux_nat_target needs to explicitly call
check_for_thread_db itself, so that is removed.

In terms of testing, this patch adds a new gdb.base/jit-elf-fork.exp
test.  It makes an inferior register a JIT code object and then fork.
It then verifies that whatever the detach-on-fork and follow-fork-child
parameters are, GDB knows about the JIT code object in all the inferiors
that survive the fork.  It verifies that the inferiors can unload that
code object.

There isn't currently a way to get visibility into GDB's idea of the JIT
code objects for each inferior.  For the purpose of this test, add the
"maintenance info jit" command.  There isn't much we can print about the
JIT code objects except their load address.  So the output looks a bit
bare, but it's good enough for the test.

gdb/ChangeLog:

	* NEWS: Mention "maint info jit" command.
	* infrun.c (follow_fork_inferior): Don't call
	solib_create_inferior_hook, call post_create_inferior if a new
	inferior was created.
	* jit.c (maint_info_jit_cmd): New.
	(_initialize_jit): Register new command.
	* linux-nat.c (linux_nat_target::follow_fork): Don't call
	check_for_thread_db.
	* linux-nat.h (check_for_thread_db): Remove declaration.
	* linux-thread-db.c (check_thread_signals): Make static.

gdb/doc/ChangeLog:

	* gdb.texinfo (Maintenance Commands): Mention "maint info jit".

gdb/testsuite/ChangeLog:

	* gdb.base/jit-elf-fork-main.c: New test.
	* gdb.base/jit-elf-fork-solib.c: New test.
	* gdb.base/jit-elf-fork.exp: New test.

Change-Id: I9a192e55b8a451c00e88100669283fc9ca60de5c
---
 gdb/NEWS                                    |   3 +
 gdb/doc/gdb.texinfo                         |   4 +
 gdb/infrun.c                                |  38 ++--
 gdb/jit.c                                   |  28 +++
 gdb/linux-nat.c                             |  15 --
 gdb/linux-nat.h                             |   3 -
 gdb/linux-thread-db.c                       |   2 +-
 gdb/testsuite/gdb.base/jit-elf-fork-main.c  | 129 ++++++++++++++
 gdb/testsuite/gdb.base/jit-elf-fork-solib.c |  25 +++
 gdb/testsuite/gdb.base/jit-elf-fork.exp     | 186 ++++++++++++++++++++
 10 files changed, 391 insertions(+), 42 deletions(-)
 create mode 100644 gdb/testsuite/gdb.base/jit-elf-fork-main.c
 create mode 100644 gdb/testsuite/gdb.base/jit-elf-fork-solib.c
 create mode 100644 gdb/testsuite/gdb.base/jit-elf-fork.exp

-- 
2.32.0

Patch

diff --git a/gdb/NEWS b/gdb/NEWS
index 56743fc9aeaa..47fb4424a699 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -101,6 +101,9 @@  maintenance flush dcache
 maintenance info target-sections
   Print GDB's internal target sections table.
 
+maintenance info jit
+  Print the JIT code objects in the inferior known to GDB.
+
 memory-tag show-logical-tag POINTER
   Print the logical tag for POINTER.
 memory-tag with-logical-tag POINTER TAG
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index d09b86cda959..6cfdd3153394 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -38931,6 +38931,10 @@  buffer.
 Control whether @value{GDBN} will skip PAD packets when computing the
 packet history.
 
+@kindex maint info jit
+@item maint info jit
+Print information about JIT code objects loaded in the current inferior.
+
 @kindex set displaced-stepping
 @kindex show displaced-stepping
 @cindex displaced stepping support
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 4bd21fde5907..791ccee6ea0b 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -429,6 +429,8 @@  holding the child stopped.  Try \"set detach-on-fork\" or \
       return true;
     }
 
+  thread_info *child_thr = nullptr;
+
   if (!follow_child)
     {
       /* Detach new forked process?  */
@@ -478,8 +480,8 @@  holding the child stopped.  Try \"set detach-on-fork\" or \
 	  switch_to_no_thread ();
 	  child_inf->symfile_flags = SYMFILE_NO_READ;
 	  child_inf->push_target (parent_inf->process_target ());
-	  thread_info *child_thr
-	    = add_thread_silent (child_inf->process_target (), child_ptid);
+	  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.  */
@@ -514,17 +516,6 @@  holding the child stopped.  Try \"set detach-on-fork\" or \
 	      /* solib_create_inferior_hook relies on the current
 		 thread.  */
 	      switch_to_thread (child_thr);
-
-	      /* Let the shared library layer (e.g., solib-svr4) learn
-		 about this new process, relocate the cloned exec, pull
-		 in shared libraries, and install the solib event
-		 breakpoint.  If a "cloned-VM" event was propagated
-		 better throughout the core, this wouldn't be
-		 required.  */
-	      scoped_restore restore_in_initial_library_scan
-		= make_scoped_restore (&child_inf->in_initial_library_scan,
-				       true);
-	      solib_create_inferior_hook (0);
 	    }
 	}
 
@@ -633,7 +624,7 @@  holding the child stopped.  Try \"set detach-on-fork\" or \
 	child_inf->push_target (target);
       }
 
-      thread_info *child_thr = add_thread_silent (target, child_ptid);
+      child_thr = add_thread_silent (target, child_ptid);
 
       /* If this is a vfork child, then the address-space is shared
 	 with the parent.  If we detached from the parent, then we can
@@ -653,15 +644,6 @@  holding the child stopped.  Try \"set detach-on-fork\" or \
 	  child_inf->symfile_flags = SYMFILE_NO_READ;
 	  set_current_program_space (child_inf->pspace);
 	  clone_program_space (child_inf->pspace, parent_pspace);
-
-	  /* Let the shared library layer (e.g., solib-svr4) learn
-	     about this new process, relocate the cloned exec, pull in
-	     shared libraries, and install the solib event breakpoint.
-	     If a "cloned-VM" event was propagated better throughout
-	     the core, this wouldn't be required.  */
-	  scoped_restore restore_in_initial_library_scan
-	    = make_scoped_restore (&child_inf->in_initial_library_scan, true);
-	  solib_create_inferior_hook (0);
 	}
 
       switch_to_thread (child_thr);
@@ -669,6 +651,16 @@  holding the child stopped.  Try \"set detach-on-fork\" or \
 
   target_follow_fork (follow_child, detach_fork);
 
+  /* If we ended up creating a new inferior, call post_create_inferior to inform
+     the various subcomponents.  */
+  if (child_thr != nullptr)
+    {
+      scoped_restore_current_thread restore;
+      switch_to_thread (child_thr);
+
+      post_create_inferior (0);
+    }
+
   return false;
 }
 
diff --git a/gdb/jit.c b/gdb/jit.c
index 1de785b8de08..1838fd966bf8 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -74,6 +74,30 @@  show_jit_debug (struct ui_file *file, int from_tty,
   fprintf_filtered (file, _("JIT debugging is %s.\n"), value);
 }
 
+/* Implementation of the "maintenance info jit" command.  */
+
+static void
+maint_info_jit_cmd (const char *args, int from_tty)
+{
+  inferior *inf = current_inferior ();
+  bool printed_header = false;
+
+  /* Print a line for each JIT-ed objfile.  */
+  for (objfile *obj : inf->pspace->objfiles ())
+    {
+      if (obj->jited_data == nullptr)
+	continue;
+
+      if (!printed_header)
+	{
+	  printf_filtered ("Base address of known JIT-ed objfiles:\n");
+	  printed_header = true;
+	}
+
+      printf_filtered (" - %s\n", paddress (obj->arch (), obj->jited_data->addr));
+    }
+}
+
 struct jit_reader
 {
   jit_reader (struct gdb_reader_funcs *f, gdb_dlhandle_up &&h)
@@ -1247,6 +1271,10 @@  _initialize_jit ()
 			   show_jit_debug,
 			   &setdebuglist, &showdebuglist);
 
+  add_cmd ("jit", class_maintenance, maint_info_jit_cmd,
+	   _("Print information about JIT-ed code objects."),
+	   &maintenanceinfolist);
+
   gdb::observers::inferior_created.attach (jit_inferior_created_hook, "jit");
   gdb::observers::inferior_execd.attach (jit_inferior_created_hook, "jit");
   gdb::observers::inferior_exit.attach (jit_inferior_exit_hook, "jit");
diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index 34a2aee41d78..d394262c2af3 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -519,18 +519,6 @@  linux_nat_target::follow_fork (bool follow_child, bool detach_fork)
 	      ptrace (PTRACE_DETACH, child_pid, 0, signo);
 	    }
 	}
-      else
-	{
-	  /* Switching inferior_ptid is not enough, because then
-	     inferior_thread () would crash by not finding the thread
-	     in the current inferior.  */
-	  scoped_restore_current_thread restore_current_thread;
-	  thread_info *child = find_thread_ptid (this, child_ptid);
-	  switch_to_thread (child);
-
-	  /* Let the thread_db layer learn about this new process.  */
-	  check_for_thread_db ();
-	}
 
       if (has_vforked)
 	{
@@ -607,9 +595,6 @@  linux_nat_target::follow_fork (bool follow_child, bool detach_fork)
       child_lp = add_lwp (inferior_ptid);
       child_lp->stopped = 1;
       child_lp->last_resume_kind = resume_stop;
-
-      /* Let the thread_db layer learn about this new process.  */
-      check_for_thread_db ();
     }
 }
 
diff --git a/gdb/linux-nat.h b/gdb/linux-nat.h
index 5426a5c69001..feeaddafe991 100644
--- a/gdb/linux-nat.h
+++ b/gdb/linux-nat.h
@@ -294,9 +294,6 @@  extern enum tribool have_ptrace_getregset;
        (LP) != NULL;							\
        (LP) = (LP)->next)
 
-/* Attempt to initialize libthread_db.  */
-void check_for_thread_db (void);
-
 /* Called from the LWP layer to inform the thread_db layer that PARENT
    spawned CHILD.  Both LWPs are currently stopped.  This function
    does whatever is required to have the child LWP under the
diff --git a/gdb/linux-thread-db.c b/gdb/linux-thread-db.c
index d1e8c22ac962..c748163b1ece 100644
--- a/gdb/linux-thread-db.c
+++ b/gdb/linux-thread-db.c
@@ -1254,7 +1254,7 @@  check_thread_signals (void)
    an inferior is created (or otherwise acquired, e.g. attached to)
    and when new shared libraries are loaded into a running process.  */
 
-void
+static void
 check_for_thread_db (void)
 {
   /* Do nothing if we couldn't load libthread_db.so.1.  */
diff --git a/gdb/testsuite/gdb.base/jit-elf-fork-main.c b/gdb/testsuite/gdb.base/jit-elf-fork-main.c
new file mode 100644
index 000000000000..7431200b05b2
--- /dev/null
+++ b/gdb/testsuite/gdb.base/jit-elf-fork-main.c
@@ -0,0 +1,129 @@ 
+/* This test program is part of GDB, the GNU debugger.
+
+   Copyright 2011-2021 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Simulate loading of JIT code.  */
+
+#include <elf.h>
+#include <link.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "jit-protocol.h"
+#include "jit-elf-util.h"
+
+static void
+usage (void)
+{
+  fprintf (stderr, "Usage: jit-elf-main libraries...\n");
+  exit (1);
+}
+
+/* Must be defined by .exp file when compiling to know
+   what address to map the ELF binary to.  */
+#ifndef LOAD_ADDRESS
+#error "Must define LOAD_ADDRESS"
+#endif
+#ifndef LOAD_INCREMENT
+#error "Must define LOAD_INCREMENT"
+#endif
+
+int
+main (int argc, char *argv[])
+{
+  int i;
+  alarm (300);
+  /* Used as backing storage for GDB to populate argv.  */
+  char *fake_argv[10];
+
+  if (argc < 2)
+    {
+      usage ();
+      exit (1);
+    }
+
+  for (i = 1; i < argc; ++i)
+    {
+      size_t obj_size;
+      void *load_addr = (void *) (size_t) (LOAD_ADDRESS + (i - 1) * LOAD_INCREMENT);
+      printf ("Loading %s as JIT at %p\n", argv[i], load_addr);
+      void *addr = load_elf (argv[i], &obj_size, load_addr);
+
+      char name[32];
+      sprintf (name, "jit_function_%04d", i);
+      int (*jit_function) (void) = (int (*) (void)) load_symbol (addr, name);
+
+      /* Link entry at the end of the list.  */
+      struct jit_code_entry *const entry = calloc (1, sizeof (*entry));
+      entry->symfile_addr = (const char *)addr;
+      entry->symfile_size = obj_size;
+      entry->prev_entry = __jit_debug_descriptor.relevant_entry;
+      __jit_debug_descriptor.relevant_entry = entry;
+
+      if (entry->prev_entry != NULL)
+	entry->prev_entry->next_entry = entry;
+      else
+	__jit_debug_descriptor.first_entry = entry;
+
+      /* Notify GDB.  */
+      __jit_debug_descriptor.action_flag = JIT_REGISTER;
+      __jit_debug_register_code ();
+
+      if (jit_function () != 42)
+	{
+	  fprintf (stderr, "unexpected return value\n");
+	  exit (1);
+	}
+    }
+
+  i = 0;  /* break before fork */
+
+  fork ();
+
+  i = 0;  /* break after fork */
+
+  /* Now unregister them all in reverse order.  */
+  while (__jit_debug_descriptor.relevant_entry != NULL)
+    {
+      struct jit_code_entry *const entry =
+	__jit_debug_descriptor.relevant_entry;
+      struct jit_code_entry *const prev_entry = entry->prev_entry;
+
+      if (prev_entry != NULL)
+	{
+	  prev_entry->next_entry = NULL;
+	  entry->prev_entry = NULL;
+	}
+      else
+	__jit_debug_descriptor.first_entry = NULL;
+
+      /* Notify GDB.  */
+      __jit_debug_descriptor.action_flag = JIT_UNREGISTER;
+      __jit_debug_register_code ();
+
+      __jit_debug_descriptor.relevant_entry = prev_entry;
+      free (entry);
+    }
+
+  return 0;  /* break before return  */
+}
diff --git a/gdb/testsuite/gdb.base/jit-elf-fork-solib.c b/gdb/testsuite/gdb.base/jit-elf-fork-solib.c
new file mode 100644
index 000000000000..4215b2dce582
--- /dev/null
+++ b/gdb/testsuite/gdb.base/jit-elf-fork-solib.c
@@ -0,0 +1,25 @@ 
+/* This test program is part of GDB, the GNU debugger.
+
+   Copyright 2011-2021 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* This simulates a JIT library.  The function name is supplied during
+   compilation as a macro.  */
+
+#ifndef FUNCTION_NAME
+#error "Must define the FUNCTION_NAME macro to set a jited function name"
+#endif
+
+int FUNCTION_NAME() { return 42; }
diff --git a/gdb/testsuite/gdb.base/jit-elf-fork.exp b/gdb/testsuite/gdb.base/jit-elf-fork.exp
new file mode 100644
index 000000000000..c8ab69437c3e
--- /dev/null
+++ b/gdb/testsuite/gdb.base/jit-elf-fork.exp
@@ -0,0 +1,186 @@ 
+# Copyright 2011-2021 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test fork handling of an inferior that has JIT-ed objfiles.
+
+if {[skip_shlib_tests]} {
+    untested "skipping shared library tests"
+    return -1
+}
+
+if {[get_compiler_info]} {
+    untested "could not get compiler info"
+    return 1
+}
+
+load_lib jit-elf-helpers.exp
+
+# The main code that loads and registers JIT objects.
+set main_basename "jit-elf-fork-main"
+set main_srcfile ${srcdir}/${subdir}/${main_basename}.c
+set main_binfile [standard_output_file ${main_basename}]
+
+# The shared library that gets loaded as JIT objects.
+set jit_solib_basename jit-elf-fork-solib
+set jit_solib_srcfile ${srcdir}/${subdir}/${jit_solib_basename}.c
+
+# Compile a shared library to use as JIT objects.
+set jit_solibs_target [compile_and_download_n_jit_so \
+		       $jit_solib_basename $jit_solib_srcfile 1]
+if { $jit_solibs_target == -1 } {
+    return
+}
+
+# Compile the main code (which loads the JIT objects).
+if { [compile_jit_main ${main_srcfile} ${main_binfile} {}] != 0 } {
+    return
+}
+
+# Set up for the tests.
+#
+# detach-on-fork and follow-fork-mode are the values to use for the GDB
+# parameters of the same names.
+#
+# Upon return, execution is stopped at the breakpoint just after fork.  Which
+# inferiors exist and which inferior is the current one depends on the
+# parameter.
+#
+# The only breakpoint left is one just before the return statement in main, so
+# that the callers can continue execution until there.
+
+proc do_setup { detach-on-fork follow-fork-mode } {
+	clean_restart ${::main_binfile}
+
+	gdb_test_no_output "set detach-on-fork ${detach-on-fork}"
+	gdb_test_no_output "set follow-fork-mode ${follow-fork-mode}"
+
+	if { ![runto_main] } {
+		fail "can't run to main"
+		return -1
+	}
+
+	# Poke desired values directly into inferior.
+	gdb_test_no_output "set var argc=2" "forging argc"
+	gdb_test_no_output "set var argv=fake_argv" "forging argv"
+	set jit_solib_target [lindex $::jit_solibs_target 0]
+	gdb_test_no_output "set var argv\[1]=\"${jit_solib_target}\"" {forging argv[1]}
+
+	# Put a breakpoint before the fork, continue there.
+	gdb_breakpoint [gdb_get_line_number "break before fork" $::main_srcfile]
+	gdb_continue_to_breakpoint "continue to before fork" ".*break before fork.*"
+
+	# We should have one JIT object loaded.
+	gdb_test "maint info jit" " - ${::hex}" "jit-ed objfiles before fork"
+
+	# Put a breakpoint just after the fork, continue there.
+	gdb_breakpoint [gdb_get_line_number "break after fork" $::main_srcfile]
+	gdb_continue_to_breakpoint "continue to after fork" ".*break after fork.*"
+
+	# We should still have one JIT object loaded in whatever inferior we are
+	# currently stopped in, regardless of the mode.
+	gdb_test "maint info jit" " - ${::hex}" "jit-ed objfiles after fork"
+
+	# Delete our breakpoints.
+	delete_breakpoints
+
+	# Put a breakpoint before return, for the convenience of our callers.
+	gdb_breakpoint [gdb_get_line_number "break before return" $::main_srcfile]
+}
+
+proc_with_prefix test_detach_on_fork_off_follow_fork_mode_parent { } {
+	if { [do_setup off parent] == -1 } {
+		return -1
+	}
+
+	# We are stopped in the parent.
+	gdb_test "inferior" "Current inferior is 1.*" "current inferior is parent"
+
+	# Switch to the child, verify there is a JIT-ed objfile.
+	gdb_test "inferior 2" "Switching to inferior 2.*"
+	gdb_test "maint info jit" " - ${::hex}" "jit-ed objfile in child"
+
+	# Continue child past JIT unload, verify there are no more JIT-ed objfiles.
+	gdb_continue_to_breakpoint "continue to before return - child" ".*break before return.*"
+	gdb_test_no_output "maint info jit" "no more jit-ed objfiles in child"
+
+	# Go back to parent, the JIT-ed objfile should still be there.
+	gdb_test "inferior 1" "Switching to inferior 1.*"
+	gdb_test "maint info jit" " - ${::hex}" "jit-ed objfile in parent"
+
+	# Continue parent past JIT unload, verify there are no more JIT-ed objfiles.
+	gdb_continue_to_breakpoint "continue to before return - parent" ".*break before return.*"
+	gdb_test_no_output "maint info jit" "no more jit-ed objfiles in parent"
+}
+
+proc_with_prefix test_detach_on_fork_off_follow_fork_mode_child { } {
+	if { [do_setup off child] == -1 } {
+		return -1
+	}
+
+	# We are stopped in the child.  This is the exact same thing as
+	# test_detach_on_fork_off_follow_fork_mode_parent, except that we are
+	# stopped in the child.
+	gdb_test "inferior" "Current inferior is 2.*" "current inferior is child"
+
+	# Switch to the parent, verify there is a JIT-ed objfile.
+	gdb_test "inferior 1" "Switching to inferior 1.*"
+	gdb_test "maint info jit" " - ${::hex}" "jit-ed objfile in parent"
+
+	# Continue parent past JIT unload, verify there are no more JIT-ed objfiles.
+	gdb_continue_to_breakpoint "continue to before return - parent" ".*break before return.*"
+	gdb_test_no_output "maint info jit" "no more jit-ed objfiles in parent"
+
+	# Go back to child, the JIT-ed objfile should still be there.
+	gdb_test "inferior 2" "Switching to inferior 2.*"
+	gdb_test "maint info jit" " - ${::hex}" "jit-ed objfile in child"
+
+	# Continue child past JIT unload, verify there are no more JIT-ed objfiles.
+	gdb_continue_to_breakpoint "continue to before return - child" ".*break before return.*"
+	gdb_test_no_output "maint info jit" "no more jit-ed objfiles in child"
+}
+
+proc_with_prefix test_detach_on_fork_on_follow_fork_mode_parent { } {
+	if { [do_setup on parent] == -1 } {
+		return -1
+	}
+
+	# We are stopped in the parent, child is detached.
+	gdb_test "inferior" "Current inferior is 1.*" "current inferior is parent"
+	gdb_test "inferior 2" "Inferior ID 2 not known." "no inferior 2"
+
+	# Continue past JIT unload, verify there are no more JIT-ed objfiles.
+	gdb_continue_to_breakpoint "continue to before return" ".*break before return.*"
+	gdb_test_no_output "maint info jit" "no more jit-ed objfiles"
+}
+
+proc_with_prefix test_detach_on_fork_on_follow_fork_mode_child { } {
+	if { [do_setup on child] == -1 } {
+		return -1
+	}
+
+	# We are stopped in the child, parent is detached.
+	gdb_test "inferior" "Current inferior is 2.*" "current inferior is child"
+	gdb_test "inferior 1" "Switching to inferior 1 \\\[<null>\\\].*"
+	gdb_test "inferior 2" "Switching to inferior 2.*"
+
+	# Continue past JIT unload, verify there are no more JIT-ed objfiles.
+	gdb_continue_to_breakpoint "continue to before return" ".*break before return.*"
+	gdb_test_no_output "maint info jit" "no more jit-ed objfiles"
+}
+
+test_detach_on_fork_off_follow_fork_mode_parent
+test_detach_on_fork_off_follow_fork_mode_child
+test_detach_on_fork_on_follow_fork_mode_parent
+test_detach_on_fork_on_follow_fork_mode_child