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 
49 namespace google_breakpad {
50 
StackFrameSymbolizer(SymbolSupplier * supplier,SourceLineResolverInterface * resolver)51 StackFrameSymbolizer::StackFrameSymbolizer(
52     SymbolSupplier* supplier,
53     SourceLineResolverInterface* resolver) : supplier_(supplier),
54                                              resolver_(resolver) { }
55 
FillSourceLineInfo(const CodeModules * modules,const CodeModules * unloaded_modules,const SystemInfo * system_info,StackFrame * frame)56 StackFrameSymbolizer::SymbolizerResult StackFrameSymbolizer::FillSourceLineInfo(
57     const CodeModules* modules,
58     const CodeModules* unloaded_modules,
59     const SystemInfo* system_info,
60     StackFrame* frame) {
61   assert(frame);
62 
63   const CodeModule* module = NULL;
64   if (modules) {
65     module = modules->GetModuleForAddress(frame->instruction);
66   }
67   if (!module && unloaded_modules) {
68     module = unloaded_modules->GetModuleForAddress(frame->instruction);
69   }
70 
71   if (!module) return kError;
72   frame->module = module;
73 
74   if (!resolver_) return kError;  // no resolver.
75   // If module is known to have missing symbol file, return.
76   if (no_symbol_modules_.find(module->code_file()) !=
77       no_symbol_modules_.end()) {
78     return kError;
79   }
80 
81   // If module is already loaded, go ahead to fill source line info and return.
82   if (resolver_->HasModule(frame->module)) {
83     resolver_->FillSourceLineInfo(frame);
84     return resolver_->IsModuleCorrupt(frame->module) ?
85         kWarningCorruptSymbols : kNoError;
86   }
87 
88   // Module needs to fetch symbol file. First check to see if supplier exists.
89   if (!supplier_) {
90     return kError;
91   }
92 
93   // Start fetching symbol from supplier.
94   string symbol_file;
95   char* symbol_data = NULL;
96   size_t symbol_data_size;
97   SymbolSupplier::SymbolResult symbol_result = supplier_->GetCStringSymbolData(
98       module, system_info, &symbol_file, &symbol_data, &symbol_data_size);
99 
100   switch (symbol_result) {
101     case SymbolSupplier::FOUND: {
102       bool load_success = resolver_->LoadModuleUsingMemoryBuffer(
103           frame->module,
104           symbol_data,
105           symbol_data_size);
106       if (resolver_->ShouldDeleteMemoryBufferAfterLoadModule()) {
107         supplier_->FreeSymbolData(module);
108       }
109 
110       if (load_success) {
111         resolver_->FillSourceLineInfo(frame);
112         return resolver_->IsModuleCorrupt(frame->module) ?
113             kWarningCorruptSymbols : kNoError;
114       } else {
115         BPLOG(ERROR) << "Failed to load symbol file in resolver.";
116         no_symbol_modules_.insert(module->code_file());
117         return kError;
118       }
119     }
120 
121     case SymbolSupplier::NOT_FOUND:
122       no_symbol_modules_.insert(module->code_file());
123       return kError;
124 
125     case SymbolSupplier::INTERRUPT:
126       return kInterrupt;
127 
128     default:
129       BPLOG(ERROR) << "Unknown SymbolResult enum: " << symbol_result;
130       return kError;
131   }
132   return kError;
133 }
134 
FindWindowsFrameInfo(const StackFrame * frame)135 WindowsFrameInfo* StackFrameSymbolizer::FindWindowsFrameInfo(
136     const StackFrame* frame) {
137   return resolver_ ? resolver_->FindWindowsFrameInfo(frame) : NULL;
138 }
139 
FindCFIFrameInfo(const StackFrame * frame)140 CFIFrameInfo* StackFrameSymbolizer::FindCFIFrameInfo(
141     const StackFrame* frame) {
142   return resolver_ ? resolver_->FindCFIFrameInfo(frame) : NULL;
143 }
144 
145 }  // namespace google_breakpad
146