mirror of
https://github.com/baldurk/renderdoc.git
synced 2026-05-05 01:20:42 +00:00
Implement OSUtility::DebuggerPresent on non-windows platforms
This commit is contained in:
@@ -317,7 +317,7 @@ namespace OSUtility
|
||||
{
|
||||
inline void ForceCrash();
|
||||
inline void DebugBreak();
|
||||
inline bool DebuggerPresent();
|
||||
bool DebuggerPresent();
|
||||
enum
|
||||
{
|
||||
Output_DebugMon,
|
||||
|
||||
@@ -77,3 +77,46 @@ int GetIdentPort(pid_t childPid)
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// because OSUtility::DebuggerPresent is called often we want it to be
|
||||
// cheap. Opening and parsing a file would cause high overhead on each
|
||||
// call, so instead we just cache it at startup. This fails in the case
|
||||
// of attaching to processes
|
||||
bool debuggerPresent = false;
|
||||
|
||||
void CacheDebuggerPresent()
|
||||
{
|
||||
FILE *f = FileIO::fopen("/proc/self/status", "r");
|
||||
|
||||
if(f == NULL)
|
||||
{
|
||||
RDCWARN("Couldn't open /proc/self/status");
|
||||
return;
|
||||
}
|
||||
|
||||
// read through the proc file to check for TracerPid
|
||||
while(ret == 0 && !feof(f))
|
||||
{
|
||||
const size_t sz = 512;
|
||||
char line[sz];
|
||||
line[sz - 1] = 0;
|
||||
fgets(line, sz - 1, f);
|
||||
|
||||
int tracerpid = 0;
|
||||
int num = sscanf(line, "TracerPid: %d", &tracerpid);
|
||||
|
||||
// found TracerPid line
|
||||
if(num == 1)
|
||||
{
|
||||
debuggerPresent = (tracerpid != 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
FileIO::fclose(f);
|
||||
}
|
||||
|
||||
bool OSUtility::DebuggerPresent()
|
||||
{
|
||||
return debuggerPresent;
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include <crt_externs.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include "os/os_specific.h"
|
||||
|
||||
char **GetCurrentEnvironment()
|
||||
@@ -133,3 +134,23 @@ int GetIdentPort(pid_t childPid)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void CacheDebuggerPresent()
|
||||
{
|
||||
}
|
||||
|
||||
// from https://developer.apple.com/library/mac/qa/qa1361/_index.html on how to detect the debugger
|
||||
bool OSUtility::DebuggerPresent()
|
||||
{
|
||||
// apple requires that this only be called in debug builds
|
||||
#if defined(RELEASE)
|
||||
return false;
|
||||
#else
|
||||
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()};
|
||||
kinfo_proc info = {};
|
||||
size_t size = sizeof(info);
|
||||
sysctl(mib, ARRAY_COUNT(mib), &info, &size, NULL, 0);
|
||||
|
||||
return info.kp_proc.p_flag & P_TRACED;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -77,3 +77,46 @@ int GetIdentPort(pid_t childPid)
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// because OSUtility::DebuggerPresent is called often we want it to be
|
||||
// cheap. Opening and parsing a file would cause high overhead on each
|
||||
// call, so instead we just cache it at startup. This fails in the case
|
||||
// of attaching to processes
|
||||
bool debuggerPresent = false;
|
||||
|
||||
void CacheDebuggerPresent()
|
||||
{
|
||||
FILE *f = FileIO::fopen("/proc/self/status", "r");
|
||||
|
||||
if(f == NULL)
|
||||
{
|
||||
RDCWARN("Couldn't open /proc/self/status");
|
||||
return;
|
||||
}
|
||||
|
||||
// read through the proc file to check for TracerPid
|
||||
while(!feof(f))
|
||||
{
|
||||
const size_t sz = 512;
|
||||
char line[sz];
|
||||
line[sz - 1] = 0;
|
||||
fgets(line, sz - 1, f);
|
||||
|
||||
int tracerpid = 0;
|
||||
int num = sscanf(line, "TracerPid: %d", &tracerpid);
|
||||
|
||||
// found TracerPid line
|
||||
if(num == 1)
|
||||
{
|
||||
debuggerPresent = (tracerpid != 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
FileIO::fclose(f);
|
||||
}
|
||||
|
||||
bool OSUtility::DebuggerPresent()
|
||||
{
|
||||
return debuggerPresent;
|
||||
}
|
||||
|
||||
@@ -46,10 +46,7 @@ inline void DebugBreak()
|
||||
{
|
||||
raise(SIGTRAP);
|
||||
}
|
||||
inline bool DebuggerPresent()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
bool DebuggerPresent();
|
||||
void WriteOutput(int channel, const char *str);
|
||||
};
|
||||
|
||||
|
||||
@@ -26,6 +26,8 @@
|
||||
#include <unistd.h>
|
||||
#include "os/os_specific.h"
|
||||
|
||||
void CacheDebuggerPresent();
|
||||
|
||||
uint64_t Timing::GetUnixTimestamp()
|
||||
{
|
||||
return (uint64_t)time(NULL);
|
||||
@@ -128,6 +130,8 @@ void Init()
|
||||
int err = pthread_key_create(&OSTLSHandle, NULL);
|
||||
if(err != 0)
|
||||
RDCFATAL("Can't allocate OS TLS slot");
|
||||
|
||||
CacheDebuggerPresent();
|
||||
}
|
||||
|
||||
void Shutdown()
|
||||
|
||||
Reference in New Issue
Block a user