File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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>
912ISystem::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
You can’t perform that action at this time.
0 commit comments