From e6f45624a5e05fcce4171080c56d5a671bd28e97 Mon Sep 17 00:00:00 2001 From: "mark@chromium.org" Date: Wed, 8 Oct 2008 15:27:40 +0000 Subject: Add Mac BeingDebugged implementation Review URL: http://codereview.chromium.org/6582 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@3009 0039d316-1c4b-4281-b951-d872f2087c98 --- base/debug_util_posix.cc | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/base/debug_util_posix.cc b/base/debug_util_posix.cc index f4da37c..a83e144 100644 --- a/base/debug_util_posix.cc +++ b/base/debug_util_posix.cc @@ -4,32 +4,58 @@ #include "base/debug_util.h" -#include +#include +#include #include #include -#include -#include +#include +#include "base/basictypes.h" #include "base/logging.h" #include "base/string_piece.h" +// static bool DebugUtil::SpawnDebuggerOnProcess(unsigned /* process_id */) { NOTIMPLEMENTED(); return false; } #if defined(OS_MACOSX) + +// Based on Apple's recommended method as described in // http://developer.apple.com/qa/qa2004/qa1361.html +// static bool DebugUtil::BeingDebugged() { - NOTIMPLEMENTED(); - return false; + // Initialize mib, which tells sysctl what info we want. In this case, + // we're looking for information about a specific process ID. + int mib[] = { + CTL_KERN, + KERN_PROC, + KERN_PROC_PID, + getpid() + }; + + // Caution: struct kinfo_proc is marked __APPLE_API_UNSTABLE. The source and + // binary interfaces may change. + struct kinfo_proc info; + size_t info_size = sizeof(info); + + int sysctl_result = sysctl(mib, arraysize(mib), &info, &info_size, NULL, 0); + DCHECK(sysctl_result == 0); + if (sysctl_result != 0) + return false; + + // This process is being debugged if the P_TRACED flag is set. + return (info.kp_proc.p_flag & P_TRACED) != 0; } #elif defined(OS_LINUX) + // We can look in /proc/self/status for TracerPid. We are likely used in crash // handling, so we are careful not to use the heap or have side effects. // Another option that is common is to try to ptrace yourself, but then we // can't detach without forking(), and that's not so great. +// static bool DebugUtil::BeingDebugged() { int status_fd = open("/proc/self/status", O_RDONLY); if (status_fd == -1) @@ -57,10 +83,10 @@ bool DebugUtil::BeingDebugged() { pid_index += tracer.size(); return pid_index < status.size() && status[pid_index] != '0'; } -#endif + +#endif // OS_LINUX // static void DebugUtil::BreakDebugger() { asm ("int3"); } - -- cgit v1.1