[email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 1 | // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 4 | |
[email protected] | 5485cdc | 2009-01-16 21:17:30 | [diff] [blame] | 5 | #include "build/build_config.h" |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 6 | #include "base/debug_util.h" |
| 7 | |
[email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 8 | #include <errno.h> |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 9 | #include <fcntl.h> |
[email protected] | d1fc437 | 2009-01-16 22:06:19 | [diff] [blame] | 10 | #include <stdio.h> |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 11 | #include <sys/stat.h> |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 12 | #include <sys/sysctl.h> |
| 13 | #include <sys/types.h> |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 14 | #include <unistd.h> |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 15 | |
[email protected] | 6e4bf0b | 2009-09-17 16:12:36 | [diff] [blame] | 16 | #if defined(OS_MACOSX) |
| 17 | #include <AvailabilityMacros.h> |
| 18 | #endif |
| 19 | |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 20 | #include "base/basictypes.h" |
[email protected] | 9543af05 | 2009-09-15 22:42:59 | [diff] [blame] | 21 | #include "base/compat_execinfo.h" |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 22 | #include "base/eintr_wrapper.h" |
[email protected] | 0dfc81b | 2008-08-25 03:44:40 | [diff] [blame] | 23 | #include "base/logging.h" |
[email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 24 | #include "base/scoped_ptr.h" |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 25 | #include "base/string_piece.h" |
| 26 | |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 27 | // static |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 28 | bool DebugUtil::SpawnDebuggerOnProcess(unsigned /* process_id */) { |
| 29 | NOTIMPLEMENTED(); |
| 30 | return false; |
| 31 | } |
| 32 | |
| 33 | #if defined(OS_MACOSX) |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 34 | |
| 35 | // Based on Apple's recommended method as described in |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 36 | // http://developer.apple.com/qa/qa2004/qa1361.html |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 37 | // static |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 38 | bool DebugUtil::BeingDebugged() { |
[email protected] | dbd9bfc | 2009-02-05 18:25:20 | [diff] [blame] | 39 | // If the process is sandboxed then we can't use the sysctl, so cache the |
| 40 | // value. |
| 41 | static bool is_set = false; |
| 42 | static bool being_debugged = false; |
| 43 | |
| 44 | if (is_set) { |
| 45 | return being_debugged; |
| 46 | } |
| 47 | |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 48 | // Initialize mib, which tells sysctl what info we want. In this case, |
| 49 | // we're looking for information about a specific process ID. |
| 50 | int mib[] = { |
| 51 | CTL_KERN, |
| 52 | KERN_PROC, |
| 53 | KERN_PROC_PID, |
| 54 | getpid() |
| 55 | }; |
| 56 | |
| 57 | // Caution: struct kinfo_proc is marked __APPLE_API_UNSTABLE. The source and |
| 58 | // binary interfaces may change. |
| 59 | struct kinfo_proc info; |
| 60 | size_t info_size = sizeof(info); |
| 61 | |
| 62 | int sysctl_result = sysctl(mib, arraysize(mib), &info, &info_size, NULL, 0); |
| 63 | DCHECK(sysctl_result == 0); |
[email protected] | dbd9bfc | 2009-02-05 18:25:20 | [diff] [blame] | 64 | if (sysctl_result != 0) { |
| 65 | is_set = true; |
| 66 | being_debugged = false; |
| 67 | return being_debugged; |
| 68 | } |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 69 | |
| 70 | // This process is being debugged if the P_TRACED flag is set. |
[email protected] | dbd9bfc | 2009-02-05 18:25:20 | [diff] [blame] | 71 | is_set = true; |
| 72 | being_debugged = (info.kp_proc.p_flag & P_TRACED) != 0; |
| 73 | return being_debugged; |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | #elif defined(OS_LINUX) |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 77 | |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 78 | // We can look in /proc/self/status for TracerPid. We are likely used in crash |
| 79 | // handling, so we are careful not to use the heap or have side effects. |
| 80 | // Another option that is common is to try to ptrace yourself, but then we |
| 81 | // can't detach without forking(), and that's not so great. |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 82 | // static |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 83 | bool DebugUtil::BeingDebugged() { |
| 84 | int status_fd = open("/proc/self/status", O_RDONLY); |
| 85 | if (status_fd == -1) |
| 86 | return false; |
| 87 | |
| 88 | // We assume our line will be in the first 1024 characters and that we can |
| 89 | // read this much all at once. In practice this will generally be true. |
| 90 | // This simplifies and speeds up things considerably. |
| 91 | char buf[1024]; |
| 92 | |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 93 | ssize_t num_read = HANDLE_EINTR(read(status_fd, buf, sizeof(buf))); |
| 94 | HANDLE_EINTR(close(status_fd)); |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 95 | |
| 96 | if (num_read <= 0) |
| 97 | return false; |
| 98 | |
[email protected] | 8a16266e | 2009-09-10 21:08:39 | [diff] [blame] | 99 | base::StringPiece status(buf, num_read); |
| 100 | base::StringPiece tracer("TracerPid:\t"); |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 101 | |
[email protected] | 8a16266e | 2009-09-10 21:08:39 | [diff] [blame] | 102 | base::StringPiece::size_type pid_index = status.find(tracer); |
| 103 | if (pid_index == base::StringPiece::npos) |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 104 | return false; |
| 105 | |
| 106 | // Our pid is 0 without a debugger, assume this for any pid starting with 0. |
| 107 | pid_index += tracer.size(); |
| 108 | return pid_index < status.size() && status[pid_index] != '0'; |
| 109 | } |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 110 | |
| 111 | #endif // OS_LINUX |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 112 | |
| 113 | // static |
| 114 | void DebugUtil::BreakDebugger() { |
[email protected] | a68eb89 | 2009-05-07 15:38:28 | [diff] [blame] | 115 | #if defined(ARCH_CPU_ARM_FAMILY) |
| 116 | asm("bkpt 0"); |
| 117 | #else |
| 118 | asm("int3"); |
[email protected] | 95fe390 | 2009-05-04 21:13:42 | [diff] [blame] | 119 | #endif |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 120 | } |
[email protected] | 5485cdc | 2009-01-16 21:17:30 | [diff] [blame] | 121 | |
[email protected] | 5485cdc | 2009-01-16 21:17:30 | [diff] [blame] | 122 | StackTrace::StackTrace() { |
[email protected] | 6e4bf0b | 2009-09-17 16:12:36 | [diff] [blame] | 123 | #if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 |
[email protected] | f358a0a | 2009-09-18 15:27:56 | [diff] [blame^] | 124 | if (backtrace == NULL) { |
[email protected] | 9543af05 | 2009-09-15 22:42:59 | [diff] [blame] | 125 | count_ = 0; |
[email protected] | 6e4bf0b | 2009-09-17 16:12:36 | [diff] [blame] | 126 | return; |
[email protected] | 9543af05 | 2009-09-15 22:42:59 | [diff] [blame] | 127 | } |
[email protected] | 6e4bf0b | 2009-09-17 16:12:36 | [diff] [blame] | 128 | #endif |
| 129 | // Though the backtrace API man page does not list any possible negative |
| 130 | // return values, we take no chance. |
| 131 | count_ = std::max(backtrace(trace_, arraysize(trace_)), 0); |
[email protected] | 5485cdc | 2009-01-16 21:17:30 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void StackTrace::PrintBacktrace() { |
[email protected] | 6e4bf0b | 2009-09-17 16:12:36 | [diff] [blame] | 135 | #if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 |
[email protected] | f358a0a | 2009-09-18 15:27:56 | [diff] [blame^] | 136 | if (backtrace_symbols_fd == NULL) |
[email protected] | 6e4bf0b | 2009-09-17 16:12:36 | [diff] [blame] | 137 | return; |
| 138 | #endif |
| 139 | fflush(stderr); |
| 140 | backtrace_symbols_fd(trace_, count_, STDERR_FILENO); |
[email protected] | 5485cdc | 2009-01-16 21:17:30 | [diff] [blame] | 141 | } |
[email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 142 | |
| 143 | void StackTrace::OutputToStream(std::ostream* os) { |
[email protected] | 6e4bf0b | 2009-09-17 16:12:36 | [diff] [blame] | 144 | #if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 |
[email protected] | f358a0a | 2009-09-18 15:27:56 | [diff] [blame^] | 145 | if (backtrace_symbols == NULL) |
[email protected] | 6e4bf0b | 2009-09-17 16:12:36 | [diff] [blame] | 146 | return; |
| 147 | #endif |
| 148 | scoped_ptr_malloc<char*> trace_symbols(backtrace_symbols(trace_, count_)); |
[email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 149 | |
[email protected] | 6e4bf0b | 2009-09-17 16:12:36 | [diff] [blame] | 150 | // If we can't retrieve the symbols, print an error and just dump the raw |
| 151 | // addresses. |
| 152 | if (trace_symbols.get() == NULL) { |
| 153 | (*os) << "Unable get symbols for backtrace (" << strerror(errno) |
| 154 | << "). Dumping raw addresses in trace:\n"; |
| 155 | for (int i = 0; i < count_; ++i) { |
| 156 | (*os) << "\t" << trace_[i] << "\n"; |
| 157 | } |
| 158 | } else { |
| 159 | (*os) << "Backtrace:\n"; |
| 160 | for (int i = 0; i < count_; ++i) { |
| 161 | (*os) << "\t" << trace_symbols.get()[i] << "\n"; |
[email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | } |