OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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 "build/build_config.h" | 5 #include "build/build_config.h" |
6 #include "base/debug_util.h" | 6 #include "base/debug_util.h" |
7 | 7 |
| 8 #include <errno.h> |
8 #include <execinfo.h> | 9 #include <execinfo.h> |
9 #include <fcntl.h> | 10 #include <fcntl.h> |
10 #include <stdio.h> | 11 #include <stdio.h> |
11 #include <sys/stat.h> | 12 #include <sys/stat.h> |
12 #include <sys/sysctl.h> | 13 #include <sys/sysctl.h> |
13 #include <sys/types.h> | 14 #include <sys/types.h> |
14 #include <unistd.h> | 15 #include <unistd.h> |
15 | 16 |
16 #include "base/basictypes.h" | 17 #include "base/basictypes.h" |
17 #include "base/logging.h" | 18 #include "base/logging.h" |
| 19 #include "base/scoped_ptr.h" |
18 #include "base/string_piece.h" | 20 #include "base/string_piece.h" |
19 | 21 |
20 // static | 22 // static |
21 bool DebugUtil::SpawnDebuggerOnProcess(unsigned /* process_id */) { | 23 bool DebugUtil::SpawnDebuggerOnProcess(unsigned /* process_id */) { |
22 NOTIMPLEMENTED(); | 24 NOTIMPLEMENTED(); |
23 return false; | 25 return false; |
24 } | 26 } |
25 | 27 |
26 #if defined(OS_MACOSX) | 28 #if defined(OS_MACOSX) |
27 | 29 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 } | 104 } |
103 | 105 |
104 #endif // OS_LINUX | 106 #endif // OS_LINUX |
105 | 107 |
106 // static | 108 // static |
107 void DebugUtil::BreakDebugger() { | 109 void DebugUtil::BreakDebugger() { |
108 asm ("int3"); | 110 asm ("int3"); |
109 } | 111 } |
110 | 112 |
111 StackTrace::StackTrace() { | 113 StackTrace::StackTrace() { |
112 static const int kMaxCallers = 256; | 114 const int kMaxCallers = 256; |
113 | 115 |
114 void* callers[kMaxCallers]; | 116 void* callers[kMaxCallers]; |
115 int count = backtrace(callers, kMaxCallers); | 117 int count = backtrace(callers, kMaxCallers); |
116 trace_.resize(count); | 118 |
117 memcpy(&trace_[0], callers, sizeof(void*) * count); | 119 // Though the backtrace API man page does not list any possible negative |
| 120 // return values, we still still exclude them because they would break the |
| 121 // memcpy code below. |
| 122 if (count > 0) { |
| 123 trace_.resize(count); |
| 124 memcpy(&trace_[0], callers, sizeof(callers[0]) * count); |
| 125 } else { |
| 126 trace_.resize(0); |
| 127 } |
118 } | 128 } |
119 | 129 |
120 void StackTrace::PrintBacktrace() { | 130 void StackTrace::PrintBacktrace() { |
121 fflush(stderr); | 131 fflush(stderr); |
122 backtrace_symbols_fd(&trace_[0], trace_.size(), STDERR_FILENO); | 132 backtrace_symbols_fd(&trace_[0], trace_.size(), STDERR_FILENO); |
123 } | 133 } |
| 134 |
| 135 void StackTrace::OutputToStream(std::ostream* os) { |
| 136 scoped_ptr_malloc<char*> trace_symbols( |
| 137 backtrace_symbols(&trace_[0], trace_.size())); |
| 138 |
| 139 // If we can't retrieve the symbols, print an error and just dump the raw |
| 140 // addresses. |
| 141 if (trace_symbols.get() == NULL) { |
| 142 (*os) << "Unable get symbols for backtrace (" << strerror(errno) |
| 143 << "). Dumping raw addresses in trace:\n"; |
| 144 for (size_t i = 0; i < trace_.size(); ++i) { |
| 145 (*os) << "\t" << trace_[i] << "\n"; |
| 146 } |
| 147 } else { |
| 148 (*os) << "Backtrace:\n"; |
| 149 for (size_t i = 0; i < trace_.size(); ++i) { |
| 150 (*os) << "\t" << trace_symbols.get()[i] << "\n"; |
| 151 } |
| 152 } |
| 153 } |
OLD | NEW |