|
1 // -*- mode: c++ -*- |
|
2 |
|
3 // Copyright (c) 2011, 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 // Author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> |
|
33 |
|
34 // dump_syms.h: Declaration of google_breakpad::DumpSymbols, a class for |
|
35 // reading debugging information from Mach-O files and writing it out as a |
|
36 // Breakpad symbol file. |
|
37 |
|
38 #include <Foundation/Foundation.h> |
|
39 #include <mach-o/loader.h> |
|
40 #include <stdio.h> |
|
41 #include <stdlib.h> |
|
42 |
|
43 #include <ostream> |
|
44 #include <string> |
|
45 #include <vector> |
|
46 |
|
47 #include "common/byte_cursor.h" |
|
48 #include "common/mac/macho_reader.h" |
|
49 #include "common/module.h" |
|
50 #include "common/symbol_data.h" |
|
51 |
|
52 namespace google_breakpad { |
|
53 |
|
54 class DumpSymbols { |
|
55 public: |
|
56 explicit DumpSymbols(SymbolData symbol_data) |
|
57 : symbol_data_(symbol_data), |
|
58 input_pathname_(), |
|
59 object_filename_(), |
|
60 contents_(), |
|
61 selected_object_file_(), |
|
62 selected_object_name_() { } |
|
63 ~DumpSymbols() { |
|
64 [input_pathname_ release]; |
|
65 [object_filename_ release]; |
|
66 [contents_ release]; |
|
67 } |
|
68 |
|
69 // Prepare to read debugging information from |filename|. |filename| may be |
|
70 // the name of a universal binary, a Mach-O file, or a dSYM bundle |
|
71 // containing either of the above. On success, return true; if there is a |
|
72 // problem reading |filename|, report it and return false. |
|
73 // |
|
74 // (This class uses NSString for filenames and related values, |
|
75 // because the Mac Foundation framework seems to support |
|
76 // filename-related operations more fully on NSString values.) |
|
77 bool Read(NSString *filename); |
|
78 |
|
79 // If this dumper's file includes an object file for |cpu_type| and |
|
80 // |cpu_subtype|, then select that object file for dumping, and return |
|
81 // true. Otherwise, return false, and leave this dumper's selected |
|
82 // architecture unchanged. |
|
83 // |
|
84 // By default, if this dumper's file contains only one object file, then |
|
85 // the dumper will dump those symbols; and if it contains more than one |
|
86 // object file, then the dumper will dump the object file whose |
|
87 // architecture matches that of this dumper program. |
|
88 bool SetArchitecture(cpu_type_t cpu_type, cpu_subtype_t cpu_subtype); |
|
89 |
|
90 // If this dumper's file includes an object file for |arch_name|, then select |
|
91 // that object file for dumping, and return true. Otherwise, return false, |
|
92 // and leave this dumper's selected architecture unchanged. |
|
93 // |
|
94 // By default, if this dumper's file contains only one object file, then |
|
95 // the dumper will dump those symbols; and if it contains more than one |
|
96 // object file, then the dumper will dump the object file whose |
|
97 // architecture matches that of this dumper program. |
|
98 bool SetArchitecture(const std::string &arch_name); |
|
99 |
|
100 // Return a pointer to an array of 'struct fat_arch' structures, |
|
101 // describing the object files contained in this dumper's file. Set |
|
102 // *|count| to the number of elements in the array. The returned array is |
|
103 // owned by this DumpSymbols instance. |
|
104 // |
|
105 // If there are no available architectures, this function |
|
106 // may return NULL. |
|
107 const struct fat_arch *AvailableArchitectures(size_t *count) { |
|
108 *count = object_files_.size(); |
|
109 if (object_files_.size() > 0) |
|
110 return &object_files_[0]; |
|
111 return NULL; |
|
112 } |
|
113 |
|
114 // Read the selected object file's debugging information, and write it out to |
|
115 // |stream|. Return true on success; if an error occurs, report it and |
|
116 // return false. |
|
117 bool WriteSymbolFile(std::ostream &stream); |
|
118 |
|
119 // As above, but simply return the debugging information in module |
|
120 // instead of writing it to a stream. The caller owns the resulting |
|
121 // module object and must delete it when finished. |
|
122 bool ReadSymbolData(Module** module); |
|
123 |
|
124 private: |
|
125 // Used internally. |
|
126 class DumperLineToModule; |
|
127 class LoadCommandDumper; |
|
128 |
|
129 // Return an identifier string for the file this DumpSymbols is dumping. |
|
130 std::string Identifier(); |
|
131 |
|
132 // Read debugging information from |dwarf_sections|, which was taken from |
|
133 // |macho_reader|, and add it to |module|. On success, return true; |
|
134 // on failure, report the problem and return false. |
|
135 bool ReadDwarf(google_breakpad::Module *module, |
|
136 const mach_o::Reader &macho_reader, |
|
137 const mach_o::SectionMap &dwarf_sections) const; |
|
138 |
|
139 // Read DWARF CFI or .eh_frame data from |section|, belonging to |
|
140 // |macho_reader|, and record it in |module|. If |eh_frame| is true, |
|
141 // then the data is .eh_frame-format data; otherwise, it is standard DWARF |
|
142 // .debug_frame data. On success, return true; on failure, report |
|
143 // the problem and return false. |
|
144 bool ReadCFI(google_breakpad::Module *module, |
|
145 const mach_o::Reader &macho_reader, |
|
146 const mach_o::Section §ion, |
|
147 bool eh_frame) const; |
|
148 |
|
149 // The selection of what type of symbol data to read/write. |
|
150 const SymbolData symbol_data_; |
|
151 |
|
152 // The name of the file or bundle whose symbols this will dump. |
|
153 // This is the path given to Read, for use in error messages. |
|
154 NSString *input_pathname_; |
|
155 |
|
156 // The name of the file this DumpSymbols will actually read debugging |
|
157 // information from. Normally, this is the same as input_pathname_, but if |
|
158 // filename refers to a dSYM bundle, then this is the resource file |
|
159 // within that bundle. |
|
160 NSString *object_filename_; |
|
161 |
|
162 // The complete contents of object_filename_, mapped into memory. |
|
163 NSData *contents_; |
|
164 |
|
165 // A vector of fat_arch structures describing the object files |
|
166 // object_filename_ contains. If object_filename_ refers to a fat binary, |
|
167 // this may have more than one element; if it refers to a Mach-O file, this |
|
168 // has exactly one element. |
|
169 vector<struct fat_arch> object_files_; |
|
170 |
|
171 // The object file in object_files_ selected to dump, or NULL if |
|
172 // SetArchitecture hasn't been called yet. |
|
173 const struct fat_arch *selected_object_file_; |
|
174 |
|
175 // A string that identifies the selected object file, for use in error |
|
176 // messages. This is usually object_filename_, but if that refers to a |
|
177 // fat binary, it includes an indication of the particular architecture |
|
178 // within that binary. |
|
179 string selected_object_name_; |
|
180 }; |
|
181 |
|
182 } // namespace google_breakpad |