如何判断OSX程序是否在调试器中运行?

时间:2015-02-26 01:24:02

标签: c++ c macos debugging

lldb或gdb是否设置了任何环境变量或有其他任何说明?

1 个答案:

答案 0 :(得分:2)

来自Google Breakpad

#include <sys/syscntl.h>

bool isInDebugger() {

  bool result=false;
  pid_t pid = getpid();
  int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
  int mibSize = sizeof(mib) / sizeof(int);
  size_t actualSize;

  if (sysctl(mib, mibSize, NULL, &actualSize, NULL, 0) == 0) {
    struct kinfo_proc *info = (struct kinfo_proc *)malloc(actualSize);

    if (info) {
      // This comes from looking at the Darwin xnu Kernel
      if (sysctl(mib, mibSize, info, &actualSize, NULL, 0) == 0)
        result = (info->kp_proc.p_flag & P_TRACED) ? true : false;

      free(info);
    }
  }

  return result;
}