我试图检测我的进程是否正在调试器中运行,在 Windows 中有很多解决方案,在 Linux 中我使用:
ptrace(PTRACE_ME,0,0,0)
并检查其返回值,我没有设法在 Mac OS X 上执行相同的基本检查。
我尝试使用
ptrace(PT_TRACE_ME,0,0,0)
打电话但是它always即使在 gdb 下运行也返回 0。
如果我将请求更改为PT_DENY_ATTACH
它正确地停止了调试,但这不是我想要实现的目标。有任何想法吗?
你只需调用该函数即可AmIBeingDebugged()
from 苹果技术问答 QA1361 https://developer.apple.com/library/archive/qa/qa1361/_index.html,此处转载是因为 Apple 有时会破坏文档链接并使旧文档难以找到:
#include <assert.h>
#include <stdbool.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/sysctl.h>
static bool AmIBeingDebugged(void)
// Returns true if the current process is being debugged (either
// running under the debugger or has a debugger attached post facto).
{
int junk;
int mib[4];
struct kinfo_proc info;
size_t size;
// 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
// we're looking for information about a specific process ID.
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PID;
mib[3] = getpid();
// Call sysctl.
size = sizeof(info);
junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);
assert(junk == 0);
// We're being debugged if the P_TRACED flag is set.
return ( (info.kp_proc.p_flag & P_TRACED) != 0 );
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)