]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ArmPkg/Library/DefaultExceptionHandlerLib/AArch64/DefaultExceptionHandler.c
ArmPkg/DefaultExceptionHandlerLib: improve formatting of backtrace
[mirror_edk2.git] / ArmPkg / Library / DefaultExceptionHandlerLib / AArch64 / DefaultExceptionHandler.c
index df30c4f125dbc09f18e18a8db71c9cdf2807dee7..2f9c2ede37c1d149dfceb3b3e85f477665f7f1fa 100644 (file)
@@ -34,6 +34,8 @@ STATIC CHAR8 *gExceptionTypeString[] = {
   "SError"\r
 };\r
 \r
+STATIC BOOLEAN mRecursiveException;\r
+\r
 CHAR8 *\r
 GetImageName (\r
   IN  UINTN  FaultAddress,\r
@@ -117,6 +119,26 @@ DescribeExceptionSyndrome (
   DEBUG ((EFI_D_ERROR, "\n %a \n", Message));\r
 }\r
 \r
+#ifndef MDEPKG_NDEBUG\r
+STATIC\r
+CONST CHAR8 *\r
+BaseName (\r
+  IN  CONST CHAR8 *FullName\r
+  )\r
+{\r
+  CONST CHAR8 *Str;\r
+\r
+  Str = FullName + AsciiStrLen (FullName);\r
+\r
+  while (--Str > FullName) {\r
+    if (*Str == '/' || *Str == '\\') {\r
+      return Str + 1;\r
+    }\r
+  }\r
+  return Str;\r
+}\r
+#endif\r
+\r
 /**\r
   This is the default action to take on an unexpected exception\r
 \r
@@ -134,17 +156,64 @@ DefaultExceptionHandler (
 {\r
   CHAR8  Buffer[100];\r
   UINTN  CharCount;\r
+  INT32  Offset;\r
+\r
+  if (mRecursiveException) {\r
+    CharCount = AsciiSPrint (Buffer, sizeof (Buffer),"\nRecursive exception occurred while dumping the CPU state\n");\r
+    SerialPortWrite ((UINT8 *) Buffer, CharCount);\r
+    CpuDeadLoop ();\r
+  }\r
+  mRecursiveException = TRUE;\r
 \r
   CharCount = AsciiSPrint (Buffer,sizeof (Buffer),"\n\n%a Exception at 0x%016lx\n", gExceptionTypeString[ExceptionType], SystemContext.SystemContextAArch64->ELR);\r
   SerialPortWrite ((UINT8 *) Buffer, CharCount);\r
 \r
   DEBUG_CODE_BEGIN ();\r
-    CHAR8  *Pdb;\r
+    CHAR8  *Pdb, *PrevPdb;\r
     UINTN  ImageBase;\r
     UINTN  PeCoffSizeOfHeader;\r
-    Pdb = GetImageName (SystemContext.SystemContextAArch64->ELR, &ImageBase, &PeCoffSizeOfHeader);\r
+    UINT64 *Fp;\r
+    UINT64 RootFp[2];\r
+    UINTN  Idx;\r
+\r
+    PrevPdb = Pdb = GetImageName (SystemContext.SystemContextAArch64->ELR, &ImageBase, &PeCoffSizeOfHeader);\r
     if (Pdb != NULL) {\r
-      DEBUG ((EFI_D_ERROR, "%a loaded at 0x%016lx \n", Pdb, ImageBase));\r
+      DEBUG ((EFI_D_ERROR, "PC 0x%012lx (0x%012lx+0x%08x) [ 0] %a\n",\r
+        SystemContext.SystemContextAArch64->ELR, ImageBase,\r
+        SystemContext.SystemContextAArch64->ELR - ImageBase, BaseName (Pdb)));\r
+\r
+      if ((UINT64 *)SystemContext.SystemContextAArch64->FP != 0) {\r
+        Idx = 0;\r
+\r
+        RootFp[0] = ((UINT64 *)SystemContext.SystemContextAArch64->FP)[0];\r
+        RootFp[1] = ((UINT64 *)SystemContext.SystemContextAArch64->FP)[1];\r
+        if (RootFp[1] != SystemContext.SystemContextAArch64->LR) {\r
+          RootFp[0] = SystemContext.SystemContextAArch64->FP;\r
+          RootFp[1] = SystemContext.SystemContextAArch64->LR;\r
+        }\r
+        for (Fp = RootFp; Fp[0] != 0; Fp = (UINT64 *)Fp[0]) {\r
+          Pdb = GetImageName (Fp[1], &ImageBase, &PeCoffSizeOfHeader);\r
+          if (Pdb != NULL) {\r
+            if (Pdb != PrevPdb) {\r
+              Idx++;\r
+              PrevPdb = Pdb;\r
+            }\r
+            DEBUG ((EFI_D_ERROR, "PC 0x%012lx (0x%012lx+0x%08x) [% 2d] %a\n",\r
+              Fp[1], ImageBase, Fp[1] - ImageBase, Idx, BaseName (Pdb)));\r
+          }\r
+        }\r
+        PrevPdb = Pdb = GetImageName (SystemContext.SystemContextAArch64->ELR, &ImageBase, &PeCoffSizeOfHeader);\r
+        DEBUG ((EFI_D_ERROR, "\n[ 0] %a\n", Pdb));\r
+\r
+        Idx = 0;\r
+        for (Fp = RootFp; Fp[0] != 0; Fp = (UINT64 *)Fp[0]) {\r
+          Pdb = GetImageName (Fp[1], &ImageBase, &PeCoffSizeOfHeader);\r
+          if (Pdb != NULL && Pdb != PrevPdb) {\r
+            DEBUG ((EFI_D_ERROR, "[% 2d] %a\n", ++Idx, Pdb));\r
+            PrevPdb = Pdb;\r
+          }\r
+        }\r
+      }\r
     }\r
   DEBUG_CODE_END ();\r
 \r
@@ -181,7 +250,18 @@ DefaultExceptionHandler (
   DEBUG ((EFI_D_ERROR, "\n ESR : EC 0x%02x  IL 0x%x  ISS 0x%08x\n", (SystemContext.SystemContextAArch64->ESR & 0xFC000000) >> 26, (SystemContext.SystemContextAArch64->ESR >> 25) & 0x1, SystemContext.SystemContextAArch64->ESR & 0x1FFFFFF ));\r
 \r
   DescribeExceptionSyndrome (SystemContext.SystemContextAArch64->ESR);\r
-  ASSERT (FALSE):\r
 \r
+  DEBUG ((EFI_D_ERROR, "\nStack dump:\n"));\r
+  for (Offset = -256; Offset < 256; Offset += 32) {\r
+    DEBUG  ((EFI_D_ERROR, "%c %013lx: %016lx %016lx %016lx %016lx\n",\r
+      Offset == 0 ? '>' : ' ',\r
+      SystemContext.SystemContextAArch64->SP + Offset,\r
+      *(UINT64 *)(SystemContext.SystemContextAArch64->SP + Offset),\r
+      *(UINT64 *)(SystemContext.SystemContextAArch64->SP + Offset + 8),\r
+      *(UINT64 *)(SystemContext.SystemContextAArch64->SP + Offset + 16),\r
+      *(UINT64 *)(SystemContext.SystemContextAArch64->SP + Offset + 24)));\r
+  }\r
+\r
+  ASSERT (FALSE);\r
   CpuDeadLoop ();\r
 }\r