• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // -*- mode: c++ -*-
2 
3 // Copyright (c) 2010, Google Inc.
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met:
9 //
10 //     * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 //     * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following disclaimer
14 // in the documentation and/or other materials provided with the
15 // distribution.
16 //     * Neither the name of Google Inc. nor the names of its
17 // contributors may be used to endorse or promote products derived from
18 // this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 
32 // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
33 
34 // dwarf_cfi_to_module.h: Define the DwarfCFIToModule class, which
35 // accepts parsed DWARF call frame info and adds it to a
36 // google_breakpad::Module object, which can write that information to
37 // a Breakpad symbol file.
38 
39 #ifndef COMMON_LINUX_DWARF_CFI_TO_MODULE_H
40 #define COMMON_LINUX_DWARF_CFI_TO_MODULE_H
41 
42 #include <assert.h>
43 #include <stdio.h>
44 
45 #include <set>
46 #include <string>
47 #include <vector>
48 
49 #include "common/module.h"
50 #include "common/dwarf/dwarf2reader.h"
51 #include "common/using_std_string.h"
52 
53 namespace google_breakpad {
54 
55 using dwarf2reader::CallFrameInfo;
56 using google_breakpad::Module;
57 using std::set;
58 using std::vector;
59 
60 // A class that accepts parsed call frame information from the DWARF
61 // CFI parser and populates a google_breakpad::Module object with the
62 // contents.
63 class DwarfCFIToModule: public CallFrameInfo::Handler {
64  public:
65 
66   // DwarfCFIToModule uses an instance of this class to report errors
67   // detected while converting DWARF CFI to Breakpad STACK CFI records.
68   class Reporter {
69    public:
70     // Create a reporter that writes messages to the standard error
71     // stream. FILE is the name of the file we're processing, and
72     // SECTION is the name of the section within that file that we're
73     // looking at (.debug_frame, .eh_frame, etc.).
Reporter(const string & file,const string & section)74     Reporter(const string &file, const string &section)
75       : file_(file), section_(section) { }
~Reporter()76     virtual ~Reporter() { }
77 
78     // The DWARF CFI entry at OFFSET cites register REG, but REG is not
79     // covered by the vector of register names passed to the
80     // DwarfCFIToModule constructor, nor does it match the return
81     // address column number for this entry.
82     virtual void UnnamedRegister(size_t offset, int reg);
83 
84     // The DWARF CFI entry at OFFSET says that REG is undefined, but the
85     // Breakpad symbol file format cannot express this.
86     virtual void UndefinedNotSupported(size_t offset, const string &reg);
87 
88     // The DWARF CFI entry at OFFSET says that REG uses a DWARF
89     // expression to find its value, but DwarfCFIToModule is not
90     // capable of translating DWARF expressions to Breakpad postfix
91     // expressions.
92     virtual void ExpressionsNotSupported(size_t offset, const string &reg);
93 
94   protected:
95     string file_, section_;
96   };
97 
98   // Register name tables. If TABLE is a vector returned by one of these
99   // functions, then TABLE[R] is the name of the register numbered R in
100   // DWARF call frame information.
101   class RegisterNames {
102    public:
103     // Intel's "x86" or IA-32.
104     static vector<string> I386();
105 
106     // AMD x86_64, AMD64, Intel EM64T, or Intel 64
107     static vector<string> X86_64();
108 
109     // ARM.
110     static vector<string> ARM();
111 
112     // ARM64, aka AARCH64.
113     static vector<string> ARM64();
114 
115     // MIPS.
116     static vector<string> MIPS();
117 
118    private:
119     // Given STRINGS, an array of C strings with SIZE elements, return an
120     // equivalent vector<string>.
121     static vector<string> MakeVector(const char * const *strings, size_t size);
122   };
123 
124   // Create a handler for the dwarf2reader::CallFrameInfo parser that
125   // records the stack unwinding information it receives in MODULE.
126   //
127   // Use REGISTER_NAMES[I] as the name of register number I; *this
128   // keeps a reference to the vector, so the vector should remain
129   // alive for as long as the DwarfCFIToModule does.
130   //
131   // Use REPORTER for reporting problems encountered in the conversion
132   // process.
DwarfCFIToModule(Module * module,const vector<string> & register_names,Reporter * reporter)133   DwarfCFIToModule(Module *module, const vector<string> &register_names,
134                    Reporter *reporter)
135       : module_(module), register_names_(register_names), reporter_(reporter),
136         entry_(NULL), return_address_(-1), cfa_name_(".cfa"), ra_name_(".ra") {
137   }
~DwarfCFIToModule()138   virtual ~DwarfCFIToModule() { delete entry_; }
139 
140   virtual bool Entry(size_t offset, uint64 address, uint64 length,
141                      uint8 version, const string &augmentation,
142                      unsigned return_address);
143   virtual bool UndefinedRule(uint64 address, int reg);
144   virtual bool SameValueRule(uint64 address, int reg);
145   virtual bool OffsetRule(uint64 address, int reg,
146                           int base_register, long offset);
147   virtual bool ValOffsetRule(uint64 address, int reg,
148                              int base_register, long offset);
149   virtual bool RegisterRule(uint64 address, int reg, int base_register);
150   virtual bool ExpressionRule(uint64 address, int reg,
151                               const string &expression);
152   virtual bool ValExpressionRule(uint64 address, int reg,
153                                  const string &expression);
154   virtual bool End();
155 
156  private:
157   // Return the name to use for register REG.
158   string RegisterName(int i);
159 
160   // Record RULE for register REG at ADDRESS.
161   void Record(Module::Address address, int reg, const string &rule);
162 
163   // The module to which we should add entries.
164   Module *module_;
165 
166   // Map from register numbers to register names.
167   const vector<string> &register_names_;
168 
169   // The reporter to use to report problems.
170   Reporter *reporter_;
171 
172   // The current entry we're constructing.
173   Module::StackFrameEntry *entry_;
174 
175   // The section offset of the current frame description entry, for
176   // use in error messages.
177   size_t entry_offset_;
178 
179   // The return address column for that entry.
180   unsigned return_address_;
181 
182   // The names of the return address and canonical frame address. Putting
183   // these here instead of using string literals allows us to share their
184   // texts in reference-counted std::string implementations (all the
185   // popular ones). Many, many rules cite these strings.
186   string cfa_name_, ra_name_;
187 
188   // A set of strings used by this CFI. Before storing a string in one of
189   // our data structures, insert it into this set, and then use the string
190   // from the set.
191   //
192   // Because std::string uses reference counting internally, simply using
193   // strings from this set, even if passed by value, assigned, or held
194   // directly in structures and containers (map<string, ...>, for example),
195   // causes those strings to share a single instance of each distinct piece
196   // of text.
197   set<string> common_strings_;
198 };
199 
200 } // namespace google_breakpad
201 
202 #endif // COMMON_LINUX_DWARF_CFI_TO_MODULE_H
203