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 <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 | |
29 // Prints the exception call stack. | 32 // Prints the exception call stack. |
30 // This is the unit tests exception filter. | 33 // This is the unit tests exception filter. |
31 long WINAPI StackDumpExceptionFilter(EXCEPTION_POINTERS* info) { | 34 long WINAPI StackDumpExceptionFilter(EXCEPTION_POINTERS* info) { |
32 debug::StackTrace(info).Print(); | 35 debug::StackTrace(info).Print(); |
33 if (g_previous_filter) | 36 if (g_previous_filter) |
34 return g_previous_filter(info); | 37 return g_previous_filter(info); |
35 return EXCEPTION_CONTINUE_SEARCH; | 38 return EXCEPTION_CONTINUE_SEARCH; |
36 } | 39 } |
37 | 40 |
38 FilePath GetExePath() { | 41 FilePath GetExePath() { |
39 wchar_t system_buffer[MAX_PATH]; | 42 wchar_t system_buffer[MAX_PATH]; |
40 GetModuleFileName(NULL, system_buffer, MAX_PATH); | 43 GetModuleFileName(NULL, system_buffer, MAX_PATH); |
41 system_buffer[MAX_PATH - 1] = L'\0'; | 44 system_buffer[MAX_PATH - 1] = L'\0'; |
42 return FilePath(system_buffer); | 45 return FilePath(system_buffer); |
43 } | 46 } |
44 | 47 |
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)) { | |
Sébastien Marchand
2016/01/06 21:42:54
AFAIK the calls to SymInitialize should be paired
| |
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 | |
45 // SymbolContext is a threadsafe singleton that wraps the DbgHelp Sym* family | 97 // SymbolContext is a threadsafe singleton that wraps the DbgHelp Sym* family |
46 // of functions. The Sym* family of functions may only be invoked by one | 98 // of functions. The Sym* family of functions may only be invoked by one |
47 // thread at a time. SymbolContext code may access a symbol server over the | 99 // thread at a time. SymbolContext code may access a symbol server over the |
48 // network while holding the lock for this singleton. In the case of high | 100 // network while holding the lock for this singleton. In the case of high |
49 // latency, this code will adversely affect performance. | 101 // latency, this code will adversely affect performance. |
50 // | 102 // |
51 // There is also a known issue where this backtrace code can interact | 103 // There is also a known issue where this backtrace code can interact |
52 // badly with breakpad if breakpad is invoked in a separate thread while | 104 // badly with breakpad if breakpad is invoked in a separate thread while |
53 // we are using the Sym* functions. This is because breakpad does now | 105 // we are using the Sym* functions. This is because breakpad does now |
54 // share a lock with this function. See this related bug: | 106 // share a lock with this function. See this related bug: |
55 // | 107 // |
56 // http://code.google.com/p/google-breakpad/issues/detail?id=311 | 108 // http://code.google.com/p/google-breakpad/issues/detail?id=311 |
57 // | 109 // |
58 // This is a very unlikely edge case, and the current solution is to | 110 // This is a very unlikely edge case, and the current solution is to |
59 // just ignore it. | 111 // just ignore it. |
60 class SymbolContext { | 112 class SymbolContext { |
61 public: | 113 public: |
62 static SymbolContext* GetInstance() { | 114 static SymbolContext* GetInstance() { |
63 // We use a leaky singleton because code may call this during process | 115 // We use a leaky singleton because code may call this during process |
64 // termination. | 116 // termination. |
65 return | 117 return |
66 Singleton<SymbolContext, LeakySingletonTraits<SymbolContext> >::get(); | 118 Singleton<SymbolContext, LeakySingletonTraits<SymbolContext> >::get(); |
67 } | 119 } |
68 | 120 |
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 | 121 // 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: | 122 // to the ostream os. The format for each line of the backtrace is: |
76 // | 123 // |
77 // <tab>SymbolName[0xAddress+Offset] (FileName:LineNo) | 124 // <tab>SymbolName[0xAddress+Offset] (FileName:LineNo) |
78 // | 125 // |
79 // This function should only be called if Init() has been called. We do not | 126 // 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 | 127 // 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 | 128 // LOG(FATAL) itself. Also, it should not be calling complex code that is |
82 // extensible like PathService since that can in turn fire CHECKs. | 129 // extensible like PathService since that can in turn fire CHECKs. |
83 void OutputTraceToStream(const void* const* trace, | 130 void OutputTraceToStream(const void* const* trace, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
125 if (has_line) { | 172 if (has_line) { |
126 (*os) << " (" << line.FileName << ":" << line.LineNumber << ")"; | 173 (*os) << " (" << line.FileName << ":" << line.LineNumber << ")"; |
127 } | 174 } |
128 (*os) << "\n"; | 175 (*os) << "\n"; |
129 } | 176 } |
130 } | 177 } |
131 | 178 |
132 private: | 179 private: |
133 friend struct DefaultSingletonTraits<SymbolContext>; | 180 friend struct DefaultSingletonTraits<SymbolContext>; |
134 | 181 |
135 SymbolContext() : init_error_(ERROR_SUCCESS) { | 182 SymbolContext() { |
136 // Initializes the symbols for the process. | 183 InitializeSymbols(); |
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 } | 184 } |
178 | 185 |
179 DWORD init_error_; | |
180 base::Lock lock_; | 186 base::Lock lock_; |
181 DISALLOW_COPY_AND_ASSIGN(SymbolContext); | 187 DISALLOW_COPY_AND_ASSIGN(SymbolContext); |
182 }; | 188 }; |
183 | 189 |
184 } // namespace | 190 } // namespace |
185 | 191 |
186 bool EnableInProcessStackDumping() { | 192 bool EnableInProcessStackDumping() { |
187 // Add stack dumping support on exception on windows. Similar to OS_POSIX | 193 // Add stack dumping support on exception on windows. Similar to OS_POSIX |
188 // signal() handling in process_util_posix.cc. | 194 // signal() handling in process_util_posix.cc. |
189 g_previous_filter = SetUnhandledExceptionFilter(&StackDumpExceptionFilter); | 195 g_previous_filter = SetUnhandledExceptionFilter(&StackDumpExceptionFilter); |
190 RouteStdioToConsole(); | 196 |
191 return true; | 197 // Need to initialize symbols early in the process or else this fails on |
198 // swarming (since symbols are in different directory than in the exes) and | |
199 // also release x64. | |
200 return InitializeSymbols(); | |
192 } | 201 } |
193 | 202 |
194 // Disable optimizations for the StackTrace::StackTrace function. It is | 203 // Disable optimizations for the StackTrace::StackTrace function. It is |
195 // important to disable at least frame pointer optimization ("y"), since | 204 // important to disable at least frame pointer optimization ("y"), since |
196 // that breaks CaptureStackBackTrace() and prevents StackTrace from working | 205 // that breaks CaptureStackBackTrace() and prevents StackTrace from working |
197 // in Release builds (it may still be janky if other frames are using FPO, | 206 // in Release builds (it may still be janky if other frames are using FPO, |
198 // but at least it will make it further). | 207 // but at least it will make it further). |
199 #if defined(COMPILER_MSVC) | 208 #if defined(COMPILER_MSVC) |
200 #pragma optimize("", off) | 209 #pragma optimize("", off) |
201 #endif | 210 #endif |
202 | 211 |
203 StackTrace::StackTrace() { | 212 StackTrace::StackTrace() { |
204 // When walking our own stack, use CaptureStackBackTrace(). | 213 // When walking our own stack, use CaptureStackBackTrace(). |
205 count_ = CaptureStackBackTrace(0, arraysize(trace_), trace_, NULL); | 214 count_ = CaptureStackBackTrace(0, arraysize(trace_), trace_, NULL); |
206 } | 215 } |
207 | 216 |
208 #if defined(COMPILER_MSVC) | 217 #if defined(COMPILER_MSVC) |
209 #pragma optimize("", on) | 218 #pragma optimize("", on) |
210 #endif | 219 #endif |
211 | 220 |
212 StackTrace::StackTrace(const EXCEPTION_POINTERS* exception_pointers) { | 221 StackTrace::StackTrace(EXCEPTION_POINTERS* exception_pointers) { |
213 // StackWalk64() may modify context record passed to it, so we will | 222 InitTrace(exception_pointers->ContextRecord); |
214 // use a copy. | |
215 CONTEXT context_record = *exception_pointers->ContextRecord; | |
216 InitTrace(&context_record); | |
217 } | 223 } |
218 | 224 |
219 StackTrace::StackTrace(const CONTEXT* context) { | 225 StackTrace::StackTrace(CONTEXT* context) { |
220 // StackWalk64() may modify context record passed to it, so we will | 226 InitTrace(context); |
221 // use a copy. | |
222 CONTEXT context_record = *context; | |
223 InitTrace(&context_record); | |
224 } | 227 } |
225 | 228 |
226 void StackTrace::InitTrace(CONTEXT* context_record) { | 229 void StackTrace::InitTrace(CONTEXT* context_record) { |
227 // When walking an exception stack, we need to use StackWalk64(). | 230 // When walking an exception stack, we need to use StackWalk64(). |
228 count_ = 0; | 231 count_ = 0; |
229 // Initialize stack walking. | 232 // Initialize stack walking. |
230 STACKFRAME64 stack_frame; | 233 STACKFRAME64 stack_frame; |
231 memset(&stack_frame, 0, sizeof(stack_frame)); | 234 memset(&stack_frame, 0, sizeof(stack_frame)); |
232 #if defined(_WIN64) | 235 #if defined(_WIN64) |
233 int machine_type = IMAGE_FILE_MACHINE_AMD64; | 236 int machine_type = IMAGE_FILE_MACHINE_AMD64; |
(...skipping 25 matching lines...) Expand all Loading... | |
259 for (size_t i = count_; i < arraysize(trace_); ++i) | 262 for (size_t i = count_; i < arraysize(trace_); ++i) |
260 trace_[i] = NULL; | 263 trace_[i] = NULL; |
261 } | 264 } |
262 | 265 |
263 void StackTrace::Print() const { | 266 void StackTrace::Print() const { |
264 OutputToStream(&std::cerr); | 267 OutputToStream(&std::cerr); |
265 } | 268 } |
266 | 269 |
267 void StackTrace::OutputToStream(std::ostream* os) const { | 270 void StackTrace::OutputToStream(std::ostream* os) const { |
268 SymbolContext* context = SymbolContext::GetInstance(); | 271 SymbolContext* context = SymbolContext::GetInstance(); |
269 DWORD error = context->init_error(); | 272 if (g_init_error != ERROR_SUCCESS) { |
270 if (error != ERROR_SUCCESS) { | 273 (*os) << "Error initializing symbols (" << g_init_error |
271 (*os) << "Error initializing symbols (" << error | |
272 << "). Dumping unresolved backtrace:\n"; | 274 << "). Dumping unresolved backtrace:\n"; |
273 for (size_t i = 0; (i < count_) && os->good(); ++i) { | 275 for (size_t i = 0; (i < count_) && os->good(); ++i) { |
274 (*os) << "\t" << trace_[i] << "\n"; | 276 (*os) << "\t" << trace_[i] << "\n"; |
275 } | 277 } |
276 } else { | 278 } else { |
277 (*os) << "Backtrace:\n"; | 279 (*os) << "Backtrace:\n"; |
278 context->OutputTraceToStream(trace_, count_, os); | 280 context->OutputTraceToStream(trace_, count_, os); |
279 } | 281 } |
280 } | 282 } |
281 | 283 |
282 } // namespace debug | 284 } // namespace debug |
283 } // namespace base | 285 } // namespace base |
OLD | NEW |