gpt4 book ai didi

iphone - 检测 iOS 应用程序是否在调试器中运行

转载 作者:IT老高 更新时间:2023-10-28 11:41:52 25 4
gpt4 key购买 nike

我将我的应用程序设置为将调试输出发送到控制台或日志文件。现在,我想在代码中决定是否

  • 它在调试器(或模拟器)中运行,因此有一个控制台窗口,我想直接在其中读取输出,或者如果
  • 没有控制台窗口,因此应将输出重定向到文件。

有没有办法确定应用程序是否在调试器中运行?

最佳答案

技术问答 1361(entry in Mac libraryentry in iOS library ;它们是相同的)中有一个来自 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 );
}

还要注意问答末尾的这个注释:

Important: Because the definition of the kinfo_proc structure (in <sys/sysctl.h>) is conditionalized by __APPLE_API_UNSTABLE, you should restrict use of the above code to the debug build of your program.

关于iphone - 检测 iOS 应用程序是否在调试器中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4744826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com