1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 #if XP_WIN && HAVE_64BIT_BUILD
7
8 # include "MozStackFrameSymbolizer.h"
9
10 # include "MinidumpAnalyzerUtils.h"
11
12 # include "processor/cfi_frame_info.h"
13
14 # include <iostream>
15 # include <sstream>
16 # include <fstream>
17
18 namespace CrashReporter {
19
20 extern MinidumpAnalyzerOptions gMinidumpAnalyzerOptions;
21
22 using google_breakpad::CFIFrameInfo;
23
MozStackFrameSymbolizer()24 MozStackFrameSymbolizer::MozStackFrameSymbolizer()
25 : StackFrameSymbolizer(nullptr, nullptr) {}
26
27 MozStackFrameSymbolizer::SymbolizerResult
FillSourceLineInfo(const CodeModules * modules,const CodeModules * unloaded_modules,const SystemInfo * system_info,StackFrame * stack_frame)28 MozStackFrameSymbolizer::FillSourceLineInfo(const CodeModules* modules,
29 const CodeModules* unloaded_modules,
30 const SystemInfo* system_info,
31 StackFrame* stack_frame) {
32 SymbolizerResult ret = StackFrameSymbolizer::FillSourceLineInfo(
33 modules, unloaded_modules, system_info, stack_frame);
34
35 if (ret == kNoError && this->HasImplementation() &&
36 stack_frame->function_name.empty()) {
37 // Breakpad's Stackwalker::InstructionAddressSeemsValid only considers an
38 // address valid if it has associated symbols.
39 //
40 // This makes sense for complete & accurate symbols, but ours may be
41 // incomplete or wrong. Returning a function name tells Breakpad we
42 // recognize this address as code, so it's OK to use in stack scanning.
43 // This function is only called with addresses that land in this module.
44 //
45 // This allows us to fall back to stack scanning in the case where we were
46 // unable to provide CFI.
47 stack_frame->function_name = "<unknown code>";
48 }
49 return ret;
50 }
51
FindCFIFrameInfo(const StackFrame * frame)52 CFIFrameInfo* MozStackFrameSymbolizer::FindCFIFrameInfo(
53 const StackFrame* frame) {
54 std::string modulePath;
55
56 // For unit testing, support loading a specified module instead of
57 // the real one.
58 bool moduleHasBeenReplaced = false;
59 if (gMinidumpAnalyzerOptions.forceUseModule.size() > 0) {
60 modulePath = gMinidumpAnalyzerOptions.forceUseModule;
61 moduleHasBeenReplaced = true;
62 } else {
63 if (!frame->module) {
64 return nullptr;
65 }
66 modulePath = frame->module->code_file();
67 }
68
69 // Get/create the unwind parser.
70 auto itMod = mModuleMap.find(modulePath);
71 std::shared_ptr<ModuleUnwindParser> unwindParser;
72 if (itMod != mModuleMap.end()) {
73 unwindParser = itMod->second;
74 } else {
75 unwindParser.reset(new ModuleUnwindParser(modulePath));
76 mModuleMap[modulePath] = unwindParser;
77 }
78
79 UnwindCFI cfi;
80 DWORD offsetAddr;
81
82 if (moduleHasBeenReplaced) {
83 // If we are replacing a module, addresses will never line up.
84 // So just act like the 1st entry is correct.
85 offsetAddr = unwindParser->GetAnyOffsetAddr();
86 } else {
87 offsetAddr = frame->instruction - frame->module->base_address();
88 }
89
90 if (!unwindParser->GetCFI(offsetAddr, cfi)) {
91 return nullptr;
92 }
93
94 std::unique_ptr<CFIFrameInfo> rules(new CFIFrameInfo());
95
96 static const size_t exprSize = 50;
97 char expr[exprSize];
98 if (cfi.stackSize == 0) {
99 snprintf(expr, exprSize, "$rsp");
100 } else {
101 snprintf(expr, exprSize, "$rsp %d +", cfi.stackSize);
102 }
103 rules->SetCFARule(expr);
104
105 if (cfi.ripOffset == 0) {
106 snprintf(expr, exprSize, ".cfa ^");
107 } else {
108 snprintf(expr, exprSize, ".cfa %d - ^", cfi.ripOffset);
109 }
110 rules->SetRARule(expr);
111
112 return rules.release();
113 }
114
115 } // namespace CrashReporter
116
117 #endif // XP_WIN && HAVE_64BIT_BUILD
118