OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/debug_util.h" | 5 #include "base/debug/debugger.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <stdio.h> | 9 #include <stdio.h> |
10 #include <stdlib.h> | 10 #include <stdlib.h> |
11 #include <sys/stat.h> | 11 #include <sys/stat.h> |
12 #include <sys/sysctl.h> | 12 #include <sys/sysctl.h> |
13 #include <sys/types.h> | 13 #include <sys/types.h> |
14 #include <unistd.h> | 14 #include <unistd.h> |
15 | 15 |
(...skipping 16 matching lines...) Expand all Loading... |
32 #include "base/logging.h" | 32 #include "base/logging.h" |
33 #include "base/safe_strerror_posix.h" | 33 #include "base/safe_strerror_posix.h" |
34 #include "base/scoped_ptr.h" | 34 #include "base/scoped_ptr.h" |
35 #include "base/string_piece.h" | 35 #include "base/string_piece.h" |
36 #include "base/stringprintf.h" | 36 #include "base/stringprintf.h" |
37 | 37 |
38 #if defined(USE_SYMBOLIZE) | 38 #if defined(USE_SYMBOLIZE) |
39 #include "base/third_party/symbolize/symbolize.h" | 39 #include "base/third_party/symbolize/symbolize.h" |
40 #endif | 40 #endif |
41 | 41 |
42 namespace { | 42 namespace base { |
43 // The prefix used for mangled symbols, per the Itanium C++ ABI: | 43 namespace debug { |
44 // http://www.codesourcery.com/cxx-abi/abi.html#mangling | |
45 const char kMangledSymbolPrefix[] = "_Z"; | |
46 | 44 |
47 // Characters that can be used for symbols, generated by Ruby: | 45 bool SpawnDebuggerOnProcess(unsigned /* process_id */) { |
48 // (('a'..'z').to_a+('A'..'Z').to_a+('0'..'9').to_a + ['_']).join | |
49 const char kSymbolCharacters[] = | |
50 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"; | |
51 | |
52 #if !defined(USE_SYMBOLIZE) | |
53 // Demangles C++ symbols in the given text. Example: | |
54 // | |
55 // "sconsbuild/Debug/base_unittests(_ZN10StackTraceC1Ev+0x20) [0x817778c]" | |
56 // => | |
57 // "sconsbuild/Debug/base_unittests(StackTrace::StackTrace()+0x20) [0x817778c]" | |
58 void DemangleSymbols(std::string* text) { | |
59 #if defined(__GLIBCXX__) | |
60 | |
61 std::string::size_type search_from = 0; | |
62 while (search_from < text->size()) { | |
63 // Look for the start of a mangled symbol, from search_from. | |
64 std::string::size_type mangled_start = | |
65 text->find(kMangledSymbolPrefix, search_from); | |
66 if (mangled_start == std::string::npos) { | |
67 break; // Mangled symbol not found. | |
68 } | |
69 | |
70 // Look for the end of the mangled symbol. | |
71 std::string::size_type mangled_end = | |
72 text->find_first_not_of(kSymbolCharacters, mangled_start); | |
73 if (mangled_end == std::string::npos) { | |
74 mangled_end = text->size(); | |
75 } | |
76 std::string mangled_symbol = | |
77 text->substr(mangled_start, mangled_end - mangled_start); | |
78 | |
79 // Try to demangle the mangled symbol candidate. | |
80 int status = 0; | |
81 scoped_ptr_malloc<char> demangled_symbol( | |
82 abi::__cxa_demangle(mangled_symbol.c_str(), NULL, 0, &status)); | |
83 if (status == 0) { // Demangling is successful. | |
84 // Remove the mangled symbol. | |
85 text->erase(mangled_start, mangled_end - mangled_start); | |
86 // Insert the demangled symbol. | |
87 text->insert(mangled_start, demangled_symbol.get()); | |
88 // Next time, we'll start right after the demangled symbol we inserted. | |
89 search_from = mangled_start + strlen(demangled_symbol.get()); | |
90 } else { | |
91 // Failed to demangle. Retry after the "_Z" we just found. | |
92 search_from = mangled_start + 2; | |
93 } | |
94 } | |
95 | |
96 #endif // defined(__GLIBCXX__) | |
97 } | |
98 #endif // !defined(USE_SYMBOLIZE) | |
99 | |
100 // Gets the backtrace as a vector of strings. If possible, resolve symbol | |
101 // names and attach these. Otherwise just use raw addresses. Returns true | |
102 // if any symbol name is resolved. Returns false on error and *may* fill | |
103 // in |error_message| if an error message is available. | |
104 bool GetBacktraceStrings(void **trace, int size, | |
105 std::vector<std::string>* trace_strings, | |
106 std::string* error_message) { | |
107 bool symbolized = false; | |
108 | |
109 #if defined(USE_SYMBOLIZE) | |
110 for (int i = 0; i < size; ++i) { | |
111 char symbol[1024]; | |
112 // Subtract by one as return address of function may be in the next | |
113 // function when a function is annotated as noreturn. | |
114 if (google::Symbolize(static_cast<char *>(trace[i]) - 1, | |
115 symbol, sizeof(symbol))) { | |
116 // Don't call DemangleSymbols() here as the symbol is demangled by | |
117 // google::Symbolize(). | |
118 trace_strings->push_back( | |
119 base::StringPrintf("%s [%p]", symbol, trace[i])); | |
120 symbolized = true; | |
121 } else { | |
122 trace_strings->push_back(base::StringPrintf("%p", trace[i])); | |
123 } | |
124 } | |
125 #else | |
126 scoped_ptr_malloc<char*> trace_symbols(backtrace_symbols(trace, size)); | |
127 if (trace_symbols.get()) { | |
128 for (int i = 0; i < size; ++i) { | |
129 std::string trace_symbol = trace_symbols.get()[i]; | |
130 DemangleSymbols(&trace_symbol); | |
131 trace_strings->push_back(trace_symbol); | |
132 } | |
133 symbolized = true; | |
134 } else { | |
135 if (error_message) | |
136 *error_message = safe_strerror(errno); | |
137 for (int i = 0; i < size; ++i) { | |
138 trace_strings->push_back(base::StringPrintf("%p", trace[i])); | |
139 } | |
140 } | |
141 #endif // defined(USE_SYMBOLIZE) | |
142 | |
143 return symbolized; | |
144 } | |
145 | |
146 } // namespace | |
147 | |
148 // static | |
149 bool DebugUtil::SpawnDebuggerOnProcess(unsigned /* process_id */) { | |
150 NOTIMPLEMENTED(); | 46 NOTIMPLEMENTED(); |
151 return false; | 47 return false; |
152 } | 48 } |
153 | 49 |
154 #if defined(OS_MACOSX) | 50 #if defined(OS_MACOSX) |
155 | 51 |
156 // Based on Apple's recommended method as described in | 52 // Based on Apple's recommended method as described in |
157 // http://developer.apple.com/qa/qa2004/qa1361.html | 53 // http://developer.apple.com/qa/qa2004/qa1361.html |
158 // static | 54 bool BeingDebugged() { |
159 bool DebugUtil::BeingDebugged() { | |
160 // If the process is sandboxed then we can't use the sysctl, so cache the | 55 // If the process is sandboxed then we can't use the sysctl, so cache the |
161 // value. | 56 // value. |
162 static bool is_set = false; | 57 static bool is_set = false; |
163 static bool being_debugged = false; | 58 static bool being_debugged = false; |
164 | 59 |
165 if (is_set) { | 60 if (is_set) { |
166 return being_debugged; | 61 return being_debugged; |
167 } | 62 } |
168 | 63 |
169 // Initialize mib, which tells sysctl what info we want. In this case, | 64 // Initialize mib, which tells sysctl what info we want. In this case, |
(...skipping 24 matching lines...) Expand all Loading... |
194 return being_debugged; | 89 return being_debugged; |
195 } | 90 } |
196 | 91 |
197 #elif defined(OS_LINUX) | 92 #elif defined(OS_LINUX) |
198 | 93 |
199 // We can look in /proc/self/status for TracerPid. We are likely used in crash | 94 // We can look in /proc/self/status for TracerPid. We are likely used in crash |
200 // handling, so we are careful not to use the heap or have side effects. | 95 // handling, so we are careful not to use the heap or have side effects. |
201 // Another option that is common is to try to ptrace yourself, but then we | 96 // Another option that is common is to try to ptrace yourself, but then we |
202 // can't detach without forking(), and that's not so great. | 97 // can't detach without forking(), and that's not so great. |
203 // static | 98 // static |
204 bool DebugUtil::BeingDebugged() { | 99 bool BeingDebugged() { |
205 int status_fd = open("/proc/self/status", O_RDONLY); | 100 int status_fd = open("/proc/self/status", O_RDONLY); |
206 if (status_fd == -1) | 101 if (status_fd == -1) |
207 return false; | 102 return false; |
208 | 103 |
209 // We assume our line will be in the first 1024 characters and that we can | 104 // We assume our line will be in the first 1024 characters and that we can |
210 // read this much all at once. In practice this will generally be true. | 105 // read this much all at once. In practice this will generally be true. |
211 // This simplifies and speeds up things considerably. | 106 // This simplifies and speeds up things considerably. |
212 char buf[1024]; | 107 char buf[1024]; |
213 | 108 |
214 ssize_t num_read = HANDLE_EINTR(read(status_fd, buf, sizeof(buf))); | 109 ssize_t num_read = HANDLE_EINTR(read(status_fd, buf, sizeof(buf))); |
215 if (HANDLE_EINTR(close(status_fd)) < 0) | 110 if (HANDLE_EINTR(close(status_fd)) < 0) |
216 return false; | 111 return false; |
217 | 112 |
218 if (num_read <= 0) | 113 if (num_read <= 0) |
219 return false; | 114 return false; |
220 | 115 |
221 base::StringPiece status(buf, num_read); | 116 StringPiece status(buf, num_read); |
222 base::StringPiece tracer("TracerPid:\t"); | 117 StringPiece tracer("TracerPid:\t"); |
223 | 118 |
224 base::StringPiece::size_type pid_index = status.find(tracer); | 119 StringPiece::size_type pid_index = status.find(tracer); |
225 if (pid_index == base::StringPiece::npos) | 120 if (pid_index == StringPiece::npos) |
226 return false; | 121 return false; |
227 | 122 |
228 // Our pid is 0 without a debugger, assume this for any pid starting with 0. | 123 // Our pid is 0 without a debugger, assume this for any pid starting with 0. |
229 pid_index += tracer.size(); | 124 pid_index += tracer.size(); |
230 return pid_index < status.size() && status[pid_index] != '0'; | 125 return pid_index < status.size() && status[pid_index] != '0'; |
231 } | 126 } |
232 | 127 |
233 #elif defined(OS_FREEBSD) | 128 #elif defined(OS_FREEBSD) |
234 | 129 |
235 bool DebugUtil::BeingDebugged() { | 130 bool DebugUtil::BeingDebugged() { |
(...skipping 19 matching lines...) Expand all Loading... |
255 // Mac: Always send SIGTRAP. | 150 // Mac: Always send SIGTRAP. |
256 | 151 |
257 #if defined(NDEBUG) && !defined(OS_MACOSX) | 152 #if defined(NDEBUG) && !defined(OS_MACOSX) |
258 #define DEBUG_BREAK() abort() | 153 #define DEBUG_BREAK() abort() |
259 #elif defined(ARCH_CPU_ARM_FAMILY) | 154 #elif defined(ARCH_CPU_ARM_FAMILY) |
260 #define DEBUG_BREAK() asm("bkpt 0") | 155 #define DEBUG_BREAK() asm("bkpt 0") |
261 #else | 156 #else |
262 #define DEBUG_BREAK() asm("int3") | 157 #define DEBUG_BREAK() asm("int3") |
263 #endif | 158 #endif |
264 | 159 |
265 // static | 160 void BreakDebugger() { |
266 void DebugUtil::BreakDebugger() { | |
267 DEBUG_BREAK(); | 161 DEBUG_BREAK(); |
268 } | 162 } |
269 | 163 |
270 StackTrace::StackTrace() { | 164 } // namespace debug |
271 #if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 | 165 } // namespace base |
272 if (backtrace == NULL) { | |
273 count_ = 0; | |
274 return; | |
275 } | |
276 #endif | |
277 // Though the backtrace API man page does not list any possible negative | |
278 // return values, we take no chance. | |
279 count_ = std::max(backtrace(trace_, arraysize(trace_)), 0); | |
280 } | |
281 | |
282 void StackTrace::PrintBacktrace() { | |
283 #if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 | |
284 if (backtrace_symbols_fd == NULL) | |
285 return; | |
286 #endif | |
287 fflush(stderr); | |
288 std::vector<std::string> trace_strings; | |
289 GetBacktraceStrings(trace_, count_, &trace_strings, NULL); | |
290 for (size_t i = 0; i < trace_strings.size(); ++i) { | |
291 std::cerr << "\t" << trace_strings[i] << "\n"; | |
292 } | |
293 } | |
294 | |
295 void StackTrace::OutputToStream(std::ostream* os) { | |
296 #if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 | |
297 if (backtrace_symbols == NULL) | |
298 return; | |
299 #endif | |
300 std::vector<std::string> trace_strings; | |
301 std::string error_message; | |
302 if (GetBacktraceStrings(trace_, count_, &trace_strings, &error_message)) { | |
303 (*os) << "Backtrace:\n"; | |
304 } else { | |
305 if (!error_message.empty()) | |
306 error_message = " (" + error_message + ")"; | |
307 (*os) << "Unable to get symbols for backtrace" << error_message << ". " | |
308 << "Dumping raw addresses in trace:\n"; | |
309 } | |
310 | |
311 for (size_t i = 0; i < trace_strings.size(); ++i) { | |
312 (*os) << "\t" << trace_strings[i] << "\n"; | |
313 } | |
314 } | |
OLD | NEW |