我想以编程方式确定iOS应用程序是否直接从XCode运行(在模拟器中或在系留设备上).我已经尝试了这里描述的-D DEBUG解决方案,但是当我从Xcode断开并重新运行应用程序时,它仍然认为它处于调试模式.我认为我正在寻找的是这个函数的Swift版本
#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 );
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*n R 49
澄清:您的C代码(以及下面的Swift版本)检查程序是否在调试器控制下运行,而不是从Xcode运行.可以调试Xcode之外的程序(通过直接调用lldb或gdb),可以从Xcode运行程序而无需调试它(如果方案设置中的"Debug Executable"复选框关闭).
您可以简单地保留C函数并从Swift中调用它.如何从Swift调用Objective-C代码中给出的配方也适用于纯C代码.
但是将代码转换为Swift实际上并不太复杂:
func amIBeingDebugged() -> Bool {
var info = kinfo_proc()
var mib : [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()]
var size = strideofValue(info)
let junk = sysctl(&mib, UInt32(mib.count), &info, &size, nil, 0)
assert(junk == 0, "sysctl failed")
return (info.kp_proc.p_flag & P_TRACED) != 0
}
Run Code Online (Sandbox Code Playgroud)
备注:
kinfo_proc()创建一个完全初始化的结构,所有字段都设置为零,因此info.kp_proc.p_flag = 0不需要设置.int类型Int32是Swift.sizeof(info)从C代码必须strideOfValue(info)
在Swift中包含结构填充.使用sizeofValue(info)
上述代码在64位设备的模拟器中始终返回false.这是最困难的部分.更新Swift 3(Xcode 8):
strideofValue并且相关功能不再存在,它们已被替换为MemoryLayout:
func amIBeingDebugged() -> Bool {
var info = kinfo_proc()
var mib : [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()]
var size = MemoryLayout<kinfo_proc>.stride
let junk = sysctl(&mib, UInt32(mib.count), &info, &size, nil, 0)
assert(junk == 0, "sysctl failed")
return (info.kp_proc.p_flag & P_TRACED) != 0
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2971 次 |
| 最近记录: |