michael@0: // -*- mode: c++ -*- michael@0: michael@0: // Copyright (c) 2010 Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // Original author: Jim Blandy michael@0: michael@0: // Add DWARF debugging information to a Breakpad symbol file. This michael@0: // file defines the DwarfCUToModule class, which accepts parsed DWARF michael@0: // data and populates a google_breakpad::Module with the results; the michael@0: // Module can then write its contents as a Breakpad symbol file. michael@0: michael@0: #ifndef COMMON_LINUX_DWARF_CU_TO_MODULE_H__ michael@0: #define COMMON_LINUX_DWARF_CU_TO_MODULE_H__ michael@0: michael@0: #include michael@0: michael@0: #include "common/language.h" michael@0: #include "common/module.h" michael@0: #include "common/dwarf/bytereader.h" michael@0: #include "common/dwarf/dwarf2diehandler.h" michael@0: #include "common/dwarf/dwarf2reader.h" michael@0: #include "common/using_std_string.h" michael@0: michael@0: namespace google_breakpad { michael@0: michael@0: using dwarf2reader::DwarfAttribute; michael@0: using dwarf2reader::DwarfForm; michael@0: using dwarf2reader::DwarfLanguage; michael@0: using dwarf2reader::DwarfTag; michael@0: michael@0: // Populate a google_breakpad::Module with DWARF debugging information. michael@0: // michael@0: // An instance of this class can be provided as a handler to a michael@0: // dwarf2reader::DIEDispatcher, which can in turn be a handler for a michael@0: // dwarf2reader::CompilationUnit DWARF parser. The handler uses the results michael@0: // of parsing to populate a google_breakpad::Module with source file, michael@0: // function, and source line information. michael@0: class DwarfCUToModule: public dwarf2reader::RootDIEHandler { michael@0: struct FilePrivate; michael@0: public: michael@0: michael@0: // Information global to the DWARF-bearing file we are processing, michael@0: // for use by DwarfCUToModule. Each DwarfCUToModule instance deals michael@0: // with a single compilation unit within the file, but information michael@0: // global to the whole file is held here. The client is responsible michael@0: // for filling it in appropriately (except for the 'file_private' michael@0: // field, which the constructor and destructor take care of), and michael@0: // then providing it to the DwarfCUToModule instance for each michael@0: // compilation unit we process in that file. michael@0: struct FileContext { michael@0: FileContext(const string &filename_arg, Module *module_arg); michael@0: ~FileContext(); michael@0: michael@0: // The name of this file, for use in error messages. michael@0: string filename; michael@0: michael@0: // A map of this file's sections, used for finding other DWARF michael@0: // sections that the .debug_info section may refer to. michael@0: dwarf2reader::SectionMap section_map; michael@0: michael@0: // The Module to which we're contributing definitions. michael@0: Module *module; michael@0: michael@0: // Inter-compilation unit data used internally by the handlers. michael@0: FilePrivate *file_private; michael@0: }; michael@0: michael@0: // An abstract base class for handlers that handle DWARF line data michael@0: // for DwarfCUToModule. DwarfCUToModule could certainly just use michael@0: // dwarf2reader::LineInfo itself directly, but decoupling things michael@0: // this way makes unit testing a little easier. michael@0: class LineToModuleHandler { michael@0: public: michael@0: LineToModuleHandler() { } michael@0: virtual ~LineToModuleHandler() { } michael@0: michael@0: // Called at the beginning of a new compilation unit, prior to calling michael@0: // ReadProgram(). compilation_dir will indicate the path that the michael@0: // current compilation unit was compiled in, consistent with the michael@0: // DW_AT_comp_dir DIE. michael@0: virtual void StartCompilationUnit(const string& compilation_dir) = 0; michael@0: michael@0: // Populate MODULE and LINES with source file names and code/line michael@0: // mappings, given a pointer to some DWARF line number data michael@0: // PROGRAM, and an overestimate of its size. Add no zero-length michael@0: // lines to LINES. michael@0: virtual void ReadProgram(const char *program, uint64 length, michael@0: Module *module, vector *lines) = 0; michael@0: }; michael@0: michael@0: // The interface DwarfCUToModule uses to report warnings. The member michael@0: // function definitions for this class write messages to stderr, but michael@0: // you can override them if you'd like to detect or report these michael@0: // conditions yourself. michael@0: class WarningReporter { michael@0: public: michael@0: // Warn about problems in the DWARF file FILENAME, in the michael@0: // compilation unit at OFFSET. michael@0: WarningReporter(const string &filename, uint64 cu_offset) michael@0: : filename_(filename), cu_offset_(cu_offset), printed_cu_header_(false), michael@0: printed_unpaired_header_(false), michael@0: uncovered_warnings_enabled_(false) { } michael@0: virtual ~WarningReporter() { } michael@0: michael@0: // Set the name of the compilation unit we're processing to NAME. michael@0: virtual void SetCUName(const string &name) { cu_name_ = name; } michael@0: michael@0: // Accessor and setter for uncovered_warnings_enabled_. michael@0: // UncoveredFunction and UncoveredLine only report a problem if that is michael@0: // true. By default, these warnings are disabled, because those michael@0: // conditions occur occasionally in healthy code. michael@0: virtual bool uncovered_warnings_enabled() const { michael@0: return uncovered_warnings_enabled_; michael@0: } michael@0: virtual void set_uncovered_warnings_enabled(bool value) { michael@0: uncovered_warnings_enabled_ = value; michael@0: } michael@0: michael@0: // A DW_AT_specification in the DIE at OFFSET refers to a DIE we michael@0: // haven't processed yet, or that wasn't marked as a declaration, michael@0: // at TARGET. michael@0: virtual void UnknownSpecification(uint64 offset, uint64 target); michael@0: michael@0: // A DW_AT_abstract_origin in the DIE at OFFSET refers to a DIE we michael@0: // haven't processed yet, or that wasn't marked as inline, at TARGET. michael@0: virtual void UnknownAbstractOrigin(uint64 offset, uint64 target); michael@0: michael@0: // We were unable to find the DWARF section named SECTION_NAME. michael@0: virtual void MissingSection(const string §ion_name); michael@0: michael@0: // The CU's DW_AT_stmt_list offset OFFSET is bogus. michael@0: virtual void BadLineInfoOffset(uint64 offset); michael@0: michael@0: // FUNCTION includes code covered by no line number data. michael@0: virtual void UncoveredFunction(const Module::Function &function); michael@0: michael@0: // Line number NUMBER in LINE_FILE, of length LENGTH, includes code michael@0: // covered by no function. michael@0: virtual void UncoveredLine(const Module::Line &line); michael@0: michael@0: // The DW_TAG_subprogram DIE at OFFSET has no name specified directly michael@0: // in the DIE, nor via a DW_AT_specification or DW_AT_abstract_origin michael@0: // link. michael@0: virtual void UnnamedFunction(uint64 offset); michael@0: michael@0: protected: michael@0: string filename_; michael@0: uint64 cu_offset_; michael@0: string cu_name_; michael@0: bool printed_cu_header_; michael@0: bool printed_unpaired_header_; michael@0: bool uncovered_warnings_enabled_; michael@0: michael@0: private: michael@0: // Print a per-CU heading, once. michael@0: void CUHeading(); michael@0: // Print an unpaired function/line heading, once. michael@0: void UncoveredHeading(); michael@0: }; michael@0: michael@0: // Create a DWARF debugging info handler for a compilation unit michael@0: // within FILE_CONTEXT. This uses information received from the michael@0: // dwarf2reader::CompilationUnit DWARF parser to populate michael@0: // FILE_CONTEXT->module. Use LINE_READER to handle the compilation michael@0: // unit's line number data. Use REPORTER to report problems with the michael@0: // data we find. michael@0: DwarfCUToModule(FileContext *file_context, michael@0: LineToModuleHandler *line_reader, michael@0: WarningReporter *reporter); michael@0: ~DwarfCUToModule(); michael@0: michael@0: void ProcessAttributeSigned(enum DwarfAttribute attr, michael@0: enum DwarfForm form, michael@0: int64 data); michael@0: void ProcessAttributeUnsigned(enum DwarfAttribute attr, michael@0: enum DwarfForm form, michael@0: uint64 data); michael@0: void ProcessAttributeString(enum DwarfAttribute attr, michael@0: enum DwarfForm form, michael@0: const string &data); michael@0: bool EndAttributes(); michael@0: DIEHandler *FindChildHandler(uint64 offset, enum DwarfTag tag); michael@0: michael@0: // Assign all our source Lines to the Functions that cover their michael@0: // addresses, and then add them to module_. michael@0: void Finish(); michael@0: michael@0: bool StartCompilationUnit(uint64 offset, uint8 address_size, michael@0: uint8 offset_size, uint64 cu_length, michael@0: uint8 dwarf_version); michael@0: bool StartRootDIE(uint64 offset, enum DwarfTag tag); michael@0: michael@0: private: michael@0: michael@0: // Used internally by the handler. Full definitions are in michael@0: // dwarf_cu_to_module.cc. michael@0: struct FilePrivate; michael@0: struct Specification; michael@0: struct CUContext; michael@0: struct DIEContext; michael@0: class GenericDIEHandler; michael@0: class FuncHandler; michael@0: class NamedScopeHandler; michael@0: michael@0: // A map from section offsets to specifications. michael@0: typedef map SpecificationByOffset; michael@0: michael@0: // Set this compilation unit's source language to LANGUAGE. michael@0: void SetLanguage(DwarfLanguage language); michael@0: michael@0: // Read source line information at OFFSET in the .debug_line michael@0: // section. Record source files in module_, but record source lines michael@0: // in lines_; we apportion them to functions in michael@0: // AssignLinesToFunctions. michael@0: void ReadSourceLines(uint64 offset); michael@0: michael@0: // Assign the lines in lines_ to the individual line lists of the michael@0: // functions in functions_. (DWARF line information maps an entire michael@0: // compilation unit at a time, and gives no indication of which michael@0: // lines belong to which functions, beyond their addresses.) michael@0: void AssignLinesToFunctions(); michael@0: michael@0: // The only reason cu_context_ and child_context_ are pointers is michael@0: // that we want to keep their definitions private to michael@0: // dwarf_cu_to_module.cc, instead of listing them all here. They are michael@0: // owned by this DwarfCUToModule: the constructor sets them, and the michael@0: // destructor deletes them. michael@0: michael@0: // The handler to use to handle line number data. michael@0: LineToModuleHandler *line_reader_; michael@0: michael@0: // This compilation unit's context. michael@0: CUContext *cu_context_; michael@0: michael@0: // A context for our children. michael@0: DIEContext *child_context_; michael@0: michael@0: // True if this compilation unit has source line information. michael@0: bool has_source_line_info_; michael@0: michael@0: // The offset of this compilation unit's line number information in michael@0: // the .debug_line section. michael@0: uint64 source_line_offset_; michael@0: michael@0: // The line numbers we have seen thus far. We accumulate these here michael@0: // during parsing. Then, in Finish, we call AssignLinesToFunctions michael@0: // to dole them out to the appropriate functions. michael@0: vector lines_; michael@0: }; michael@0: michael@0: } // namespace google_breakpad michael@0: michael@0: #endif // COMMON_LINUX_DWARF_CU_TO_MODULE_H__