Skip to content

Commit

Permalink
Reland "[lldb] Refactor CrashReason"
Browse files Browse the repository at this point in the history
This reverts commit 71c4d18.
The reinterpret casts were not needed.
  • Loading branch information
DavidSpickett committed Mar 15, 2023
1 parent 13a0b48 commit 6db766a
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 95 deletions.
3 changes: 1 addition & 2 deletions lldb/source/Plugins/Process/FreeBSD/NativeThreadFreeBSD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
17 changes: 8 additions & 9 deletions lldb/source/Plugins/Process/Linux/NativeThreadLinux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Plugins/Process/Linux/NativeThreadLinux.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions lldb/source/Plugins/Process/NetBSD/NativeThreadNetBSD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
125 changes: 84 additions & 41 deletions lldb/source/Plugins/Process/POSIX/CrashReason.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 << ")";
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
41 changes: 1 addition & 40 deletions lldb/source/Plugins/Process/POSIX/CrashReason.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_

0 comments on commit 6db766a

Please sign in to comment.