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