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