summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
author Vas Crabb <vas@vastheman.com>2025-04-18 01:30:01 +1000
committer Vas Crabb <vas@vastheman.com>2025-04-18 01:30:01 +1000
commit5bf3b42ae4056773ce8e9929d146ea07c545da0e (patch)
treea0e31b1c50c13b3b11de8aada47076bcaebd37f4
parent1f6f0197df571e0604650295129712275840fb2b (diff)
lib/osdlib_unix.cpp: Added code to detect attached debugger on Linux.
-rw-r--r--src/osd/modules/lib/osdlib_unix.cpp51
1 files changed, 46 insertions, 5 deletions
diff --git a/src/osd/modules/lib/osdlib_unix.cpp b/src/osd/modules/lib/osdlib_unix.cpp
index 36004580222..1cfa9b1f183 100644
--- a/src/osd/modules/lib/osdlib_unix.cpp
+++ b/src/osd/modules/lib/osdlib_unix.cpp
@@ -17,8 +17,10 @@
#include <csignal>
#include <cstdio>
#include <cstdlib>
+#include <cstring>
#include <iomanip>
#include <memory>
+#include <string_view>
#include <dlfcn.h>
#include <sys/mman.h>
@@ -60,13 +62,52 @@ void osd_process_kill()
void osd_break_into_debugger(const char *message)
{
-#ifdef MAME_DEBUG
- printf("MAME exception: %s\n", message);
- printf("Attempting to fall into debugger\n");
- kill(getpid(), SIGTRAP);
+#if defined(__linux__)
+ bool do_break = false;
+ FILE *const f = std::fopen("/proc/self/status", "r");
+ if (f)
+ {
+ using namespace std::literals;
+
+ std::string_view const tag = "TracerPid:\t"sv;
+ char buf[128];
+ bool ignore = false;
+ while (std::fgets(buf, std::size(buf), f))
+ {
+ // ignore excessively long lines
+ auto const len = strnlen(buf, std::size(buf));
+ bool const noeol = !len || ('\n' != buf[len - 1]);
+ if (ignore || noeol)
+ {
+ ignore = noeol;
+ continue;
+ }
+
+ if (!std::strncmp(buf, tag.data(), tag.length()))
+ {
+ long tpid;
+ if ((std::sscanf(buf + tag.length(), "%ld", &tpid) == 1) && (0 != tpid))
+ do_break = true;
+ break;
+ }
+ }
+ std::fclose(f);
+ }
+#elif defined(MAME_DEBUG)
+ bool const do_break = true;
#else
- printf("Ignoring MAME exception: %s\n", message);
+ bool const do_break = false;
#endif
+ if (do_break)
+ {
+ printf("MAME exception: %s\n", message);
+ printf("Attempting to fall into debugger\n");
+ kill(getpid(), SIGTRAP);
+ }
+ else
+ {
+ printf("Ignoring MAME exception: %s\n", message);
+ }
}