From b512e1542a2c2ffacb3bca9371fe40431a83e97e Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Sun, 6 Mar 2022 22:59:47 +0100 Subject: [PATCH] Don't grab the mouse when debugging (#97) * Add platform-dependent code to detect attached debugger * Only do relative mouse mode when debugger is not attached * Do an abort when a debugger is attached --- src/BRSRC13/CORE/V1DB/v1dbfile.c | 2 +- src/harness/CMakeLists.txt | 14 ++++++++ src/harness/include/harness/trace.h | 22 +++++++----- src/harness/platforms/platform.h | 6 ++++ src/harness/platforms/platform_linux.c | 45 ++++++++++++++++++++++++ src/harness/platforms/platform_macosx.c | 44 +++++++++++++++++++++++ src/harness/platforms/platform_windows.c | 5 +++ src/harness/sdl/gl_renderer.c | 7 +++- 8 files changed, 134 insertions(+), 11 deletions(-) create mode 100644 src/harness/platforms/platform.h create mode 100644 src/harness/platforms/platform_linux.c create mode 100644 src/harness/platforms/platform_macosx.c create mode 100644 src/harness/platforms/platform_windows.c diff --git a/src/BRSRC13/CORE/V1DB/v1dbfile.c b/src/BRSRC13/CORE/V1DB/v1dbfile.c index 37db94e4..b4cff9be 100644 --- a/src/BRSRC13/CORE/V1DB/v1dbfile.c +++ b/src/BRSRC13/CORE/V1DB/v1dbfile.c @@ -969,7 +969,7 @@ int FopRead_TRANSFORM(br_datafile* df, br_uint_32 id, br_uint_32 length, br_uint } } if (t == 7) { - LOG_PANIC("transform type not found!") + LOG_PANIC("transform type not found!"); } tp = (br_transform*)BrResAllocate(v1db.res, sizeof(br_transform), BR_MEMORY_TRANSFORM); tp->type = t; diff --git a/src/harness/CMakeLists.txt b/src/harness/CMakeLists.txt index c6990126..350c27cb 100644 --- a/src/harness/CMakeLists.txt +++ b/src/harness/CMakeLists.txt @@ -52,3 +52,17 @@ target_sources(harness PRIVATE platforms/null.h stack_trace_handler.h ) + +if(WIN32) + target_sources(harness PRIVATE + platforms/platform_windows.c + ) +elseif(APPLE) +target_sources(harness PRIVATE + platforms/platform_macosx.c +) +else() + target_sources(harness PRIVATE + platforms/platform_linux.c + ) +endif() diff --git a/src/harness/include/harness/trace.h b/src/harness/include/harness/trace.h index c6de6583..9485f168 100644 --- a/src/harness/include/harness/trace.h +++ b/src/harness/include/harness/trace.h @@ -10,6 +10,7 @@ #endif extern int harness_debug_level; +extern int PlatformIsDebuggerPresent(void); void Harness_Debug_PrintStack(); @@ -45,9 +46,14 @@ void debug_print_matrix4(const char* fmt, const char* fn, char* name, br_matrix4 #define LOG_MATRIX4(msg, m) debug_print_matrix4("\033[0;34m[DEBUG] %s ", __FUNCTION__, msg, m) #define LOG_INFO(...) debug_printf("\033[0;34m[INFO] %s ", __FUNCTION__, __VA_ARGS__) #define LOG_WARN(...) debug_printf("\033[0;33m[WARN] %s ", __FUNCTION__, __VA_ARGS__) -#define LOG_PANIC(...) \ - debug_printf("\033[0;31m[PANIC] %s ", __FUNCTION__, __VA_ARGS__); \ - exit(1); +#define LOG_PANIC(...) \ + do { \ + debug_printf("\033[0;31m[PANIC] %s ", __FUNCTION__, __VA_ARGS__); \ + if (PlatformIsDebuggerPresent()) \ + abort(); \ + else \ + exit(1); \ + } while (0) #define LOG_WARN_ONCE(...) \ static int warn_printed = 0; \ @@ -56,13 +62,11 @@ void debug_print_matrix4(const char* fmt, const char* fn, char* name, br_matrix4 warn_printed = 1; \ } -#define NOT_IMPLEMENTED() \ - debug_printf("\033[0;31m[PANIC] %s ", __FUNCTION__, "%s", "not implemented"); \ - exit(1); +#define NOT_IMPLEMENTED() \ + LOG_PANIC("not implemented") -#define TELL_ME_IF_WE_PASS_THIS_WAY() \ - debug_printf("\033[0;31m[PANIC] %s ", __FUNCTION__, "%s", "code path not expected"); \ - exit(1); +#define TELL_ME_IF_WE_PASS_THIS_WAY() \ + LOG_PANIC("code path not expected") #define STUB() \ debug_printf("\033[0;31m[WARN] %s ", __FUNCTION__, "%s", "stubbed"); diff --git a/src/harness/platforms/platform.h b/src/harness/platforms/platform.h new file mode 100644 index 00000000..d2262f24 --- /dev/null +++ b/src/harness/platforms/platform.h @@ -0,0 +1,6 @@ +#ifndef HARNESS_PLATFORM_H +#define HARNESS_PLATFORM_H + +int PlatformIsDebuggerPresent(void); + +#endif diff --git a/src/harness/platforms/platform_linux.c b/src/harness/platforms/platform_linux.c new file mode 100644 index 00000000..bd981f5b --- /dev/null +++ b/src/harness/platforms/platform_linux.c @@ -0,0 +1,45 @@ +#include "platforms/platform.h" + +#include +#include +#include +#include +#include + +#define TRACER_PID_STRING "TracerPid:" + +int PlatformIsDebuggerPresent() { + char buf[4096]; + int status_fd; + ssize_t num_read; + char* tracer_pid_ptr; + char* char_ptr; + + status_fd = open("/proc/self/status", O_RDONLY); + if (status_fd == -1) { + return 0; + } + + num_read = read(status_fd, buf, sizeof(buf) - 1); + close(status_fd); + if (num_read <= 0) { + return 0; + } + + buf[num_read] = '\0'; + tracer_pid_ptr = strstr(buf, TRACER_PID_STRING); + if (tracer_pid_ptr == NULL) { + return 0; + } + + for (char_ptr = tracer_pid_ptr + sizeof(TRACER_PID_STRING) - 1; char_ptr <= buf + num_read; ++char_ptr) + { + if (isspace(*char_ptr)) { + continue; + } else { + return isdigit(*char_ptr) != 0 && *char_ptr != '0'; + } + } + + return 0; +} diff --git a/src/harness/platforms/platform_macosx.c b/src/harness/platforms/platform_macosx.c new file mode 100644 index 00000000..e51c6e39 --- /dev/null +++ b/src/harness/platforms/platform_macosx.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include + + +// https://developer.apple.com/library/archive/qa/qa1361/_index.html +// FIXME: +// Important: Because the definition of the kinfo_proc structure (in ) is conditionalized by __APPLE_API_UNSTABLE, +// you should restrict use of the above code to the debug build of your program. + +int PlatformIsDebuggerPresent() + // Returns true if the current process is being debugged (either + // running under the debugger or has a debugger attached post facto). +{ + int junk; + int mib[4]; + struct kinfo_proc info; + size_t size; + + // Initialize the flags so that, if sysctl fails for some bizarre + // reason, we get a predictable result. + + info.kp_proc.p_flag = 0; + + // Initialize mib, which tells sysctl the info we want, in this case + // we're looking for information about a specific process ID. + + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = KERN_PROC_PID; + mib[3] = getpid(); + + // Call sysctl. + + size = sizeof(info); + junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0); + assert(junk == 0); + + // We're being debugged if the P_TRACED flag is set. + + return ((info.kp_proc.p_flag & P_TRACED) != 0); +} diff --git a/src/harness/platforms/platform_windows.c b/src/harness/platforms/platform_windows.c new file mode 100644 index 00000000..da742259 --- /dev/null +++ b/src/harness/platforms/platform_windows.c @@ -0,0 +1,5 @@ +#include + +int PlatformIsDebuggerPresent() { + return IsDebuggerPresent(); +} diff --git a/src/harness/sdl/gl_renderer.c b/src/harness/sdl/gl_renderer.c index ab28cce5..6afd51f2 100644 --- a/src/harness/sdl/gl_renderer.c +++ b/src/harness/sdl/gl_renderer.c @@ -5,6 +5,7 @@ #include "gl_renderer_shaders.h" #include "harness.h" #include "harness/trace.h" +#include "platforms/platform.h" #include #include @@ -264,7 +265,11 @@ void GLRenderer_CreateWindow(char* title, int width, int height, int pRender_wid if (!window) { LOG_PANIC("Failed to create window"); } - SDL_SetRelativeMouseMode(SDL_TRUE); + + // Don't grab the mouse when a debugger is present + if (!PlatformIsDebuggerPresent()) { + SDL_SetRelativeMouseMode(SDL_TRUE); + } // SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);