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_t line;
57   // Beginning address for this function
58   uint64_t lowpc;
59   // End address for this function.
60   uint64_t highpc;
61   // Ranges offset
62   uint64_t 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_t 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_t 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_t dir_num, uint64_t mod_time, uint64_t 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_t address, uint64_t length,
104                        uint32_t file_num, uint32_t line_num,
105                        uint32_t column_num);
106 
107  private:
108   LineMap* linemap_;
109   std::vector<SourceFileInfo>* files_;
110   std::vector<string>* dirs_;
111 };
112 
113 class CUFunctionInfoHandler: public Dwarf2Handler {
114  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)115   CUFunctionInfoHandler(std::vector<SourceFileInfo>* files,
116                         std::vector<string>* dirs,
117                         LineMap* linemap,
118                         FunctionMap* offset_to_funcinfo,
119                         FunctionMap* address_to_funcinfo,
120                         CULineInfoHandler* linehandler,
121                         const SectionMap& sections,
122                         ByteReader* reader)
123       : files_(files), dirs_(dirs), linemap_(linemap),
124         offset_to_funcinfo_(offset_to_funcinfo),
125         address_to_funcinfo_(address_to_funcinfo),
126         linehandler_(linehandler), sections_(sections),
127         reader_(reader), current_function_info_(NULL) { }
128 
~CUFunctionInfoHandler()129   virtual ~CUFunctionInfoHandler() { }
130 
131   // Start to process a compilation unit at OFFSET from the beginning of the
132   // .debug_info section.  We want to see all compilation units, so we
133   // always return true.
134 
135   virtual bool StartCompilationUnit(uint64_t offset, uint8_t address_size,
136                                     uint8_t offset_size, uint64_t cu_length,
137                                     uint8_t dwarf_version);
138 
139   // Start to process a DIE at OFFSET from the beginning of the
140   // .debug_info section.  We only care about function related DIE's.
141   virtual bool StartDIE(uint64_t offset, enum DwarfTag tag);
142 
143   // Called when we have an attribute with unsigned data to give to
144   // our handler.  The attribute is for the DIE at OFFSET from the
145   // beginning of the .debug_info section, has a name of ATTR, a form of
146   // FORM, and the actual data of the attribute is in DATA.
147   virtual void ProcessAttributeUnsigned(uint64_t offset,
148                                         enum DwarfAttribute attr,
149                                         enum DwarfForm form,
150                                         uint64_t data);
151 
152   // Called when we have an attribute with a DIE reference to give to
153   // our handler.  The attribute is for the DIE at OFFSET from the
154   // beginning of the .debug_info section, has a name of ATTR, a form of
155   // FORM, and the offset of the referenced DIE from the start of the
156   // .debug_info section is in DATA.
157   virtual void ProcessAttributeReference(uint64_t offset,
158                                          enum DwarfAttribute attr,
159                                          enum DwarfForm form,
160                                          uint64_t data);
161 
162   // Called when we have an attribute with string data to give to
163   // our handler.  The attribute is for the DIE at OFFSET from the
164   // beginning of the .debug_info section, has a name of ATTR, a form of
165   // FORM, and the actual data of the attribute is in DATA.
166   virtual void ProcessAttributeString(uint64_t offset,
167                                       enum DwarfAttribute attr,
168                                       enum DwarfForm form,
169                                       const string& data);
170 
171   // Called when finished processing the DIE at OFFSET.
172   // Because DWARF2/3 specifies a tree of DIEs, you may get starts
173   // before ends of the previous DIE, as we process children before
174   // ending the parent.
175   virtual void EndDIE(uint64_t offset);
176 
177  private:
178   std::vector<SourceFileInfo>* files_;
179   std::vector<string>* dirs_;
180   LineMap* linemap_;
181   FunctionMap* offset_to_funcinfo_;
182   FunctionMap* address_to_funcinfo_;
183   CULineInfoHandler* linehandler_;
184   const SectionMap& sections_;
185   ByteReader* reader_;
186   FunctionInfo* current_function_info_;
187   uint64_t current_compilation_unit_offset_;
188 };
189 
190 }  // namespace dwarf2reader
191 #endif  // COMMON_DWARF_FUNCTIONINFO_H__
192