OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/debug_util.h" | |
6 | |
7 #include <errno.h> | |
8 #include <fcntl.h> | |
9 #include <stdio.h> | |
10 #include <stdlib.h> | |
11 #include <sys/stat.h> | |
12 #include <sys/sysctl.h> | |
13 #include <sys/types.h> | |
14 #include <unistd.h> | |
15 | |
16 #include <string> | |
17 #include <vector> | |
18 | |
19 #if defined(__GLIBCXX__) | |
20 #include <cxxabi.h> | |
21 #endif | |
22 | |
23 #if defined(OS_MACOSX) | |
24 #include <AvailabilityMacros.h> | |
25 #endif | |
26 | |
27 #include <iostream> | |
28 | |
29 #include "base/basictypes.h" | |
30 #include "base/compat_execinfo.h" | |
31 #include "base/eintr_wrapper.h" | |
32 #include "base/logging.h" | |
33 #include "base/safe_strerror_posix.h" | |
34 #include "base/scoped_ptr.h" | |
35 #include "base/string_piece.h" | |
36 #include "base/stringprintf.h" | |
37 | |
38 #if defined(USE_SYMBOLIZE) | |
39 #include "base/third_party/symbolize/symbolize.h" | |
40 #endif | |
41 | |
42 namespace { | |
43 // The prefix used for mangled symbols, per the Itanium C++ ABI: | |
44 // http://www.codesourcery.com/cxx-abi/abi.html#mangling | |
45 const char kMangledSymbolPrefix[] = "_Z"; | |
46 | |
47 // Characters that can be used for symbols, generated by Ruby: | |
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(); | |
151 return false; | |
152 } | |
153 | |
154 #if defined(OS_MACOSX) | |
155 | |
156 // Based on Apple's recommended method as described in | |
157 // http://developer.apple.com/qa/qa2004/qa1361.html | |
158 // static | |
159 bool DebugUtil::BeingDebugged() { | |
160 // If the process is sandboxed then we can't use the sysctl, so cache the | |
161 // value. | |
162 static bool is_set = false; | |
163 static bool being_debugged = false; | |
164 | |
165 if (is_set) { | |
166 return being_debugged; | |
167 } | |
168 | |
169 // Initialize mib, which tells sysctl what info we want. In this case, | |
170 // we're looking for information about a specific process ID. | |
171 int mib[] = { | |
172 CTL_KERN, | |
173 KERN_PROC, | |
174 KERN_PROC_PID, | |
175 getpid() | |
176 }; | |
177 | |
178 // Caution: struct kinfo_proc is marked __APPLE_API_UNSTABLE. The source and | |
179 // binary interfaces may change. | |
180 struct kinfo_proc info; | |
181 size_t info_size = sizeof(info); | |
182 | |
183 int sysctl_result = sysctl(mib, arraysize(mib), &info, &info_size, NULL, 0); | |
184 DCHECK_EQ(sysctl_result, 0); | |
185 if (sysctl_result != 0) { | |
186 is_set = true; | |
187 being_debugged = false; | |
188 return being_debugged; | |
189 } | |
190 | |
191 // This process is being debugged if the P_TRACED flag is set. | |
192 is_set = true; | |
193 being_debugged = (info.kp_proc.p_flag & P_TRACED) != 0; | |
194 return being_debugged; | |
195 } | |
196 | |
197 #elif defined(OS_LINUX) | |
198 | |
199 // 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. | |
201 // 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. | |
203 // static | |
204 bool DebugUtil::BeingDebugged() { | |
205 int status_fd = open("/proc/self/status", O_RDONLY); | |
206 if (status_fd == -1) | |
207 return false; | |
208 | |
209 // 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. | |
211 // This simplifies and speeds up things considerably. | |
212 char buf[1024]; | |
213 | |
214 ssize_t num_read = HANDLE_EINTR(read(status_fd, buf, sizeof(buf))); | |
215 if (HANDLE_EINTR(close(status_fd)) < 0) | |
216 return false; | |
217 | |
218 if (num_read <= 0) | |
219 return false; | |
220 | |
221 base::StringPiece status(buf, num_read); | |
222 base::StringPiece tracer("TracerPid:\t"); | |
223 | |
224 base::StringPiece::size_type pid_index = status.find(tracer); | |
225 if (pid_index == base::StringPiece::npos) | |
226 return false; | |
227 | |
228 // Our pid is 0 without a debugger, assume this for any pid starting with 0. | |
229 pid_index += tracer.size(); | |
230 return pid_index < status.size() && status[pid_index] != '0'; | |
231 } | |
232 | |
233 #elif defined(OS_FREEBSD) | |
234 | |
235 bool DebugUtil::BeingDebugged() { | |
236 // TODO(benl): can we determine this under FreeBSD? | |
237 NOTIMPLEMENTED(); | |
238 return false; | |
239 } | |
240 | |
241 #endif // defined(OS_FREEBSD) | |
242 | |
243 // We want to break into the debugger in Debug mode, and cause a crash dump in | |
244 // Release mode. Breakpad behaves as follows: | |
245 // | |
246 // +-------+-----------------+-----------------+ | |
247 // | OS | Dump on SIGTRAP | Dump on SIGABRT | | |
248 // +-------+-----------------+-----------------+ | |
249 // | Linux | N | Y | | |
250 // | Mac | Y | N | | |
251 // +-------+-----------------+-----------------+ | |
252 // | |
253 // Thus we do the following: | |
254 // Linux: Debug mode, send SIGTRAP; Release mode, send SIGABRT. | |
255 // Mac: Always send SIGTRAP. | |
256 | |
257 #if defined(NDEBUG) && !defined(OS_MACOSX) | |
258 #define DEBUG_BREAK() abort() | |
259 #elif defined(ARCH_CPU_ARM_FAMILY) | |
260 #define DEBUG_BREAK() asm("bkpt 0") | |
261 #else | |
262 #define DEBUG_BREAK() asm("int3") | |
263 #endif | |
264 | |
265 // static | |
266 void DebugUtil::BreakDebugger() { | |
267 DEBUG_BREAK(); | |
268 } | |
269 | |
270 StackTrace::StackTrace() { | |
271 #if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 | |
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 |