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

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