File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,9 @@ class CSystemLinux final : public ISystemPOSIX
16
16
17
17
NBL_API2 SystemInfo getSystemInfo () const override ;
18
18
};
19
+
20
+ bool isDebuggerAttached ();
21
+
19
22
#endif
20
23
}
21
24
Original file line number Diff line number Diff line change @@ -6,6 +6,9 @@ using namespace nbl::system;
6
6
#ifdef _NBL_PLATFORM_LINUX_
7
7
8
8
#include < sys/sysinfo.h>
9
+ #include < sys/stat.h>
10
+ #include < fcntl.h>
11
+ #include < unistd.h>
9
12
ISystem::SystemInfo CSystemLinux::getSystemInfo () const
10
13
{
11
14
SystemInfo info;
@@ -26,4 +29,37 @@ ISystem::SystemInfo CSystemLinux::getSystemInfo() const
26
29
27
30
return info;
28
31
}
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
+
29
65
#endif
You can’t perform that action at this time.
0 commit comments