Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // -*- mode: c++ -*- |
michael@0 | 2 | |
michael@0 | 3 | // Copyright (c) 2010 Google Inc. |
michael@0 | 4 | // All rights reserved. |
michael@0 | 5 | // |
michael@0 | 6 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 7 | // modification, are permitted provided that the following conditions are |
michael@0 | 8 | // met: |
michael@0 | 9 | // |
michael@0 | 10 | // * Redistributions of source code must retain the above copyright |
michael@0 | 11 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 12 | // * Redistributions in binary form must reproduce the above |
michael@0 | 13 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 14 | // in the documentation and/or other materials provided with the |
michael@0 | 15 | // distribution. |
michael@0 | 16 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 17 | // contributors may be used to endorse or promote products derived from |
michael@0 | 18 | // this software without specific prior written permission. |
michael@0 | 19 | // |
michael@0 | 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 31 | |
michael@0 | 32 | // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> |
michael@0 | 33 | |
michael@0 | 34 | // Add DWARF debugging information to a Breakpad symbol file. This |
michael@0 | 35 | // file defines the DwarfCUToModule class, which accepts parsed DWARF |
michael@0 | 36 | // data and populates a google_breakpad::Module with the results; the |
michael@0 | 37 | // Module can then write its contents as a Breakpad symbol file. |
michael@0 | 38 | |
michael@0 | 39 | #ifndef COMMON_LINUX_DWARF_CU_TO_MODULE_H__ |
michael@0 | 40 | #define COMMON_LINUX_DWARF_CU_TO_MODULE_H__ |
michael@0 | 41 | |
michael@0 | 42 | #include <string> |
michael@0 | 43 | |
michael@0 | 44 | #include "common/language.h" |
michael@0 | 45 | #include "common/module.h" |
michael@0 | 46 | #include "common/dwarf/bytereader.h" |
michael@0 | 47 | #include "common/dwarf/dwarf2diehandler.h" |
michael@0 | 48 | #include "common/dwarf/dwarf2reader.h" |
michael@0 | 49 | #include "common/using_std_string.h" |
michael@0 | 50 | |
michael@0 | 51 | namespace google_breakpad { |
michael@0 | 52 | |
michael@0 | 53 | using dwarf2reader::DwarfAttribute; |
michael@0 | 54 | using dwarf2reader::DwarfForm; |
michael@0 | 55 | using dwarf2reader::DwarfLanguage; |
michael@0 | 56 | using dwarf2reader::DwarfTag; |
michael@0 | 57 | |
michael@0 | 58 | // Populate a google_breakpad::Module with DWARF debugging information. |
michael@0 | 59 | // |
michael@0 | 60 | // An instance of this class can be provided as a handler to a |
michael@0 | 61 | // dwarf2reader::DIEDispatcher, which can in turn be a handler for a |
michael@0 | 62 | // dwarf2reader::CompilationUnit DWARF parser. The handler uses the results |
michael@0 | 63 | // of parsing to populate a google_breakpad::Module with source file, |
michael@0 | 64 | // function, and source line information. |
michael@0 | 65 | class DwarfCUToModule: public dwarf2reader::RootDIEHandler { |
michael@0 | 66 | struct FilePrivate; |
michael@0 | 67 | public: |
michael@0 | 68 | |
michael@0 | 69 | // Information global to the DWARF-bearing file we are processing, |
michael@0 | 70 | // for use by DwarfCUToModule. Each DwarfCUToModule instance deals |
michael@0 | 71 | // with a single compilation unit within the file, but information |
michael@0 | 72 | // global to the whole file is held here. The client is responsible |
michael@0 | 73 | // for filling it in appropriately (except for the 'file_private' |
michael@0 | 74 | // field, which the constructor and destructor take care of), and |
michael@0 | 75 | // then providing it to the DwarfCUToModule instance for each |
michael@0 | 76 | // compilation unit we process in that file. |
michael@0 | 77 | struct FileContext { |
michael@0 | 78 | FileContext(const string &filename_arg, Module *module_arg); |
michael@0 | 79 | ~FileContext(); |
michael@0 | 80 | |
michael@0 | 81 | // The name of this file, for use in error messages. |
michael@0 | 82 | string filename; |
michael@0 | 83 | |
michael@0 | 84 | // A map of this file's sections, used for finding other DWARF |
michael@0 | 85 | // sections that the .debug_info section may refer to. |
michael@0 | 86 | dwarf2reader::SectionMap section_map; |
michael@0 | 87 | |
michael@0 | 88 | // The Module to which we're contributing definitions. |
michael@0 | 89 | Module *module; |
michael@0 | 90 | |
michael@0 | 91 | // Inter-compilation unit data used internally by the handlers. |
michael@0 | 92 | FilePrivate *file_private; |
michael@0 | 93 | }; |
michael@0 | 94 | |
michael@0 | 95 | // An abstract base class for handlers that handle DWARF line data |
michael@0 | 96 | // for DwarfCUToModule. DwarfCUToModule could certainly just use |
michael@0 | 97 | // dwarf2reader::LineInfo itself directly, but decoupling things |
michael@0 | 98 | // this way makes unit testing a little easier. |
michael@0 | 99 | class LineToModuleHandler { |
michael@0 | 100 | public: |
michael@0 | 101 | LineToModuleHandler() { } |
michael@0 | 102 | virtual ~LineToModuleHandler() { } |
michael@0 | 103 | |
michael@0 | 104 | // Called at the beginning of a new compilation unit, prior to calling |
michael@0 | 105 | // ReadProgram(). compilation_dir will indicate the path that the |
michael@0 | 106 | // current compilation unit was compiled in, consistent with the |
michael@0 | 107 | // DW_AT_comp_dir DIE. |
michael@0 | 108 | virtual void StartCompilationUnit(const string& compilation_dir) = 0; |
michael@0 | 109 | |
michael@0 | 110 | // Populate MODULE and LINES with source file names and code/line |
michael@0 | 111 | // mappings, given a pointer to some DWARF line number data |
michael@0 | 112 | // PROGRAM, and an overestimate of its size. Add no zero-length |
michael@0 | 113 | // lines to LINES. |
michael@0 | 114 | virtual void ReadProgram(const char *program, uint64 length, |
michael@0 | 115 | Module *module, vector<Module::Line> *lines) = 0; |
michael@0 | 116 | }; |
michael@0 | 117 | |
michael@0 | 118 | // The interface DwarfCUToModule uses to report warnings. The member |
michael@0 | 119 | // function definitions for this class write messages to stderr, but |
michael@0 | 120 | // you can override them if you'd like to detect or report these |
michael@0 | 121 | // conditions yourself. |
michael@0 | 122 | class WarningReporter { |
michael@0 | 123 | public: |
michael@0 | 124 | // Warn about problems in the DWARF file FILENAME, in the |
michael@0 | 125 | // compilation unit at OFFSET. |
michael@0 | 126 | WarningReporter(const string &filename, uint64 cu_offset) |
michael@0 | 127 | : filename_(filename), cu_offset_(cu_offset), printed_cu_header_(false), |
michael@0 | 128 | printed_unpaired_header_(false), |
michael@0 | 129 | uncovered_warnings_enabled_(false) { } |
michael@0 | 130 | virtual ~WarningReporter() { } |
michael@0 | 131 | |
michael@0 | 132 | // Set the name of the compilation unit we're processing to NAME. |
michael@0 | 133 | virtual void SetCUName(const string &name) { cu_name_ = name; } |
michael@0 | 134 | |
michael@0 | 135 | // Accessor and setter for uncovered_warnings_enabled_. |
michael@0 | 136 | // UncoveredFunction and UncoveredLine only report a problem if that is |
michael@0 | 137 | // true. By default, these warnings are disabled, because those |
michael@0 | 138 | // conditions occur occasionally in healthy code. |
michael@0 | 139 | virtual bool uncovered_warnings_enabled() const { |
michael@0 | 140 | return uncovered_warnings_enabled_; |
michael@0 | 141 | } |
michael@0 | 142 | virtual void set_uncovered_warnings_enabled(bool value) { |
michael@0 | 143 | uncovered_warnings_enabled_ = value; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | // A DW_AT_specification in the DIE at OFFSET refers to a DIE we |
michael@0 | 147 | // haven't processed yet, or that wasn't marked as a declaration, |
michael@0 | 148 | // at TARGET. |
michael@0 | 149 | virtual void UnknownSpecification(uint64 offset, uint64 target); |
michael@0 | 150 | |
michael@0 | 151 | // A DW_AT_abstract_origin in the DIE at OFFSET refers to a DIE we |
michael@0 | 152 | // haven't processed yet, or that wasn't marked as inline, at TARGET. |
michael@0 | 153 | virtual void UnknownAbstractOrigin(uint64 offset, uint64 target); |
michael@0 | 154 | |
michael@0 | 155 | // We were unable to find the DWARF section named SECTION_NAME. |
michael@0 | 156 | virtual void MissingSection(const string §ion_name); |
michael@0 | 157 | |
michael@0 | 158 | // The CU's DW_AT_stmt_list offset OFFSET is bogus. |
michael@0 | 159 | virtual void BadLineInfoOffset(uint64 offset); |
michael@0 | 160 | |
michael@0 | 161 | // FUNCTION includes code covered by no line number data. |
michael@0 | 162 | virtual void UncoveredFunction(const Module::Function &function); |
michael@0 | 163 | |
michael@0 | 164 | // Line number NUMBER in LINE_FILE, of length LENGTH, includes code |
michael@0 | 165 | // covered by no function. |
michael@0 | 166 | virtual void UncoveredLine(const Module::Line &line); |
michael@0 | 167 | |
michael@0 | 168 | // The DW_TAG_subprogram DIE at OFFSET has no name specified directly |
michael@0 | 169 | // in the DIE, nor via a DW_AT_specification or DW_AT_abstract_origin |
michael@0 | 170 | // link. |
michael@0 | 171 | virtual void UnnamedFunction(uint64 offset); |
michael@0 | 172 | |
michael@0 | 173 | protected: |
michael@0 | 174 | string filename_; |
michael@0 | 175 | uint64 cu_offset_; |
michael@0 | 176 | string cu_name_; |
michael@0 | 177 | bool printed_cu_header_; |
michael@0 | 178 | bool printed_unpaired_header_; |
michael@0 | 179 | bool uncovered_warnings_enabled_; |
michael@0 | 180 | |
michael@0 | 181 | private: |
michael@0 | 182 | // Print a per-CU heading, once. |
michael@0 | 183 | void CUHeading(); |
michael@0 | 184 | // Print an unpaired function/line heading, once. |
michael@0 | 185 | void UncoveredHeading(); |
michael@0 | 186 | }; |
michael@0 | 187 | |
michael@0 | 188 | // Create a DWARF debugging info handler for a compilation unit |
michael@0 | 189 | // within FILE_CONTEXT. This uses information received from the |
michael@0 | 190 | // dwarf2reader::CompilationUnit DWARF parser to populate |
michael@0 | 191 | // FILE_CONTEXT->module. Use LINE_READER to handle the compilation |
michael@0 | 192 | // unit's line number data. Use REPORTER to report problems with the |
michael@0 | 193 | // data we find. |
michael@0 | 194 | DwarfCUToModule(FileContext *file_context, |
michael@0 | 195 | LineToModuleHandler *line_reader, |
michael@0 | 196 | WarningReporter *reporter); |
michael@0 | 197 | ~DwarfCUToModule(); |
michael@0 | 198 | |
michael@0 | 199 | void ProcessAttributeSigned(enum DwarfAttribute attr, |
michael@0 | 200 | enum DwarfForm form, |
michael@0 | 201 | int64 data); |
michael@0 | 202 | void ProcessAttributeUnsigned(enum DwarfAttribute attr, |
michael@0 | 203 | enum DwarfForm form, |
michael@0 | 204 | uint64 data); |
michael@0 | 205 | void ProcessAttributeString(enum DwarfAttribute attr, |
michael@0 | 206 | enum DwarfForm form, |
michael@0 | 207 | const string &data); |
michael@0 | 208 | bool EndAttributes(); |
michael@0 | 209 | DIEHandler *FindChildHandler(uint64 offset, enum DwarfTag tag); |
michael@0 | 210 | |
michael@0 | 211 | // Assign all our source Lines to the Functions that cover their |
michael@0 | 212 | // addresses, and then add them to module_. |
michael@0 | 213 | void Finish(); |
michael@0 | 214 | |
michael@0 | 215 | bool StartCompilationUnit(uint64 offset, uint8 address_size, |
michael@0 | 216 | uint8 offset_size, uint64 cu_length, |
michael@0 | 217 | uint8 dwarf_version); |
michael@0 | 218 | bool StartRootDIE(uint64 offset, enum DwarfTag tag); |
michael@0 | 219 | |
michael@0 | 220 | private: |
michael@0 | 221 | |
michael@0 | 222 | // Used internally by the handler. Full definitions are in |
michael@0 | 223 | // dwarf_cu_to_module.cc. |
michael@0 | 224 | struct FilePrivate; |
michael@0 | 225 | struct Specification; |
michael@0 | 226 | struct CUContext; |
michael@0 | 227 | struct DIEContext; |
michael@0 | 228 | class GenericDIEHandler; |
michael@0 | 229 | class FuncHandler; |
michael@0 | 230 | class NamedScopeHandler; |
michael@0 | 231 | |
michael@0 | 232 | // A map from section offsets to specifications. |
michael@0 | 233 | typedef map<uint64, Specification> SpecificationByOffset; |
michael@0 | 234 | |
michael@0 | 235 | // Set this compilation unit's source language to LANGUAGE. |
michael@0 | 236 | void SetLanguage(DwarfLanguage language); |
michael@0 | 237 | |
michael@0 | 238 | // Read source line information at OFFSET in the .debug_line |
michael@0 | 239 | // section. Record source files in module_, but record source lines |
michael@0 | 240 | // in lines_; we apportion them to functions in |
michael@0 | 241 | // AssignLinesToFunctions. |
michael@0 | 242 | void ReadSourceLines(uint64 offset); |
michael@0 | 243 | |
michael@0 | 244 | // Assign the lines in lines_ to the individual line lists of the |
michael@0 | 245 | // functions in functions_. (DWARF line information maps an entire |
michael@0 | 246 | // compilation unit at a time, and gives no indication of which |
michael@0 | 247 | // lines belong to which functions, beyond their addresses.) |
michael@0 | 248 | void AssignLinesToFunctions(); |
michael@0 | 249 | |
michael@0 | 250 | // The only reason cu_context_ and child_context_ are pointers is |
michael@0 | 251 | // that we want to keep their definitions private to |
michael@0 | 252 | // dwarf_cu_to_module.cc, instead of listing them all here. They are |
michael@0 | 253 | // owned by this DwarfCUToModule: the constructor sets them, and the |
michael@0 | 254 | // destructor deletes them. |
michael@0 | 255 | |
michael@0 | 256 | // The handler to use to handle line number data. |
michael@0 | 257 | LineToModuleHandler *line_reader_; |
michael@0 | 258 | |
michael@0 | 259 | // This compilation unit's context. |
michael@0 | 260 | CUContext *cu_context_; |
michael@0 | 261 | |
michael@0 | 262 | // A context for our children. |
michael@0 | 263 | DIEContext *child_context_; |
michael@0 | 264 | |
michael@0 | 265 | // True if this compilation unit has source line information. |
michael@0 | 266 | bool has_source_line_info_; |
michael@0 | 267 | |
michael@0 | 268 | // The offset of this compilation unit's line number information in |
michael@0 | 269 | // the .debug_line section. |
michael@0 | 270 | uint64 source_line_offset_; |
michael@0 | 271 | |
michael@0 | 272 | // The line numbers we have seen thus far. We accumulate these here |
michael@0 | 273 | // during parsing. Then, in Finish, we call AssignLinesToFunctions |
michael@0 | 274 | // to dole them out to the appropriate functions. |
michael@0 | 275 | vector<Module::Line> lines_; |
michael@0 | 276 | }; |
michael@0 | 277 | |
michael@0 | 278 | } // namespace google_breakpad |
michael@0 | 279 | |
michael@0 | 280 | #endif // COMMON_LINUX_DWARF_CU_TO_MODULE_H__ |