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

Side by Side Diff: base/debug/stack_trace_win.cc

Issue 1284083002: Print stack traces in child processes when browser tests failed. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: more fixes Created 5 years, 4 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
« no previous file with comments | « base/debug/stack_trace_posix.cc ('k') | base/process/launch_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <windows.h> 7 #include <windows.h>
8 #include <dbghelp.h> 8 #include <dbghelp.h>
9 9
10 #include <iostream> 10 #include <iostream>
11 11
12 #include "base/basictypes.h" 12 #include "base/basictypes.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/memory/singleton.h" 14 #include "base/memory/singleton.h"
15 #include "base/process/launch.h" 15 #include "base/process/launch.h"
16 #include "base/strings/string_util.h" 16 #include "base/strings/string_util.h"
17 #include "base/synchronization/lock.h" 17 #include "base/synchronization/lock.h"
18 #include "base/win/windows_version.h" 18 #include "base/win/windows_version.h"
19 19
20 namespace base { 20 namespace base {
21 namespace debug { 21 namespace debug {
22 22
23 namespace { 23 namespace {
24 24
25 // Previous unhandled filter. Will be called if not NULL when we intercept an 25 // Previous unhandled filter. Will be called if not NULL when we intercept an
26 // exception. Only used in unit tests. 26 // exception. Only used in unit tests.
27 LPTOP_LEVEL_EXCEPTION_FILTER g_previous_filter = NULL; 27 LPTOP_LEVEL_EXCEPTION_FILTER g_previous_filter = NULL;
28 28
29 DWORD g_init_error = ERROR_SUCCESS;
30
29 // Prints the exception call stack. 31 // Prints the exception call stack.
30 // This is the unit tests exception filter. 32 // This is the unit tests exception filter.
31 long WINAPI StackDumpExceptionFilter(EXCEPTION_POINTERS* info) { 33 long WINAPI StackDumpExceptionFilter(EXCEPTION_POINTERS* info) {
32 debug::StackTrace(info).Print(); 34 debug::StackTrace(info).Print();
33 if (g_previous_filter) 35 if (g_previous_filter)
34 return g_previous_filter(info); 36 return g_previous_filter(info);
35 return EXCEPTION_CONTINUE_SEARCH; 37 return EXCEPTION_CONTINUE_SEARCH;
36 } 38 }
37 39
38 FilePath GetExePath() { 40 FilePath GetExePath() {
(...skipping 20 matching lines...) Expand all
59 // just ignore it. 61 // just ignore it.
60 class SymbolContext { 62 class SymbolContext {
61 public: 63 public:
62 static SymbolContext* GetInstance() { 64 static SymbolContext* GetInstance() {
63 // We use a leaky singleton because code may call this during process 65 // We use a leaky singleton because code may call this during process
64 // termination. 66 // termination.
65 return 67 return
66 Singleton<SymbolContext, LeakySingletonTraits<SymbolContext> >::get(); 68 Singleton<SymbolContext, LeakySingletonTraits<SymbolContext> >::get();
67 } 69 }
68 70
69 // Returns the error code of a failed initialization.
70 DWORD init_error() const {
71 return init_error_;
72 }
73
74 // For the given trace, attempts to resolve the symbols, and output a trace 71 // For the given trace, attempts to resolve the symbols, and output a trace
75 // to the ostream os. The format for each line of the backtrace is: 72 // to the ostream os. The format for each line of the backtrace is:
76 // 73 //
77 // <tab>SymbolName[0xAddress+Offset] (FileName:LineNo) 74 // <tab>SymbolName[0xAddress+Offset] (FileName:LineNo)
78 // 75 //
79 // This function should only be called if Init() has been called. We do not 76 // This function should only be called if Init() has been called. We do not
80 // LOG(FATAL) here because this code is called might be triggered by a 77 // LOG(FATAL) here because this code is called might be triggered by a
81 // LOG(FATAL) itself. Also, it should not be calling complex code that is 78 // LOG(FATAL) itself. Also, it should not be calling complex code that is
82 // extensible like PathService since that can in turn fire CHECKs. 79 // extensible like PathService since that can in turn fire CHECKs.
83 void OutputTraceToStream(const void* const* trace, 80 void OutputTraceToStream(const void* const* trace,
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 if (has_line) { 122 if (has_line) {
126 (*os) << " (" << line.FileName << ":" << line.LineNumber << ")"; 123 (*os) << " (" << line.FileName << ":" << line.LineNumber << ")";
127 } 124 }
128 (*os) << "\n"; 125 (*os) << "\n";
129 } 126 }
130 } 127 }
131 128
132 private: 129 private:
133 friend struct DefaultSingletonTraits<SymbolContext>; 130 friend struct DefaultSingletonTraits<SymbolContext>;
134 131
135 SymbolContext() : init_error_(ERROR_SUCCESS) { 132 SymbolContext() {
136 // Initializes the symbols for the process.
137 // Defer symbol load until they're needed, use undecorated names, and
138 // get line numbers.
139 SymSetOptions(SYMOPT_DEFERRED_LOADS |
140 SYMOPT_UNDNAME |
141 SYMOPT_LOAD_LINES);
142 if (!SymInitialize(GetCurrentProcess(), NULL, TRUE)) {
143 init_error_ = GetLastError();
144 // TODO(awong): Handle error: SymInitialize can fail with
145 // ERROR_INVALID_PARAMETER.
146 // When it fails, we should not call debugbreak since it kills the current
147 // process (prevents future tests from running or kills the browser
148 // process).
149 DLOG(ERROR) << "SymInitialize failed: " << init_error_;
150 return;
151 }
152
153 init_error_ = ERROR_SUCCESS;
154
155 // When transferring the binaries e.g. between bots, path put
156 // into the executable will get off. To still retrieve symbols correctly,
157 // add the directory of the executable to symbol search path.
158 // All following errors are non-fatal.
159 const size_t kSymbolsArraySize = 1024;
160 scoped_ptr<wchar_t[]> symbols_path(new wchar_t[kSymbolsArraySize]);
161
162 // Note: The below function takes buffer size as number of characters,
163 // not number of bytes!
164 if (!SymGetSearchPathW(GetCurrentProcess(),
165 symbols_path.get(),
166 kSymbolsArraySize)) {
167 DLOG(WARNING) << "SymGetSearchPath failed: ";
168 return;
169 }
170
171 std::wstring new_path(std::wstring(symbols_path.get()) +
172 L";" + GetExePath().DirName().value());
173 if (!SymSetSearchPathW(GetCurrentProcess(), new_path.c_str())) {
174 DLOG(WARNING) << "SymSetSearchPath failed.";
175 return;
176 }
177 } 133 }
178 134
179 DWORD init_error_;
180 base::Lock lock_; 135 base::Lock lock_;
181 DISALLOW_COPY_AND_ASSIGN(SymbolContext); 136 DISALLOW_COPY_AND_ASSIGN(SymbolContext);
182 }; 137 };
183 138
184 } // namespace 139 } // namespace
185 140
186 bool EnableInProcessStackDumping() { 141 bool EnableInProcessStackDumping() {
187 // Add stack dumping support on exception on windows. Similar to OS_POSIX 142 // Add stack dumping support on exception on windows. Similar to OS_POSIX
188 // signal() handling in process_util_posix.cc. 143 // signal() handling in process_util_posix.cc.
189 g_previous_filter = SetUnhandledExceptionFilter(&StackDumpExceptionFilter); 144 g_previous_filter = SetUnhandledExceptionFilter(&StackDumpExceptionFilter);
190 RouteStdioToConsole(); 145 RouteStdioToConsole();
146
147 // Need to initialize symbols early in the process or else
148 // SymGetSymFromAddr64 fails on release x64 builds.
149
150 // Defer symbol load until they're needed, use undecorated names, and get line
151 // numbers.
152 SymSetOptions(SYMOPT_DEFERRED_LOADS |
153 SYMOPT_UNDNAME |
154 SYMOPT_LOAD_LINES);
155 if (!SymInitialize(GetCurrentProcess(), NULL, TRUE)) {
156 g_init_error = GetLastError();
157 // TODO(awong): Handle error: SymInitialize can fail with
158 // ERROR_INVALID_PARAMETER.
159 // When it fails, we should not call debugbreak since it kills the current
160 // process (prevents future tests from running or kills the browser
161 // process).
162 DLOG(ERROR) << "SymInitialize failed: " << g_init_error;
163 return false;
164 }
165
166
167 // When transferring the binaries e.g. between bots, path put
168 // into the executable will get off. To still retrieve symbols correctly,
169 // add the directory of the executable to symbol search path.
170 // All following errors are non-fatal.
171 const size_t kSymbolsArraySize = 1024;
172 scoped_ptr<wchar_t[]> symbols_path(new wchar_t[kSymbolsArraySize]);
173
174 // Note: The below function takes buffer size as number of characters,
175 // not number of bytes!
176 if (!SymGetSearchPathW(GetCurrentProcess(),
177 symbols_path.get(),
178 kSymbolsArraySize)) {
179 DLOG(WARNING) << "SymGetSearchPath failed: ";
180 return false;
181 }
182
183 std::wstring new_path(std::wstring(symbols_path.get()) +
184 L";" + GetExePath().DirName().value());
185 if (!SymSetSearchPathW(GetCurrentProcess(), new_path.c_str())) {
186 DLOG(WARNING) << "SymSetSearchPath failed.";
187 return false;
188 }
189
190 g_init_error = ERROR_SUCCESS;
191 return true; 191 return true;
192 } 192 }
193 193
194 // Disable optimizations for the StackTrace::StackTrace function. It is 194 // Disable optimizations for the StackTrace::StackTrace function. It is
195 // important to disable at least frame pointer optimization ("y"), since 195 // important to disable at least frame pointer optimization ("y"), since
196 // that breaks CaptureStackBackTrace() and prevents StackTrace from working 196 // that breaks CaptureStackBackTrace() and prevents StackTrace from working
197 // in Release builds (it may still be janky if other frames are using FPO, 197 // in Release builds (it may still be janky if other frames are using FPO,
198 // but at least it will make it further). 198 // but at least it will make it further).
199 #if defined(COMPILER_MSVC) 199 #if defined(COMPILER_MSVC)
200 #pragma optimize("", off) 200 #pragma optimize("", off)
201 #endif 201 #endif
202 202
203 StackTrace::StackTrace() { 203 StackTrace::StackTrace() {
204 // When walking our own stack, use CaptureStackBackTrace(). 204 // When walking our own stack, use CaptureStackBackTrace().
205 count_ = CaptureStackBackTrace(0, arraysize(trace_), trace_, NULL); 205 count_ = CaptureStackBackTrace(0, arraysize(trace_), trace_, NULL);
206 } 206 }
207 207
208 #if defined(COMPILER_MSVC) 208 #if defined(COMPILER_MSVC)
209 #pragma optimize("", on) 209 #pragma optimize("", on)
210 #endif 210 #endif
211 211
212 StackTrace::StackTrace(const EXCEPTION_POINTERS* exception_pointers) { 212 StackTrace::StackTrace(EXCEPTION_POINTERS* exception_pointers) {
213 // StackWalk64() may modify context record passed to it, so we will 213 InitTrace(exception_pointers->ContextRecord);
214 // use a copy.
215 CONTEXT context_record = *exception_pointers->ContextRecord;
216 InitTrace(&context_record);
217 } 214 }
218 215
219 StackTrace::StackTrace(const CONTEXT* context) { 216 StackTrace::StackTrace(CONTEXT* context) {
220 // StackWalk64() may modify context record passed to it, so we will 217 InitTrace(context);
221 // use a copy.
222 CONTEXT context_record = *context;
223 InitTrace(&context_record);
224 } 218 }
225 219
226 void StackTrace::InitTrace(CONTEXT* context_record) { 220 void StackTrace::InitTrace(CONTEXT* context_record) {
227 // When walking an exception stack, we need to use StackWalk64(). 221 // When walking an exception stack, we need to use StackWalk64().
228 count_ = 0; 222 count_ = 0;
229 // Initialize stack walking. 223 // Initialize stack walking.
230 STACKFRAME64 stack_frame; 224 STACKFRAME64 stack_frame;
231 memset(&stack_frame, 0, sizeof(stack_frame)); 225 memset(&stack_frame, 0, sizeof(stack_frame));
232 #if defined(_WIN64) 226 #if defined(_WIN64)
233 int machine_type = IMAGE_FILE_MACHINE_AMD64; 227 int machine_type = IMAGE_FILE_MACHINE_AMD64;
(...skipping 25 matching lines...) Expand all
259 for (size_t i = count_; i < arraysize(trace_); ++i) 253 for (size_t i = count_; i < arraysize(trace_); ++i)
260 trace_[i] = NULL; 254 trace_[i] = NULL;
261 } 255 }
262 256
263 void StackTrace::Print() const { 257 void StackTrace::Print() const {
264 OutputToStream(&std::cerr); 258 OutputToStream(&std::cerr);
265 } 259 }
266 260
267 void StackTrace::OutputToStream(std::ostream* os) const { 261 void StackTrace::OutputToStream(std::ostream* os) const {
268 SymbolContext* context = SymbolContext::GetInstance(); 262 SymbolContext* context = SymbolContext::GetInstance();
269 DWORD error = context->init_error(); 263 if (g_init_error != ERROR_SUCCESS) {
270 if (error != ERROR_SUCCESS) { 264 (*os) << "Error initializing symbols (" << g_init_error
271 (*os) << "Error initializing symbols (" << error
272 << "). Dumping unresolved backtrace:\n"; 265 << "). Dumping unresolved backtrace:\n";
273 for (size_t i = 0; (i < count_) && os->good(); ++i) { 266 for (size_t i = 0; (i < count_) && os->good(); ++i) {
274 (*os) << "\t" << trace_[i] << "\n"; 267 (*os) << "\t" << trace_[i] << "\n";
275 } 268 }
276 } else { 269 } else {
277 (*os) << "Backtrace:\n"; 270 (*os) << "Backtrace:\n";
278 context->OutputTraceToStream(trace_, count_, os); 271 context->OutputTraceToStream(trace_, count_, os);
279 } 272 }
280 } 273 }
281 274
282 } // namespace debug 275 } // namespace debug
283 } // namespace base 276 } // namespace base
OLDNEW
« no previous file with comments | « base/debug/stack_trace_posix.cc ('k') | base/process/launch_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698