Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(243)

Side by Side Diff: base/debug_util_posix.cc

Issue 201050: Print stack trace on exception in unit tests on Windows.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 11 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2006-2009 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 <errno.h>
9 #include <execinfo.h> 9 #include <execinfo.h>
10 #include <fcntl.h> 10 #include <fcntl.h>
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 // static 109 // static
110 void DebugUtil::BreakDebugger() { 110 void DebugUtil::BreakDebugger() {
111 #if defined(ARCH_CPU_ARM_FAMILY) 111 #if defined(ARCH_CPU_ARM_FAMILY)
112 asm("bkpt 0"); 112 asm("bkpt 0");
113 #else 113 #else
114 asm("int3"); 114 asm("int3");
115 #endif 115 #endif
116 } 116 }
117 117
118 StackTrace::StackTrace() { 118 StackTrace::StackTrace() {
119 const int kMaxCallers = 256;
120
121 void* callers[kMaxCallers];
122 int count = backtrace(callers, kMaxCallers);
123
124 // Though the backtrace API man page does not list any possible negative 119 // Though the backtrace API man page does not list any possible negative
125 // return values, we still still exclude them because they would break the 120 // return values, we take no chance.
126 // memcpy code below. 121 count_ = std::max(backtrace(trace_, arraysize(trace_)), 0);
127 if (count > 0) {
128 trace_.resize(count);
129 memcpy(&trace_[0], callers, sizeof(callers[0]) * count);
130 } else {
131 trace_.resize(0);
132 }
133 } 122 }
134 123
135 void StackTrace::PrintBacktrace() { 124 void StackTrace::PrintBacktrace() {
136 fflush(stderr); 125 fflush(stderr);
137 backtrace_symbols_fd(&trace_[0], trace_.size(), STDERR_FILENO); 126 backtrace_symbols_fd(trace_, count_, STDERR_FILENO);
138 } 127 }
139 128
140 void StackTrace::OutputToStream(std::ostream* os) { 129 void StackTrace::OutputToStream(std::ostream* os) {
141 scoped_ptr_malloc<char*> trace_symbols( 130 scoped_ptr_malloc<char*> trace_symbols(backtrace_symbols(trace_, count_));
142 backtrace_symbols(&trace_[0], trace_.size()));
143 131
144 // If we can't retrieve the symbols, print an error and just dump the raw 132 // If we can't retrieve the symbols, print an error and just dump the raw
145 // addresses. 133 // addresses.
146 if (trace_symbols.get() == NULL) { 134 if (trace_symbols.get() == NULL) {
147 (*os) << "Unable get symbols for backtrace (" << strerror(errno) 135 (*os) << "Unable get symbols for backtrace (" << strerror(errno)
148 << "). Dumping raw addresses in trace:\n"; 136 << "). Dumping raw addresses in trace:\n";
149 for (size_t i = 0; i < trace_.size(); ++i) { 137 for (int i = 0; i < count_; ++i) {
150 (*os) << "\t" << trace_[i] << "\n"; 138 (*os) << "\t" << trace_[i] << "\n";
151 } 139 }
152 } else { 140 } else {
153 (*os) << "Backtrace:\n"; 141 (*os) << "Backtrace:\n";
154 for (size_t i = 0; i < trace_.size(); ++i) { 142 for (int i = 0; i < count_; ++i) {
155 (*os) << "\t" << trace_symbols.get()[i] << "\n"; 143 (*os) << "\t" << trace_symbols.get()[i] << "\n";
156 } 144 }
157 } 145 }
158 } 146 }
OLDNEW
« no previous file with comments | « base/debug_util.cc ('k') | base/debug_util_win.cc » ('j') | base/debug_util_win.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698