diff --git a/Utilities/Thread.cpp b/Utilities/Thread.cpp index ff29628ee9..2b6c08b9e9 100644 --- a/Utilities/Thread.cpp +++ b/Utilities/Thread.cpp @@ -2702,3 +2702,27 @@ u64 thread_ctrl::get_thread_affinity_mask() return -1; #endif } + +std::pair thread_ctrl::get_thread_stack() +{ +#ifdef _WIN32 + ULONG_PTR _min = 0; + ULONG_PTR _max = 0; + GetCurrentThreadStackLimits(&_min, &_max); + const std::size_t ssize = _max - _min; + const auto saddr = reinterpret_cast(_min); +#else + void* saddr = 0; + std::size_t ssize = 0; + pthread_attr_t attr; +#ifdef __linux__ + pthread_getattr_np(pthread_self(), &attr); + pthread_attr_getstack(&attr, &saddr, &ssize); +#else + pthread_attr_get_np(pthread_self(), &attr); + pthread_attr_getstackaddr(&attr, &saddr); + pthread_attr_getstacksize(&attr, &ssize); +#endif +#endif + return {saddr, ssize}; +} diff --git a/Utilities/Thread.h b/Utilities/Thread.h index c4f84f8c6a..a0df14578b 100644 --- a/Utilities/Thread.h +++ b/Utilities/Thread.h @@ -262,6 +262,9 @@ public: // Miscellaneous static u64 get_thread_affinity_mask(); + // Get current thread stack addr and size + static std::pair get_thread_stack(); + private: // Miscellaneous static const u64 process_affinity_mask;