Skip to content

Commit fe75310

Browse files
committed
move definition and implementation to ISystem.h and ISystem.cpp
1 parent a8586e7 commit fe75310

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

include/nbl/system/CSystemLinux.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ class CSystemLinux final : public ISystemPOSIX
1717
NBL_API2 SystemInfo getSystemInfo() const override;
1818
};
1919

20-
bool isDebuggerAttached();
21-
2220
#endif
2321
}
2422

include/nbl/system/CSystemWin32.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,6 @@ class NBL_API2 CSystemWin32 : public ISystem
120120
return true;
121121
}
122122
};
123-
124-
bool NBL_API2 isDebuggerAttached();
125-
126123
}
127124
#endif
128125

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/ISystem.cpp

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,47 @@ 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+
364+
bool ISystem::isDebuggerAttached()
365+
{
366+
constexpr char debuggerPidStr[] = "TracerPid:";
367+
constexpr size_t bufSize = 4096;
368+
char buf[bufSize];
369+
370+
const int status = open("/proc/self/status");
371+
if (status == -1)
372+
return false;
373+
374+
const size_t numRead = read(status, static_cast<void*>(buf), sizeof(buf) - 1);
375+
close(status);
376+
377+
buf[numRead] = '\0';
378+
const auto offset = strstr(buf, tracerPidStr);
379+
if (not offset)
380+
return false;
381+
382+
auto isSpace = [](const char c) { return c == ' '; };
383+
auto isDigit = [](const char c) { return c >= '0' && c <= '9'; };
384+
385+
for (const char* cPtr = offset + sizeof(tracerPidStr) - 1; cPtr <= buf + numRead; cPtr++)
386+
{
387+
if (isSpace(*cPtr))
388+
continue;
389+
else
390+
return isDigit(*cPtr) && *cPtr != '0';
391+
}
392+
393+
return false;
394+
}
395+
#endif

0 commit comments

Comments
 (0)