-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added initial support for tracing function calls and printing e…
…xception stack traces
- Loading branch information
Showing
16 changed files
with
272 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
project(tracing) | ||
|
||
option(IMHEX_TRACE_EXCEPTIONS "Hook thrown exceptions to display a stack trace when possible" ON) | ||
option(IMHEX_INSTRUMENT_FUNCTIONS "Hook all function entries and exits to profile things in Tracy" OFF) | ||
|
||
add_library(tracing OBJECT | ||
source/stacktrace.cpp | ||
source/exceptions.cpp | ||
) | ||
target_include_directories(tracing PUBLIC include) | ||
target_link_libraries(tracing PRIVATE stdc++exp) | ||
|
||
if (NOT IMHEX_DISABLE_STACKTRACE) | ||
if (WIN32) | ||
message(STATUS "StackWalk enabled!") | ||
target_link_libraries(tracing PRIVATE DbgHelp.lib) | ||
else () | ||
find_package(Backtrace) | ||
if (${Backtrace_FOUND}) | ||
message(STATUS "Backtrace enabled! Header: ${Backtrace_HEADER}") | ||
|
||
if (Backtrace_HEADER STREQUAL "backtrace.h") | ||
target_link_libraries(tracing PRIVATE ${Backtrace_LIBRARY}) | ||
target_include_directories(tracing PRIVATE ${Backtrace_INCLUDE_DIR}) | ||
target_compile_definitions(tracing PRIVATE BACKTRACE_HEADER=<${Backtrace_HEADER}>) | ||
target_compile_definitions(tracing PRIVATE HEX_HAS_BACKTRACE) | ||
elseif (Backtrace_HEADER STREQUAL "execinfo.h") | ||
target_link_libraries(tracing PRIVATE ${Backtrace_LIBRARY}) | ||
target_include_directories(tracing PRIVATE ${Backtrace_INCLUDE_DIR}) | ||
target_compile_definitions(tracing PRIVATE BACKTRACE_HEADER=<${Backtrace_HEADER}>) | ||
target_compile_definitions(tracing PRIVATE HEX_HAS_EXECINFO) | ||
endif() | ||
endif() | ||
endif() | ||
|
||
target_link_libraries(tracing PRIVATE LLVMDemangle) | ||
endif() | ||
|
||
if (IMHEX_TRACE_EXCEPTIONS) | ||
target_link_options(tracing PUBLIC "-Wl,--wrap=__cxa_throw") | ||
endif() | ||
|
||
if (IMHEX_INSTRUMENT_FUNCTIONS) | ||
target_sources(tracing PUBLIC | ||
source/instr_entry.cpp | ||
source/instrumentation.cpp | ||
) | ||
|
||
FetchContent_Declare( | ||
tracy | ||
GIT_REPOSITORY https://github.com/wolfpld/tracy.git | ||
GIT_TAG v0.10 | ||
GIT_SHALLOW TRUE | ||
GIT_PROGRESS TRUE | ||
) | ||
FetchContent_MakeAvailable(tracy) | ||
target_compile_options(TracyClient PRIVATE "-Wno-error") | ||
|
||
target_compile_options(tracing PUBLIC "-finstrument-functions") | ||
target_link_libraries(tracing PRIVATE TracyClient) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#pragma once | ||
|
||
#include <hex/trace/stacktrace.hpp> | ||
|
||
#include <optional> | ||
|
||
namespace hex::trace { | ||
|
||
std::optional<StackTrace> getLastExceptionStackTrace(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#pragma once | ||
|
||
#include <map> | ||
#include <memory> | ||
|
||
|
12 changes: 6 additions & 6 deletions
12
main/gui/include/stacktrace.hpp → lib/trace/include/hex/trace/stacktrace.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
#pragma once | ||
|
||
#include <hex.hpp> | ||
|
||
#include <cstdint> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace hex::stacktrace { | ||
namespace hex::trace { | ||
|
||
struct StackFrame { | ||
std::string file; | ||
std::string function; | ||
u32 line; | ||
std::uint32_t line; | ||
}; | ||
|
||
void initialize(); | ||
using StackTrace = std::vector<StackFrame>; | ||
|
||
std::vector<StackFrame> getStackTrace(); | ||
void initialize(); | ||
StackTrace getStackTrace(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include <hex/trace/exceptions.hpp> | ||
|
||
namespace hex::trace { | ||
|
||
static std::optional<StackTrace> s_lastExceptionStackTrace; | ||
std::optional<StackTrace> getLastExceptionStackTrace() { | ||
if (!s_lastExceptionStackTrace.has_value()) | ||
return std::nullopt; | ||
|
||
auto result = s_lastExceptionStackTrace.value(); | ||
s_lastExceptionStackTrace.reset(); | ||
|
||
return result; | ||
} | ||
|
||
} | ||
|
||
extern "C" { | ||
|
||
[[noreturn]] void __real___cxa_throw(void* thrownException, void* type, void (*destructor)(void*)); | ||
[[noreturn]] void __wrap___cxa_throw(void* thrownException, void* type, void (*destructor)(void*)) { | ||
hex::trace::s_lastExceptionStackTrace = hex::trace::getStackTrace(); | ||
__real___cxa_throw(thrownException, type, destructor); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <mutex> | ||
|
||
namespace hex { | ||
|
||
void functionEntry(void *); | ||
void functionExit(void *); | ||
|
||
} | ||
|
||
extern "C" { | ||
|
||
static std::mutex s_mutex; | ||
|
||
[[gnu::no_instrument_function]] | ||
void __cyg_profile_func_enter(void *functionAddress, void *) { | ||
std::scoped_lock lock(s_mutex); | ||
|
||
hex::functionEntry(functionAddress); | ||
} | ||
|
||
[[gnu::no_instrument_function]] | ||
void __cyg_profile_func_exit(void *functionAddress, void *) { | ||
std::scoped_lock lock(s_mutex); | ||
|
||
hex::functionExit(functionAddress); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <hex/trace/instrumentation.hpp> | ||
|
||
namespace hex { | ||
|
||
void functionEntry([[maybe_unused]] void *functionAddress) { | ||
|
||
} | ||
|
||
void functionExit([[maybe_unused]] void *functionAddress) { | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.