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