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

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

Issue 1299583002: Revert of Print stack traces in child processes when browser tests failed. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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 bool g_initialized_symbols = false;
30 DWORD g_init_error = ERROR_SUCCESS;
31
32 // Prints the exception call stack. 29 // Prints the exception call stack.
33 // This is the unit tests exception filter. 30 // This is the unit tests exception filter.
34 long WINAPI StackDumpExceptionFilter(EXCEPTION_POINTERS* info) { 31 long WINAPI StackDumpExceptionFilter(EXCEPTION_POINTERS* info) {
35 debug::StackTrace(info).Print(); 32 debug::StackTrace(info).Print();
36 if (g_previous_filter) 33 if (g_previous_filter)
37 return g_previous_filter(info); 34 return g_previous_filter(info);
38 return EXCEPTION_CONTINUE_SEARCH; 35 return EXCEPTION_CONTINUE_SEARCH;
39 } 36 }
40 37
41 FilePath GetExePath() { 38 FilePath GetExePath() {
42 wchar_t system_buffer[MAX_PATH]; 39 wchar_t system_buffer[MAX_PATH];
43 GetModuleFileName(NULL, system_buffer, MAX_PATH); 40 GetModuleFileName(NULL, system_buffer, MAX_PATH);
44 system_buffer[MAX_PATH - 1] = L'\0'; 41 system_buffer[MAX_PATH - 1] = L'\0';
45 return FilePath(system_buffer); 42 return FilePath(system_buffer);
46 } 43 }
47 44
48 bool InitializeSymbols() {
49 if (g_initialized_symbols)
50 return g_init_error == ERROR_SUCCESS;
51 g_initialized_symbols = true;
52 // Defer symbol load until they're needed, use undecorated names, and get line
53 // numbers.
54 SymSetOptions(SYMOPT_DEFERRED_LOADS |
55 SYMOPT_UNDNAME |
56 SYMOPT_LOAD_LINES);
57 if (!SymInitialize(GetCurrentProcess(), NULL, TRUE)) {
58 g_init_error = GetLastError();
59 // TODO(awong): Handle error: SymInitialize can fail with
60 // ERROR_INVALID_PARAMETER.
61 // When it fails, we should not call debugbreak since it kills the current
62 // process (prevents future tests from running or kills the browser
63 // process).
64 DLOG(ERROR) << "SymInitialize failed: " << g_init_error;
65 return false;
66 }
67
68 // When transferring the binaries e.g. between bots, path put
69 // into the executable will get off. To still retrieve symbols correctly,
70 // add the directory of the executable to symbol search path.
71 // All following errors are non-fatal.
72 const size_t kSymbolsArraySize = 1024;
73 scoped_ptr<wchar_t[]> symbols_path(new wchar_t[kSymbolsArraySize]);
74
75 // Note: The below function takes buffer size as number of characters,
76 // not number of bytes!
77 if (!SymGetSearchPathW(GetCurrentProcess(),
78 symbols_path.get(),
79 kSymbolsArraySize)) {
80 DLOG(WARNING) << "SymGetSearchPath failed: " << g_init_error;
81 g_init_error = GetLastError();
82 return false;
83 }
84
85 std::wstring new_path(std::wstring(symbols_path.get()) +
86 L";" + GetExePath().DirName().value());
87 if (!SymSetSearchPathW(GetCurrentProcess(), new_path.c_str())) {
88 g_init_error = GetLastError();
89 DLOG(WARNING) << "SymSetSearchPath failed." << g_init_error;
90 return false;
91 }
92
93 g_init_error = ERROR_SUCCESS;
94 return true;
95 }
96
97 // SymbolContext is a threadsafe singleton that wraps the DbgHelp Sym* family 45 // SymbolContext is a threadsafe singleton that wraps the DbgHelp Sym* family
98 // of functions. The Sym* family of functions may only be invoked by one 46 // of functions. The Sym* family of functions may only be invoked by one
99 // thread at a time. SymbolContext code may access a symbol server over the 47 // thread at a time. SymbolContext code may access a symbol server over the
100 // network while holding the lock for this singleton. In the case of high 48 // network while holding the lock for this singleton. In the case of high
101 // latency, this code will adversely affect performance. 49 // latency, this code will adversely affect performance.
102 // 50 //
103 // There is also a known issue where this backtrace code can interact 51 // There is also a known issue where this backtrace code can interact
104 // badly with breakpad if breakpad is invoked in a separate thread while 52 // badly with breakpad if breakpad is invoked in a separate thread while
105 // we are using the Sym* functions. This is because breakpad does now 53 // we are using the Sym* functions. This is because breakpad does now
106 // share a lock with this function. See this related bug: 54 // share a lock with this function. See this related bug:
107 // 55 //
108 // http://code.google.com/p/google-breakpad/issues/detail?id=311 56 // http://code.google.com/p/google-breakpad/issues/detail?id=311
109 // 57 //
110 // This is a very unlikely edge case, and the current solution is to 58 // This is a very unlikely edge case, and the current solution is to
111 // just ignore it. 59 // just ignore it.
112 class SymbolContext { 60 class SymbolContext {
113 public: 61 public:
114 static SymbolContext* GetInstance() { 62 static SymbolContext* GetInstance() {
115 // We use a leaky singleton because code may call this during process 63 // We use a leaky singleton because code may call this during process
116 // termination. 64 // termination.
117 return 65 return
118 Singleton<SymbolContext, LeakySingletonTraits<SymbolContext> >::get(); 66 Singleton<SymbolContext, LeakySingletonTraits<SymbolContext> >::get();
119 } 67 }
120 68
69 // Returns the error code of a failed initialization.
70 DWORD init_error() const {
71 return init_error_;
72 }
73
121 // For the given trace, attempts to resolve the symbols, and output a trace 74 // For the given trace, attempts to resolve the symbols, and output a trace
122 // to the ostream os. The format for each line of the backtrace is: 75 // to the ostream os. The format for each line of the backtrace is:
123 // 76 //
124 // <tab>SymbolName[0xAddress+Offset] (FileName:LineNo) 77 // <tab>SymbolName[0xAddress+Offset] (FileName:LineNo)
125 // 78 //
126 // This function should only be called if Init() has been called. We do not 79 // This function should only be called if Init() has been called. We do not
127 // LOG(FATAL) here because this code is called might be triggered by a 80 // LOG(FATAL) here because this code is called might be triggered by a
128 // LOG(FATAL) itself. Also, it should not be calling complex code that is 81 // LOG(FATAL) itself. Also, it should not be calling complex code that is
129 // extensible like PathService since that can in turn fire CHECKs. 82 // extensible like PathService since that can in turn fire CHECKs.
130 void OutputTraceToStream(const void* const* trace, 83 void OutputTraceToStream(const void* const* trace,
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 if (has_line) { 125 if (has_line) {
173 (*os) << " (" << line.FileName << ":" << line.LineNumber << ")"; 126 (*os) << " (" << line.FileName << ":" << line.LineNumber << ")";
174 } 127 }
175 (*os) << "\n"; 128 (*os) << "\n";
176 } 129 }
177 } 130 }
178 131
179 private: 132 private:
180 friend struct DefaultSingletonTraits<SymbolContext>; 133 friend struct DefaultSingletonTraits<SymbolContext>;
181 134
182 SymbolContext() { 135 SymbolContext() : init_error_(ERROR_SUCCESS) {
183 InitializeSymbols(); 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 }
184 } 177 }
185 178
179 DWORD init_error_;
186 base::Lock lock_; 180 base::Lock lock_;
187 DISALLOW_COPY_AND_ASSIGN(SymbolContext); 181 DISALLOW_COPY_AND_ASSIGN(SymbolContext);
188 }; 182 };
189 183
190 } // namespace 184 } // namespace
191 185
192 bool EnableInProcessStackDumping() { 186 bool EnableInProcessStackDumping() {
193 // Add stack dumping support on exception on windows. Similar to OS_POSIX 187 // Add stack dumping support on exception on windows. Similar to OS_POSIX
194 // signal() handling in process_util_posix.cc. 188 // signal() handling in process_util_posix.cc.
195 g_previous_filter = SetUnhandledExceptionFilter(&StackDumpExceptionFilter); 189 g_previous_filter = SetUnhandledExceptionFilter(&StackDumpExceptionFilter);
196 RouteStdioToConsole(); 190 RouteStdioToConsole();
197 191 return true;
198 // Need to initialize symbols early in the process or else this fails on
199 // swarming (since symbols are in different directory than in the exes) and
200 // also release x64.
201 return InitializeSymbols();
202 } 192 }
203 193
204 // Disable optimizations for the StackTrace::StackTrace function. It is 194 // Disable optimizations for the StackTrace::StackTrace function. It is
205 // important to disable at least frame pointer optimization ("y"), since 195 // important to disable at least frame pointer optimization ("y"), since
206 // that breaks CaptureStackBackTrace() and prevents StackTrace from working 196 // that breaks CaptureStackBackTrace() and prevents StackTrace from working
207 // 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,
208 // but at least it will make it further). 198 // but at least it will make it further).
209 #if defined(COMPILER_MSVC) 199 #if defined(COMPILER_MSVC)
210 #pragma optimize("", off) 200 #pragma optimize("", off)
211 #endif 201 #endif
212 202
213 StackTrace::StackTrace() { 203 StackTrace::StackTrace() {
214 // When walking our own stack, use CaptureStackBackTrace(). 204 // When walking our own stack, use CaptureStackBackTrace().
215 count_ = CaptureStackBackTrace(0, arraysize(trace_), trace_, NULL); 205 count_ = CaptureStackBackTrace(0, arraysize(trace_), trace_, NULL);
216 } 206 }
217 207
218 #if defined(COMPILER_MSVC) 208 #if defined(COMPILER_MSVC)
219 #pragma optimize("", on) 209 #pragma optimize("", on)
220 #endif 210 #endif
221 211
222 StackTrace::StackTrace(EXCEPTION_POINTERS* exception_pointers) { 212 StackTrace::StackTrace(const EXCEPTION_POINTERS* exception_pointers) {
223 InitTrace(exception_pointers->ContextRecord); 213 // StackWalk64() may modify context record passed to it, so we will
214 // use a copy.
215 CONTEXT context_record = *exception_pointers->ContextRecord;
216 InitTrace(&context_record);
224 } 217 }
225 218
226 StackTrace::StackTrace(CONTEXT* context) { 219 StackTrace::StackTrace(const CONTEXT* context) {
227 InitTrace(context); 220 // StackWalk64() may modify context record passed to it, so we will
221 // use a copy.
222 CONTEXT context_record = *context;
223 InitTrace(&context_record);
228 } 224 }
229 225
230 void StackTrace::InitTrace(CONTEXT* context_record) { 226 void StackTrace::InitTrace(CONTEXT* context_record) {
231 // When walking an exception stack, we need to use StackWalk64(). 227 // When walking an exception stack, we need to use StackWalk64().
232 count_ = 0; 228 count_ = 0;
233 // Initialize stack walking. 229 // Initialize stack walking.
234 STACKFRAME64 stack_frame; 230 STACKFRAME64 stack_frame;
235 memset(&stack_frame, 0, sizeof(stack_frame)); 231 memset(&stack_frame, 0, sizeof(stack_frame));
236 #if defined(_WIN64) 232 #if defined(_WIN64)
237 int machine_type = IMAGE_FILE_MACHINE_AMD64; 233 int machine_type = IMAGE_FILE_MACHINE_AMD64;
(...skipping 25 matching lines...) Expand all
263 for (size_t i = count_; i < arraysize(trace_); ++i) 259 for (size_t i = count_; i < arraysize(trace_); ++i)
264 trace_[i] = NULL; 260 trace_[i] = NULL;
265 } 261 }
266 262
267 void StackTrace::Print() const { 263 void StackTrace::Print() const {
268 OutputToStream(&std::cerr); 264 OutputToStream(&std::cerr);
269 } 265 }
270 266
271 void StackTrace::OutputToStream(std::ostream* os) const { 267 void StackTrace::OutputToStream(std::ostream* os) const {
272 SymbolContext* context = SymbolContext::GetInstance(); 268 SymbolContext* context = SymbolContext::GetInstance();
273 if (g_init_error != ERROR_SUCCESS) { 269 DWORD error = context->init_error();
274 (*os) << "Error initializing symbols (" << g_init_error 270 if (error != ERROR_SUCCESS) {
271 (*os) << "Error initializing symbols (" << error
275 << "). Dumping unresolved backtrace:\n"; 272 << "). Dumping unresolved backtrace:\n";
276 for (size_t i = 0; (i < count_) && os->good(); ++i) { 273 for (size_t i = 0; (i < count_) && os->good(); ++i) {
277 (*os) << "\t" << trace_[i] << "\n"; 274 (*os) << "\t" << trace_[i] << "\n";
278 } 275 }
279 } else { 276 } else {
280 (*os) << "Backtrace:\n"; 277 (*os) << "Backtrace:\n";
281 context->OutputTraceToStream(trace_, count_, os); 278 context->OutputTraceToStream(trace_, count_, os);
282 } 279 }
283 } 280 }
284 281
285 } // namespace debug 282 } // namespace debug
286 } // namespace base 283 } // 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