From 21bdfeafcc830bfdf8936c3387f665fa67d34633 Mon Sep 17 00:00:00 2001 From: Ian Petersen Date: Sat, 31 Aug 2024 20:44:23 -0700 Subject: [PATCH] Start reading return and frame addresses with MSVC --- include/unifex/config.hpp | 25 +++++++++++++++++++++++++ include/unifex/tracing/async_stack.hpp | 18 +++++++----------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/include/unifex/config.hpp b/include/unifex/config.hpp index 9457b1d5..95d48327 100644 --- a/include/unifex/config.hpp +++ b/include/unifex/config.hpp @@ -308,4 +308,29 @@ # else # define UNIFEX_NO_ASYNC_STACKS 0 # endif +#elif UNIFEX_NO_ASYNC_STACKS +// make sure the truthy value is 1 +# undef UNIFEX_NO_ASYNC_STACKS +# define UNIFEX_NO_ASYNC_STACKS 1 +#else +// make sure the falsey value is 0 +# undef UNIFEX_NO_ASYNC_STACKS +# define UNIFEX_NO_ASYNC_STACKS 0 #endif // !defined(UNIFEX_NO_ASYNC_STACKS) + +#if UNIFEX_HAS_BUILTIN(__builtin_return_address) +# define UNIFEX_RETURN_ADDRESS __builtin_return_address(0) +#elif defined(_MSC_VER) +# define UNIFEX_RETURN_ADDRESS _ReturnAddress() +#else +# define UNIFEX_RETURN_ADDRESS nullptr +#endif + +#if UNIFEX_HAS_BUILTIN(__builtin_frame_address) +# define UNIFEX_FRAME_ADDRESS __builtin_frame_address(0) +#elif defined(_MSC_VER) +// not sure, yet, that this is right, but it's where we're starting +# define UNIFEX_FRAME_ADDRESS _AddressOfReturnAddress() +#else +# define UNIFEX_FRAME_ADDRESS nullptr +#endif diff --git a/include/unifex/tracing/async_stack.hpp b/include/unifex/tracing/async_stack.hpp index ca52d394..e8b287fd 100644 --- a/include/unifex/tracing/async_stack.hpp +++ b/include/unifex/tracing/async_stack.hpp @@ -22,6 +22,11 @@ #include #include +#if defined(_MSC_VER) +// needed for _ReturnAddress() and _AddressOfReturnAddress() +#include +#endif + #include namespace unifex { @@ -273,13 +278,8 @@ struct instruction_ptr final { // Generally a function that uses this macro should be declared FOLLY_NOINLINE // to prevent this returning surprising results in cases where the function // is inlined. -#if UNIFEX_HAS_BUILTIN(__builtin_return_address) - static constexpr instruction_ptr - read_return_address(void* p = __builtin_return_address(0)) noexcept { -#else static constexpr instruction_ptr - read_return_address(void* p = nullptr) noexcept { -#endif + read_return_address(void* p = UNIFEX_RETURN_ADDRESS) noexcept { return instruction_ptr{p}; } @@ -311,12 +311,8 @@ struct frame_ptr { // Generally a function that uses this macro should be declared FOLLY_NOINLINE // to prevent this returning surprising results in cases where the function // is inlined. -#if UNIFEX_HAS_BUILTIN(__builtin_frame_address) static constexpr frame_ptr - read_frame_pointer(void* p = __builtin_frame_address(0)) noexcept { -#else - static constexpr frame_ptr read_frame_pointer(void* p = nullptr) noexcept { -#endif + read_frame_pointer(void* p = UNIFEX_FRAME_ADDRESS) noexcept { return frame_ptr{p}; }