1// Copyright (c) 2012 Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30// Implementation of StackFrameSymbolizer, which encapsulates the logic of how
31// SourceLineResolverInterface interacts with SymbolSupplier to fill source
32// line information in a stack frame, and also looks up WindowsFrameInfo or
33// CFIFrameInfo for a stack frame.
34
35#include "google_breakpad/processor/stack_frame_symbolizer.h"
36
37#include <assert.h>
38
39#include "common/scoped_ptr.h"
40#include "google_breakpad/processor/code_module.h"
41#include "google_breakpad/processor/code_modules.h"
42#include "google_breakpad/processor/source_line_resolver_interface.h"
43#include "google_breakpad/processor/stack_frame.h"
44#include "google_breakpad/processor/symbol_supplier.h"
45#include "google_breakpad/processor/system_info.h"
46#include "processor/linked_ptr.h"
47#include "processor/logging.h"
48
49namespace google_breakpad {
50
51StackFrameSymbolizer::StackFrameSymbolizer(
52 SymbolSupplier* supplier,
53 SourceLineResolverInterface* resolver) : supplier_(supplier),
54 resolver_(resolver) { }
55
56StackFrameSymbolizer::SymbolizerResult StackFrameSymbolizer::FillSourceLineInfo(
57 const CodeModules* modules,
58 const CodeModules* unloaded_modules,
59 const SystemInfo* system_info,
60 StackFrame* frame,
61 std::deque<std::unique_ptr<StackFrame>>* inlined_frames) {
62 assert(frame);
63
64 const CodeModule* module = NULL;
65 if (modules) {
66 module = modules->GetModuleForAddress(frame->instruction);
67 }
68 if (!module && unloaded_modules) {
69 module = unloaded_modules->GetModuleForAddress(frame->instruction);
70 }
71
72 if (!module) return kError;
73 frame->module = module;
74
75 if (!resolver_) return kError; // no resolver.
76 // If module is known to have missing symbol file, return.
77 if (no_symbol_modules_.find(module->code_file()) !=
78 no_symbol_modules_.end()) {
79 return kError;
80 }
81
82 // If module is already loaded, go ahead to fill source line info and return.
83 if (resolver_->HasModule(frame->module)) {
84 resolver_->FillSourceLineInfo(frame, inlined_frames);
85 return resolver_->IsModuleCorrupt(frame->module) ?
86 kWarningCorruptSymbols : kNoError;
87 }
88
89 // Module needs to fetch symbol file. First check to see if supplier exists.
90 if (!supplier_) {
91 return kError;
92 }
93
94 // Start fetching symbol from supplier.
95 string symbol_file;
96 char* symbol_data = NULL;
97 size_t symbol_data_size;
98 SymbolSupplier::SymbolResult symbol_result = supplier_->GetCStringSymbolData(
99 module, system_info, &symbol_file, &symbol_data, &symbol_data_size);
100
101 switch (symbol_result) {
102 case SymbolSupplier::FOUND: {
103 bool load_success = resolver_->LoadModuleUsingMemoryBuffer(
104 frame->module,
105 symbol_data,
106 symbol_data_size);
107 if (resolver_->ShouldDeleteMemoryBufferAfterLoadModule()) {
108 supplier_->FreeSymbolData(module);
109 }
110
111 if (load_success) {
112 resolver_->FillSourceLineInfo(frame, inlined_frames);
113 return resolver_->IsModuleCorrupt(frame->module) ?
114 kWarningCorruptSymbols : kNoError;
115 } else {
116 BPLOG(ERROR) << "Failed to load symbol file in resolver.";
117 no_symbol_modules_.insert(module->code_file());
118 return kError;
119 }
120 }
121
122 case SymbolSupplier::NOT_FOUND:
123 no_symbol_modules_.insert(module->code_file());
124 return kError;
125
126 case SymbolSupplier::INTERRUPT:
127 return kInterrupt;
128
129 default:
130 BPLOG(ERROR) << "Unknown SymbolResult enum: " << symbol_result;
131 return kError;
132 }
133 return kError;
134}
135
136WindowsFrameInfo* StackFrameSymbolizer::FindWindowsFrameInfo(
137 const StackFrame* frame) {
138 return resolver_ ? resolver_->FindWindowsFrameInfo(frame) : NULL;
139}
140
141CFIFrameInfo* StackFrameSymbolizer::FindCFIFrameInfo(
142 const StackFrame* frame) {
143 return resolver_ ? resolver_->FindCFIFrameInfo(frame) : NULL;
144}
145
146} // namespace google_breakpad
147