levin@chromium.org | 5c52868 | 2011-03-28 10:54:15 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
brettw@chromium.org | 2f49b12 | 2010-10-26 13:07:50 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/debug/debugger.h" |
abarth@chromium.org | c68999c | 2010-12-01 16:36:54 +0900 | [diff] [blame] | 6 | #include "build/build_config.h" |
brettw@chromium.org | 2f49b12 | 2010-10-26 13:07:50 +0900 | [diff] [blame] | 7 | |
| 8 | #include <errno.h> |
brettw@chromium.org | 6d89907 | 2010-12-30 09:16:19 +0900 | [diff] [blame] | 9 | #include <execinfo.h> |
brettw@chromium.org | 2f49b12 | 2010-10-26 13:07:50 +0900 | [diff] [blame] | 10 | #include <fcntl.h> |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
phajdan.jr@chromium.org | 22b86ac | 2011-04-15 15:15:04 +0900 | [diff] [blame] | 13 | #include <sys/param.h> |
brettw@chromium.org | 2f49b12 | 2010-10-26 13:07:50 +0900 | [diff] [blame] | 14 | #include <sys/stat.h> |
brettw@chromium.org | 2f49b12 | 2010-10-26 13:07:50 +0900 | [diff] [blame] | 15 | #include <sys/types.h> |
| 16 | #include <unistd.h> |
| 17 | |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 | |
| 21 | #if defined(__GLIBCXX__) |
| 22 | #include <cxxabi.h> |
| 23 | #endif |
| 24 | |
| 25 | #if defined(OS_MACOSX) |
| 26 | #include <AvailabilityMacros.h> |
| 27 | #endif |
| 28 | |
chromium@hybridsource.org | 8f85a6a | 2011-06-25 13:54:41 +0900 | [diff] [blame^] | 29 | #if defined(OS_MACOSX) || defined(OS_OPENBSD) || defined(OS_FREEBSD) |
| 30 | #include <sys/sysctl.h> |
| 31 | #endif |
| 32 | |
brettw@chromium.org | 2f49b12 | 2010-10-26 13:07:50 +0900 | [diff] [blame] | 33 | #include <iostream> |
| 34 | |
| 35 | #include "base/basictypes.h" |
brettw@chromium.org | 2f49b12 | 2010-10-26 13:07:50 +0900 | [diff] [blame] | 36 | #include "base/eintr_wrapper.h" |
| 37 | #include "base/logging.h" |
levin@chromium.org | 5c52868 | 2011-03-28 10:54:15 +0900 | [diff] [blame] | 38 | #include "base/memory/scoped_ptr.h" |
brettw@chromium.org | 2f49b12 | 2010-10-26 13:07:50 +0900 | [diff] [blame] | 39 | #include "base/safe_strerror_posix.h" |
brettw@chromium.org | 2f49b12 | 2010-10-26 13:07:50 +0900 | [diff] [blame] | 40 | #include "base/string_piece.h" |
| 41 | #include "base/stringprintf.h" |
| 42 | |
| 43 | #if defined(USE_SYMBOLIZE) |
| 44 | #include "base/third_party/symbolize/symbolize.h" |
| 45 | #endif |
| 46 | |
| 47 | namespace base { |
| 48 | namespace debug { |
| 49 | |
| 50 | bool SpawnDebuggerOnProcess(unsigned /* process_id */) { |
| 51 | NOTIMPLEMENTED(); |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | #if defined(OS_MACOSX) |
| 56 | |
| 57 | // Based on Apple's recommended method as described in |
| 58 | // http://developer.apple.com/qa/qa2004/qa1361.html |
| 59 | bool BeingDebugged() { |
| 60 | // If the process is sandboxed then we can't use the sysctl, so cache the |
| 61 | // value. |
| 62 | static bool is_set = false; |
| 63 | static bool being_debugged = false; |
| 64 | |
| 65 | if (is_set) { |
| 66 | return being_debugged; |
| 67 | } |
| 68 | |
| 69 | // Initialize mib, which tells sysctl what info we want. In this case, |
| 70 | // we're looking for information about a specific process ID. |
| 71 | int mib[] = { |
| 72 | CTL_KERN, |
| 73 | KERN_PROC, |
| 74 | KERN_PROC_PID, |
| 75 | getpid() |
| 76 | }; |
| 77 | |
| 78 | // Caution: struct kinfo_proc is marked __APPLE_API_UNSTABLE. The source and |
| 79 | // binary interfaces may change. |
| 80 | struct kinfo_proc info; |
| 81 | size_t info_size = sizeof(info); |
| 82 | |
| 83 | int sysctl_result = sysctl(mib, arraysize(mib), &info, &info_size, NULL, 0); |
| 84 | DCHECK_EQ(sysctl_result, 0); |
| 85 | if (sysctl_result != 0) { |
| 86 | is_set = true; |
| 87 | being_debugged = false; |
| 88 | return being_debugged; |
| 89 | } |
| 90 | |
| 91 | // This process is being debugged if the P_TRACED flag is set. |
| 92 | is_set = true; |
| 93 | being_debugged = (info.kp_proc.p_flag & P_TRACED) != 0; |
| 94 | return being_debugged; |
| 95 | } |
| 96 | |
| 97 | #elif defined(OS_LINUX) |
| 98 | |
| 99 | // We can look in /proc/self/status for TracerPid. We are likely used in crash |
| 100 | // handling, so we are careful not to use the heap or have side effects. |
| 101 | // Another option that is common is to try to ptrace yourself, but then we |
| 102 | // can't detach without forking(), and that's not so great. |
| 103 | // static |
| 104 | bool BeingDebugged() { |
| 105 | int status_fd = open("/proc/self/status", O_RDONLY); |
| 106 | if (status_fd == -1) |
| 107 | return false; |
| 108 | |
| 109 | // We assume our line will be in the first 1024 characters and that we can |
| 110 | // read this much all at once. In practice this will generally be true. |
| 111 | // This simplifies and speeds up things considerably. |
| 112 | char buf[1024]; |
| 113 | |
| 114 | ssize_t num_read = HANDLE_EINTR(read(status_fd, buf, sizeof(buf))); |
| 115 | if (HANDLE_EINTR(close(status_fd)) < 0) |
| 116 | return false; |
| 117 | |
| 118 | if (num_read <= 0) |
| 119 | return false; |
| 120 | |
| 121 | StringPiece status(buf, num_read); |
| 122 | StringPiece tracer("TracerPid:\t"); |
| 123 | |
| 124 | StringPiece::size_type pid_index = status.find(tracer); |
| 125 | if (pid_index == StringPiece::npos) |
| 126 | return false; |
| 127 | |
| 128 | // Our pid is 0 without a debugger, assume this for any pid starting with 0. |
| 129 | pid_index += tracer.size(); |
| 130 | return pid_index < status.size() && status[pid_index] != '0'; |
| 131 | } |
| 132 | |
abarth@chromium.org | c68999c | 2010-12-01 16:36:54 +0900 | [diff] [blame] | 133 | #elif defined(OS_NACL) |
| 134 | |
| 135 | bool BeingDebugged() { |
| 136 | NOTIMPLEMENTED(); |
| 137 | return false; |
| 138 | } |
| 139 | |
chromium@hybridsource.org | 8f85a6a | 2011-06-25 13:54:41 +0900 | [diff] [blame^] | 140 | #else |
brettw@chromium.org | 2f49b12 | 2010-10-26 13:07:50 +0900 | [diff] [blame] | 141 | |
brettw@google.com | b39f6c0 | 2011-01-02 01:08:52 +0900 | [diff] [blame] | 142 | bool BeingDebugged() { |
brettw@chromium.org | 2f49b12 | 2010-10-26 13:07:50 +0900 | [diff] [blame] | 143 | // TODO(benl): can we determine this under FreeBSD? |
| 144 | NOTIMPLEMENTED(); |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | #endif // defined(OS_FREEBSD) |
| 149 | |
| 150 | // We want to break into the debugger in Debug mode, and cause a crash dump in |
| 151 | // Release mode. Breakpad behaves as follows: |
| 152 | // |
| 153 | // +-------+-----------------+-----------------+ |
| 154 | // | OS | Dump on SIGTRAP | Dump on SIGABRT | |
| 155 | // +-------+-----------------+-----------------+ |
| 156 | // | Linux | N | Y | |
| 157 | // | Mac | Y | N | |
| 158 | // +-------+-----------------+-----------------+ |
| 159 | // |
| 160 | // Thus we do the following: |
| 161 | // Linux: Debug mode, send SIGTRAP; Release mode, send SIGABRT. |
| 162 | // Mac: Always send SIGTRAP. |
| 163 | |
| 164 | #if defined(NDEBUG) && !defined(OS_MACOSX) |
| 165 | #define DEBUG_BREAK() abort() |
abarth@chromium.org | c68999c | 2010-12-01 16:36:54 +0900 | [diff] [blame] | 166 | #elif defined(OS_NACL) |
| 167 | // The NaCl verifier doesn't let use use int3. For now, we call abort(). We |
| 168 | // should ask for advice from some NaCl experts about the optimum thing here. |
| 169 | // http://code.google.com/p/nativeclient/issues/detail?id=645 |
| 170 | #define DEBUG_BREAK() abort() |
brettw@chromium.org | 2f49b12 | 2010-10-26 13:07:50 +0900 | [diff] [blame] | 171 | #elif defined(ARCH_CPU_ARM_FAMILY) |
| 172 | #define DEBUG_BREAK() asm("bkpt 0") |
| 173 | #else |
| 174 | #define DEBUG_BREAK() asm("int3") |
| 175 | #endif |
| 176 | |
| 177 | void BreakDebugger() { |
| 178 | DEBUG_BREAK(); |
cevans@chromium.org | b3bfed5 | 2011-01-21 14:02:17 +0900 | [diff] [blame] | 179 | #if defined(NDEBUG) |
| 180 | _exit(1); |
| 181 | #endif |
brettw@chromium.org | 2f49b12 | 2010-10-26 13:07:50 +0900 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | } // namespace debug |
| 185 | } // namespace base |