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

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