OLD | NEW |
| (Empty) |
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 // Add DWARF debugging information to a Breakpad symbol file. This | |
35 // file defines the DwarfCUToModule class, which accepts parsed DWARF | |
36 // data and populates a google_breakpad::Module with the results; the | |
37 // Module can then write its contents as a Breakpad symbol file. | |
38 | |
39 #ifndef COMMON_LINUX_DWARF_CU_TO_MODULE_H__ | |
40 #define COMMON_LINUX_DWARF_CU_TO_MODULE_H__ | |
41 | |
42 #include <string> | |
43 | |
44 //#include <elf.h> | |
45 //#include <link.h> | |
46 #include "common/language.h" | |
47 #include "common/module.h" | |
48 #include "common/dwarf/bytereader.h" | |
49 #include "common/dwarf/dwarf2diehandler.h" | |
50 #include "common/dwarf/dwarf2reader.h" | |
51 | |
52 namespace google_breakpad { | |
53 | |
54 using dwarf2reader::AttributeList; | |
55 using dwarf2reader::DwarfAttribute; | |
56 using dwarf2reader::DwarfForm; | |
57 using dwarf2reader::DwarfLanguage; | |
58 using dwarf2reader::DwarfTag; | |
59 | |
60 // Populate a google_breakpad::Module with DWARF debugging information. | |
61 // | |
62 // An instance of this class can be provided as a handler to a | |
63 // dwarf2reader::CompilationUnit DWARF parser. The handler uses the | |
64 // results of parsing to populate a google_breakpad::Module with | |
65 // source file, function, and source line information. | |
66 class DwarfCUToModule: public dwarf2reader::RootDIEHandler { | |
67 | |
68 struct FilePrivate; | |
69 public: | |
70 | |
71 // Information global to the DWARF-bearing file we are processing, | |
72 // for use by DwarfCUToModule. Each DwarfCUToModule instance deals | |
73 // with a single compilation unit within the file, but information | |
74 // global to the whole file is held here. The client is responsible | |
75 // for filling it in appropriately (except for the 'file_private' | |
76 // field, which the constructor and destructor take care of), and | |
77 // then providing it to the DwarfCUToModule instance for each | |
78 // compilation unit we process in that file. | |
79 struct FileContext { | |
80 FileContext(const string &filename_arg, Module *module_arg); | |
81 ~FileContext(); | |
82 | |
83 // The name of this file, for use in error messages. | |
84 string filename; | |
85 | |
86 // A map of this file's sections, used for finding other DWARF | |
87 // sections that the .debug_info section may refer to. | |
88 dwarf2reader::SectionMap section_map; | |
89 | |
90 // The Module to which we're contributing definitions. | |
91 Module *module; | |
92 | |
93 // Inter-compilation unit data used internally by the handlers. | |
94 FilePrivate *file_private; | |
95 }; | |
96 | |
97 // An abstract base class for functors that handle DWARF line data | |
98 // for DwarfCUToModule. DwarfCUToModule could certainly just use | |
99 // dwarf2reader::LineInfo itself directly, but decoupling things | |
100 // this way makes unit testing a little easier. | |
101 class LineToModuleFunctor { | |
102 public: | |
103 LineToModuleFunctor() { } | |
104 virtual ~LineToModuleFunctor() { } | |
105 | |
106 // Populate MODULE and LINES with source file names and code/line | |
107 // mappings, given a pointer to some DWARF line number data | |
108 // PROGRAM, and an overestimate of its size. Add no zero-length | |
109 // lines to LINES. | |
110 virtual void operator()(const char *program, uint64 length, | |
111 Module *module, vector<Module::Line> *lines) = 0; | |
112 }; | |
113 | |
114 // The interface DwarfCUToModule uses to report warnings. The member | |
115 // function definitions for this class write messages to stderr, but | |
116 // you can override them if you'd like to detect or report these | |
117 // conditions yourself. | |
118 class WarningReporter { | |
119 public: | |
120 // Warn about problems in the DWARF file FILENAME, in the | |
121 // compilation unit at OFFSET. | |
122 WarningReporter(const string &filename, uint64 cu_offset) | |
123 : filename_(filename), cu_offset_(cu_offset), printed_cu_header_(false), | |
124 printed_unpaired_header_(false), | |
125 uncovered_warnings_enabled_(false) { } | |
126 virtual ~WarningReporter() { } | |
127 | |
128 // Set the name of the compilation unit we're processing to NAME. | |
129 virtual void SetCUName(const string &name) { cu_name_ = name; } | |
130 | |
131 // Accessor and setter for uncovered_warnings_enabled_. | |
132 // UncoveredFunction and UncoveredLine only report a problem if that is | |
133 // true. By default, these warnings are disabled, because those | |
134 // conditions occur occasionally in healthy code. | |
135 virtual bool uncovered_warnings_enabled() const { | |
136 return uncovered_warnings_enabled_; | |
137 } | |
138 virtual void set_uncovered_warnings_enabled(bool value) { | |
139 uncovered_warnings_enabled_ = value; | |
140 } | |
141 | |
142 // A DW_AT_specification in the DIE at OFFSET refers to a DIE we | |
143 // haven't processed yet, or that wasn't marked as a declaration, | |
144 // at TARGET. | |
145 virtual void UnknownSpecification(uint64 offset, uint64 target); | |
146 | |
147 // A DW_AT_abstract_origin in the DIE at OFFSET refers to a DIE we | |
148 // haven't processed yet, or that wasn't marked as inline, at TARGET. | |
149 virtual void UnknownAbstractOrigin(uint64 offset, uint64 target); | |
150 | |
151 // We were unable to find the DWARF section named SECTION_NAME. | |
152 virtual void MissingSection(const string §ion_name); | |
153 | |
154 // The CU's DW_AT_stmt_list offset OFFSET is bogus. | |
155 virtual void BadLineInfoOffset(uint64 offset); | |
156 | |
157 // FUNCTION includes code covered by no line number data. | |
158 virtual void UncoveredFunction(const Module::Function &function); | |
159 | |
160 // Line number NUMBER in LINE_FILE, of length LENGTH, includes code | |
161 // covered by no function. | |
162 virtual void UncoveredLine(const Module::Line &line); | |
163 | |
164 protected: | |
165 string filename_; | |
166 uint64 cu_offset_; | |
167 string cu_name_; | |
168 bool printed_cu_header_; | |
169 bool printed_unpaired_header_; | |
170 bool uncovered_warnings_enabled_; | |
171 | |
172 private: | |
173 // Print a per-CU heading, once. | |
174 void CUHeading(); | |
175 // Print an unpaired function/line heading, once. | |
176 void UncoveredHeading(); | |
177 }; | |
178 | |
179 // Create a DWARF debugging info handler for a compilation unit | |
180 // within FILE_CONTEXT. This uses information received from the | |
181 // dwarf2reader::CompilationUnit DWARF parser to populate | |
182 // FILE_CONTEXT->module. Use LINE_READER to handle the compilation | |
183 // unit's line number data. Use REPORTER to report problems with the | |
184 // data we find. | |
185 DwarfCUToModule(FileContext *file_context, | |
186 LineToModuleFunctor *line_reader, | |
187 WarningReporter *reporter); | |
188 ~DwarfCUToModule(); | |
189 | |
190 void ProcessAttributeSigned(enum DwarfAttribute attr, | |
191 enum DwarfForm form, | |
192 int64 data); | |
193 void ProcessAttributeUnsigned(enum DwarfAttribute attr, | |
194 enum DwarfForm form, | |
195 uint64 data); | |
196 void ProcessAttributeString(enum DwarfAttribute attr, | |
197 enum DwarfForm form, | |
198 const string &data); | |
199 bool EndAttributes(); | |
200 DIEHandler *FindChildHandler(uint64 offset, enum DwarfTag tag, | |
201 const AttributeList &attrs); | |
202 | |
203 // Assign all our source Lines to the Functions that cover their | |
204 // addresses, and then add them to module_. | |
205 void Finish(); | |
206 | |
207 bool StartCompilationUnit(uint64 offset, uint8 address_size, | |
208 uint8 offset_size, uint64 cu_length, | |
209 uint8 dwarf_version); | |
210 bool StartRootDIE(uint64 offset, enum DwarfTag tag, | |
211 const AttributeList& attrs); | |
212 | |
213 private: | |
214 | |
215 // Used internally by the handler. Full definitions are in | |
216 // dwarf_cu_to_module.cc. | |
217 struct Specification; | |
218 struct CUContext; | |
219 struct DIEContext; | |
220 class GenericDIEHandler; | |
221 class FuncHandler; | |
222 class NamedScopeHandler; | |
223 | |
224 // A map from section offsets to specifications. | |
225 typedef map<uint64, Specification> SpecificationByOffset; | |
226 | |
227 // Set this compilation unit's source language to LANGUAGE. | |
228 void SetLanguage(DwarfLanguage language); | |
229 | |
230 // Read source line information at OFFSET in the .debug_line | |
231 // section. Record source files in module_, but record source lines | |
232 // in lines_; we apportion them to functions in | |
233 // AssignLinesToFunctions. | |
234 void ReadSourceLines(uint64 offset); | |
235 | |
236 // Assign the lines in lines_ to the individual line lists of the | |
237 // functions in functions_. (DWARF line information maps an entire | |
238 // compilation unit at a time, and gives no indication of which | |
239 // lines belong to which functions, beyond their addresses.) | |
240 void AssignLinesToFunctions(); | |
241 | |
242 // The only reason cu_context_ and child_context_ are pointers is | |
243 // that we want to keep their definitions private to | |
244 // dwarf_cu_to_module.cc, instead of listing them all here. They are | |
245 // owned by this DwarfCUToModule: the constructor sets them, and the | |
246 // destructor deletes them. | |
247 | |
248 // The functor to use to handle line number data. | |
249 LineToModuleFunctor *line_reader_; | |
250 | |
251 // This compilation unit's context. | |
252 CUContext *cu_context_; | |
253 | |
254 // A context for our children. | |
255 DIEContext *child_context_; | |
256 | |
257 // True if this compilation unit has source line information. | |
258 bool has_source_line_info_; | |
259 | |
260 // The offset of this compilation unit's line number information in | |
261 // the .debug_line section. | |
262 uint64 source_line_offset_; | |
263 | |
264 // The line numbers we have seen thus far. We accumulate these here | |
265 // during parsing. Then, in Finish, we call AssignLinesToFunctions | |
266 // to dole them out to the appropriate functions. | |
267 vector<Module::Line> lines_; | |
268 }; | |
269 | |
270 } // namespace google_breakpad | |
271 | |
272 #endif // COMMON_LINUX_DWARF_CU_TO_MODULE_H__ | |
OLD | NEW |