Home | History | Annotate | Download | only in dwarf
      1 // Copyright (c) 2010 Google Inc. All Rights Reserved.
      2 //
      3 // Redistribution and use in source and binary forms, with or without
      4 // modification, are permitted provided that the following conditions are
      5 // met:
      6 //
      7 //     * Redistributions of source code must retain the above copyright
      8 // notice, this list of conditions and the following disclaimer.
      9 //     * Redistributions in binary form must reproduce the above
     10 // copyright notice, this list of conditions and the following disclaimer
     11 // in the documentation and/or other materials provided with the
     12 // distribution.
     13 //     * Neither the name of Google Inc. nor the names of its
     14 // contributors may be used to endorse or promote products derived from
     15 // this software without specific prior written permission.
     16 //
     17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 
     29 
     30 // This file contains the definitions for a DWARF2/3 information
     31 // collector that uses the DWARF2/3 reader interface to build a mapping
     32 // of addresses to files, lines, and functions.
     33 
     34 #ifndef COMMON_DWARF_FUNCTIONINFO_H__
     35 #define COMMON_DWARF_FUNCTIONINFO_H__
     36 
     37 #include <map>
     38 #include <string>
     39 #include <utility>
     40 #include <vector>
     41 
     42 #include "common/dwarf/dwarf2reader.h"
     43 #include "common/using_std_string.h"
     44 
     45 
     46 namespace dwarf2reader {
     47 
     48 struct FunctionInfo {
     49   // Name of the function
     50   string name;
     51   // Mangled name of the function
     52   string mangled_name;
     53   // File containing this function
     54   string file;
     55   // Line number for start of function.
     56   uint32 line;
     57   // Beginning address for this function
     58   uint64 lowpc;
     59   // End address for this function.
     60   uint64 highpc;
     61 };
     62 
     63 struct SourceFileInfo {
     64   // Name of the source file name
     65   string name;
     66   // Low address of source file name
     67   uint64 lowpc;
     68 };
     69 
     70 typedef std::map<uint64, FunctionInfo*> FunctionMap;
     71 typedef std::map<uint64, std::pair<string, uint32> > LineMap;
     72 
     73 // This class is a basic line info handler that fills in the dirs,
     74 // file, and linemap passed into it with the data produced from the
     75 // LineInfoHandler.
     76 class CULineInfoHandler: public LineInfoHandler {
     77  public:
     78 
     79   //
     80   CULineInfoHandler(std::vector<SourceFileInfo>* files,
     81                     std::vector<string>* dirs,
     82                     LineMap* linemap);
     83   virtual ~CULineInfoHandler() { }
     84 
     85   // Called when we define a directory.  We just place NAME into dirs_
     86   // at position DIR_NUM.
     87   virtual void DefineDir(const string& name, uint32 dir_num);
     88 
     89   // Called when we define a filename.  We just place
     90   // concat(dirs_[DIR_NUM], NAME) into files_ at position FILE_NUM.
     91   virtual void DefineFile(const string& name, int32 file_num,
     92                           uint32 dir_num, uint64 mod_time, uint64 length);
     93 
     94 
     95   // Called when the line info reader has a new line, address pair
     96   // ready for us. ADDRESS is the address of the code, LENGTH is the
     97   // length of its machine code in bytes, FILE_NUM is the file number
     98   // containing the code, LINE_NUM is the line number in that file for
     99   // the code, and COLUMN_NUM is the column number the code starts at,
    100   // if we know it (0 otherwise).
    101   virtual void AddLine(uint64 address, uint64 length,
    102                        uint32 file_num, uint32 line_num, uint32 column_num);
    103 
    104  private:
    105   LineMap* linemap_;
    106   std::vector<SourceFileInfo>* files_;
    107   std::vector<string>* dirs_;
    108 };
    109 
    110 class CUFunctionInfoHandler: public Dwarf2Handler {
    111  public:
    112   CUFunctionInfoHandler(std::vector<SourceFileInfo>* files,
    113                         std::vector<string>* dirs,
    114                         LineMap* linemap,
    115                         FunctionMap* offset_to_funcinfo,
    116                         FunctionMap* address_to_funcinfo,
    117                         CULineInfoHandler* linehandler,
    118                         const SectionMap& sections,
    119                         ByteReader* reader)
    120       : files_(files), dirs_(dirs), linemap_(linemap),
    121         offset_to_funcinfo_(offset_to_funcinfo),
    122         address_to_funcinfo_(address_to_funcinfo),
    123         linehandler_(linehandler), sections_(sections),
    124         reader_(reader), current_function_info_(NULL) { }
    125 
    126   virtual ~CUFunctionInfoHandler() { }
    127 
    128   // Start to process a compilation unit at OFFSET from the beginning of the
    129   // .debug_info section.  We want to see all compilation units, so we
    130   // always return true.
    131 
    132   virtual bool StartCompilationUnit(uint64 offset, uint8 address_size,
    133                                     uint8 offset_size, uint64 cu_length,
    134                                     uint8 dwarf_version);
    135 
    136   // Start to process a DIE at OFFSET from the beginning of the
    137   // .debug_info section.  We only care about function related DIE's.
    138   virtual bool StartDIE(uint64 offset, enum DwarfTag tag);
    139 
    140   // Called when we have an attribute with unsigned data to give to
    141   // our handler.  The attribute is for the DIE at OFFSET from the
    142   // beginning of the .debug_info section, has a name of ATTR, a form of
    143   // FORM, and the actual data of the attribute is in DATA.
    144   virtual void ProcessAttributeUnsigned(uint64 offset,
    145                                         enum DwarfAttribute attr,
    146                                         enum DwarfForm form,
    147                                         uint64 data);
    148 
    149   // Called when we have an attribute with a DIE reference to give to
    150   // our handler.  The attribute is for the DIE at OFFSET from the
    151   // beginning of the .debug_info section, has a name of ATTR, a form of
    152   // FORM, and the offset of the referenced DIE from the start of the
    153   // .debug_info section is in DATA.
    154   virtual void ProcessAttributeReference(uint64 offset,
    155                                          enum DwarfAttribute attr,
    156                                          enum DwarfForm form,
    157                                          uint64 data);
    158 
    159   // Called when we have an attribute with string data to give to
    160   // our handler.  The attribute is for the DIE at OFFSET from the
    161   // beginning of the .debug_info section, has a name of ATTR, a form of
    162   // FORM, and the actual data of the attribute is in DATA.
    163   virtual void ProcessAttributeString(uint64 offset,
    164                                       enum DwarfAttribute attr,
    165                                       enum DwarfForm form,
    166                                       const string& data);
    167 
    168   // Called when finished processing the DIE at OFFSET.
    169   // Because DWARF2/3 specifies a tree of DIEs, you may get starts
    170   // before ends of the previous DIE, as we process children before
    171   // ending the parent.
    172   virtual void EndDIE(uint64 offset);
    173 
    174  private:
    175   std::vector<SourceFileInfo>* files_;
    176   std::vector<string>* dirs_;
    177   LineMap* linemap_;
    178   FunctionMap* offset_to_funcinfo_;
    179   FunctionMap* address_to_funcinfo_;
    180   CULineInfoHandler* linehandler_;
    181   const SectionMap& sections_;
    182   ByteReader* reader_;
    183   FunctionInfo* current_function_info_;
    184   uint64 current_compilation_unit_offset_;
    185 };
    186 
    187 }  // namespace dwarf2reader
    188 #endif  // COMMON_DWARF_FUNCTIONINFO_H__
    189