Chromium Code Reviews| 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 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 Loading... | |
| 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 Loading... | |
| 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 | |
| 
 
scottmg
2015/08/13 15:48:54
I don't think this is right here if someone calls
 
jam
2015/08/13 15:59:27
Done.
 
 | |
| 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 // When transferring the binaries e.g. between bots, path put | |
| 167 // into the executable will get off. To still retrieve symbols correctly, | |
| 168 // add the directory of the executable to symbol search path. | |
| 169 // All following errors are non-fatal. | |
| 170 const size_t kSymbolsArraySize = 1024; | |
| 171 scoped_ptr<wchar_t[]> symbols_path(new wchar_t[kSymbolsArraySize]); | |
| 172 | |
| 173 // Note: The below function takes buffer size as number of characters, | |
| 174 // not number of bytes! | |
| 175 if (!SymGetSearchPathW(GetCurrentProcess(), | |
| 176 symbols_path.get(), | |
| 177 kSymbolsArraySize)) { | |
| 178 DLOG(WARNING) << "SymGetSearchPath failed: "; | |
| 179 return false; | |
| 180 } | |
| 181 | |
| 182 std::wstring new_path(std::wstring(symbols_path.get()) + | |
| 183 L";" + GetExePath().DirName().value()); | |
| 184 if (!SymSetSearchPathW(GetCurrentProcess(), new_path.c_str())) { | |
| 185 DLOG(WARNING) << "SymSetSearchPath failed."; | |
| 186 return false; | |
| 187 } | |
| 188 | |
| 189 g_init_error = ERROR_SUCCESS; | |
| 191 return true; | 190 return true; | 
| 192 } | 191 } | 
| 193 | 192 | 
| 194 // Disable optimizations for the StackTrace::StackTrace function. It is | 193 // Disable optimizations for the StackTrace::StackTrace function. It is | 
| 195 // important to disable at least frame pointer optimization ("y"), since | 194 // important to disable at least frame pointer optimization ("y"), since | 
| 196 // that breaks CaptureStackBackTrace() and prevents StackTrace from working | 195 // that breaks CaptureStackBackTrace() and prevents StackTrace from working | 
| 197 // in Release builds (it may still be janky if other frames are using FPO, | 196 // in Release builds (it may still be janky if other frames are using FPO, | 
| 198 // but at least it will make it further). | 197 // but at least it will make it further). | 
| 199 #if defined(COMPILER_MSVC) | 198 #if defined(COMPILER_MSVC) | 
| 200 #pragma optimize("", off) | 199 #pragma optimize("", off) | 
| 201 #endif | 200 #endif | 
| 202 | 201 | 
| 203 StackTrace::StackTrace() { | 202 StackTrace::StackTrace() { | 
| 204 // When walking our own stack, use CaptureStackBackTrace(). | 203 // When walking our own stack, use CaptureStackBackTrace(). | 
| 205 count_ = CaptureStackBackTrace(0, arraysize(trace_), trace_, NULL); | 204 count_ = CaptureStackBackTrace(0, arraysize(trace_), trace_, NULL); | 
| 206 } | 205 } | 
| 207 | 206 | 
| 208 #if defined(COMPILER_MSVC) | 207 #if defined(COMPILER_MSVC) | 
| 209 #pragma optimize("", on) | 208 #pragma optimize("", on) | 
| 210 #endif | 209 #endif | 
| 211 | 210 | 
| 212 StackTrace::StackTrace(const EXCEPTION_POINTERS* exception_pointers) { | 211 StackTrace::StackTrace(EXCEPTION_POINTERS* exception_pointers) { | 
| 213 // StackWalk64() may modify context record passed to it, so we will | 212 InitTrace(exception_pointers->ContextRecord); | 
| 214 // use a copy. | |
| 215 CONTEXT context_record = *exception_pointers->ContextRecord; | |
| 216 InitTrace(&context_record); | |
| 217 } | 213 } | 
| 218 | 214 | 
| 219 StackTrace::StackTrace(const CONTEXT* context) { | 215 StackTrace::StackTrace(CONTEXT* context) { | 
| 220 // StackWalk64() may modify context record passed to it, so we will | 216 InitTrace(context); | 
| 221 // use a copy. | |
| 222 CONTEXT context_record = *context; | |
| 223 InitTrace(&context_record); | |
| 224 } | 217 } | 
| 225 | 218 | 
| 226 void StackTrace::InitTrace(CONTEXT* context_record) { | 219 void StackTrace::InitTrace(CONTEXT* context_record) { | 
| 227 // When walking an exception stack, we need to use StackWalk64(). | 220 // When walking an exception stack, we need to use StackWalk64(). | 
| 228 count_ = 0; | 221 count_ = 0; | 
| 229 // Initialize stack walking. | 222 // Initialize stack walking. | 
| 230 STACKFRAME64 stack_frame; | 223 STACKFRAME64 stack_frame; | 
| 231 memset(&stack_frame, 0, sizeof(stack_frame)); | 224 memset(&stack_frame, 0, sizeof(stack_frame)); | 
| 232 #if defined(_WIN64) | 225 #if defined(_WIN64) | 
| 233 int machine_type = IMAGE_FILE_MACHINE_AMD64; | 226 int machine_type = IMAGE_FILE_MACHINE_AMD64; | 
| (...skipping 25 matching lines...) Expand all Loading... | |
| 259 for (size_t i = count_; i < arraysize(trace_); ++i) | 252 for (size_t i = count_; i < arraysize(trace_); ++i) | 
| 260 trace_[i] = NULL; | 253 trace_[i] = NULL; | 
| 261 } | 254 } | 
| 262 | 255 | 
| 263 void StackTrace::Print() const { | 256 void StackTrace::Print() const { | 
| 264 OutputToStream(&std::cerr); | 257 OutputToStream(&std::cerr); | 
| 265 } | 258 } | 
| 266 | 259 | 
| 267 void StackTrace::OutputToStream(std::ostream* os) const { | 260 void StackTrace::OutputToStream(std::ostream* os) const { | 
| 268 SymbolContext* context = SymbolContext::GetInstance(); | 261 SymbolContext* context = SymbolContext::GetInstance(); | 
| 269 DWORD error = context->init_error(); | 262 if (g_init_error != ERROR_SUCCESS) { | 
| 270 if (error != ERROR_SUCCESS) { | 263 (*os) << "Error initializing symbols (" << g_init_error | 
| 271 (*os) << "Error initializing symbols (" << error | |
| 272 << "). Dumping unresolved backtrace:\n"; | 264 << "). Dumping unresolved backtrace:\n"; | 
| 273 for (size_t i = 0; (i < count_) && os->good(); ++i) { | 265 for (size_t i = 0; (i < count_) && os->good(); ++i) { | 
| 274 (*os) << "\t" << trace_[i] << "\n"; | 266 (*os) << "\t" << trace_[i] << "\n"; | 
| 275 } | 267 } | 
| 276 } else { | 268 } else { | 
| 277 (*os) << "Backtrace:\n"; | 269 (*os) << "Backtrace:\n"; | 
| 278 context->OutputTraceToStream(trace_, count_, os); | 270 context->OutputTraceToStream(trace_, count_, os); | 
| 279 } | 271 } | 
| 280 } | 272 } | 
| 281 | 273 | 
| 282 } // namespace debug | 274 } // namespace debug | 
| 283 } // namespace base | 275 } // namespace base | 
| OLD | NEW |