Skip to content

Commit a8586e7

Browse files
committed
linux variant of nbl::system::isDebuggerAttached
1 parent 62974ca commit a8586e7

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

include/nbl/system/CSystemLinux.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ class CSystemLinux final : public ISystemPOSIX
1616

1717
NBL_API2 SystemInfo getSystemInfo() const override;
1818
};
19+
20+
bool isDebuggerAttached();
21+
1922
#endif
2023
}
2124

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

0 commit comments

Comments
 (0)