飘云阁

 找回密码
 加入我们

QQ登录

只需一步,快速开始

查看: 3175|回复: 1

[iOS] Detecting the Debugger

  [复制链接]
  • TA的每日心情
    奋斗
    2015-10-29 08:08
  • 签到天数: 3 天

    [LV.2]偶尔看看I

    发表于 2015-1-19 11:17:26 | 显示全部楼层 |阅读模式
    本帖最后由 creantan 于 2015-1-19 11:20 编辑

    Q:  How do I determine if I'm being run under the debugger?
    A: How do I determine if I'm being run under the debugger?
    The code in Listing 1 shows the best way to do this.

    1. #include <assert.h>
    2. #include <stdbool.h>
    3. #include <sys/types.h>
    4. #include <unistd.h>
    5. #include <sys/sysctl.h>

    6. static bool AmIBeingDebugged(void)
    7.     // Returns true if the current process is being debugged (either
    8.     // running under the debugger or has a debugger attached post facto).
    9. {
    10.     int                 junk;
    11.     int                 mib[4];
    12.     struct kinfo_proc   info;
    13.     size_t              size;

    14.     // Initialize the flags so that, if sysctl fails for some bizarre
    15.     // reason, we get a predictable result.

    16.     info.kp_proc.p_flag = 0;

    17.     // Initialize mib, which tells sysctl the info we want, in this case
    18.     // we're looking for information about a specific process ID.

    19.     mib[0] = CTL_KERN;
    20.     mib[1] = KERN_PROC;
    21.     mib[2] = KERN_PROC_PID;
    22.     mib[3] = getpid();

    23.     // Call sysctl.

    24.     size = sizeof(info);
    25.     junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);
    26.     assert(junk == 0);

    27.     // We're being debugged if the P_TRACED flag is set.

    28.     return ( (info.kp_proc.p_flag & P_TRACED) != 0 );
    29. }
    复制代码
    1. 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.
    复制代码
    https://developer.apple.com/library/mac/qa/qa1361/_index.html
    PYG19周年生日快乐!
  • TA的每日心情
    奋斗
    2017-3-10 20:59
  • 签到天数: 243 天

    [LV.8]以坛为家I

    发表于 2015-1-19 11:56:22 | 显示全部楼层
    沙发,不懂,只有支持
    PYG19周年生日快乐!
    您需要登录后才可以回帖 登录 | 加入我们

    本版积分规则

    快速回复 返回顶部 返回列表