1// Copyright (c) 2010 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// source_line_resolver_base.h: SourceLineResolverBase, an (incomplete)
31// implementation of SourceLineResolverInterface. It serves as a common base
32// class for concrete implementations: FastSourceLineResolver and
33// BasicSourceLineResolver. It is designed for refactoring that removes
34// code redundancy in the two concrete source line resolver classes.
35//
36// See "google_breakpad/processor/source_line_resolver_interface.h" for more
37// documentation.
38
39// Author: Siyang Xie (lambxsy@google.com)
40
41#ifndef GOOGLE_BREAKPAD_PROCESSOR_SOURCE_LINE_RESOLVER_BASE_H__
42#define GOOGLE_BREAKPAD_PROCESSOR_SOURCE_LINE_RESOLVER_BASE_H__
43
44#include <deque>
45#include <map>
46#include <set>
47#include <string>
48
49#include "google_breakpad/processor/source_line_resolver_interface.h"
50
51namespace google_breakpad {
52
53using std::map;
54using std::set;
55
56// Forward declaration.
57// ModuleFactory is a simple factory interface for creating a Module instance
58// at run-time.
59class ModuleFactory;
60
61class SourceLineResolverBase : public SourceLineResolverInterface {
62 public:
63 // Read the symbol_data from a file with given file_name.
64 // The part of code was originally in BasicSourceLineResolver::Module's
65 // LoadMap() method.
66 // Place dynamically allocated heap buffer in symbol_data. Caller has the
67 // ownership of the buffer, and should call delete [] to free the buffer.
68 static bool ReadSymbolFile(const string& file_name,
69 char** symbol_data,
70 size_t* symbol_data_size);
71
72 protected:
73 // Users are not allowed create SourceLineResolverBase instance directly.
74 SourceLineResolverBase(ModuleFactory* module_factory);
75 virtual ~SourceLineResolverBase();
76
77 // Virtual methods inherited from SourceLineResolverInterface.
78 virtual bool LoadModule(const CodeModule* module, const string& map_file);
79 virtual bool LoadModuleUsingMapBuffer(const CodeModule* module,
80 const string& map_buffer);
81 virtual bool LoadModuleUsingMemoryBuffer(const CodeModule* module,
82 char* memory_buffer,
83 size_t memory_buffer_size);
84 virtual bool ShouldDeleteMemoryBufferAfterLoadModule();
85 virtual void UnloadModule(const CodeModule* module);
86 virtual bool HasModule(const CodeModule* module);
87 virtual bool IsModuleCorrupt(const CodeModule* module);
88 virtual void FillSourceLineInfo(
89 StackFrame* frame,
90 std::deque<std::unique_ptr<StackFrame>>* inlined_frames);
91 virtual WindowsFrameInfo* FindWindowsFrameInfo(const StackFrame* frame);
92 virtual CFIFrameInfo* FindCFIFrameInfo(const StackFrame* frame);
93
94 // Nested structs and classes.
95 struct InlineOrigin;
96 struct Inline;
97 struct Line;
98 struct Function;
99 struct PublicSymbol;
100 struct CompareString {
101 bool operator()(const string& s1, const string& s2) const;
102 };
103 // Module is an interface for an in-memory symbol file.
104 class Module;
105 class AutoFileCloser;
106
107 // All of the modules that are loaded.
108 typedef map<string, Module*, CompareString> ModuleMap;
109 ModuleMap* modules_;
110
111 // The loaded modules that were detecting to be corrupt during load.
112 typedef set<string, CompareString> ModuleSet;
113 ModuleSet* corrupt_modules_;
114
115 // All of heap-allocated buffers that are owned locally by resolver.
116 typedef std::map<string, char*, CompareString> MemoryMap;
117 MemoryMap* memory_buffers_;
118
119 // Creates a concrete module at run-time.
120 ModuleFactory* module_factory_;
121
122 private:
123 // ModuleFactory needs to have access to protected type Module.
124 friend class ModuleFactory;
125
126 // Disallow unwanted copy ctor and assignment operator
127 SourceLineResolverBase(const SourceLineResolverBase&);
128 void operator=(const SourceLineResolverBase&);
129};
130
131} // namespace google_breakpad
132
133#endif // GOOGLE_BREAKPAD_PROCESSOR_SOURCE_LINE_RESOLVER_BASE_H__
134