Added code to capture stacktrace whenever an exception is thrown.
This commit is contained in:
		
							parent
							
								
									adbb4fd0d1
								
							
						
					
					
						commit
						c44350269a
					
				@ -1,10 +1,10 @@
 | 
			
		||||
 | 
			
		||||
#include "./stacktrace.hpp"
 | 
			
		||||
 | 
			
		||||
#include <cstdint>
 | 
			
		||||
#include <optional>
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <backtrace.h>
 | 
			
		||||
#include "../detect.hpp"
 | 
			
		||||
 | 
			
		||||
namespace mijin
 | 
			
		||||
{
 | 
			
		||||
@ -32,6 +32,8 @@ struct BacktraceData
 | 
			
		||||
// internal variables
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
thread_local Optional<Stacktrace> gCurrentExceptionStackTrace;
 | 
			
		||||
 | 
			
		||||
//
 | 
			
		||||
// internal functions
 | 
			
		||||
//
 | 
			
		||||
@ -85,4 +87,42 @@ Result<Stacktrace> captureStacktrace(unsigned skipFrames) noexcept
 | 
			
		||||
    return Stacktrace(std::move(btData.stackframes));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const Optional<Stacktrace>& getExceptionStacktrace() noexcept
 | 
			
		||||
{
 | 
			
		||||
    return gCurrentExceptionStackTrace;
 | 
			
		||||
}
 | 
			
		||||
} // namespace mijin
 | 
			
		||||
 | 
			
		||||
#if MIJIN_STDLIB == MIJIN_STDLIB_GLIBC
 | 
			
		||||
#include <dlfcn.h>
 | 
			
		||||
 | 
			
		||||
namespace
 | 
			
		||||
{
 | 
			
		||||
using cxa_throw_type = void (*)(void*, std::type_info*, void(*)(void*));
 | 
			
		||||
using cxa_rethrow_type = void (*)();
 | 
			
		||||
 | 
			
		||||
cxa_throw_type orig_cxa_throw = nullptr; // Address of the original __cxa_throw
 | 
			
		||||
// cxa_rethrow_type orig_cxa_rethrow = nullptr; // Address of the original __cxa_rethrow
 | 
			
		||||
 | 
			
		||||
extern "C" void __cxa_throw(void* thrown_exception, std::type_info* tinfo, void (*dest)(void*))
 | 
			
		||||
{
 | 
			
		||||
    if (!orig_cxa_throw) {
 | 
			
		||||
        orig_cxa_throw = reinterpret_cast<cxa_throw_type>(dlsym(RTLD_NEXT, "__cxa_throw"));
 | 
			
		||||
    }
 | 
			
		||||
    if (mijin::Result<mijin::Stacktrace> stacktrace = mijin::captureStacktrace(); stacktrace.isSuccess())
 | 
			
		||||
    {
 | 
			
		||||
        mijin::gCurrentExceptionStackTrace = std::move(*stacktrace);
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        mijin::gCurrentExceptionStackTrace.reset();
 | 
			
		||||
    }
 | 
			
		||||
    if (orig_cxa_throw) {
 | 
			
		||||
        orig_cxa_throw(thrown_exception, tinfo, dest);
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
        std::abort();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
@ -58,6 +58,7 @@ public:
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
[[nodiscard]] Result<Stacktrace> captureStacktrace(unsigned skipFrames = 0) noexcept;
 | 
			
		||||
[[nodiscard]] const Optional<Stacktrace>& getExceptionStacktrace() noexcept;
 | 
			
		||||
 | 
			
		||||
template<typename TStream>
 | 
			
		||||
TStream& operator<<(TStream& stream, const Stackframe& stackframe)
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user