Blame SOURCES/gdb-rhbz1842691-corefile-mem-access-12of15.patch

6543d1
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
6543d1
From: Keith Seitz <keiths@redhat.com>
6543d1
Date: Tue, 28 Jul 2020 09:32:50 -0400
6543d1
Subject: gdb-rhbz1842691-corefile-mem-access-12of15.patch
6543d1
6543d1
;; Add new command "maint print core-file-backed-mappings"
6543d1
;; Kevin Buettner, RH BZ 1842961
6543d1
6543d1
   Author: Kevin Buettner <kevinb@redhat.com>
6543d1
   Date:   Fri Jul 3 21:55:51 2020 -0700
6543d1
6543d1
    Add new command "maint print core-file-backed-mappings"
6543d1
6543d1
    I wrote a read_core_file_mappings method for FreeBSD and then registered
6543d1
    this gdbarch method.  I saw some strange behavior while testing it and
6543d1
    wanted a way to make sure that mappings were being correctly loaded
6543d1
    into corelow.c, so I wrote the new command which is the topic of this
6543d1
    commit.  I think it might be occasionally useful for debugging strange
6543d1
    corefile behavior.
6543d1
6543d1
    With regard to FreeBSD, my work isn't ready yet.  Unlike Linux,
6543d1
    FreeBSD puts all mappings into its core file note.  And, unlike Linux,
6543d1
    it doesn't dump load segments which occupy no space in the file.  So
6543d1
    my (perhaps naive) implementation of a FreeBSD read_core_file_mappings
6543d1
    didn't work all that well:  I saw more failures in the corefile2.exp
6543d1
    tests than without it.  I think it should be possible to make FreeBSD
6543d1
    work as well as Linux, but it will require doing something with all of
6543d1
    the mappings, not just the file based mappings that I was considering.
6543d1
6543d1
    In the v4 series, Pedro asked the following:
6543d1
6543d1
        I don't understand what this command provides that "info proc
6543d1
        mappings" doesn't?  Can you give an example of when you'd use this
6543d1
        command over "info proc mappings" ?
6543d1
6543d1
    On Linux, "info proc mappings" and "maint print core-file-backed-mappings"
6543d1
    will produce similar, possibly identical, output.  This need not be
6543d1
    the case for other OSes.  E.g. on FreeBSD, had I finished the
6543d1
    implementation, the output from these commands would have been very
6543d1
    different.  The FreeBSD "info proc mappings" command would show
6543d1
    additional (non-file-backed) mappings in addition to at least one
6543d1
    additional field (memory permissions) for each mapping.
6543d1
6543d1
    As noted earlier, I was seeing some unexpected behavior while working
6543d1
    on the FreeBSD implementation and wanted to be certain that the
6543d1
    mappings were being correctly loaded by corelow.c.  "info proc
6543d1
    mappings" prints the core file mappings, but doesn't tell us anything
6543d1
    about whether they've been loaded by corelow.c This new maintenance
6543d1
    command directly interrogates the data structures and prints the
6543d1
    values found there.
6543d1
6543d1
    gdb/ChangeLog:
6543d1
6543d1
    	* corelow.c (gdbcmd.h): Include.
6543d1
    	(core_target::info_proc_mappings): New method.
6543d1
    	(get_current_core_target): New function.
6543d1
    	(maintenance_print_core_file_backed_mappings): New function.
6543d1
    	(_initialize_corelow): Add core-file-backed-mappings to
6543d1
    	"maint print" commands.
6543d1
6543d1
diff --git a/gdb/corelow.c b/gdb/corelow.c
6543d1
--- a/gdb/corelow.c
6543d1
+++ b/gdb/corelow.c
6543d1
@@ -118,6 +118,9 @@ public:
6543d1
 				  const char *human_name,
6543d1
 				  bool required);
6543d1
 
6543d1
+  /* See definition.  */
6543d1
+  void info_proc_mappings (struct gdbarch *gdbarch);
6543d1
+
6543d1
 private: /* per-core data */
6543d1
 
6543d1
   /* The core's section table.  Note that these target sections are
6543d1
@@ -1292,6 +1295,86 @@ core_target::info_proc (const char *args, enum info_proc_what request)
6543d1
   return true;
6543d1
 }
6543d1
 
6543d1
+/* Get a pointer to the current core target.  If not connected to a
6543d1
+   core target, return NULL.  */
6543d1
+
6543d1
+static core_target *
6543d1
+get_current_core_target ()
6543d1
+{
6543d1
+  target_ops *proc_target = find_target_at (process_stratum);
6543d1
+  return dynamic_cast<core_target *> (proc_target);
6543d1
+}
6543d1
+
6543d1
+/* Display file backed mappings from core file.  */
6543d1
+
6543d1
+void
6543d1
+core_target::info_proc_mappings (struct gdbarch *gdbarch)
6543d1
+{
6543d1
+  if (m_core_file_mappings.sections != m_core_file_mappings.sections_end)
6543d1
+    {
6543d1
+      printf_filtered (_("Mapped address spaces:\n\n"));
6543d1
+      if (gdbarch_addr_bit (gdbarch) == 32)
6543d1
+	{
6543d1
+	  printf_filtered ("\t%10s %10s %10s %10s %s\n",
6543d1
+			   "Start Addr",
6543d1
+			   "  End Addr",
6543d1
+			   "      Size", "    Offset", "objfile");
6543d1
+	}
6543d1
+      else
6543d1
+	{
6543d1
+	  printf_filtered ("  %18s %18s %10s %10s %s\n",
6543d1
+			   "Start Addr",
6543d1
+			   "  End Addr",
6543d1
+			   "      Size", "    Offset", "objfile");
6543d1
+	}
6543d1
+    }
6543d1
+
6543d1
+  for (const struct target_section *tsp = m_core_file_mappings.sections;
6543d1
+       tsp < m_core_file_mappings.sections_end;
6543d1
+       tsp++)
6543d1
+    {
6543d1
+      ULONGEST start = tsp->addr;
6543d1
+      ULONGEST end = tsp->endaddr;
6543d1
+      ULONGEST file_ofs = tsp->the_bfd_section->filepos;
6543d1
+      const char *filename = bfd_get_filename (tsp->the_bfd_section->owner);
6543d1
+
6543d1
+      if (gdbarch_addr_bit (gdbarch) == 32)
6543d1
+	printf_filtered ("\t%10s %10s %10s %10s %s\n",
6543d1
+			 paddress (gdbarch, start),
6543d1
+			 paddress (gdbarch, end),
6543d1
+			 hex_string (end - start),
6543d1
+			 hex_string (file_ofs),
6543d1
+			 filename);
6543d1
+      else
6543d1
+	printf_filtered ("  %18s %18s %10s %10s %s\n",
6543d1
+			 paddress (gdbarch, start),
6543d1
+			 paddress (gdbarch, end),
6543d1
+			 hex_string (end - start),
6543d1
+			 hex_string (file_ofs),
6543d1
+			 filename);
6543d1
+    }
6543d1
+}
6543d1
+
6543d1
+/* Implement "maintenance print core-file-backed-mappings" command.  
6543d1
+
6543d1
+   If mappings are loaded, the results should be similar to the
6543d1
+   mappings shown by "info proc mappings".  This command is mainly a
6543d1
+   debugging tool for GDB developers to make sure that the expected
6543d1
+   mappings are present after loading a core file.  For Linux, the
6543d1
+   output provided by this command will be very similar (if not
6543d1
+   identical) to that provided by "info proc mappings".  This is not
6543d1
+   necessarily the case for other OSes which might provide
6543d1
+   more/different information in the "info proc mappings" output.  */
6543d1
+
6543d1
+static void
6543d1
+maintenance_print_core_file_backed_mappings (const char *args, int from_tty)
6543d1
+{
6543d1
+  core_target *targ = get_current_core_target ();
6543d1
+  if (targ != nullptr)
6543d1
+    targ->info_proc_mappings (targ->core_gdbarch ());
6543d1
+}
6543d1
+
6543d1
+void _initialize_corelow ();
6543d1
 void
6543d1
 _initialize_corelow (void)
6543d1
 {
6543d1
@@ -1303,4 +1386,8 @@ Set whether CORE-FILE loads the build-id associated files automatically."), _("\
6543d1
 Show whether CORE-FILE loads the build-id associated files automatically."),
6543d1
 			   NULL, NULL, NULL,
6543d1
 			   &setlist, &showlist);
6543d1
+  add_cmd ("core-file-backed-mappings", class_maintenance,
6543d1
+           maintenance_print_core_file_backed_mappings,
6543d1
+	   _("Print core file's file-backed mappings"),
6543d1
+	   &maintenanceprintlist);
6543d1
 }