From: Mikko Rasa Date: Wed, 16 Mar 2022 10:10:40 +0000 (+0200) Subject: Add utilities to interface with an attached debugger X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=b5c1bea5084768c7382c376383032c1a6ea16803;p=libs%2Fcore.git Add utilities to interface with an attached debugger --- diff --git a/source/debug/debugapi.cpp b/source/debug/debugapi.cpp new file mode 100644 index 0000000..8fafdd9 --- /dev/null +++ b/source/debug/debugapi.cpp @@ -0,0 +1,59 @@ +#ifndef _WIN32 +#include +#endif +#include +#include +#include +#include +#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(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 index 0000000..80dcc4c --- /dev/null +++ b/source/debug/debugapi.h @@ -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