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