nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0080-Make-grub_fatal-also-backtrace.patch

8631a2
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8631a2
From: Peter Jones <pjones@redhat.com>
8631a2
Date: Wed, 27 Jan 2016 09:22:42 -0500
8631a2
Subject: [PATCH] Make grub_fatal() also backtrace.
8631a2
8631a2
---
8631a2
 grub-core/Makefile.core.def     |  3 ++
8631a2
 grub-core/kern/misc.c           |  8 +++++-
8631a2
 grub-core/lib/arm64/backtrace.c | 62 +++++++++++++++++++++++++++++++++++++++++
8631a2
 grub-core/lib/backtrace.c       |  2 ++
8631a2
 grub-core/lib/i386/backtrace.c  | 14 +++++++++-
8631a2
 5 files changed, 87 insertions(+), 2 deletions(-)
8631a2
 create mode 100644 grub-core/lib/arm64/backtrace.c
8631a2
8631a2
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
030dc3
index 991891a6e09..27563743ba9 100644
8631a2
--- a/grub-core/Makefile.core.def
8631a2
+++ b/grub-core/Makefile.core.def
8631a2
@@ -175,6 +175,9 @@ kernel = {
8631a2
 
8631a2
   softdiv = lib/division.c;
8631a2
 
8631a2
+  x86 = lib/i386/backtrace.c;
8631a2
+  x86 = lib/backtrace.c;
8631a2
+
8631a2
   i386 = kern/i386/dl.c;
8631a2
   i386_xen = kern/i386/dl.c;
8631a2
 
8631a2
diff --git a/grub-core/kern/misc.c b/grub-core/kern/misc.c
030dc3
index f1fab700048..5ce89a40c68 100644
8631a2
--- a/grub-core/kern/misc.c
8631a2
+++ b/grub-core/kern/misc.c
8631a2
@@ -24,6 +24,7 @@
8631a2
 #include <grub/term.h>
8631a2
 #include <grub/env.h>
8631a2
 #include <grub/i18n.h>
8631a2
+#include <grub/backtrace.h>
8631a2
 
8631a2
 union printf_arg
8631a2
 {
8631a2
@@ -1101,8 +1102,13 @@ grub_xasprintf (const char *fmt, ...)
8631a2
 static void __attribute__ ((noreturn))
8631a2
 grub_abort (void)
8631a2
 {
8631a2
+#ifndef GRUB_UTIL
8631a2
+#if defined(__i386__) || defined(__x86_64__)
8631a2
+  grub_backtrace();
8631a2
+#endif
8631a2
+#endif
8631a2
   grub_printf ("\nAborted.");
8631a2
-  
8631a2
+
8631a2
 #ifndef GRUB_UTIL
8631a2
   if (grub_term_inputs)
8631a2
 #endif
8631a2
diff --git a/grub-core/lib/arm64/backtrace.c b/grub-core/lib/arm64/backtrace.c
8631a2
new file mode 100644
030dc3
index 00000000000..1079b5380e1
8631a2
--- /dev/null
8631a2
+++ b/grub-core/lib/arm64/backtrace.c
8631a2
@@ -0,0 +1,62 @@
8631a2
+/*
8631a2
+ *  GRUB  --  GRand Unified Bootloader
8631a2
+ *  Copyright (C) 2009  Free Software Foundation, Inc.
8631a2
+ *
8631a2
+ *  GRUB is free software: you can redistribute it and/or modify
8631a2
+ *  it under the terms of the GNU General Public License as published by
8631a2
+ *  the Free Software Foundation, either version 3 of the License, or
8631a2
+ *  (at your option) any later version.
8631a2
+ *
8631a2
+ *  GRUB is distributed in the hope that it will be useful,
8631a2
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
8631a2
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8631a2
+ *  GNU General Public License for more details.
8631a2
+ *
8631a2
+ *  You should have received a copy of the GNU General Public License
8631a2
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
8631a2
+ */
8631a2
+
8631a2
+#include <grub/misc.h>
8631a2
+#include <grub/command.h>
8631a2
+#include <grub/err.h>
8631a2
+#include <grub/dl.h>
8631a2
+#include <grub/mm.h>
8631a2
+#include <grub/term.h>
8631a2
+#include <grub/backtrace.h>
8631a2
+
8631a2
+#define MAX_STACK_FRAME 102400
8631a2
+
8631a2
+void
8631a2
+grub_backtrace_pointer (int frame)
8631a2
+{
8631a2
+  while (1)
8631a2
+    {
8631a2
+      void *lp = __builtin_return_address (frame);
8631a2
+      if (!lp)
8631a2
+	break;
8631a2
+
8631a2
+      lp = __builtin_extract_return_addr (lp);
8631a2
+
8631a2
+      grub_printf ("%p: ", lp);
8631a2
+      grub_backtrace_print_address (lp);
8631a2
+      grub_printf (" (");
8631a2
+      for (i = 0; i < 2; i++)
8631a2
+	grub_printf ("%p,", ((void **)ptr) [i + 2]);
8631a2
+      grub_printf ("%p)\n", ((void **)ptr) [i + 2]);
8631a2
+      nptr = *(void **)ptr;
8631a2
+      if (nptr < ptr || (void **) nptr - (void **) ptr > MAX_STACK_FRAME
8631a2
+	  || nptr == ptr)
8631a2
+	{
8631a2
+	  grub_printf ("Invalid stack frame at %p (%p)\n", ptr, nptr);
8631a2
+	  break;
8631a2
+	}
8631a2
+      ptr = nptr;
8631a2
+    }
8631a2
+}
8631a2
+
8631a2
+void
8631a2
+grub_backtrace (void)
8631a2
+{
8631a2
+  grub_backtrace_pointer (1);
8631a2
+}
8631a2
+
8631a2
diff --git a/grub-core/lib/backtrace.c b/grub-core/lib/backtrace.c
030dc3
index 825a8800e25..c0ad6ab8be1 100644
8631a2
--- a/grub-core/lib/backtrace.c
8631a2
+++ b/grub-core/lib/backtrace.c
8631a2
@@ -29,6 +29,7 @@ GRUB_MOD_LICENSE ("GPLv3+");
8631a2
 void
8631a2
 grub_backtrace_print_address (void *addr)
8631a2
 {
8631a2
+#ifndef GRUB_UTIL
8631a2
   grub_dl_t mod;
8631a2
 
8631a2
   FOR_DL_MODULES (mod)
8631a2
@@ -44,6 +45,7 @@ grub_backtrace_print_address (void *addr)
8631a2
 	}
8631a2
   }
8631a2
 
8631a2
+#endif
8631a2
   grub_printf ("%p", addr);
8631a2
 }
8631a2
 
8631a2
diff --git a/grub-core/lib/i386/backtrace.c b/grub-core/lib/i386/backtrace.c
030dc3
index c3e03c7275c..c67273db3ae 100644
8631a2
--- a/grub-core/lib/i386/backtrace.c
8631a2
+++ b/grub-core/lib/i386/backtrace.c
8631a2
@@ -15,11 +15,23 @@
8631a2
  *  You should have received a copy of the GNU General Public License
8631a2
  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
8631a2
  */
8631a2
+#include <config.h>
8631a2
+#ifdef GRUB_UTIL
8631a2
+#define REALLY_GRUB_UTIL GRUB_UTIL
8631a2
+#undef GRUB_UTIL
8631a2
+#endif
8631a2
+
8631a2
+#include <grub/symbol.h>
8631a2
+#include <grub/dl.h>
8631a2
+
8631a2
+#ifdef REALLY_GRUB_UTIL
8631a2
+#define GRUB_UTIL REALLY_GRUB_UTIL
8631a2
+#undef REALLY_GRUB_UTIL
8631a2
+#endif
8631a2
 
8631a2
 #include <grub/misc.h>
8631a2
 #include <grub/command.h>
8631a2
 #include <grub/err.h>
8631a2
-#include <grub/dl.h>
8631a2
 #include <grub/mm.h>
8631a2
 #include <grub/term.h>
8631a2
 #include <grub/backtrace.h>