File tree Expand file tree Collapse file tree 4 files changed +45
-6
lines changed Expand file tree Collapse file tree 4 files changed +45
-6
lines changed Original file line number Diff line number Diff line change @@ -17,8 +17,6 @@ class CSystemLinux final : public ISystemPOSIX
17
17
NBL_API2 SystemInfo getSystemInfo () const override ;
18
18
};
19
19
20
- bool isDebuggerAttached ();
21
-
22
20
#endif
23
21
}
24
22
Original file line number Diff line number Diff line change @@ -120,9 +120,6 @@ class NBL_API2 CSystemWin32 : public ISystem
120
120
return true ;
121
121
}
122
122
};
123
-
124
- bool NBL_API2 isDebuggerAttached ();
125
-
126
123
}
127
124
#endif
128
125
Original file line number Diff line number Diff line change @@ -167,6 +167,7 @@ class NBL_API2 ISystem : public core::IReferenceCounted
167
167
};
168
168
virtual SystemInfo getSystemInfo () const = 0;
169
169
170
+ static bool isDebuggerAttached ();
170
171
171
172
protected:
172
173
// all file operations take place serially on a dedicated thread (to make fibers possible in the future)
Original file line number Diff line number Diff line change @@ -349,4 +349,47 @@ bool ISystem::areBuiltinsMounted() const
349
349
return false ;
350
350
351
351
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
You can’t perform that action at this time.
0 commit comments