[Lldb-commits] [lldb] 6db766a - Reland "[lldb] Refactor CrashReason"

David Spickett via lldb-commits lldb-commits at lists.llvm.org
Wed Mar 15 03:16:14 PDT 2023


Author: David Spickett
Date: 2023-03-15T10:15:44Z
New Revision: 6db766aa1a079085a3952f62c769cad16a1cbf3e

URL: https://github.com/llvm/llvm-project/commit/6db766aa1a079085a3952f62c769cad16a1cbf3e
DIFF: https://github.com/llvm/llvm-project/commit/6db766aa1a079085a3952f62c769cad16a1cbf3e.diff

LOG: Reland "[lldb] Refactor CrashReason"

This reverts commit 71c4d186f1cf247f1aa45f4fd1b38f350b68d123.
The reinterpret casts were not needed.

Added: 
    

Modified: 
    lldb/source/Plugins/Process/FreeBSD/NativeThreadFreeBSD.cpp
    lldb/source/Plugins/Process/Linux/NativeThreadLinux.cpp
    lldb/source/Plugins/Process/Linux/NativeThreadLinux.h
    lldb/source/Plugins/Process/NetBSD/NativeThreadNetBSD.cpp
    lldb/source/Plugins/Process/POSIX/CrashReason.cpp
    lldb/source/Plugins/Process/POSIX/CrashReason.h

Removed: 
    


################################################################################
diff  --git a/lldb/source/Plugins/Process/FreeBSD/NativeThreadFreeBSD.cpp b/lldb/source/Plugins/Process/FreeBSD/NativeThreadFreeBSD.cpp
index 8e6399bcf9c79..449ec27e0da8f 100644
--- a/lldb/source/Plugins/Process/FreeBSD/NativeThreadFreeBSD.cpp
+++ b/lldb/source/Plugins/Process/FreeBSD/NativeThreadFreeBSD.cpp
@@ -90,8 +90,7 @@ void NativeThreadFreeBSD::SetStoppedBySignal(uint32_t signo,
     case SIGBUS:
     case SIGFPE:
     case SIGILL:
-      const auto reason = GetCrashReason(*info);
-      m_stop_description = GetCrashReasonString(reason, *info);
+      m_stop_description = GetCrashReasonString(*info);
       break;
     }
   }

diff  --git a/lldb/source/Plugins/Process/Linux/NativeThreadLinux.cpp b/lldb/source/Plugins/Process/Linux/NativeThreadLinux.cpp
index 9b9dfe5214601..b62e9f643fa79 100644
--- a/lldb/source/Plugins/Process/Linux/NativeThreadLinux.cpp
+++ b/lldb/source/Plugins/Process/Linux/NativeThreadLinux.cpp
@@ -294,19 +294,19 @@ void NativeThreadLinux::SetStoppedBySignal(uint32_t signo,
     case SIGBUS:
     case SIGFPE:
     case SIGILL:
-      const auto reason = GetCrashReason(*info);
-      m_stop_description = GetCrashReasonString(reason, *info);
-
-      if (reason == CrashReason::eSyncTagCheckFault) {
-        AnnotateSyncTagCheckFault(info);
-      }
-
+      m_stop_description = GetCrashReasonString(*info);
+#ifndef SEGV_MTESERR
+#define SEGV_MTESERR 9
+#endif
+      if (info->si_signo == SIGSEGV && info->si_code == SEGV_MTESERR)
+        AnnotateSyncTagCheckFault(
+            reinterpret_cast<lldb::addr_t>(info->si_addr));
       break;
     }
   }
 }
 
-void NativeThreadLinux::AnnotateSyncTagCheckFault(const siginfo_t *info) {
+void NativeThreadLinux::AnnotateSyncTagCheckFault(lldb::addr_t fault_addr) {
   int32_t allocation_tag_type = 0;
   switch (GetProcess().GetArchitecture().GetMachine()) {
   // aarch64_32 deliberately not here because there's no 32 bit MTE
@@ -331,7 +331,6 @@ void NativeThreadLinux::AnnotateSyncTagCheckFault(const siginfo_t *info) {
   m_stop_description.pop_back();
 
   std::stringstream ss;
-  lldb::addr_t fault_addr = reinterpret_cast<uintptr_t>(info->si_addr);
   std::unique_ptr<MemoryTagManager> manager(std::move(details->manager));
 
   ss << " logical tag: 0x" << std::hex << manager->GetLogicalTag(fault_addr);

diff  --git a/lldb/source/Plugins/Process/Linux/NativeThreadLinux.h b/lldb/source/Plugins/Process/Linux/NativeThreadLinux.h
index 030a4012f46a7..1051c3ab68bf1 100644
--- a/lldb/source/Plugins/Process/Linux/NativeThreadLinux.h
+++ b/lldb/source/Plugins/Process/Linux/NativeThreadLinux.h
@@ -110,7 +110,7 @@ class NativeThreadLinux : public NativeThreadProtocol {
   /// Extend m_stop_description with logical and allocation tag values.
   /// If there is an error along the way just add the information we were able
   /// to get.
-  void AnnotateSyncTagCheckFault(const siginfo_t *info);
+  void AnnotateSyncTagCheckFault(lldb::addr_t fault_addr);
 
   // Member Variables
   lldb::StateType m_state;

diff  --git a/lldb/source/Plugins/Process/NetBSD/NativeThreadNetBSD.cpp b/lldb/source/Plugins/Process/NetBSD/NativeThreadNetBSD.cpp
index 995fe3fa78e8b..f561c21b9d91c 100644
--- a/lldb/source/Plugins/Process/NetBSD/NativeThreadNetBSD.cpp
+++ b/lldb/source/Plugins/Process/NetBSD/NativeThreadNetBSD.cpp
@@ -90,8 +90,7 @@ void NativeThreadNetBSD::SetStoppedBySignal(uint32_t signo,
     case SIGBUS:
     case SIGFPE:
     case SIGILL:
-      const auto reason = GetCrashReason(*info);
-      m_stop_description = GetCrashReasonString(reason, *info);
+      m_stop_description = GetCrashReasonString(*info);
       break;
     }
   }

diff  --git a/lldb/source/Plugins/Process/POSIX/CrashReason.cpp b/lldb/source/Plugins/Process/POSIX/CrashReason.cpp
index 31ec03c64bdeb..058ec6ad2d975 100644
--- a/lldb/source/Plugins/Process/POSIX/CrashReason.cpp
+++ b/lldb/source/Plugins/Process/POSIX/CrashReason.cpp
@@ -12,6 +12,42 @@
 
 #include <sstream>
 
+enum class CrashReason {
+  eInvalidCrashReason,
+
+  // SIGSEGV crash reasons.
+  eInvalidAddress,
+  ePrivilegedAddress,
+  eBoundViolation,
+  eAsyncTagCheckFault,
+  eSyncTagCheckFault,
+
+  // SIGILL crash reasons.
+  eIllegalOpcode,
+  eIllegalOperand,
+  eIllegalAddressingMode,
+  eIllegalTrap,
+  ePrivilegedOpcode,
+  ePrivilegedRegister,
+  eCoprocessorError,
+  eInternalStackError,
+
+  // SIGBUS crash reasons,
+  eIllegalAlignment,
+  eIllegalAddress,
+  eHardwareError,
+
+  // SIGFPE crash reasons,
+  eIntegerDivideByZero,
+  eIntegerOverflow,
+  eFloatDivideByZero,
+  eFloatOverflow,
+  eFloatUnderflow,
+  eFloatInexactResult,
+  eFloatInvalidOperation,
+  eFloatSubscriptRange
+};
+
 static void AppendFaultAddr(std::string &str, lldb::addr_t addr) {
   std::stringstream ss;
   ss << " (fault address: 0x" << std::hex << addr << ")";
@@ -37,10 +73,8 @@ static void AppendBounds(std::string &str, lldb::addr_t lower_bound,
 }
 #endif
 
-static CrashReason GetCrashReasonForSIGSEGV(const siginfo_t &info) {
-  assert(info.si_signo == SIGSEGV);
-
-  switch (info.si_code) {
+static CrashReason GetCrashReasonForSIGSEGV(int code) {
+  switch (code) {
 #ifdef SI_KERNEL
   case SI_KERNEL:
     // Some platforms will occasionally send nonstandard spurious SI_KERNEL
@@ -73,10 +107,8 @@ static CrashReason GetCrashReasonForSIGSEGV(const siginfo_t &info) {
   return CrashReason::eInvalidCrashReason;
 }
 
-static CrashReason GetCrashReasonForSIGILL(const siginfo_t &info) {
-  assert(info.si_signo == SIGILL);
-
-  switch (info.si_code) {
+static CrashReason GetCrashReasonForSIGILL(int code) {
+  switch (code) {
   case ILL_ILLOPC:
     return CrashReason::eIllegalOpcode;
   case ILL_ILLOPN:
@@ -98,10 +130,8 @@ static CrashReason GetCrashReasonForSIGILL(const siginfo_t &info) {
   return CrashReason::eInvalidCrashReason;
 }
 
-static CrashReason GetCrashReasonForSIGFPE(const siginfo_t &info) {
-  assert(info.si_signo == SIGFPE);
-
-  switch (info.si_code) {
+static CrashReason GetCrashReasonForSIGFPE(int code) {
+  switch (code) {
   case FPE_INTDIV:
     return CrashReason::eIntegerDivideByZero;
   case FPE_INTOVF:
@@ -123,10 +153,8 @@ static CrashReason GetCrashReasonForSIGFPE(const siginfo_t &info) {
   return CrashReason::eInvalidCrashReason;
 }
 
-static CrashReason GetCrashReasonForSIGBUS(const siginfo_t &info) {
-  assert(info.si_signo == SIGBUS);
-
-  switch (info.si_code) {
+static CrashReason GetCrashReasonForSIGBUS(int code) {
+  switch (code) {
   case BUS_ADRALN:
     return CrashReason::eIllegalAlignment;
   case BUS_ADRERR:
@@ -138,25 +166,8 @@ static CrashReason GetCrashReasonForSIGBUS(const siginfo_t &info) {
   return CrashReason::eInvalidCrashReason;
 }
 
-std::string GetCrashReasonString(CrashReason reason, const siginfo_t &info) {
-  std::string str;
-
-// make sure that siginfo_t has the bound fields available.
-#if defined(si_lower) && defined(si_upper)
-  if (reason == CrashReason::eBoundViolation) {
-    str = "signal SIGSEGV";
-    AppendBounds(str, reinterpret_cast<uintptr_t>(info.si_lower),
-                 reinterpret_cast<uintptr_t>(info.si_upper),
-                 reinterpret_cast<uintptr_t>(info.si_addr));
-    return str;
-  }
-#endif
-
-  return GetCrashReasonString(reason,
-                              reinterpret_cast<uintptr_t>(info.si_addr));
-}
-
-std::string GetCrashReasonString(CrashReason reason, lldb::addr_t fault_addr) {
+static std::string GetCrashReasonString(CrashReason reason,
+                                        lldb::addr_t fault_addr) {
   std::string str;
 
   switch (reason) {
@@ -244,18 +255,50 @@ std::string GetCrashReasonString(CrashReason reason, lldb::addr_t fault_addr) {
   return str;
 }
 
-CrashReason GetCrashReason(const siginfo_t &info) {
-  switch (info.si_signo) {
+static CrashReason GetCrashReason(int signo, int code) {
+  switch (signo) {
   case SIGSEGV:
-    return GetCrashReasonForSIGSEGV(info);
+    return GetCrashReasonForSIGSEGV(code);
   case SIGBUS:
-    return GetCrashReasonForSIGBUS(info);
+    return GetCrashReasonForSIGBUS(code);
   case SIGFPE:
-    return GetCrashReasonForSIGFPE(info);
+    return GetCrashReasonForSIGFPE(code);
   case SIGILL:
-    return GetCrashReasonForSIGILL(info);
+    return GetCrashReasonForSIGILL(code);
   }
 
   assert(false && "unexpected signal");
   return CrashReason::eInvalidCrashReason;
 }
+
+static std::string GetCrashReasonString(int signo, int code, lldb::addr_t addr,
+                                        std::optional<lldb::addr_t> lower,
+                                        std::optional<lldb::addr_t> upper) {
+  CrashReason reason = GetCrashReason(signo, code);
+
+  if (lower && upper) {
+    std::string str;
+    if (reason == CrashReason::eBoundViolation) {
+      str = "signal SIGSEGV";
+      AppendBounds(str, *lower, *upper, addr);
+      return str;
+    }
+  }
+
+  return GetCrashReasonString(reason, addr);
+}
+
+std::string GetCrashReasonString(const siginfo_t &info) {
+#if defined(si_lower) && defined(si_upper)
+  std::optional<lldb::addr_t> lower =
+      reinterpret_cast<lldb::addr_t>(info.si_lower);
+  std::optional<lldb::addr_t> upper =
+      reinterpret_cast<lldb::addr_t>(info.si_upper);
+#else
+  std::optional<lldb::addr_t> lower;
+  std::optional<lldb::addr_t> upper;
+#endif
+  return GetCrashReasonString(info.si_signo, info.si_code,
+                              reinterpret_cast<uintptr_t>(info.si_addr), lower,
+                              upper);
+}

diff  --git a/lldb/source/Plugins/Process/POSIX/CrashReason.h b/lldb/source/Plugins/Process/POSIX/CrashReason.h
index 466cd14e69842..2177726b76f0e 100644
--- a/lldb/source/Plugins/Process/POSIX/CrashReason.h
+++ b/lldb/source/Plugins/Process/POSIX/CrashReason.h
@@ -15,45 +15,6 @@
 
 #include <string>
 
-enum class CrashReason {
-  eInvalidCrashReason,
-
-  // SIGSEGV crash reasons.
-  eInvalidAddress,
-  ePrivilegedAddress,
-  eBoundViolation,
-  eAsyncTagCheckFault,
-  eSyncTagCheckFault,
-
-  // SIGILL crash reasons.
-  eIllegalOpcode,
-  eIllegalOperand,
-  eIllegalAddressingMode,
-  eIllegalTrap,
-  ePrivilegedOpcode,
-  ePrivilegedRegister,
-  eCoprocessorError,
-  eInternalStackError,
-
-  // SIGBUS crash reasons,
-  eIllegalAlignment,
-  eIllegalAddress,
-  eHardwareError,
-
-  // SIGFPE crash reasons,
-  eIntegerDivideByZero,
-  eIntegerOverflow,
-  eFloatDivideByZero,
-  eFloatOverflow,
-  eFloatUnderflow,
-  eFloatInexactResult,
-  eFloatInvalidOperation,
-  eFloatSubscriptRange
-};
-
-std::string GetCrashReasonString(CrashReason reason, lldb::addr_t fault_addr);
-std::string GetCrashReasonString(CrashReason reason, const siginfo_t &info);
-
-CrashReason GetCrashReason(const siginfo_t &info);
+std::string GetCrashReasonString(const siginfo_t &info);
 
 #endif // #ifndef liblldb_CrashReason_H_


        


More information about the lldb-commits mailing list