检测iOS应用程序是否在调试器中运行 [英] Detecting if iOS app is run in debugger

查看:99
本文介绍了检测iOS应用程序是否在调试器中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了我的应用程序将调试输出发送到控制台或日志文件。现在,我想在代码中决定是否在调试器(或模拟器)中运行,并且因此具有控制台的




  • 窗口,我想直接读取输出,或者如果

  • 没有控制台窗口,因此输出应该被重定向到一个文件。



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

      解决方案

      p>苹果公司有一个功能来检测是否在技术问答1361中调试了一个程序( Mac库中的条目 iOS库中的条目;它们是相同的)。



      技术问答代码

        #include< ; assert.h> 
      #include< stdbool.h>
      #include< sys / types.h>
      #include< unistd.h>
      #include< sys / sysctl.h>

      static bool AmIBeingDebugged(void)
      //如果正在调试当前进程(
      //在调试器下运行或调试器附加事后),则返回true。
      {
      int junk;
      int mib [4];
      struct kinfo_proc info;
      size_t size;

      //初始化标志,以便如果sysctl失败了一些奇怪的
      //原因,我们得到一个可预测的结果。

      info.kp_proc.p_flag = 0;

      //初始化mib,它告诉sysctl我们想要的信息,在这种情况下
      //我们正在寻找有关特定进程ID的信息。

      mib [0] = CTL_KERN;
      mib [1] = KERN_PROC;
      mib [2] = KERN_PROC_PID;
      mib [3] = getpid();

      //调用sysctl。

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

      //如果设置了P_TRACED标志,我们正在调试。

      return((info.kp_proc.p_flag& P_TRACED)!= 0);
      }

      还要注意Q& A: / p>


      重要提示:因为 kinfo_proc 结构的定义(在< sys / sysctl.h> )以 __ APPLE_API_UNSTABLE 为条件,您应该限制使用上述代码到您的程序的调试版本。



      I set up my application to either send debugging output to console or a log file. Now, I'd like to decide with in the code whether

      • it is run in the debugger (or simulator) and have thus a console window where I would like to read the output directly or if
      • there is no console window and thus, the output should be redirected to a file.

      Is there a way to determine if the app runs in the debugger?

      解决方案

      There's a function from Apple to detect whether a program is being debugged in the Technical Q&A 1361 (entry in Mac library and entry in iOS library; they are identical).

      Code from the Technical Q&A:

      #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 );
      }
      

      Also pay attention to this note at the end of the Q&A:

      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.

      这篇关于检测iOS应用程序是否在调试器中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆