feat(exception_handler): use single stack_trace instance (#979)

This commit is contained in:
Yimura 2023-02-12 01:09:27 +01:00 committed by GitHub
parent 04bae0bc93
commit 0701f63ccf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 24 deletions

View File

@ -13,7 +13,8 @@ namespace big
{
RemoveVectoredExceptionHandler(m_exception_handler);
}
inline static stack_trace trace;
LONG vectored_exception_handler(EXCEPTION_POINTERS* exception_info)
{
const auto exception_code = exception_info->ExceptionRecord->ExceptionCode;
@ -22,17 +23,14 @@ namespace big
exception_code == DBG_PRINTEXCEPTION_WIDE_C)
return EXCEPTION_CONTINUE_SEARCH;
stack_trace stack_trace(exception_info);
LOG(FATAL) << stack_trace;
trace.new_stack_trace(exception_info);
LOG(FATAL) << trace;
ZyanU64 opcode_address = exception_info->ContextRecord->Rip;
ZydisDisassembledInstruction instruction;
ZydisDisassembleIntel(ZYDIS_MACHINE_MODE_LONG_64, opcode_address, reinterpret_cast<void*>(opcode_address), 32, &instruction);
if(stack_trace.m_ret_context.Rip)
*exception_info->ContextRecord = stack_trace.m_ret_context;
else
exception_info->ContextRecord->Rip += instruction.info.length;
exception_info->ContextRecord->Rip += instruction.info.length;
return EXCEPTION_CONTINUE_EXECUTION;
}

View File

@ -5,20 +5,10 @@
namespace big
{
stack_trace::stack_trace(EXCEPTION_POINTERS* exception_info) :
m_exception_info(exception_info),
stack_trace::stack_trace() :
m_frame_pointers(32)
{
static std::mutex m;
std::lock_guard lock(m);
SymInitialize(GetCurrentProcess(), nullptr, true);
m_dump << exception_code_to_string(exception_info->ExceptionRecord->ExceptionCode) << '\n';
dump_module_info();
dump_registers();
dump_stacktrace();
m_dump << "\n--------End of exception--------\n";
}
stack_trace::~stack_trace()
@ -26,6 +16,20 @@ namespace big
SymCleanup(GetCurrentProcess());
}
void stack_trace::new_stack_trace(EXCEPTION_POINTERS *exception_info)
{
static std::mutex m;
std::lock_guard lock(m);
m_exception_info = exception_info;
m_dump << exception_code_to_string(exception_info->ExceptionRecord->ExceptionCode) << '\n';
dump_module_info();
dump_registers();
dump_stacktrace();
m_dump << "\n--------End of exception--------\n";
}
std::string stack_trace::str() const
{
return m_dump.str();
@ -172,9 +176,6 @@ namespace big
break;
}
m_frame_pointers[i] = frame.AddrPC.Offset;
if (i == 1)
m_ret_context = context;
}
}

View File

@ -6,11 +6,10 @@ namespace big
class stack_trace
{
public:
stack_trace(EXCEPTION_POINTERS* exception_info);
stack_trace();
virtual ~stack_trace();
CONTEXT m_ret_context{};
void new_stack_trace(EXCEPTION_POINTERS* exception_info);
std::string str() const;
friend std::ostream& operator<< (std::ostream& os, const stack_trace& st);