[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] | 79b6fa6 | 2009-10-14 03:01:44 | [diff] [blame] | 16 | #if defined(__GLIBCXX__) |
| 17 | #include <cxxabi.h> |
| 18 | #endif |
| 19 | |
[email protected] | 48c27f7 | 2010-01-26 06:26:26 | [diff] [blame^] | 20 | #include <iostream> |
[email protected] | 79b6fa6 | 2009-10-14 03:01:44 | [diff] [blame] | 21 | #include <string> |
| 22 | |
[email protected] | 6e4bf0b | 2009-09-17 16:12:36 | [diff] [blame] | 23 | #if defined(OS_MACOSX) |
| 24 | #include <AvailabilityMacros.h> |
| 25 | #endif |
| 26 | |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 27 | #include "base/basictypes.h" |
[email protected] | 9543af05 | 2009-09-15 22:42:59 | [diff] [blame] | 28 | #include "base/compat_execinfo.h" |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 29 | #include "base/eintr_wrapper.h" |
[email protected] | 0dfc81b | 2008-08-25 03:44:40 | [diff] [blame] | 30 | #include "base/logging.h" |
[email protected] | 57b76567 | 2009-10-13 18:27:40 | [diff] [blame] | 31 | #include "base/safe_strerror_posix.h" |
[email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 32 | #include "base/scoped_ptr.h" |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 33 | #include "base/string_piece.h" |
[email protected] | 48c27f7 | 2010-01-26 06:26:26 | [diff] [blame^] | 34 | #include "base/string_util.h" |
| 35 | |
| 36 | #if defined(USE_SYMBOLIZE) |
| 37 | #include "base/third_party/symbolize/symbolize.h" |
| 38 | #endif |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 39 | |
[email protected] | 79b6fa6 | 2009-10-14 03:01:44 | [diff] [blame] | 40 | namespace { |
| 41 | // The prefix used for mangled symbols, per the Itanium C++ ABI: |
| 42 | // http://www.codesourcery.com/cxx-abi/abi.html#mangling |
| 43 | const char kMangledSymbolPrefix[] = "_Z"; |
| 44 | |
| 45 | // Characters that can be used for symbols, generated by Ruby: |
| 46 | // (('a'..'z').to_a+('A'..'Z').to_a+('0'..'9').to_a + ['_']).join |
| 47 | const char kSymbolCharacters[] = |
| 48 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"; |
| 49 | |
| 50 | // Demangles C++ symbols in the given text. Example: |
| 51 | // |
| 52 | // "sconsbuild/Debug/base_unittests(_ZN10StackTraceC1Ev+0x20) [0x817778c]" |
| 53 | // => |
| 54 | // "sconsbuild/Debug/base_unittests(StackTrace::StackTrace()+0x20) [0x817778c]" |
| 55 | void DemangleSymbols(std::string* text) { |
| 56 | #if defined(__GLIBCXX__) |
| 57 | |
| 58 | std::string::size_type search_from = 0; |
| 59 | while (search_from < text->size()) { |
| 60 | // Look for the start of a mangled symbol, from search_from. |
| 61 | std::string::size_type mangled_start = |
| 62 | text->find(kMangledSymbolPrefix, search_from); |
| 63 | if (mangled_start == std::string::npos) { |
| 64 | break; // Mangled symbol not found. |
| 65 | } |
| 66 | |
| 67 | // Look for the end of the mangled symbol. |
| 68 | std::string::size_type mangled_end = |
| 69 | text->find_first_not_of(kSymbolCharacters, mangled_start); |
| 70 | if (mangled_end == std::string::npos) { |
| 71 | mangled_end = text->size(); |
| 72 | } |
| 73 | std::string mangled_symbol = |
| 74 | text->substr(mangled_start, mangled_end - mangled_start); |
| 75 | |
| 76 | // Try to demangle the mangled symbol candidate. |
| 77 | int status = 0; |
| 78 | scoped_ptr_malloc<char> demangled_symbol( |
| 79 | abi::__cxa_demangle(mangled_symbol.c_str(), NULL, 0, &status)); |
| 80 | if (status == 0) { // Demangling is successful. |
| 81 | // Remove the mangled symbol. |
| 82 | text->erase(mangled_start, mangled_end - mangled_start); |
| 83 | // Insert the demangled symbol. |
| 84 | text->insert(mangled_start, demangled_symbol.get()); |
| 85 | // Next time, we'll start right after the demangled symbol we inserted. |
| 86 | search_from = mangled_start + strlen(demangled_symbol.get()); |
| 87 | } else { |
| 88 | // Failed to demangle. Retry after the "_Z" we just found. |
| 89 | search_from = mangled_start + 2; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | #endif // defined(__GLIBCXX__) |
| 94 | } |
[email protected] | 48c27f7 | 2010-01-26 06:26:26 | [diff] [blame^] | 95 | |
| 96 | // Gets the backtrace as a vector of strings. If possible, resolve symbol |
| 97 | // names and attach these. Otherwise just use raw addresses. Returns true |
| 98 | // if any symbol name is resolved. |
| 99 | bool GetBacktraceStrings(void **trace, int size, |
| 100 | std::vector<std::string>* trace_strings) { |
| 101 | bool symbolized = false; |
| 102 | |
| 103 | #if defined(USE_SYMBOLIZE) |
| 104 | for (int i = 0; i < size; ++i) { |
| 105 | char symbol[1024]; |
| 106 | // Subtract by one as return address of function may be in the next |
| 107 | // function when a function is annotated as noreturn. |
| 108 | if (google::Symbolize(static_cast<char *>(trace[i]) - 1, |
| 109 | symbol, sizeof(symbol))) { |
| 110 | // Don't call DemangleSymbols() here as the symbol is demangled by |
| 111 | // google::Symbolize(). |
| 112 | trace_strings->push_back(StringPrintf("%s [%p]", symbol, trace[i])); |
| 113 | symbolized = true; |
| 114 | } else { |
| 115 | trace_strings->push_back(StringPrintf("%p", trace[i])); |
| 116 | } |
| 117 | } |
| 118 | #else |
| 119 | scoped_ptr_malloc<char*> trace_symbols(backtrace_symbols(trace, size)); |
| 120 | if (trace_symbols.get()) { |
| 121 | for (int i = 0; i < size; ++i) { |
| 122 | std::string trace_symbol = trace_symbols.get()[i]; |
| 123 | DemangleSymbols(&trace_symbol); |
| 124 | trace_strings->push_back(trace_symbol); |
| 125 | } |
| 126 | symbolized = true; |
| 127 | } else { |
| 128 | for (int i = 0; i < size; ++i) { |
| 129 | trace_strings->push_back(StringPrintf("%p", trace[i])); |
| 130 | } |
| 131 | } |
| 132 | #endif // defined(USE_SYMBOLIZE) |
| 133 | |
| 134 | return symbolized; |
| 135 | } |
| 136 | |
[email protected] | 79b6fa6 | 2009-10-14 03:01:44 | [diff] [blame] | 137 | } // namespace |
| 138 | |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 139 | // static |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 140 | bool DebugUtil::SpawnDebuggerOnProcess(unsigned /* process_id */) { |
| 141 | NOTIMPLEMENTED(); |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | #if defined(OS_MACOSX) |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 146 | |
| 147 | // Based on Apple's recommended method as described in |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 148 | // http://developer.apple.com/qa/qa2004/qa1361.html |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 149 | // static |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 150 | bool DebugUtil::BeingDebugged() { |
[email protected] | dbd9bfc | 2009-02-05 18:25:20 | [diff] [blame] | 151 | // If the process is sandboxed then we can't use the sysctl, so cache the |
| 152 | // value. |
| 153 | static bool is_set = false; |
| 154 | static bool being_debugged = false; |
| 155 | |
| 156 | if (is_set) { |
| 157 | return being_debugged; |
| 158 | } |
| 159 | |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 160 | // Initialize mib, which tells sysctl what info we want. In this case, |
| 161 | // we're looking for information about a specific process ID. |
| 162 | int mib[] = { |
| 163 | CTL_KERN, |
| 164 | KERN_PROC, |
| 165 | KERN_PROC_PID, |
| 166 | getpid() |
| 167 | }; |
| 168 | |
| 169 | // Caution: struct kinfo_proc is marked __APPLE_API_UNSTABLE. The source and |
| 170 | // binary interfaces may change. |
| 171 | struct kinfo_proc info; |
| 172 | size_t info_size = sizeof(info); |
| 173 | |
| 174 | int sysctl_result = sysctl(mib, arraysize(mib), &info, &info_size, NULL, 0); |
| 175 | DCHECK(sysctl_result == 0); |
[email protected] | dbd9bfc | 2009-02-05 18:25:20 | [diff] [blame] | 176 | if (sysctl_result != 0) { |
| 177 | is_set = true; |
| 178 | being_debugged = false; |
| 179 | return being_debugged; |
| 180 | } |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 181 | |
| 182 | // This process is being debugged if the P_TRACED flag is set. |
[email protected] | dbd9bfc | 2009-02-05 18:25:20 | [diff] [blame] | 183 | is_set = true; |
| 184 | being_debugged = (info.kp_proc.p_flag & P_TRACED) != 0; |
| 185 | return being_debugged; |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | #elif defined(OS_LINUX) |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 189 | |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 190 | // We can look in /proc/self/status for TracerPid. We are likely used in crash |
| 191 | // handling, so we are careful not to use the heap or have side effects. |
| 192 | // Another option that is common is to try to ptrace yourself, but then we |
| 193 | // can't detach without forking(), and that's not so great. |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 194 | // static |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 195 | bool DebugUtil::BeingDebugged() { |
| 196 | int status_fd = open("/proc/self/status", O_RDONLY); |
| 197 | if (status_fd == -1) |
| 198 | return false; |
| 199 | |
| 200 | // We assume our line will be in the first 1024 characters and that we can |
| 201 | // read this much all at once. In practice this will generally be true. |
| 202 | // This simplifies and speeds up things considerably. |
| 203 | char buf[1024]; |
| 204 | |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 205 | ssize_t num_read = HANDLE_EINTR(read(status_fd, buf, sizeof(buf))); |
| 206 | HANDLE_EINTR(close(status_fd)); |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 207 | |
| 208 | if (num_read <= 0) |
| 209 | return false; |
| 210 | |
[email protected] | 8a16266e | 2009-09-10 21:08:39 | [diff] [blame] | 211 | base::StringPiece status(buf, num_read); |
| 212 | base::StringPiece tracer("TracerPid:\t"); |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 213 | |
[email protected] | 8a16266e | 2009-09-10 21:08:39 | [diff] [blame] | 214 | base::StringPiece::size_type pid_index = status.find(tracer); |
| 215 | if (pid_index == base::StringPiece::npos) |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 216 | return false; |
| 217 | |
| 218 | // Our pid is 0 without a debugger, assume this for any pid starting with 0. |
| 219 | pid_index += tracer.size(); |
| 220 | return pid_index < status.size() && status[pid_index] != '0'; |
| 221 | } |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 222 | |
[email protected] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 223 | #elif defined(OS_FREEBSD) |
| 224 | |
| 225 | bool DebugUtil::BeingDebugged() { |
| 226 | // TODO(benl): can we determine this under FreeBSD? |
| 227 | NOTIMPLEMENTED(); |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | #endif |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 232 | |
| 233 | // static |
| 234 | void DebugUtil::BreakDebugger() { |
[email protected] | a68eb89 | 2009-05-07 15:38:28 | [diff] [blame] | 235 | #if defined(ARCH_CPU_ARM_FAMILY) |
| 236 | asm("bkpt 0"); |
| 237 | #else |
| 238 | asm("int3"); |
[email protected] | 95fe390 | 2009-05-04 21:13:42 | [diff] [blame] | 239 | #endif |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 240 | } |
[email protected] | 5485cdc | 2009-01-16 21:17:30 | [diff] [blame] | 241 | |
[email protected] | 5485cdc | 2009-01-16 21:17:30 | [diff] [blame] | 242 | StackTrace::StackTrace() { |
[email protected] | 6e4bf0b | 2009-09-17 16:12:36 | [diff] [blame] | 243 | #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] | 244 | if (backtrace == NULL) { |
[email protected] | 9543af05 | 2009-09-15 22:42:59 | [diff] [blame] | 245 | count_ = 0; |
[email protected] | 6e4bf0b | 2009-09-17 16:12:36 | [diff] [blame] | 246 | return; |
[email protected] | 9543af05 | 2009-09-15 22:42:59 | [diff] [blame] | 247 | } |
[email protected] | 6e4bf0b | 2009-09-17 16:12:36 | [diff] [blame] | 248 | #endif |
| 249 | // Though the backtrace API man page does not list any possible negative |
| 250 | // return values, we take no chance. |
| 251 | count_ = std::max(backtrace(trace_, arraysize(trace_)), 0); |
[email protected] | 5485cdc | 2009-01-16 21:17:30 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | void StackTrace::PrintBacktrace() { |
[email protected] | 6e4bf0b | 2009-09-17 16:12:36 | [diff] [blame] | 255 | #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] | 256 | if (backtrace_symbols_fd == NULL) |
[email protected] | 6e4bf0b | 2009-09-17 16:12:36 | [diff] [blame] | 257 | return; |
| 258 | #endif |
| 259 | fflush(stderr); |
[email protected] | 48c27f7 | 2010-01-26 06:26:26 | [diff] [blame^] | 260 | std::vector<std::string> trace_strings; |
| 261 | GetBacktraceStrings(trace_, count_, &trace_strings); |
| 262 | for (size_t i = 0; i < trace_strings.size(); ++i) { |
| 263 | std::cerr << "\t" << trace_strings[i] << "\n"; |
| 264 | } |
[email protected] | 5485cdc | 2009-01-16 21:17:30 | [diff] [blame] | 265 | } |
[email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 266 | |
| 267 | void StackTrace::OutputToStream(std::ostream* os) { |
[email protected] | 6e4bf0b | 2009-09-17 16:12:36 | [diff] [blame] | 268 | #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] | 269 | if (backtrace_symbols == NULL) |
[email protected] | 6e4bf0b | 2009-09-17 16:12:36 | [diff] [blame] | 270 | return; |
| 271 | #endif |
[email protected] | 48c27f7 | 2010-01-26 06:26:26 | [diff] [blame^] | 272 | std::vector<std::string> trace_strings; |
| 273 | if (GetBacktraceStrings(trace_, count_, &trace_strings)) { |
| 274 | (*os) << "Backtrace:\n"; |
| 275 | } else { |
[email protected] | 57b76567 | 2009-10-13 18:27:40 | [diff] [blame] | 276 | (*os) << "Unable get symbols for backtrace (" << safe_strerror(errno) |
[email protected] | 6e4bf0b | 2009-09-17 16:12:36 | [diff] [blame] | 277 | << "). Dumping raw addresses in trace:\n"; |
[email protected] | 48c27f7 | 2010-01-26 06:26:26 | [diff] [blame^] | 278 | } |
| 279 | |
| 280 | for (size_t i = 0; i < trace_strings.size(); ++i) { |
| 281 | (*os) << "\t" << trace_strings[i] << "\n"; |
[email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 282 | } |
| 283 | } |