检测Swift应用程序是否在Xcode中运行

30

我想以编程方式确定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 );
}

1
你可以从Swift调用C函数,因此你不需要真正翻译它。 - Martin R
Martin,那就是我要回答的答案,把它放在答案里,我会点赞的。 - Knight0fDragon
这是一个快速的、未经测试的尝试,将代码转换为Swift(只是为了好玩):https://gist.github.com/getaaron/8d48489274a873835636。我现在没有时间进一步尝试它,但也许它可以帮助你入门。 - Aaron Brager
@Knight0fDragon:将其翻译为Swift是更有趣的挑战 :) - Martin R
2个回答

66

澄清:你的C代码(和下面的Swift版本)检查程序是否在调试器控制下运行,而不是是否从Xcode运行。一个人可以在Xcode之外调试程序(通过直接调用lldb或gdb),也可以在不调试的情况下从Xcode运行程序(如果方案设置中的“Debug可执行文件”复选框关闭)。


您可以简单地保留C函数并从Swift中调用它。 在如何从Swift调用Objective-C代码?中提供的方法同样适用于纯C代码。

但将该代码转换为Swift实际上并不太复杂:

func amIBeingDebugged() -> Bool {
    // Buffer for "sysctl(...)" call's result.
    var info = kinfo_proc()
    // Counts buffer's size in bytes (like C/C++'s `sizeof`).
    var size = MemoryLayout.stride(ofValue: info)
    // Tells we want info about own process.
    var mib : [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()]
    // Call the API (and assert success).
    let junk = sysctl(&mib, UInt32(mib.count), &info, &size, nil, 0)
    assert(junk == 0, "sysctl failed")
    // Finally, checks if debugger's flag is present yet.
    return (info.kp_proc.p_flag & P_TRACED) != 0
}

Swift 5 更新 (Xcode 10.7): strideofValue 和相关函数已经不存在了, 它们被 MemoryLayout.stride(ofValue:) 替代。

备注:

  • kinfo_proc() 创建一个完全初始化的结构体,所有字段都设置为零,因此设置 info.kp_proc.p_flag = 0 是不必要的。
  • C 语言中的 int 类型在 Swift 中是 Int32
  • C 代码中的 sizeof(info) 在 Swift 中需要改为 strideOfValue(info),以包括结构体填充。使用 sizeofValue(info) 会导致上述代码在模拟器中始终返回 false。这是最难搞定的部分。

Swift 2 逻辑:

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
}

3
使用strideOfValue很不错。我之前并不知道它和其他方法的区别。 - Aaron Brager
嗨,马丁,你能提供一个 Obj-C 的 Xcode 8 解决方案来检测调试器吗?苹果提供的那个似乎不再正常工作了。谢谢!:-) - Hans Knöchel
@HansKnoechel:问题中的代码是纯C,可以在Objective-C中毫无问题地使用。它为什么不能“正常工作”? - Martin R

0

对于那些寻找更简单解决方案的人 - 这个非常完美:

func isDebuggerAttached() -> Bool {
    return getppid() != 1
}

我不这么认为。您可以将调试器附加到从Finder启动的应用程序(在这种情况下,父ID为1),也可以从命令行启动没有调试器的应用程序(在这种情况下,父ID与1不同)。 - Martin R

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接