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: // dwarf_cfi_to_module.h: Define the DwarfCFIToModule class, which michael@0: // accepts parsed DWARF call frame info and adds it to a michael@0: // google_breakpad::Module object, which can write that information to michael@0: // a Breakpad symbol file. michael@0: michael@0: #ifndef COMMON_LINUX_DWARF_CFI_TO_MODULE_H michael@0: #define COMMON_LINUX_DWARF_CFI_TO_MODULE_H michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "common/module.h" michael@0: #include "common/dwarf/dwarf2reader.h" michael@0: #include "common/using_std_string.h" michael@0: #include "common/unique_string.h" michael@0: michael@0: namespace google_breakpad { michael@0: michael@0: using dwarf2reader::CallFrameInfo; michael@0: using google_breakpad::Module; michael@0: using std::set; michael@0: using std::vector; michael@0: michael@0: // A class that accepts parsed call frame information from the DWARF michael@0: // CFI parser and populates a google_breakpad::Module object with the michael@0: // contents. michael@0: class DwarfCFIToModule: public CallFrameInfo::Handler { michael@0: public: michael@0: michael@0: // DwarfCFIToModule uses an instance of this class to report errors michael@0: // detected while converting DWARF CFI to Breakpad STACK CFI records. michael@0: class Reporter { michael@0: public: michael@0: // Create a reporter that writes messages to the standard error michael@0: // stream. FILE is the name of the file we're processing, and michael@0: // SECTION is the name of the section within that file that we're michael@0: // looking at (.debug_frame, .eh_frame, etc.). michael@0: Reporter(const string &file, const string §ion) michael@0: : file_(file), section_(section) { } michael@0: virtual ~Reporter() { } michael@0: michael@0: // The DWARF CFI entry at OFFSET cites register REG, but REG is not michael@0: // covered by the vector of register names passed to the michael@0: // DwarfCFIToModule constructor, nor does it match the return michael@0: // address column number for this entry. michael@0: virtual void UnnamedRegister(size_t offset, int reg); michael@0: michael@0: // The DWARF CFI entry at OFFSET says that REG is undefined, but the michael@0: // Breakpad symbol file format cannot express this. michael@0: virtual void UndefinedNotSupported(size_t offset, michael@0: const UniqueString* reg); michael@0: michael@0: // The DWARF CFI entry at OFFSET says that REG uses a DWARF michael@0: // expression to find its value, but DwarfCFIToModule is not michael@0: // capable of translating DWARF expressions to Breakpad postfix michael@0: // expressions. michael@0: virtual void ExpressionsNotSupported(size_t offset, michael@0: const UniqueString* reg); michael@0: michael@0: protected: michael@0: string file_, section_; michael@0: }; michael@0: michael@0: // Register name tables. If TABLE is a vector returned by one of these michael@0: // functions, then TABLE[R] is the name of the register numbered R in michael@0: // DWARF call frame information. michael@0: class RegisterNames { michael@0: public: michael@0: // Intel's "x86" or IA-32. michael@0: static vector I386(); michael@0: michael@0: // AMD x86_64, AMD64, Intel EM64T, or Intel 64 michael@0: static vector X86_64(); michael@0: michael@0: // ARM. michael@0: static vector ARM(); michael@0: michael@0: private: michael@0: // Given STRINGS, an array of C strings with SIZE elements, return an michael@0: // equivalent vector. michael@0: static vector MakeVector(const char * const *strings, michael@0: size_t size); michael@0: }; michael@0: michael@0: // Create a handler for the dwarf2reader::CallFrameInfo parser that michael@0: // records the stack unwinding information it receives in MODULE. michael@0: // michael@0: // Use REGISTER_NAMES[I] as the name of register number I; *this michael@0: // keeps a reference to the vector, so the vector should remain michael@0: // alive for as long as the DwarfCFIToModule does. michael@0: // michael@0: // Use REPORTER for reporting problems encountered in the conversion michael@0: // process. michael@0: DwarfCFIToModule(Module *module, michael@0: const vector ®ister_names, michael@0: Reporter *reporter) michael@0: : module_(module), register_names_(register_names), reporter_(reporter), michael@0: entry_(NULL), return_address_(-1) { michael@0: } michael@0: virtual ~DwarfCFIToModule() { delete entry_; } michael@0: michael@0: virtual bool Entry(size_t offset, uint64 address, uint64 length, michael@0: uint8 version, const string &augmentation, michael@0: unsigned return_address); michael@0: virtual bool UndefinedRule(uint64 address, int reg); michael@0: virtual bool SameValueRule(uint64 address, int reg); michael@0: virtual bool OffsetRule(uint64 address, int reg, michael@0: int base_register, long offset); michael@0: virtual bool ValOffsetRule(uint64 address, int reg, michael@0: int base_register, long offset); michael@0: virtual bool RegisterRule(uint64 address, int reg, int base_register); michael@0: virtual bool ExpressionRule(uint64 address, int reg, michael@0: const string &expression); michael@0: virtual bool ValExpressionRule(uint64 address, int reg, michael@0: const string &expression); michael@0: virtual bool End(); michael@0: michael@0: private: michael@0: // Return the name to use for register REG. michael@0: const UniqueString* RegisterName(int i); michael@0: michael@0: // Record RULE for register REG at ADDRESS. michael@0: void Record(Module::Address address, int reg, const Module::Expr &rule); michael@0: michael@0: // The module to which we should add entries. michael@0: Module *module_; michael@0: michael@0: // Map from register numbers to register names. michael@0: const vector ®ister_names_; michael@0: michael@0: // The reporter to use to report problems. michael@0: Reporter *reporter_; michael@0: michael@0: // The current entry we're constructing. michael@0: Module::StackFrameEntry *entry_; michael@0: michael@0: // The section offset of the current frame description entry, for michael@0: // use in error messages. michael@0: size_t entry_offset_; michael@0: michael@0: // The return address column for that entry. michael@0: unsigned return_address_; michael@0: }; michael@0: michael@0: } // namespace google_breakpad michael@0: michael@0: #endif // COMMON_LINUX_DWARF_CFI_TO_MODULE_H