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   // Ranges offset
62   uint64 ranges;
63 };
64 
65 struct SourceFileInfo {
66   // Name of the source file name
67   string name;
68   // Low address of source file name
69   uint64 lowpc;
70 };
71 
72 typedef std::map<uint64, FunctionInfo*> FunctionMap;
73 typedef std::map<uint64, std::pair<string, uint32> > LineMap;
74 
75 // This class is a basic line info handler that fills in the dirs,
76 // file, and linemap passed into it with the data produced from the
77 // LineInfoHandler.
78 class CULineInfoHandler: public LineInfoHandler {
79  public:
80 
81   //
82   CULineInfoHandler(std::vector<SourceFileInfo>* files,
83                     std::vector<string>* dirs,
84                     LineMap* linemap);
~CULineInfoHandler()85   virtual ~CULineInfoHandler() { }
86 
87   // Called when we define a directory.  We just place NAME into dirs_
88   // at position DIR_NUM.
89   virtual void DefineDir(const string& name, uint32 dir_num);
90 
91   // Called when we define a filename.  We just place
92   // concat(dirs_[DIR_NUM], NAME) into files_ at position FILE_NUM.
93   virtual void DefineFile(const string& name, int32 file_num,
94                           uint32 dir_num, uint64 mod_time, uint64 length);
95 
96 
97   // Called when the line info reader has a new line, address pair
98   // ready for us. ADDRESS is the address of the code, LENGTH is the
99   // length of its machine code in bytes, FILE_NUM is the file number
100   // containing the code, LINE_NUM is the line number in that file for
101   // the code, and COLUMN_NUM is the column number the code starts at,
102   // if we know it (0 otherwise).
103   virtual void AddLine(uint64 address, uint64 length,
104                        uint32 file_num, uint32 line_num, uint32 column_num);
105 
106  private:
107   LineMap* linemap_;
108   std::vector<SourceFileInfo>* files_;
109   std::vector<string>* dirs_;
110 };
111 
112 class CUFunctionInfoHandler: public Dwarf2Handler {
113  public:
CUFunctionInfoHandler(std::vector<SourceFileInfo> * files,std::vector<string> * dirs,LineMap * linemap,FunctionMap * offset_to_funcinfo,FunctionMap * address_to_funcinfo,CULineInfoHandler * linehandler,const SectionMap & sections,ByteReader * reader)114   CUFunctionInfoHandler(std::vector<SourceFileInfo>* files,
115                         std::vector<string>* dirs,
116                         LineMap* linemap,
117                         FunctionMap* offset_to_funcinfo,
118                         FunctionMap* address_to_funcinfo,
119                         CULineInfoHandler* linehandler,
120                         const SectionMap& sections,
121                         ByteReader* reader)
122       : files_(files), dirs_(dirs), linemap_(linemap),
123         offset_to_funcinfo_(offset_to_funcinfo),
124         address_to_funcinfo_(address_to_funcinfo),
125         linehandler_(linehandler), sections_(sections),
126         reader_(reader), current_function_info_(NULL) { }
127 
~CUFunctionInfoHandler()128   virtual ~CUFunctionInfoHandler() { }
129 
130   // Start to process a compilation unit at OFFSET from the beginning of the
131   // .debug_info section.  We want to see all compilation units, so we
132   // always return true.
133 
134   virtual bool StartCompilationUnit(uint64 offset, uint8 address_size,
135                                     uint8 offset_size, uint64 cu_length,
136                                     uint8 dwarf_version);
137 
138   // Start to process a DIE at OFFSET from the beginning of the
139   // .debug_info section.  We only care about function related DIE's.
140   virtual bool StartDIE(uint64 offset, enum DwarfTag tag);
141 
142   // Called when we have an attribute with unsigned data to give to
143   // our handler.  The attribute is for the DIE at OFFSET from the
144   // beginning of the .debug_info section, has a name of ATTR, a form of
145   // FORM, and the actual data of the attribute is in DATA.
146   virtual void ProcessAttributeUnsigned(uint64 offset,
147                                         enum DwarfAttribute attr,
148                                         enum DwarfForm form,
149                                         uint64 data);
150 
151   // Called when we have an attribute with a DIE reference to give to
152   // our handler.  The attribute is for the DIE at OFFSET from the
153   // beginning of the .debug_info section, has a name of ATTR, a form of
154   // FORM, and the offset of the referenced DIE from the start of the
155   // .debug_info section is in DATA.
156   virtual void ProcessAttributeReference(uint64 offset,
157                                          enum DwarfAttribute attr,
158                                          enum DwarfForm form,
159                                          uint64 data);
160 
161   // Called when we have an attribute with string data to give to
162   // our handler.  The attribute is for the DIE at OFFSET from the
163   // beginning of the .debug_info section, has a name of ATTR, a form of
164   // FORM, and the actual data of the attribute is in DATA.
165   virtual void ProcessAttributeString(uint64 offset,
166                                       enum DwarfAttribute attr,
167                                       enum DwarfForm form,
168                                       const string& data);
169 
170   // Called when finished processing the DIE at OFFSET.
171   // Because DWARF2/3 specifies a tree of DIEs, you may get starts
172   // before ends of the previous DIE, as we process children before
173   // ending the parent.
174   virtual void EndDIE(uint64 offset);
175 
176  private:
177   std::vector<SourceFileInfo>* files_;
178   std::vector<string>* dirs_;
179   LineMap* linemap_;
180   FunctionMap* offset_to_funcinfo_;
181   FunctionMap* address_to_funcinfo_;
182   CULineInfoHandler* linehandler_;
183   const SectionMap& sections_;
184   ByteReader* reader_;
185   FunctionInfo* current_function_info_;
186   uint64 current_compilation_unit_offset_;
187 };
188 
189 }  // namespace dwarf2reader
190 #endif  // COMMON_DWARF_FUNCTIONINFO_H__
191