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
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
return true;
}
+ thread_info *child_thr = nullptr;
+
if (!follow_child)
{
/* Detach new forked process? */
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. */
/* 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);
}
}
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
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);
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;
}
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)
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");
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)
{
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 ();
}
}
(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
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. */
--- /dev/null
+/* 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 */
+}
--- /dev/null
+/* 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; }
--- /dev/null
+# 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