Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #include "base/debug_util.h" |
michael@0 | 6 | |
michael@0 | 7 | #include <windows.h> |
michael@0 | 8 | #include <dbghelp.h> |
michael@0 | 9 | #include <iostream> |
michael@0 | 10 | |
michael@0 | 11 | #include "base/basictypes.h" |
michael@0 | 12 | #include "base/lock.h" |
michael@0 | 13 | #include "base/logging.h" |
michael@0 | 14 | #include "base/singleton.h" |
michael@0 | 15 | |
michael@0 | 16 | namespace { |
michael@0 | 17 | |
michael@0 | 18 | // Minimalist key reader. |
michael@0 | 19 | // Note: Does not use the CRT. |
michael@0 | 20 | bool RegReadString(HKEY root, const wchar_t* subkey, |
michael@0 | 21 | const wchar_t* value_name, wchar_t* buffer, int* len) { |
michael@0 | 22 | HKEY key = NULL; |
michael@0 | 23 | DWORD res = RegOpenKeyEx(root, subkey, 0, KEY_READ, &key); |
michael@0 | 24 | if (ERROR_SUCCESS != res || key == NULL) |
michael@0 | 25 | return false; |
michael@0 | 26 | |
michael@0 | 27 | DWORD type = 0; |
michael@0 | 28 | DWORD buffer_size = *len * sizeof(wchar_t); |
michael@0 | 29 | // We don't support REG_EXPAND_SZ. |
michael@0 | 30 | res = RegQueryValueEx(key, value_name, NULL, &type, |
michael@0 | 31 | reinterpret_cast<BYTE*>(buffer), &buffer_size); |
michael@0 | 32 | if (ERROR_SUCCESS == res && buffer_size != 0 && type == REG_SZ) { |
michael@0 | 33 | // Make sure the buffer is NULL terminated. |
michael@0 | 34 | buffer[*len - 1] = 0; |
michael@0 | 35 | *len = lstrlen(buffer); |
michael@0 | 36 | RegCloseKey(key); |
michael@0 | 37 | return true; |
michael@0 | 38 | } |
michael@0 | 39 | RegCloseKey(key); |
michael@0 | 40 | return false; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | // Replaces each "%ld" in input per a value. Not efficient but it works. |
michael@0 | 44 | // Note: Does not use the CRT. |
michael@0 | 45 | bool StringReplace(const wchar_t* input, int value, wchar_t* output, |
michael@0 | 46 | int output_len) { |
michael@0 | 47 | memset(output, 0, output_len*sizeof(wchar_t)); |
michael@0 | 48 | int input_len = lstrlen(input); |
michael@0 | 49 | |
michael@0 | 50 | for (int i = 0; i < input_len; ++i) { |
michael@0 | 51 | int current_output_len = lstrlen(output); |
michael@0 | 52 | |
michael@0 | 53 | if (input[i] == L'%' && input[i + 1] == L'l' && input[i + 2] == L'd') { |
michael@0 | 54 | // Make sure we have enough place left. |
michael@0 | 55 | if ((current_output_len + 12) >= output_len) |
michael@0 | 56 | return false; |
michael@0 | 57 | |
michael@0 | 58 | // Cheap _itow(). |
michael@0 | 59 | wsprintf(output+current_output_len, L"%d", value); |
michael@0 | 60 | i += 2; |
michael@0 | 61 | } else { |
michael@0 | 62 | if (current_output_len >= output_len) |
michael@0 | 63 | return false; |
michael@0 | 64 | output[current_output_len] = input[i]; |
michael@0 | 65 | } |
michael@0 | 66 | } |
michael@0 | 67 | return true; |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | // SymbolContext is a threadsafe singleton that wraps the DbgHelp Sym* family |
michael@0 | 71 | // of functions. The Sym* family of functions may only be invoked by one |
michael@0 | 72 | // thread at a time. SymbolContext code may access a symbol server over the |
michael@0 | 73 | // network while holding the lock for this singleton. In the case of high |
michael@0 | 74 | // latency, this code will adversly affect performance. |
michael@0 | 75 | // |
michael@0 | 76 | // There is also a known issue where this backtrace code can interact |
michael@0 | 77 | // badly with breakpad if breakpad is invoked in a separate thread while |
michael@0 | 78 | // we are using the Sym* functions. This is because breakpad does now |
michael@0 | 79 | // share a lock with this function. See this related bug: |
michael@0 | 80 | // |
michael@0 | 81 | // http://code.google.com/p/google-breakpad/issues/detail?id=311 |
michael@0 | 82 | // |
michael@0 | 83 | // This is a very unlikely edge case, and the current solution is to |
michael@0 | 84 | // just ignore it. |
michael@0 | 85 | class SymbolContext { |
michael@0 | 86 | public: |
michael@0 | 87 | static SymbolContext* Get() { |
michael@0 | 88 | // We use a leaky singleton because code may call this during process |
michael@0 | 89 | // termination. |
michael@0 | 90 | return |
michael@0 | 91 | Singleton<SymbolContext, LeakySingletonTraits<SymbolContext> >::get(); |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | // Initializes the symbols for the process if it hasn't been done yet. |
michael@0 | 95 | // Subsequent calls will not reinitialize the symbol, but instead return |
michael@0 | 96 | // the error code from the first call. |
michael@0 | 97 | bool Init() { |
michael@0 | 98 | AutoLock lock(lock_); |
michael@0 | 99 | if (!initialized_) { |
michael@0 | 100 | process_ = GetCurrentProcess(); |
michael@0 | 101 | |
michael@0 | 102 | // Defer symbol load until they're needed, use undecorated names, and |
michael@0 | 103 | // get line numbers. |
michael@0 | 104 | SymSetOptions(SYMOPT_DEFERRED_LOADS | |
michael@0 | 105 | SYMOPT_UNDNAME | |
michael@0 | 106 | SYMOPT_LOAD_LINES); |
michael@0 | 107 | if (SymInitialize(process_, NULL, TRUE)) { |
michael@0 | 108 | init_error_ = ERROR_SUCCESS; |
michael@0 | 109 | } else { |
michael@0 | 110 | init_error_ = GetLastError(); |
michael@0 | 111 | } |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | initialized_ = true; |
michael@0 | 115 | return init_error_ == ERROR_SUCCESS; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | // Returns the error code of a failed initialization. This should only be |
michael@0 | 119 | // called if Init() has been called. We do not CHROMIUM_LOG(FATAL) here because |
michael@0 | 120 | // this code is called might be triggered by a CHROMIUM_LOG(FATAL) itself. Instead, |
michael@0 | 121 | // we log an ERROR, and return ERROR_INVALID_DATA. |
michael@0 | 122 | DWORD init_error() { |
michael@0 | 123 | if (!initialized_) { |
michael@0 | 124 | CHROMIUM_LOG(ERROR) << "Calling GetInitError() before Init() was called. " |
michael@0 | 125 | << "Returning ERROR_INVALID_DATA."; |
michael@0 | 126 | return ERROR_INVALID_DATA; |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | return init_error_; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | // Returns the process this was initialized for. This should only be |
michael@0 | 133 | // called if Init() has been called. We CHROMIUM_LOG(ERROR) in this situation. |
michael@0 | 134 | // CHROMIUM_LOG(FATAL) is not used because this code is might be triggered |
michael@0 | 135 | // by a CHROMIUM_LOG(FATAL) itself. |
michael@0 | 136 | HANDLE process() { |
michael@0 | 137 | if (!initialized_) { |
michael@0 | 138 | CHROMIUM_LOG(ERROR) << "Calling process() before Init() was called. " |
michael@0 | 139 | << "Returning NULL."; |
michael@0 | 140 | return NULL; |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | return process_; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | // For the given trace, attempts to resolve the symbols, and output a trace |
michael@0 | 147 | // to the ostream os. The format for each line of the backtrace is: |
michael@0 | 148 | // |
michael@0 | 149 | // <tab>SymbolName[0xAddress+Offset] (FileName:LineNo) |
michael@0 | 150 | // |
michael@0 | 151 | // This function should only be called if Init() has been called. We do not |
michael@0 | 152 | // CHROMIUM_LOG(FATAL) here because this code is called might be triggered by a |
michael@0 | 153 | // CHROMIUM_LOG(FATAL) itself. |
michael@0 | 154 | void OutputTraceToStream(const std::vector<void*>& trace, std::ostream* os) { |
michael@0 | 155 | AutoLock lock(lock_); |
michael@0 | 156 | |
michael@0 | 157 | for (size_t i = 0; (i < trace.size()) && os->good(); ++i) { |
michael@0 | 158 | const int kMaxNameLength = 256; |
michael@0 | 159 | DWORD_PTR frame = reinterpret_cast<DWORD_PTR>(trace[i]); |
michael@0 | 160 | |
michael@0 | 161 | // Code adapted from MSDN example: |
michael@0 | 162 | // http://msdn.microsoft.com/en-us/library/ms680578(VS.85).aspx |
michael@0 | 163 | ULONG64 buffer[ |
michael@0 | 164 | (sizeof(SYMBOL_INFO) + |
michael@0 | 165 | kMaxNameLength * sizeof(wchar_t) + |
michael@0 | 166 | sizeof(ULONG64) - 1) / |
michael@0 | 167 | sizeof(ULONG64)]; |
michael@0 | 168 | |
michael@0 | 169 | // Initialize symbol information retrieval structures. |
michael@0 | 170 | DWORD64 sym_displacement = 0; |
michael@0 | 171 | PSYMBOL_INFO symbol = reinterpret_cast<PSYMBOL_INFO>(&buffer[0]); |
michael@0 | 172 | symbol->SizeOfStruct = sizeof(SYMBOL_INFO); |
michael@0 | 173 | symbol->MaxNameLen = kMaxNameLength; |
michael@0 | 174 | BOOL has_symbol = SymFromAddr(process(), frame, |
michael@0 | 175 | &sym_displacement, symbol); |
michael@0 | 176 | |
michael@0 | 177 | // Attempt to retrieve line number information. |
michael@0 | 178 | DWORD line_displacement = 0; |
michael@0 | 179 | IMAGEHLP_LINE64 line = {}; |
michael@0 | 180 | line.SizeOfStruct = sizeof(IMAGEHLP_LINE64); |
michael@0 | 181 | BOOL has_line = SymGetLineFromAddr64(process(), frame, |
michael@0 | 182 | &line_displacement, &line); |
michael@0 | 183 | |
michael@0 | 184 | // Output the backtrace line. |
michael@0 | 185 | (*os) << "\t"; |
michael@0 | 186 | if (has_symbol) { |
michael@0 | 187 | (*os) << symbol->Name << " [0x" << trace[i] << "+" |
michael@0 | 188 | << sym_displacement << "]"; |
michael@0 | 189 | } else { |
michael@0 | 190 | // If there is no symbol informtion, add a spacer. |
michael@0 | 191 | (*os) << "(No symbol) [0x" << trace[i] << "]"; |
michael@0 | 192 | } |
michael@0 | 193 | if (has_line) { |
michael@0 | 194 | (*os) << " (" << line.FileName << ":" << line.LineNumber << ")"; |
michael@0 | 195 | } |
michael@0 | 196 | (*os) << "\n"; |
michael@0 | 197 | } |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | SymbolContext() |
michael@0 | 201 | : initialized_(false), |
michael@0 | 202 | process_(NULL), |
michael@0 | 203 | init_error_(ERROR_SUCCESS) { |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | private: |
michael@0 | 207 | Lock lock_; |
michael@0 | 208 | bool initialized_; |
michael@0 | 209 | HANDLE process_; |
michael@0 | 210 | DWORD init_error_; |
michael@0 | 211 | |
michael@0 | 212 | DISALLOW_COPY_AND_ASSIGN(SymbolContext); |
michael@0 | 213 | }; |
michael@0 | 214 | |
michael@0 | 215 | } // namespace |
michael@0 | 216 | |
michael@0 | 217 | // Note: Does not use the CRT. |
michael@0 | 218 | bool DebugUtil::SpawnDebuggerOnProcess(unsigned process_id) { |
michael@0 | 219 | wchar_t reg_value[1026]; |
michael@0 | 220 | int len = arraysize(reg_value); |
michael@0 | 221 | if (RegReadString(HKEY_LOCAL_MACHINE, |
michael@0 | 222 | L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug", |
michael@0 | 223 | L"Debugger", reg_value, &len)) { |
michael@0 | 224 | wchar_t command_line[1026]; |
michael@0 | 225 | if (StringReplace(reg_value, process_id, command_line, |
michael@0 | 226 | arraysize(command_line))) { |
michael@0 | 227 | // We don't mind if the debugger is present because it will simply fail |
michael@0 | 228 | // to attach to this process. |
michael@0 | 229 | STARTUPINFO startup_info = {0}; |
michael@0 | 230 | startup_info.cb = sizeof(startup_info); |
michael@0 | 231 | PROCESS_INFORMATION process_info = {0}; |
michael@0 | 232 | |
michael@0 | 233 | if (CreateProcess(NULL, command_line, NULL, NULL, FALSE, 0, NULL, NULL, |
michael@0 | 234 | &startup_info, &process_info)) { |
michael@0 | 235 | CloseHandle(process_info.hThread); |
michael@0 | 236 | WaitForInputIdle(process_info.hProcess, 10000); |
michael@0 | 237 | CloseHandle(process_info.hProcess); |
michael@0 | 238 | return true; |
michael@0 | 239 | } |
michael@0 | 240 | } |
michael@0 | 241 | } |
michael@0 | 242 | return false; |
michael@0 | 243 | } |
michael@0 | 244 | |
michael@0 | 245 | // static |
michael@0 | 246 | bool DebugUtil::BeingDebugged() { |
michael@0 | 247 | return ::IsDebuggerPresent() != 0; |
michael@0 | 248 | } |
michael@0 | 249 | |
michael@0 | 250 | // static |
michael@0 | 251 | void DebugUtil::BreakDebugger() { |
michael@0 | 252 | __debugbreak(); |
michael@0 | 253 | } |
michael@0 | 254 | |
michael@0 | 255 | StackTrace::StackTrace() { |
michael@0 | 256 | // From http://msdn.microsoft.com/en-us/library/bb204633(VS.85).aspx, |
michael@0 | 257 | // the sum of FramesToSkip and FramesToCapture must be less than 63, |
michael@0 | 258 | // so set it to 62. |
michael@0 | 259 | const int kMaxCallers = 62; |
michael@0 | 260 | |
michael@0 | 261 | void* callers[kMaxCallers]; |
michael@0 | 262 | // TODO(ajwong): Migrate this to StackWalk64. |
michael@0 | 263 | int count = CaptureStackBackTrace(0, kMaxCallers, callers, NULL); |
michael@0 | 264 | if (count > 0) { |
michael@0 | 265 | trace_.resize(count); |
michael@0 | 266 | memcpy(&trace_[0], callers, sizeof(callers[0]) * count); |
michael@0 | 267 | } else { |
michael@0 | 268 | trace_.resize(0); |
michael@0 | 269 | } |
michael@0 | 270 | } |
michael@0 | 271 | |
michael@0 | 272 | void StackTrace::PrintBacktrace() { |
michael@0 | 273 | OutputToStream(&std::cerr); |
michael@0 | 274 | } |
michael@0 | 275 | |
michael@0 | 276 | void StackTrace::OutputToStream(std::ostream* os) { |
michael@0 | 277 | SymbolContext* context = SymbolContext::Get(); |
michael@0 | 278 | |
michael@0 | 279 | if (context->Init() != ERROR_SUCCESS) { |
michael@0 | 280 | DWORD error = context->init_error(); |
michael@0 | 281 | (*os) << "Error initializing symbols (" << error |
michael@0 | 282 | << "). Dumping unresolved backtrace:\n"; |
michael@0 | 283 | for (size_t i = 0; (i < trace_.size()) && os->good(); ++i) { |
michael@0 | 284 | (*os) << "\t" << trace_[i] << "\n"; |
michael@0 | 285 | } |
michael@0 | 286 | } else { |
michael@0 | 287 | (*os) << "Backtrace:\n"; |
michael@0 | 288 | context->OutputTraceToStream(trace_, os); |
michael@0 | 289 | } |
michael@0 | 290 | } |