mirror of
https://github.com/OpenMW/openmw.git
synced 2025-06-18 10:11:32 +00:00
Handle sysctl errors without assert
This commit is contained in:
parent
4efc0e20a3
commit
0095cb604f
1 changed files with 26 additions and 25 deletions
|
@ -471,7 +471,22 @@ static bool crashCatcherInstallHandlers(char** argv)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool is_debugger_present()
|
namespace
|
||||||
|
{
|
||||||
|
#if defined(__APPLE__)
|
||||||
|
bool isDebuggerPresent(const auto& info)
|
||||||
|
{
|
||||||
|
return (info.kp_proc.p_flag & P_TRACED) != 0;
|
||||||
|
}
|
||||||
|
#elif defined(__FreeBSD__)
|
||||||
|
bool isDebuggerPresent(const auto& info)
|
||||||
|
{
|
||||||
|
return (info.ki_flag & P_TRACED) != 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool isDebuggerPresent()
|
||||||
{
|
{
|
||||||
#if defined(__linux__)
|
#if defined(__linux__)
|
||||||
std::filesystem::path procstatus = std::filesystem::path("/proc/self/status");
|
std::filesystem::path procstatus = std::filesystem::path("/proc/self/status");
|
||||||
|
@ -490,37 +505,23 @@ static bool is_debugger_present()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
#elif defined(__APPLE__)
|
#elif defined(__APPLE__) || defined(__FreeBSD__)
|
||||||
struct kinfo_proc info;
|
struct kinfo_proc info;
|
||||||
|
std::memset(&info, 0, sizeof(info));
|
||||||
// Initialize the flags so that, if sysctl fails for some bizarre
|
|
||||||
// reason, we get a predictable result.
|
|
||||||
|
|
||||||
info.kp_proc.p_flag = 0;
|
|
||||||
|
|
||||||
// Initialize mib, which tells sysctl the info we want, in this case
|
// Initialize mib, which tells sysctl the info we want, in this case
|
||||||
// we're looking for information about a specific process ID.
|
// we're looking for information about a specific process ID.
|
||||||
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() };
|
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() };
|
||||||
|
|
||||||
// Call sysctl.
|
|
||||||
|
|
||||||
size_t size = sizeof(info);
|
size_t size = sizeof(info);
|
||||||
const int junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, nullptr, 0);
|
if (sysctl(mib, std::size(mib), &info, &size, nullptr, 0) == -1)
|
||||||
assert(junk == 0);
|
{
|
||||||
|
Log(Debug::Warning) << "Failed to call sysctl, assuming no debugger: "
|
||||||
// We're being debugged if the P_TRACED flag is set.
|
<< std::generic_category().message(errno);
|
||||||
|
|
||||||
return (info.kp_proc.p_flag & P_TRACED) != 0;
|
|
||||||
#elif defined(__FreeBSD__)
|
|
||||||
struct kinfo_proc info;
|
|
||||||
size_t size = sizeof(info);
|
|
||||||
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() };
|
|
||||||
|
|
||||||
if (sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, nullptr, 0) == 0)
|
|
||||||
return (info.ki_flag & P_TRACED) != 0;
|
|
||||||
else
|
|
||||||
perror("Failed to retrieve process info");
|
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return isDebuggerPresent(info);
|
||||||
#else
|
#else
|
||||||
return false;
|
return false;
|
||||||
#endif
|
#endif
|
||||||
|
@ -533,7 +534,7 @@ void crashCatcherInstall(int argc, char** argv, const std::filesystem::path& cra
|
||||||
if (argc == 2 && strcmp(argv[1], crash_switch) == 0)
|
if (argc == 2 && strcmp(argv[1], crash_switch) == 0)
|
||||||
handleCrash(Files::pathToUnicodeString(crashLogPath).c_str());
|
handleCrash(Files::pathToUnicodeString(crashLogPath).c_str());
|
||||||
|
|
||||||
if (is_debugger_present())
|
if (isDebuggerPresent())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (crashCatcherInstallHandlers(argv))
|
if (crashCatcherInstallHandlers(argv))
|
||||||
|
|
Loading…
Reference in a new issue