]> git.tdb.fi Git - libs/core.git/commitdiff
Add utilities to interface with an attached debugger
authorMikko Rasa <tdb@tdb.fi>
Wed, 16 Mar 2022 10:10:40 +0000 (12:10 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 16 Mar 2022 10:12:15 +0000 (12:12 +0200)
source/debug/debugapi.cpp [new file with mode: 0644]
source/debug/debugapi.h [new file with mode: 0644]

diff --git a/source/debug/debugapi.cpp b/source/debug/debugapi.cpp
new file mode 100644 (file)
index 0000000..8fafdd9
--- /dev/null
@@ -0,0 +1,59 @@
+#ifndef _WIN32
+#include <signal.h>
+#endif
+#include <msp/fs/utils.h>
+#include <msp/io/file.h>
+#include <msp/strings/format.h>
+#include <msp/strings/utils.h>
+#include "debugapi.h"
+
+using namespace std;
+
+namespace Msp {
+namespace Debug {
+
+DebuggerType check_debugger(bool force_recheck)
+{
+       static DebuggerType debugger = NONE;
+       static bool checked = false;
+
+       if(!checked || force_recheck)
+       {
+               checked = true;
+
+#ifndef _WIN32
+               unsigned tracer_pid = 0;
+               IO::BufferedFile status("/proc/self/status");
+               string line;
+               while(status.getline(line))
+                       if(!line.compare(0, 10, "TracerPid:"))
+                       {
+                               tracer_pid = lexical_cast<unsigned>(strip(line.substr(10)));
+                               break;
+                       }
+
+               if(tracer_pid)
+               {
+                       FS::Path tracer_cmd = FS::readlink(format("/proc/%d/exe", tracer_pid));
+                       if(FS::basename(tracer_cmd)=="gdb")
+                               debugger = GDB;
+                       else
+                               debugger = UNKNOWN;
+               }
+#endif
+       }
+
+       return debugger;
+}
+
+void debug_break()
+{
+#ifdef _WIN32
+       __debugbreak();
+#else
+       raise(SIGTRAP);
+#endif
+}
+
+} // namespace Debug
+} // namespace Msp
diff --git a/source/debug/debugapi.h b/source/debug/debugapi.h
new file mode 100644 (file)
index 0000000..80dcc4c
--- /dev/null
@@ -0,0 +1,20 @@
+#ifndef MSP_DEBUG_DEBUGAPI_H_
+#define MSP_DEBUG_DEBUGAPI_H_
+
+namespace Msp {
+namespace Debug {
+
+enum DebuggerType
+{
+       NONE,
+       GDB,
+       UNKNOWN,
+};
+
+DebuggerType check_debugger(bool = false);
+void debug_break();
+
+} // namespace Debug
+} // namespace Msp
+
+#endif