From 25315498841259323e18f549d1ad9d9aba6632cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Fri, 18 Sep 2026 23:50:32 -0700 Subject: [PATCH] Added bx::isDebuggerPresent. (#430) --- include/bx/debug.h | 6 ++++++ src/debug.cpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/include/bx/debug.h b/include/bx/debug.h index bfd3b78..cf833c7 100644 --- a/include/bx/debug.h +++ b/include/bx/debug.h @@ -19,6 +19,12 @@ namespace bx /// void debugBreak(); + /// Check whether a debugger is present or not. + /// + /// @returns True when debugger is present. Otherwise returns false. + /// + bool isDebuggerPresent(); + /// Write string to debug output. /// /// @param[in] _str Zero terminated string to write. diff --git a/src/debug.cpp b/src/debug.cpp index c2a2626..8a23c54 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -17,6 +17,11 @@ # include // backtrace, backtrace_symbols # include // _Unwind_Backtrace # include + +# if BX_PLATFORM_OSX +# include // sysctl +# include // getpid +# endif // BX_PLATFORM_OSX #endif // BX_PLATFORM_* #ifndef BX_CONFIG_CALLSTACK_USE_EXECINFO @@ -79,6 +84,7 @@ extern "C" __declspec(dllimport) unsigned int __stdcall SetErrorMode(unsigned in #elif BX_PLATFORM_WINDOWS \ || BX_PLATFORM_WINRT \ || BX_PLATFORM_XBOXONE +extern "C" __declspec(dllimport) bool __stdcall IsDebuggerPresent(); extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA(const char* _str); extern "C" __declspec(dllimport) void* __stdcall GetStdHandle(unsigned long _stdHandle); extern "C" __declspec(dllimport) int __stdcall WriteFile(void* _file, const void* _buffer, unsigned long _sizeInBytes, unsigned long* _outNumberOfBytesWritten, struct _OVERLAPPED* _overlapped); @@ -128,6 +134,28 @@ namespace bx #endif // BX } + bool isDebuggerPresent() + { +#if BX_PLATFORM_WINDOWS + return 0 != ::IsDebuggerPresent(); +#elif BX_PLATFORM_OSX + struct kinfo_proc info; + size_t size = sizeof(info); + int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() }; + + bx::memSet(&info, 0, sizeof(info) ); + + if (0 != sysctl(mib, BX_COUNTOF(mib), &info, &size, NULL, 0) ) + { + return false; + } + + return 0 != (info.kp_proc.p_flag & P_TRACED); +#else + return false; +#endif // BX_PLATFORM_* + } + #if BX_PLATFORM_WINDOWS \ || BX_PLATFORM_WINRT \ || BX_PLATFORM_XBOXONE