| 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> |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 | 204 |
| 205 StackTrace::StackTrace() { | 205 StackTrace::StackTrace() { |
| 206 // When walking our own stack, use CaptureStackBackTrace(). | 206 // When walking our own stack, use CaptureStackBackTrace(). |
| 207 count_ = CaptureStackBackTrace(0, arraysize(trace_), trace_, NULL); | 207 count_ = CaptureStackBackTrace(0, arraysize(trace_), trace_, NULL); |
| 208 } | 208 } |
| 209 | 209 |
| 210 #if defined(COMPILER_MSVC) | 210 #if defined(COMPILER_MSVC) |
| 211 #pragma optimize("", on) | 211 #pragma optimize("", on) |
| 212 #endif | 212 #endif |
| 213 | 213 |
| 214 StackTrace::StackTrace(EXCEPTION_POINTERS* exception_pointers) { | 214 StackTrace::StackTrace(const EXCEPTION_POINTERS* exception_pointers) { |
| 215 // When walking an exception stack, we need to use StackWalk64(). | 215 // When walking an exception stack, we need to use StackWalk64(). |
| 216 count_ = 0; | 216 count_ = 0; |
| 217 // StackWalk64() may modify context record passed to it, so we will |
| 218 // use a copy. |
| 219 CONTEXT context_record = *exception_pointers->ContextRecord; |
| 217 // Initialize stack walking. | 220 // Initialize stack walking. |
| 218 STACKFRAME64 stack_frame; | 221 STACKFRAME64 stack_frame; |
| 219 memset(&stack_frame, 0, sizeof(stack_frame)); | 222 memset(&stack_frame, 0, sizeof(stack_frame)); |
| 220 #if defined(_WIN64) | 223 #if defined(_WIN64) |
| 221 int machine_type = IMAGE_FILE_MACHINE_AMD64; | 224 int machine_type = IMAGE_FILE_MACHINE_AMD64; |
| 222 stack_frame.AddrPC.Offset = exception_pointers->ContextRecord->Rip; | 225 stack_frame.AddrPC.Offset = context_record.Rip; |
| 223 stack_frame.AddrFrame.Offset = exception_pointers->ContextRecord->Rbp; | 226 stack_frame.AddrFrame.Offset = context_record.Rbp; |
| 224 stack_frame.AddrStack.Offset = exception_pointers->ContextRecord->Rsp; | 227 stack_frame.AddrStack.Offset = context_record.Rsp; |
| 225 #else | 228 #else |
| 226 int machine_type = IMAGE_FILE_MACHINE_I386; | 229 int machine_type = IMAGE_FILE_MACHINE_I386; |
| 227 stack_frame.AddrPC.Offset = exception_pointers->ContextRecord->Eip; | 230 stack_frame.AddrPC.Offset = context_record.Eip; |
| 228 stack_frame.AddrFrame.Offset = exception_pointers->ContextRecord->Ebp; | 231 stack_frame.AddrFrame.Offset = context_record.Ebp; |
| 229 stack_frame.AddrStack.Offset = exception_pointers->ContextRecord->Esp; | 232 stack_frame.AddrStack.Offset = context_record.Esp; |
| 230 #endif | 233 #endif |
| 231 stack_frame.AddrPC.Mode = AddrModeFlat; | 234 stack_frame.AddrPC.Mode = AddrModeFlat; |
| 232 stack_frame.AddrFrame.Mode = AddrModeFlat; | 235 stack_frame.AddrFrame.Mode = AddrModeFlat; |
| 233 stack_frame.AddrStack.Mode = AddrModeFlat; | 236 stack_frame.AddrStack.Mode = AddrModeFlat; |
| 234 while (StackWalk64(machine_type, | 237 while (StackWalk64(machine_type, |
| 235 GetCurrentProcess(), | 238 GetCurrentProcess(), |
| 236 GetCurrentThread(), | 239 GetCurrentThread(), |
| 237 &stack_frame, | 240 &stack_frame, |
| 238 exception_pointers->ContextRecord, | 241 &context_record, |
| 239 NULL, | 242 NULL, |
| 240 &SymFunctionTableAccess64, | 243 &SymFunctionTableAccess64, |
| 241 &SymGetModuleBase64, | 244 &SymGetModuleBase64, |
| 242 NULL) && | 245 NULL) && |
| 243 count_ < arraysize(trace_)) { | 246 count_ < arraysize(trace_)) { |
| 244 trace_[count_++] = reinterpret_cast<void*>(stack_frame.AddrPC.Offset); | 247 trace_[count_++] = reinterpret_cast<void*>(stack_frame.AddrPC.Offset); |
| 245 } | 248 } |
| 246 | 249 |
| 247 for (size_t i = count_; i < arraysize(trace_); ++i) | 250 for (size_t i = count_; i < arraysize(trace_); ++i) |
| 248 trace_[i] = NULL; | 251 trace_[i] = NULL; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 262 (*os) << "\t" << trace_[i] << "\n"; | 265 (*os) << "\t" << trace_[i] << "\n"; |
| 263 } | 266 } |
| 264 } else { | 267 } else { |
| 265 (*os) << "Backtrace:\n"; | 268 (*os) << "Backtrace:\n"; |
| 266 context->OutputTraceToStream(trace_, count_, os); | 269 context->OutputTraceToStream(trace_, count_, os); |
| 267 } | 270 } |
| 268 } | 271 } |
| 269 | 272 |
| 270 } // namespace debug | 273 } // namespace debug |
| 271 } // namespace base | 274 } // namespace base |
| OLD | NEW |