1 | // -*- mode: c++ -*- |
2 | |
3 | // Copyright (c) 2010, Google Inc. |
4 | // All rights reserved. |
5 | // |
6 | // Redistribution and use in source and binary forms, with or without |
7 | // modification, are permitted provided that the following conditions are |
8 | // met: |
9 | // |
10 | // * Redistributions of source code must retain the above copyright |
11 | // notice, this list of conditions and the following disclaimer. |
12 | // * Redistributions in binary form must reproduce the above |
13 | // copyright notice, this list of conditions and the following disclaimer |
14 | // in the documentation and/or other materials provided with the |
15 | // distribution. |
16 | // * Neither the name of Google Inc. nor the names of its |
17 | // contributors may be used to endorse or promote products derived from |
18 | // this software without specific prior written permission. |
19 | // |
20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
31 | |
32 | // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> |
33 | |
34 | // dwarf_cfi_to_module.h: Define the DwarfCFIToModule class, which |
35 | // accepts parsed DWARF call frame info and adds it to a |
36 | // google_breakpad::Module object, which can write that information to |
37 | // a Breakpad symbol file. |
38 | |
39 | #ifndef COMMON_LINUX_DWARF_CFI_TO_MODULE_H |
40 | #define COMMON_LINUX_DWARF_CFI_TO_MODULE_H |
41 | |
42 | #include <assert.h> |
43 | #include <stdio.h> |
44 | |
45 | #include <set> |
46 | #include <string> |
47 | #include <vector> |
48 | |
49 | #include "common/module.h" |
50 | #include "common/dwarf/dwarf2reader.h" |
51 | #include "common/using_std_string.h" |
52 | |
53 | namespace google_breakpad { |
54 | |
55 | using google_breakpad::Module; |
56 | using std::set; |
57 | using std::vector; |
58 | |
59 | // A class that accepts parsed call frame information from the DWARF |
60 | // CFI parser and populates a google_breakpad::Module object with the |
61 | // contents. |
62 | class DwarfCFIToModule: public CallFrameInfo::Handler { |
63 | public: |
64 | |
65 | // DwarfCFIToModule uses an instance of this class to report errors |
66 | // detected while converting DWARF CFI to Breakpad STACK CFI records. |
67 | class Reporter { |
68 | public: |
69 | // Create a reporter that writes messages to the standard error |
70 | // stream. FILE is the name of the file we're processing, and |
71 | // SECTION is the name of the section within that file that we're |
72 | // looking at (.debug_frame, .eh_frame, etc.). |
73 | Reporter(const string& file, const string& section) |
74 | : file_(file), section_(section) { } |
75 | virtual ~Reporter() { } |
76 | |
77 | // The DWARF CFI entry at OFFSET cites register REG, but REG is not |
78 | // covered by the vector of register names passed to the |
79 | // DwarfCFIToModule constructor, nor does it match the return |
80 | // address column number for this entry. |
81 | virtual void UnnamedRegister(size_t offset, int reg); |
82 | |
83 | // The DWARF CFI entry at OFFSET says that REG is undefined, but the |
84 | // Breakpad symbol file format cannot express this. |
85 | virtual void UndefinedNotSupported(size_t offset, const string& reg); |
86 | |
87 | // The DWARF CFI entry at OFFSET says that REG uses a DWARF |
88 | // expression to find its value, but DwarfCFIToModule is not |
89 | // capable of translating DWARF expressions to Breakpad postfix |
90 | // expressions. |
91 | virtual void ExpressionsNotSupported(size_t offset, const string& reg); |
92 | |
93 | protected: |
94 | string file_, section_; |
95 | }; |
96 | |
97 | // Register name tables. If TABLE is a vector returned by one of these |
98 | // functions, then TABLE[R] is the name of the register numbered R in |
99 | // DWARF call frame information. |
100 | class RegisterNames { |
101 | public: |
102 | // Intel's "x86" or IA-32. |
103 | static vector<string> I386(); |
104 | |
105 | // AMD x86_64, AMD64, Intel EM64T, or Intel 64 |
106 | static vector<string> X86_64(); |
107 | |
108 | // ARM. |
109 | static vector<string> ARM(); |
110 | |
111 | // ARM64, aka AARCH64. |
112 | static vector<string> ARM64(); |
113 | |
114 | // MIPS. |
115 | static vector<string> MIPS(); |
116 | |
117 | private: |
118 | // Given STRINGS, an array of C strings with SIZE elements, return an |
119 | // equivalent vector<string>. |
120 | static vector<string> MakeVector(const char* const* strings, size_t size); |
121 | }; |
122 | |
123 | // Create a handler for the CallFrameInfo parser that |
124 | // records the stack unwinding information it receives in MODULE. |
125 | // |
126 | // Use REGISTER_NAMES[I] as the name of register number I; *this |
127 | // keeps a reference to the vector, so the vector should remain |
128 | // alive for as long as the DwarfCFIToModule does. |
129 | // |
130 | // Use REPORTER for reporting problems encountered in the conversion |
131 | // process. |
132 | DwarfCFIToModule(Module* module, const vector<string>& register_names, |
133 | Reporter* reporter) |
134 | : module_(module), register_names_(register_names), reporter_(reporter), |
135 | entry_(NULL), return_address_(-1), cfa_name_(".cfa" ), ra_name_(".ra" ) { |
136 | } |
137 | virtual ~DwarfCFIToModule() { delete entry_; } |
138 | |
139 | virtual bool Entry(size_t offset, uint64_t address, uint64_t length, |
140 | uint8_t version, const string& augmentation, |
141 | unsigned return_address); |
142 | virtual bool UndefinedRule(uint64_t address, int reg); |
143 | virtual bool SameValueRule(uint64_t address, int reg); |
144 | virtual bool OffsetRule(uint64_t address, int reg, |
145 | int base_register, long offset); |
146 | virtual bool ValOffsetRule(uint64_t address, int reg, |
147 | int base_register, long offset); |
148 | virtual bool RegisterRule(uint64_t address, int reg, int base_register); |
149 | virtual bool ExpressionRule(uint64_t address, int reg, |
150 | const string& expression); |
151 | virtual bool ValExpressionRule(uint64_t address, int reg, |
152 | const string& expression); |
153 | virtual bool End(); |
154 | |
155 | private: |
156 | // Return the name to use for register REG. |
157 | string RegisterName(int i); |
158 | |
159 | // Record RULE for register REG at ADDRESS. |
160 | void Record(Module::Address address, int reg, const string& rule); |
161 | |
162 | // The module to which we should add entries. |
163 | Module* module_; |
164 | |
165 | // Map from register numbers to register names. |
166 | const vector<string>& register_names_; |
167 | |
168 | // The reporter to use to report problems. |
169 | Reporter* reporter_; |
170 | |
171 | // The current entry we're constructing. |
172 | Module::StackFrameEntry* entry_; |
173 | |
174 | // The section offset of the current frame description entry, for |
175 | // use in error messages. |
176 | size_t entry_offset_; |
177 | |
178 | // The return address column for that entry. |
179 | unsigned return_address_; |
180 | |
181 | // The names of the return address and canonical frame address. Putting |
182 | // these here instead of using string literals allows us to share their |
183 | // texts in reference-counted string implementations (all the |
184 | // popular ones). Many, many rules cite these strings. |
185 | string cfa_name_, ra_name_; |
186 | |
187 | // A set of strings used by this CFI. Before storing a string in one of |
188 | // our data structures, insert it into this set, and then use the string |
189 | // from the set. |
190 | // |
191 | // Because string uses reference counting internally, simply using |
192 | // strings from this set, even if passed by value, assigned, or held |
193 | // directly in structures and containers (map<string, ...>, for example), |
194 | // causes those strings to share a single instance of each distinct piece |
195 | // of text. |
196 | set<string> common_strings_; |
197 | }; |
198 | |
199 | } // namespace google_breakpad |
200 | |
201 | #endif // COMMON_LINUX_DWARF_CFI_TO_MODULE_H |
202 | |