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/stack_trace.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 base { |
| 43 namespace debug { |
| 44 |
42 namespace { | 45 namespace { |
| 46 |
43 // The prefix used for mangled symbols, per the Itanium C++ ABI: | 47 // The prefix used for mangled symbols, per the Itanium C++ ABI: |
44 // http://www.codesourcery.com/cxx-abi/abi.html#mangling | 48 // http://www.codesourcery.com/cxx-abi/abi.html#mangling |
45 const char kMangledSymbolPrefix[] = "_Z"; | 49 const char kMangledSymbolPrefix[] = "_Z"; |
46 | 50 |
47 // Characters that can be used for symbols, generated by Ruby: | 51 // Characters that can be used for symbols, generated by Ruby: |
48 // (('a'..'z').to_a+('A'..'Z').to_a+('0'..'9').to_a + ['_']).join | 52 // (('a'..'z').to_a+('A'..'Z').to_a+('0'..'9').to_a + ['_']).join |
49 const char kSymbolCharacters[] = | 53 const char kSymbolCharacters[] = |
50 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"; | 54 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"; |
51 | 55 |
52 #if !defined(USE_SYMBOLIZE) | 56 #if !defined(USE_SYMBOLIZE) |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 trace_strings->push_back(base::StringPrintf("%p", trace[i])); | 142 trace_strings->push_back(base::StringPrintf("%p", trace[i])); |
139 } | 143 } |
140 } | 144 } |
141 #endif // defined(USE_SYMBOLIZE) | 145 #endif // defined(USE_SYMBOLIZE) |
142 | 146 |
143 return symbolized; | 147 return symbolized; |
144 } | 148 } |
145 | 149 |
146 } // namespace | 150 } // namespace |
147 | 151 |
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() { | 152 StackTrace::StackTrace() { |
271 #if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 | 153 #if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 |
272 if (backtrace == NULL) { | 154 if (backtrace == NULL) { |
273 count_ = 0; | 155 count_ = 0; |
274 return; | 156 return; |
275 } | 157 } |
276 #endif | 158 #endif |
277 // Though the backtrace API man page does not list any possible negative | 159 // Though the backtrace API man page does not list any possible negative |
278 // return values, we take no chance. | 160 // return values, we take no chance. |
279 count_ = std::max(backtrace(trace_, arraysize(trace_)), 0); | 161 count_ = std::max(backtrace(trace_, arraysize(trace_)), 0); |
(...skipping 25 matching lines...) Expand all Loading... |
305 if (!error_message.empty()) | 187 if (!error_message.empty()) |
306 error_message = " (" + error_message + ")"; | 188 error_message = " (" + error_message + ")"; |
307 (*os) << "Unable to get symbols for backtrace" << error_message << ". " | 189 (*os) << "Unable to get symbols for backtrace" << error_message << ". " |
308 << "Dumping raw addresses in trace:\n"; | 190 << "Dumping raw addresses in trace:\n"; |
309 } | 191 } |
310 | 192 |
311 for (size_t i = 0; i < trace_strings.size(); ++i) { | 193 for (size_t i = 0; i < trace_strings.size(); ++i) { |
312 (*os) << "\t" << trace_strings[i] << "\n"; | 194 (*os) << "\t" << trace_strings[i] << "\n"; |
313 } | 195 } |
314 } | 196 } |
| 197 |
| 198 } // namespace debug |
| 199 } // namespace base |
OLD | NEW |