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

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

Issue 3945002: Move debug-related stuff from base to the base/debug directory and use the... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 10 years, 2 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 | Annotate | Revision Log
« no previous file with comments | « base/debug/debugger_posix.cc ('k') | base/debug/leak_annotations.h » ('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) 2006-2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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_util.h" 5 #include "base/debug/debugger.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 "base/basictypes.h"
11 #include "base/debug_util.h"
12 #include "base/logging.h"
11 13
12 #include "base/basictypes.h" 14 namespace base {
13 #include "base/lock.h" 15 namespace debug {
14 #include "base/logging.h"
15 #include "base/singleton.h"
16 16
17 namespace { 17 namespace {
18 18
19 // Minimalist key reader. 19 // Minimalist key reader.
20 // Note: Does not use the CRT. 20 // Note: Does not use the CRT.
21 bool RegReadString(HKEY root, const wchar_t* subkey, 21 bool RegReadString(HKEY root, const wchar_t* subkey,
22 const wchar_t* value_name, wchar_t* buffer, int* len) { 22 const wchar_t* value_name, wchar_t* buffer, int* len) {
23 HKEY key = NULL; 23 HKEY key = NULL;
24 DWORD res = RegOpenKeyEx(root, subkey, 0, KEY_READ, &key); 24 DWORD res = RegOpenKeyEx(root, subkey, 0, KEY_READ, &key);
25 if (ERROR_SUCCESS != res || key == NULL) 25 if (ERROR_SUCCESS != res || key == NULL)
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 i += 2; 61 i += 2;
62 } else { 62 } else {
63 if (current_output_len >= output_len) 63 if (current_output_len >= output_len)
64 return false; 64 return false;
65 output[current_output_len] = input[i]; 65 output[current_output_len] = input[i];
66 } 66 }
67 } 67 }
68 return true; 68 return true;
69 } 69 }
70 70
71 // SymbolContext is a threadsafe singleton that wraps the DbgHelp Sym* family
72 // of functions. The Sym* family of functions may only be invoked by one
73 // thread at a time. SymbolContext code may access a symbol server over the
74 // network while holding the lock for this singleton. In the case of high
75 // latency, this code will adversly affect performance.
76 //
77 // There is also a known issue where this backtrace code can interact
78 // badly with breakpad if breakpad is invoked in a separate thread while
79 // we are using the Sym* functions. This is because breakpad does now
80 // share a lock with this function. See this related bug:
81 //
82 // http://code.google.com/p/google-breakpad/issues/detail?id=311
83 //
84 // This is a very unlikely edge case, and the current solution is to
85 // just ignore it.
86 class SymbolContext {
87 public:
88 static SymbolContext* Get() {
89 // We use a leaky singleton because code may call this during process
90 // termination.
91 return
92 Singleton<SymbolContext, LeakySingletonTraits<SymbolContext> >::get();
93 }
94
95 // Returns the error code of a failed initialization.
96 DWORD init_error() const {
97 return init_error_;
98 }
99
100 // For the given trace, attempts to resolve the symbols, and output a trace
101 // to the ostream os. The format for each line of the backtrace is:
102 //
103 // <tab>SymbolName[0xAddress+Offset] (FileName:LineNo)
104 //
105 // This function should only be called if Init() has been called. We do not
106 // LOG(FATAL) here because this code is called might be triggered by a
107 // LOG(FATAL) itself.
108 void OutputTraceToStream(const void* const* trace,
109 int count,
110 std::ostream* os) {
111 AutoLock lock(lock_);
112
113 for (size_t i = 0; (i < count) && os->good(); ++i) {
114 const int kMaxNameLength = 256;
115 DWORD_PTR frame = reinterpret_cast<DWORD_PTR>(trace[i]);
116
117 // Code adapted from MSDN example:
118 // http://msdn.microsoft.com/en-us/library/ms680578(VS.85).aspx
119 ULONG64 buffer[
120 (sizeof(SYMBOL_INFO) +
121 kMaxNameLength * sizeof(wchar_t) +
122 sizeof(ULONG64) - 1) /
123 sizeof(ULONG64)];
124 memset(buffer, 0, sizeof(buffer));
125
126 // Initialize symbol information retrieval structures.
127 DWORD64 sym_displacement = 0;
128 PSYMBOL_INFO symbol = reinterpret_cast<PSYMBOL_INFO>(&buffer[0]);
129 symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
130 symbol->MaxNameLen = kMaxNameLength - 1;
131 BOOL has_symbol = SymFromAddr(GetCurrentProcess(), frame,
132 &sym_displacement, symbol);
133
134 // Attempt to retrieve line number information.
135 DWORD line_displacement = 0;
136 IMAGEHLP_LINE64 line = {};
137 line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
138 BOOL has_line = SymGetLineFromAddr64(GetCurrentProcess(), frame,
139 &line_displacement, &line);
140
141 // Output the backtrace line.
142 (*os) << "\t";
143 if (has_symbol) {
144 (*os) << symbol->Name << " [0x" << trace[i] << "+"
145 << sym_displacement << "]";
146 } else {
147 // If there is no symbol informtion, add a spacer.
148 (*os) << "(No symbol) [0x" << trace[i] << "]";
149 }
150 if (has_line) {
151 (*os) << " (" << line.FileName << ":" << line.LineNumber << ")";
152 }
153 (*os) << "\n";
154 }
155 }
156
157 private:
158 friend struct DefaultSingletonTraits<SymbolContext>;
159
160 SymbolContext() : init_error_(ERROR_SUCCESS) {
161 // Initializes the symbols for the process.
162 // Defer symbol load until they're needed, use undecorated names, and
163 // get line numbers.
164 SymSetOptions(SYMOPT_DEFERRED_LOADS |
165 SYMOPT_UNDNAME |
166 SYMOPT_LOAD_LINES);
167 if (SymInitialize(GetCurrentProcess(), NULL, TRUE)) {
168 init_error_ = ERROR_SUCCESS;
169 } else {
170 init_error_ = GetLastError();
171 // TODO(awong): Handle error: SymInitialize can fail with
172 // ERROR_INVALID_PARAMETER.
173 // When it fails, we should not call debugbreak since it kills the current
174 // process (prevents future tests from running or kills the browser
175 // process).
176 DLOG(ERROR) << "SymInitialize failed: " << init_error_;
177 }
178 }
179
180 DWORD init_error_;
181 Lock lock_;
182 DISALLOW_COPY_AND_ASSIGN(SymbolContext);
183 };
184
185 } // namespace 71 } // namespace
186 72
187 // Note: Does not use the CRT. 73 // Note: Does not use the CRT.
188 bool DebugUtil::SpawnDebuggerOnProcess(unsigned process_id) { 74 bool SpawnDebuggerOnProcess(unsigned process_id) {
189 wchar_t reg_value[1026]; 75 wchar_t reg_value[1026];
190 int len = arraysize(reg_value); 76 int len = arraysize(reg_value);
191 if (RegReadString(HKEY_LOCAL_MACHINE, 77 if (RegReadString(HKEY_LOCAL_MACHINE,
192 L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug", 78 L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug",
193 L"Debugger", reg_value, &len)) { 79 L"Debugger", reg_value, &len)) {
194 wchar_t command_line[1026]; 80 wchar_t command_line[1026];
195 if (StringReplace(reg_value, process_id, command_line, 81 if (StringReplace(reg_value, process_id, command_line,
196 arraysize(command_line))) { 82 arraysize(command_line))) {
197 // We don't mind if the debugger is present because it will simply fail 83 // We don't mind if the debugger is present because it will simply fail
198 // to attach to this process. 84 // to attach to this process.
199 STARTUPINFO startup_info = {0}; 85 STARTUPINFO startup_info = {0};
200 startup_info.cb = sizeof(startup_info); 86 startup_info.cb = sizeof(startup_info);
201 PROCESS_INFORMATION process_info = {0}; 87 PROCESS_INFORMATION process_info = {0};
202 88
203 if (CreateProcess(NULL, command_line, NULL, NULL, FALSE, 0, NULL, NULL, 89 if (CreateProcess(NULL, command_line, NULL, NULL, FALSE, 0, NULL, NULL,
204 &startup_info, &process_info)) { 90 &startup_info, &process_info)) {
205 CloseHandle(process_info.hThread); 91 CloseHandle(process_info.hThread);
206 WaitForInputIdle(process_info.hProcess, 10000); 92 WaitForInputIdle(process_info.hProcess, 10000);
207 CloseHandle(process_info.hProcess); 93 CloseHandle(process_info.hProcess);
208 return true; 94 return true;
209 } 95 }
210 } 96 }
211 } 97 }
212 return false; 98 return false;
213 } 99 }
214 100
215 // static 101 bool BeingDebugged() {
216 bool DebugUtil::BeingDebugged() {
217 return ::IsDebuggerPresent() != 0; 102 return ::IsDebuggerPresent() != 0;
218 } 103 }
219 104
220 // static 105 void BreakDebugger() {
221 void DebugUtil::BreakDebugger() { 106 if (DebugUtil::AreDialogsSuppressed())
222 if (suppress_dialogs_)
223 _exit(1); 107 _exit(1);
224
225 __debugbreak(); 108 __debugbreak();
226 } 109 }
227 110
228 StackTrace::StackTrace() { 111 } // namespace debug
229 // When walking our own stack, use CaptureStackBackTrace(). 112 } // namespace base
230 count_ = CaptureStackBackTrace(0, arraysize(trace_), trace_, NULL);
231 }
232
233 StackTrace::StackTrace(EXCEPTION_POINTERS* exception_pointers) {
234 // When walking an exception stack, we need to use StackWalk64().
235 count_ = 0;
236 // Initialize stack walking.
237 STACKFRAME64 stack_frame;
238 memset(&stack_frame, 0, sizeof(stack_frame));
239 #if defined(_WIN64)
240 int machine_type = IMAGE_FILE_MACHINE_AMD64;
241 stack_frame.AddrPC.Offset = exception_pointers->ContextRecord->Rip;
242 stack_frame.AddrFrame.Offset = exception_pointers->ContextRecord->Rbp;
243 stack_frame.AddrStack.Offset = exception_pointers->ContextRecord->Rsp;
244 #else
245 int machine_type = IMAGE_FILE_MACHINE_I386;
246 stack_frame.AddrPC.Offset = exception_pointers->ContextRecord->Eip;
247 stack_frame.AddrFrame.Offset = exception_pointers->ContextRecord->Ebp;
248 stack_frame.AddrStack.Offset = exception_pointers->ContextRecord->Esp;
249 #endif
250 stack_frame.AddrPC.Mode = AddrModeFlat;
251 stack_frame.AddrFrame.Mode = AddrModeFlat;
252 stack_frame.AddrStack.Mode = AddrModeFlat;
253 while (StackWalk64(machine_type,
254 GetCurrentProcess(),
255 GetCurrentThread(),
256 &stack_frame,
257 exception_pointers->ContextRecord,
258 NULL,
259 &SymFunctionTableAccess64,
260 &SymGetModuleBase64,
261 NULL) &&
262 count_ < arraysize(trace_)) {
263 trace_[count_++] = reinterpret_cast<void*>(stack_frame.AddrPC.Offset);
264 }
265 }
266
267 void StackTrace::PrintBacktrace() {
268 OutputToStream(&std::cerr);
269 }
270
271 void StackTrace::OutputToStream(std::ostream* os) {
272 SymbolContext* context = SymbolContext::Get();
273 DWORD error = context->init_error();
274 if (error != ERROR_SUCCESS) {
275 (*os) << "Error initializing symbols (" << error
276 << "). Dumping unresolved backtrace:\n";
277 for (int i = 0; (i < count_) && os->good(); ++i) {
278 (*os) << "\t" << trace_[i] << "\n";
279 }
280 } else {
281 (*os) << "Backtrace:\n";
282 context->OutputTraceToStream(trace_, count_, os);
283 }
284 }
OLDNEW
« no previous file with comments | « base/debug/debugger_posix.cc ('k') | base/debug/leak_annotations.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698