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

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