|
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 #include "common/unique_string.h" |
|
53 |
|
54 namespace google_breakpad { |
|
55 |
|
56 using dwarf2reader::CallFrameInfo; |
|
57 using google_breakpad::Module; |
|
58 using std::set; |
|
59 using std::vector; |
|
60 |
|
61 // A class that accepts parsed call frame information from the DWARF |
|
62 // CFI parser and populates a google_breakpad::Module object with the |
|
63 // contents. |
|
64 class DwarfCFIToModule: public CallFrameInfo::Handler { |
|
65 public: |
|
66 |
|
67 // DwarfCFIToModule uses an instance of this class to report errors |
|
68 // detected while converting DWARF CFI to Breakpad STACK CFI records. |
|
69 class Reporter { |
|
70 public: |
|
71 // Create a reporter that writes messages to the standard error |
|
72 // stream. FILE is the name of the file we're processing, and |
|
73 // SECTION is the name of the section within that file that we're |
|
74 // looking at (.debug_frame, .eh_frame, etc.). |
|
75 Reporter(const string &file, const string §ion) |
|
76 : file_(file), section_(section) { } |
|
77 virtual ~Reporter() { } |
|
78 |
|
79 // The DWARF CFI entry at OFFSET cites register REG, but REG is not |
|
80 // covered by the vector of register names passed to the |
|
81 // DwarfCFIToModule constructor, nor does it match the return |
|
82 // address column number for this entry. |
|
83 virtual void UnnamedRegister(size_t offset, int reg); |
|
84 |
|
85 // The DWARF CFI entry at OFFSET says that REG is undefined, but the |
|
86 // Breakpad symbol file format cannot express this. |
|
87 virtual void UndefinedNotSupported(size_t offset, |
|
88 const UniqueString* reg); |
|
89 |
|
90 // The DWARF CFI entry at OFFSET says that REG uses a DWARF |
|
91 // expression to find its value, but DwarfCFIToModule is not |
|
92 // capable of translating DWARF expressions to Breakpad postfix |
|
93 // expressions. |
|
94 virtual void ExpressionsNotSupported(size_t offset, |
|
95 const UniqueString* reg); |
|
96 |
|
97 protected: |
|
98 string file_, section_; |
|
99 }; |
|
100 |
|
101 // Register name tables. If TABLE is a vector returned by one of these |
|
102 // functions, then TABLE[R] is the name of the register numbered R in |
|
103 // DWARF call frame information. |
|
104 class RegisterNames { |
|
105 public: |
|
106 // Intel's "x86" or IA-32. |
|
107 static vector<const UniqueString*> I386(); |
|
108 |
|
109 // AMD x86_64, AMD64, Intel EM64T, or Intel 64 |
|
110 static vector<const UniqueString*> X86_64(); |
|
111 |
|
112 // ARM. |
|
113 static vector<const UniqueString*> ARM(); |
|
114 |
|
115 private: |
|
116 // Given STRINGS, an array of C strings with SIZE elements, return an |
|
117 // equivalent vector<string>. |
|
118 static vector<const UniqueString*> MakeVector(const char * const *strings, |
|
119 size_t size); |
|
120 }; |
|
121 |
|
122 // Create a handler for the dwarf2reader::CallFrameInfo parser that |
|
123 // records the stack unwinding information it receives in MODULE. |
|
124 // |
|
125 // Use REGISTER_NAMES[I] as the name of register number I; *this |
|
126 // keeps a reference to the vector, so the vector should remain |
|
127 // alive for as long as the DwarfCFIToModule does. |
|
128 // |
|
129 // Use REPORTER for reporting problems encountered in the conversion |
|
130 // process. |
|
131 DwarfCFIToModule(Module *module, |
|
132 const vector<const UniqueString*> ®ister_names, |
|
133 Reporter *reporter) |
|
134 : module_(module), register_names_(register_names), reporter_(reporter), |
|
135 entry_(NULL), return_address_(-1) { |
|
136 } |
|
137 virtual ~DwarfCFIToModule() { delete entry_; } |
|
138 |
|
139 virtual bool Entry(size_t offset, uint64 address, uint64 length, |
|
140 uint8 version, const string &augmentation, |
|
141 unsigned return_address); |
|
142 virtual bool UndefinedRule(uint64 address, int reg); |
|
143 virtual bool SameValueRule(uint64 address, int reg); |
|
144 virtual bool OffsetRule(uint64 address, int reg, |
|
145 int base_register, long offset); |
|
146 virtual bool ValOffsetRule(uint64 address, int reg, |
|
147 int base_register, long offset); |
|
148 virtual bool RegisterRule(uint64 address, int reg, int base_register); |
|
149 virtual bool ExpressionRule(uint64 address, int reg, |
|
150 const string &expression); |
|
151 virtual bool ValExpressionRule(uint64 address, int reg, |
|
152 const string &expression); |
|
153 virtual bool End(); |
|
154 |
|
155 private: |
|
156 // Return the name to use for register REG. |
|
157 const UniqueString* RegisterName(int i); |
|
158 |
|
159 // Record RULE for register REG at ADDRESS. |
|
160 void Record(Module::Address address, int reg, const Module::Expr &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<const UniqueString*> ®ister_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 |
|
182 } // namespace google_breakpad |
|
183 |
|
184 #endif // COMMON_LINUX_DWARF_CFI_TO_MODULE_H |