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

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

Issue 2248393002: Replace DumpBacktrace with Chromium's StackTrace implementation. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Fix mac Created 4 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
OLDNEW
(Empty)
1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/base/debug/stack_trace.h"
6
7 #include <windows.h>
8 #include <dbghelp.h>
9 #include <Shlwapi.h>
10 #include <stddef.h>
11
12 #include <iostream>
13 #include <memory>
14
15 #include "src/base/logging.h"
16 #include "src/base/macros.h"
17
18 namespace v8 {
19 namespace base {
20 namespace debug {
21
22 namespace {
23
24 // Previous unhandled filter. Will be called if not NULL when we intercept an
25 // exception. Only used in unit tests.
26 LPTOP_LEVEL_EXCEPTION_FILTER g_previous_filter = NULL;
27
28 bool g_dump_stack_in_signal_handler = true;
29 bool g_initialized_symbols = false;
30 DWORD g_init_error = ERROR_SUCCESS;
31
32 // Prints the exception call stack.
33 // This is the unit tests exception filter.
34 long WINAPI StackDumpExceptionFilter(EXCEPTION_POINTERS* info) { // NOLINT
35 if (g_dump_stack_in_signal_handler) {
36 debug::StackTrace(info).Print();
37 }
38 if (g_previous_filter) return g_previous_filter(info);
39 return EXCEPTION_CONTINUE_SEARCH;
40 }
41
42 void GetExePath(wchar_t* path_out) {
43 GetModuleFileName(NULL, path_out, MAX_PATH);
44 path_out[MAX_PATH - 1] = L'\0';
45 PathRemoveFileSpec(path_out);
46 }
47
48 bool InitializeSymbols() {
49 if (g_initialized_symbols) return g_init_error == ERROR_SUCCESS;
50 g_initialized_symbols = true;
51 // Defer symbol load until they're needed, use undecorated names, and get line
52 // numbers.
53 SymSetOptions(SYMOPT_DEFERRED_LOADS | SYMOPT_UNDNAME | SYMOPT_LOAD_LINES);
54 if (!SymInitialize(GetCurrentProcess(), NULL, TRUE)) {
55 g_init_error = GetLastError();
56 // TODO(awong): Handle error: SymInitialize can fail with
57 // ERROR_INVALID_PARAMETER.
58 // When it fails, we should not call debugbreak since it kills the current
59 // process (prevents future tests from running or kills the browser
60 // process).
61 return false;
62 }
63
64 // When transferring the binaries e.g. between bots, path put
65 // into the executable will get off. To still retrieve symbols correctly,
66 // add the directory of the executable to symbol search path.
67 // All following errors are non-fatal.
68 const size_t kSymbolsArraySize = 1024;
69 std::unique_ptr<wchar_t[]> symbols_path(new wchar_t[kSymbolsArraySize]);
70
71 // Note: The below function takes buffer size as number of characters,
72 // not number of bytes!
73 if (!SymGetSearchPathW(GetCurrentProcess(), symbols_path.get(),
74 kSymbolsArraySize)) {
75 g_init_error = GetLastError();
76 return false;
77 }
78
79 wchar_t exe_path[MAX_PATH];
80 GetExePath(exe_path);
81 std::wstring new_path(std::wstring(symbols_path.get()) + L";" +
82 std::wstring(exe_path));
83 if (!SymSetSearchPathW(GetCurrentProcess(), new_path.c_str())) {
84 g_init_error = GetLastError();
85 return false;
86 }
87
88 g_init_error = ERROR_SUCCESS;
89 return true;
90 }
91
92 // For the given trace, attempts to resolve the symbols, and output a trace
93 // to the ostream os. The format for each line of the backtrace is:
94 //
95 // <tab>SymbolName[0xAddress+Offset] (FileName:LineNo)
96 //
97 // This function should only be called if Init() has been called. We do not
98 // LOG(FATAL) here because this code is called might be triggered by a
99 // LOG(FATAL) itself. Also, it should not be calling complex code that is
100 // extensible like PathService since that can in turn fire CHECKs.
101 void OutputTraceToStream(const void* const* trace, size_t count,
102 std::ostream* os) {
103 for (size_t i = 0; (i < count) && os->good(); ++i) {
104 const int kMaxNameLength = 256;
105 DWORD_PTR frame = reinterpret_cast<DWORD_PTR>(trace[i]);
106
107 // Code adapted from MSDN example:
108 // http://msdn.microsoft.com/en-us/library/ms680578(VS.85).aspx
109 ULONG64 buffer[(sizeof(SYMBOL_INFO) + kMaxNameLength * sizeof(wchar_t) +
110 sizeof(ULONG64) - 1) /
111 sizeof(ULONG64)];
112 memset(buffer, 0, sizeof(buffer));
113
114 // Initialize symbol information retrieval structures.
115 DWORD64 sym_displacement = 0;
116 PSYMBOL_INFO symbol = reinterpret_cast<PSYMBOL_INFO>(&buffer[0]);
117 symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
118 symbol->MaxNameLen = kMaxNameLength - 1;
119 BOOL has_symbol =
120 SymFromAddr(GetCurrentProcess(), frame, &sym_displacement, symbol);
121
122 // Attempt to retrieve line number information.
123 DWORD line_displacement = 0;
124 IMAGEHLP_LINE64 line = {};
125 line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
126 BOOL has_line = SymGetLineFromAddr64(GetCurrentProcess(), frame,
127 &line_displacement, &line);
128
129 // Output the backtrace line.
130 (*os) << "\t";
131 if (has_symbol) {
132 (*os) << symbol->Name << " [0x" << trace[i] << "+" << sym_displacement
133 << "]";
134 } else {
135 // If there is no symbol information, add a spacer.
136 (*os) << "(No symbol) [0x" << trace[i] << "]";
137 }
138 if (has_line) {
139 (*os) << " (" << line.FileName << ":" << line.LineNumber << ")";
140 }
141 (*os) << "\n";
142 }
143 }
144
145 } // namespace
146
147 bool EnableInProcessStackDumping() {
148 // Add stack dumping support on exception on windows. Similar to OS_POSIX
149 // signal() handling in process_util_posix.cc.
150 g_previous_filter = SetUnhandledExceptionFilter(&StackDumpExceptionFilter);
151 g_dump_stack_in_signal_handler = true;
152
153 // Need to initialize symbols early in the process or else this fails on
154 // swarming (since symbols are in different directory than in the exes) and
155 // also release x64.
156 return InitializeSymbols();
157 }
158
159 void DisableSignalStackDump() {
160 g_dump_stack_in_signal_handler = false;
161 }
162
163 // Disable optimizations for the StackTrace::StackTrace function. It is
164 // important to disable at least frame pointer optimization ("y"), since
165 // that breaks CaptureStackBackTrace() and prevents StackTrace from working
166 // in Release builds (it may still be janky if other frames are using FPO,
167 // but at least it will make it further).
168 #if defined(COMPILER_MSVC)
169 #pragma optimize("", off)
170 #endif
171
172 StackTrace::StackTrace() {
173 // When walking our own stack, use CaptureStackBackTrace().
174 count_ = CaptureStackBackTrace(0, arraysize(trace_), trace_, NULL);
175 }
176
177 #if defined(COMPILER_MSVC)
178 #pragma optimize("", on)
179 #endif
180
181 StackTrace::StackTrace(EXCEPTION_POINTERS* exception_pointers) {
182 InitTrace(exception_pointers->ContextRecord);
183 }
184
185 StackTrace::StackTrace(const CONTEXT* context) { InitTrace(context); }
186
187 void StackTrace::InitTrace(const CONTEXT* context_record) {
188 // StackWalk64 modifies the register context in place, so we have to copy it
189 // so that downstream exception handlers get the right context. The incoming
190 // context may have had more register state (YMM, etc) than we need to unwind
191 // the stack. Typically StackWalk64 only needs integer and control registers.
192 CONTEXT context_copy;
193 memcpy(&context_copy, context_record, sizeof(context_copy));
194 context_copy.ContextFlags = CONTEXT_INTEGER | CONTEXT_CONTROL;
195
196 // When walking an exception stack, we need to use StackWalk64().
197 count_ = 0;
198 // Initialize stack walking.
199 STACKFRAME64 stack_frame;
200 memset(&stack_frame, 0, sizeof(stack_frame));
201 #if defined(_WIN64)
202 int machine_type = IMAGE_FILE_MACHINE_AMD64;
203 stack_frame.AddrPC.Offset = context_record->Rip;
204 stack_frame.AddrFrame.Offset = context_record->Rbp;
205 stack_frame.AddrStack.Offset = context_record->Rsp;
206 #else
207 int machine_type = IMAGE_FILE_MACHINE_I386;
208 stack_frame.AddrPC.Offset = context_record->Eip;
209 stack_frame.AddrFrame.Offset = context_record->Ebp;
210 stack_frame.AddrStack.Offset = context_record->Esp;
211 #endif
212 stack_frame.AddrPC.Mode = AddrModeFlat;
213 stack_frame.AddrFrame.Mode = AddrModeFlat;
214 stack_frame.AddrStack.Mode = AddrModeFlat;
215 while (StackWalk64(machine_type, GetCurrentProcess(), GetCurrentThread(),
216 &stack_frame, &context_copy, NULL,
217 &SymFunctionTableAccess64, &SymGetModuleBase64, NULL) &&
218 count_ < arraysize(trace_)) {
219 trace_[count_++] = reinterpret_cast<void*>(stack_frame.AddrPC.Offset);
220 }
221
222 for (size_t i = count_; i < arraysize(trace_); ++i) trace_[i] = NULL;
223 }
224
225 void StackTrace::Print() const { OutputToStream(&std::cerr); }
226
227 void StackTrace::OutputToStream(std::ostream* os) const {
228 InitializeSymbols();
229 if (g_init_error != ERROR_SUCCESS) {
230 (*os) << "Error initializing symbols (" << g_init_error
231 << "). Dumping unresolved backtrace:\n";
232 for (size_t i = 0; (i < count_) && os->good(); ++i) {
233 (*os) << "\t" << trace_[i] << "\n";
234 }
235 } else {
236 (*os) << "\n";
237 (*os) << "==== C stack trace ===============================\n";
238 (*os) << "\n";
239 OutputTraceToStream(trace_, count_, os);
240 }
241 }
242
243 } // namespace debug
244 } // namespace base
245 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698