Skip to content

Commit 957a726

Browse files
authored
Merge pull request #835 from Devsh-Graphics-Programming/yas_is_debugger_attached
nbl::system::isDebuggerAttached
2 parents 1ae0d58 + 4b8faf0 commit 957a726

File tree

6 files changed

+90
-2
lines changed

6 files changed

+90
-2
lines changed

include/nbl/system/CSystemLinux.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class CSystemLinux final : public ISystemPOSIX
1616

1717
NBL_API2 SystemInfo getSystemInfo() const override;
1818
};
19+
1920
#endif
2021
}
2122

include/nbl/system/CSystemWin32.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ class NBL_API2 CSystemWin32 : public ISystem
120120
return true;
121121
}
122122
};
123-
124123
}
125124
#endif
126125

include/nbl/system/ISystem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ class NBL_API2 ISystem : public core::IReferenceCounted
167167
};
168168
virtual SystemInfo getSystemInfo() const = 0;
169169

170+
static bool isDebuggerAttached();
170171

171172
protected:
172173
// all file operations take place serially on a dedicated thread (to make fibers possible in the future)

src/nbl/system/CSystemLinux.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ using namespace nbl::system;
66
#ifdef _NBL_PLATFORM_LINUX_
77

88
#include <sys/sysinfo.h>
9+
#include <sys/stat.h>
10+
#include <fcntl.h>
11+
#include <unistd.h>
912
ISystem::SystemInfo CSystemLinux::getSystemInfo() const
1013
{
1114
SystemInfo info;
@@ -26,4 +29,37 @@ ISystem::SystemInfo CSystemLinux::getSystemInfo() const
2629

2730
return info;
2831
}
32+
33+
bool isDebuggerAttached()
34+
{
35+
constexpr char tracerPidStr[] = "TracerPid:";
36+
char buf[4096];
37+
38+
const int status = open("/proc/self/status");
39+
if (status == -1)
40+
return false;
41+
42+
const size_t numRead = read(status, static_cast<void*>(buf), sizeof(buf) - 1);
43+
close(status);
44+
45+
buf[numRead] = '\0';
46+
const auto offset = strstr(buf, tracerPidStr);
47+
if (not offset)
48+
return false;
49+
50+
// few helper lambdas
51+
auto isSpace = [](char c) { return c == ' '; };
52+
auto isDigit = [](char c) { return c >= '0' && c <= '9'; };
53+
54+
for (const char* cPtr = offset + sizeof(tracerPidStr) - 1; cPtr <= buf + numRead; cPtr++)
55+
{
56+
if (isSpace(*cPtr))
57+
continue;
58+
else
59+
return isDigit(*cPtr) && *cPtr != '0';
60+
}
61+
62+
return false;
63+
}
64+
2965
#endif

src/nbl/system/CSystemWin32.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,10 @@ core::smart_refctd_ptr<ISystemFile> CSystemWin32::CCaller::createFile(const std:
101101
}
102102
return core::make_smart_refctd_ptr<CFileWin32>(core::smart_refctd_ptr<ISystem>(m_system),path(filename),flags,_mappedPtr,_native,_fileMappingObj);
103103
}
104+
105+
bool isDebuggerAttached()
106+
{
107+
return IsDebuggerPresent();
108+
}
109+
104110
#endif

src/nbl/system/ISystem.cpp

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,49 @@ bool ISystem::areBuiltinsMounted() const
349349
return false;
350350

351351
return true;
352-
}
352+
}
353+
354+
#ifdef _NBL_PLATFORM_WINDOWS_
355+
bool ISystem::isDebuggerAttached()
356+
{
357+
return IsDebuggerPresent();
358+
}
359+
#elif defined(_NBL_PLATFORM_LINUX_)
360+
#include <sys/stat.h>
361+
#include <fcntl.h>
362+
#include <unistd.h>
363+
#include <cstring>
364+
365+
bool ISystem::isDebuggerAttached()
366+
{
367+
constexpr char debuggerPidStr[] = "TracerPid:";
368+
constexpr size_t bufSize = 4096;
369+
370+
const int status = open("/proc/self/status", O_RDONLY);
371+
if (status == -1)
372+
return false;
373+
374+
char buf[bufSize];
375+
const size_t numRead = read(status, static_cast<void*>(buf), bufSize - 1);
376+
close(status);
377+
378+
buf[numRead] = '\0';
379+
const auto offset = strstr(buf, debuggerPidStr);
380+
if (not offset)
381+
return false;
382+
383+
384+
auto isSpace = [](const char c) { return c == ' ' || c == '\t'; };
385+
auto isDigit = [](const char c) { return c >= '0' && c <= '9'; };
386+
387+
for (const char* cPtr = offset + sizeof(debuggerPidStr) - 1; cPtr <= buf + numRead; cPtr++)
388+
{
389+
if (isSpace(*cPtr))
390+
continue;
391+
else
392+
return isDigit(*cPtr) && *cPtr != '0';
393+
}
394+
395+
return false;
396+
}
397+
#endif

0 commit comments

Comments
 (0)