summaryrefslogtreecommitdiffstats
path: root/toolkit/crashreporter/minidump-analyzer/MozStackFrameSymbolizer.cpp
blob: 7d7e3c4b2f2c5dda917eb483effd5a15dfc4bbac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#if XP_WIN && HAVE_64BIT_BUILD

#  include "MozStackFrameSymbolizer.h"

#  include "MinidumpAnalyzerUtils.h"

#  include "processor/cfi_frame_info.h"

#  include <sstream>
#  include <fstream>

namespace CrashReporter {

extern MinidumpAnalyzerOptions gMinidumpAnalyzerOptions;

using google_breakpad::CFIFrameInfo;

MozStackFrameSymbolizer::MozStackFrameSymbolizer()
    : StackFrameSymbolizer(nullptr, nullptr) {}

MozStackFrameSymbolizer::SymbolizerResult
MozStackFrameSymbolizer::FillSourceLineInfo(const CodeModules* modules,
                                            const CodeModules* unloaded_modules,
                                            const SystemInfo* system_info,
                                            StackFrame* stack_frame) {
  SymbolizerResult ret = StackFrameSymbolizer::FillSourceLineInfo(
      modules, unloaded_modules, system_info, stack_frame);

  if (ret == kNoError && this->HasImplementation() &&
      stack_frame->function_name.empty()) {
    // Breakpad's Stackwalker::InstructionAddressSeemsValid only considers an
    // address valid if it has associated symbols.
    //
    // This makes sense for complete & accurate symbols, but ours may be
    // incomplete or wrong. Returning a function name tells Breakpad we
    // recognize this address as code, so it's OK to use in stack scanning.
    // This function is only called with addresses that land in this module.
    //
    // This allows us to fall back to stack scanning in the case where we were
    // unable to provide CFI.
    stack_frame->function_name = "<unknown code>";
  }
  return ret;
}

CFIFrameInfo* MozStackFrameSymbolizer::FindCFIFrameInfo(
    const StackFrame* frame) {
  std::string modulePath;

  // For unit testing, support loading a specified module instead of
  // the real one.
  bool moduleHasBeenReplaced = false;
  if (gMinidumpAnalyzerOptions.forceUseModule.size() > 0) {
    modulePath = gMinidumpAnalyzerOptions.forceUseModule;
    moduleHasBeenReplaced = true;
  } else {
    if (!frame->module) {
      return nullptr;
    }
    modulePath = frame->module->code_file();
  }

  // Get/create the unwind parser.
  auto itMod = mModuleMap.find(modulePath);
  std::shared_ptr<ModuleUnwindParser> unwindParser;
  if (itMod != mModuleMap.end()) {
    unwindParser = itMod->second;
  } else {
    unwindParser.reset(new ModuleUnwindParser(modulePath));
    mModuleMap[modulePath] = unwindParser;
  }

  UnwindCFI cfi;
  DWORD offsetAddr;

  if (moduleHasBeenReplaced) {
    // If we are replacing a module, addresses will never line up.
    // So just act like the 1st entry is correct.
    offsetAddr = unwindParser->GetAnyOffsetAddr();
  } else {
    offsetAddr = frame->instruction - frame->module->base_address();
  }

  if (!unwindParser->GetCFI(offsetAddr, cfi)) {
    return nullptr;
  }

  std::unique_ptr<CFIFrameInfo> rules(new CFIFrameInfo());

  static const size_t exprSize = 50;
  char expr[exprSize];
  if (cfi.stackSize == 0) {
    snprintf(expr, exprSize, "$rsp");
  } else {
    snprintf(expr, exprSize, "$rsp %d +", cfi.stackSize);
  }
  rules->SetCFARule(expr);

  if (cfi.ripOffset == 0) {
    snprintf(expr, exprSize, ".cfa ^");
  } else {
    snprintf(expr, exprSize, ".cfa %d - ^", cfi.ripOffset);
  }
  rules->SetRARule(expr);

  return rules.release();
}

}  // namespace CrashReporter

#endif  // XP_WIN && HAVE_64BIT_BUILD