OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/stack_trace.h" | 5 #include "base/debug/stack_trace.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <execinfo.h> | 8 #include <execinfo.h> |
9 #include <fcntl.h> | 9 #include <fcntl.h> |
10 #include <stdio.h> | 10 #include <stdio.h> |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 } | 141 } |
142 } | 142 } |
143 #endif // defined(USE_SYMBOLIZE) | 143 #endif // defined(USE_SYMBOLIZE) |
144 | 144 |
145 return symbolized; | 145 return symbolized; |
146 } | 146 } |
147 | 147 |
148 } // namespace | 148 } // namespace |
149 | 149 |
150 StackTrace::StackTrace() { | 150 StackTrace::StackTrace() { |
151 #if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 | |
152 if (backtrace == NULL) { | |
153 count_ = 0; | |
154 return; | |
155 } | |
156 #endif | |
157 // Though the backtrace API man page does not list any possible negative | 151 // Though the backtrace API man page does not list any possible negative |
158 // return values, we take no chance. | 152 // return values, we take no chance. |
159 count_ = std::max(backtrace(trace_, arraysize(trace_)), 0); | 153 count_ = std::max(backtrace(trace_, arraysize(trace_)), 0); |
160 } | 154 } |
161 | 155 |
162 void StackTrace::PrintBacktrace() const { | 156 void StackTrace::PrintBacktrace() const { |
163 #if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 | |
164 if (backtrace_symbols_fd == NULL) | |
165 return; | |
166 #endif | |
167 fflush(stderr); | 157 fflush(stderr); |
168 std::vector<std::string> trace_strings; | 158 std::vector<std::string> trace_strings; |
169 GetBacktraceStrings(trace_, count_, &trace_strings, NULL); | 159 GetBacktraceStrings(trace_, count_, &trace_strings, NULL); |
170 for (size_t i = 0; i < trace_strings.size(); ++i) { | 160 for (size_t i = 0; i < trace_strings.size(); ++i) { |
171 fprintf(stderr, "\t%s\n", trace_strings[i].c_str()); | 161 fprintf(stderr, "\t%s\n", trace_strings[i].c_str()); |
172 } | 162 } |
173 } | 163 } |
174 | 164 |
175 void StackTrace::OutputToStream(std::ostream* os) const { | 165 void StackTrace::OutputToStream(std::ostream* os) const { |
176 #if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 | |
177 if (backtrace_symbols == NULL) | |
178 return; | |
179 #endif | |
180 std::vector<std::string> trace_strings; | 166 std::vector<std::string> trace_strings; |
181 std::string error_message; | 167 std::string error_message; |
182 if (GetBacktraceStrings(trace_, count_, &trace_strings, &error_message)) { | 168 if (GetBacktraceStrings(trace_, count_, &trace_strings, &error_message)) { |
183 (*os) << "Backtrace:\n"; | 169 (*os) << "Backtrace:\n"; |
184 } else { | 170 } else { |
185 if (!error_message.empty()) | 171 if (!error_message.empty()) |
186 error_message = " (" + error_message + ")"; | 172 error_message = " (" + error_message + ")"; |
187 (*os) << "Unable to get symbols for backtrace" << error_message << ". " | 173 (*os) << "Unable to get symbols for backtrace" << error_message << ". " |
188 << "Dumping raw addresses in trace:\n"; | 174 << "Dumping raw addresses in trace:\n"; |
189 } | 175 } |
190 | 176 |
191 for (size_t i = 0; i < trace_strings.size(); ++i) { | 177 for (size_t i = 0; i < trace_strings.size(); ++i) { |
192 (*os) << "\t" << trace_strings[i] << "\n"; | 178 (*os) << "\t" << trace_strings[i] << "\n"; |
193 } | 179 } |
194 } | 180 } |
195 | 181 |
196 } // namespace debug | 182 } // namespace debug |
197 } // namespace base | 183 } // namespace base |
OLD | NEW |