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