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

How can I tell if an OSX program is running in the debugger?

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

摘自 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;
}