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

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