• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // dump_syms_tool.cc: Command line tool that uses the DumpSymbols class.
33 // TODO(waylonis): accept stdin
34 
35 #include <mach-o/arch.h>
36 #include <unistd.h>
37 
38 #include <algorithm>
39 #include <iostream>
40 #include <vector>
41 
42 #include "common/mac/dump_syms.h"
43 #include "common/mac/arch_utilities.h"
44 #include "common/mac/macho_utilities.h"
45 #include "common/scoped_ptr.h"
46 
47 using google_breakpad::DumpSymbols;
48 using google_breakpad::Module;
49 using google_breakpad::scoped_ptr;
50 using std::vector;
51 
52 struct Options {
OptionsOptions53   Options()
54       : srcPath(), dsymPath(), arch(), header_only(false),
55         cfi(true), handle_inter_cu_refs(true) {}
56 
57   string srcPath;
58   string dsymPath;
59   const NXArchInfo *arch;
60   bool header_only;
61   bool cfi;
62   bool handle_inter_cu_refs;
63 };
64 
StackFrameEntryComparator(const Module::StackFrameEntry * a,const Module::StackFrameEntry * b)65 static bool StackFrameEntryComparator(const Module::StackFrameEntry* a,
66                                       const Module::StackFrameEntry* b) {
67   return a->address < b->address;
68 }
69 
70 // Copy the CFI data from |from_module| into |to_module|, for any non-
71 // overlapping ranges.
CopyCFIDataBetweenModules(Module * to_module,const Module * from_module)72 static void CopyCFIDataBetweenModules(Module* to_module,
73                                       const Module* from_module) {
74   typedef vector<Module::StackFrameEntry*>::const_iterator Iterator;
75 
76   // Get the CFI data from both the source and destination modules and ensure
77   // it is sorted by start address.
78   vector<Module::StackFrameEntry*> from_data;
79   from_module->GetStackFrameEntries(&from_data);
80   std::sort(from_data.begin(), from_data.end(), &StackFrameEntryComparator);
81 
82   vector<Module::StackFrameEntry*> to_data;
83   to_module->GetStackFrameEntries(&to_data);
84   std::sort(to_data.begin(), to_data.end(), &StackFrameEntryComparator);
85 
86   Iterator to_it = to_data.begin();
87 
88   for (Iterator it = from_data.begin(); it != from_data.end(); ++it) {
89     Module::StackFrameEntry* from_entry = *it;
90     Module::Address from_entry_end = from_entry->address + from_entry->size;
91 
92     // Find the first CFI record in the |to_module| that does not have an
93     // address less than the entry to be copied.
94     while (to_it != to_data.end()) {
95       if (from_entry->address > (*to_it)->address)
96         ++to_it;
97       else
98         break;
99     }
100 
101     // If the entry does not overlap, then it is safe to copy to |to_module|.
102     if (to_it == to_data.end() || (from_entry->address < (*to_it)->address &&
103             from_entry_end < (*to_it)->address)) {
104       to_module->AddStackFrameEntry(new Module::StackFrameEntry(*from_entry));
105     }
106   }
107 }
108 
Start(const Options & options)109 static bool Start(const Options &options) {
110   SymbolData symbol_data = options.cfi ? ALL_SYMBOL_DATA : NO_CFI;
111   DumpSymbols dump_symbols(symbol_data, options.handle_inter_cu_refs);
112 
113   // For x86_64 binaries, the CFI data is in the __TEXT,__eh_frame of the
114   // Mach-O file, which is not copied into the dSYM. Whereas in i386, the CFI
115   // data is in the __DWARF,__debug_frame section, which is moved into the
116   // dSYM. Therefore, to get x86_64 CFI data, dump_syms needs to look at both
117   // the dSYM and the Mach-O file. If both paths are present and CFI was
118   // requested, then consider the Module as "split" and dump all the debug data
119   // from the primary debug info file, the dSYM, and then dump additional CFI
120   // data from the source Mach-O file.
121   bool split_module =
122     !options.dsymPath.empty() && !options.srcPath.empty() && options.cfi;
123   const string& primary_file =
124     split_module ? options.dsymPath : options.srcPath;
125 
126   if (!dump_symbols.Read(primary_file))
127     return false;
128 
129   if (options.arch) {
130     if (!dump_symbols.SetArchitecture(options.arch->cputype,
131                                       options.arch->cpusubtype)) {
132       fprintf(stderr, "%s: no architecture '%s' is present in file.\n",
133               primary_file.c_str(), options.arch->name);
134       size_t available_size;
135       const SuperFatArch *available =
136         dump_symbols.AvailableArchitectures(&available_size);
137       if (available_size == 1)
138         fprintf(stderr, "the file's architecture is: ");
139       else
140         fprintf(stderr, "architectures present in the file are:\n");
141       for (size_t i = 0; i < available_size; i++) {
142         const SuperFatArch *arch = &available[i];
143         const NXArchInfo *arch_info =
144           google_breakpad::BreakpadGetArchInfoFromCpuType(
145               arch->cputype, arch->cpusubtype);
146         if (arch_info)
147           fprintf(stderr, "%s (%s)\n", arch_info->name, arch_info->description);
148         else
149           fprintf(stderr, "unrecognized cpu type 0x%x, subtype 0x%x\n",
150                   arch->cputype, arch->cpusubtype);
151       }
152       return false;
153     }
154   }
155 
156   if (options.header_only)
157     return dump_symbols.WriteSymbolFileHeader(std::cout);
158 
159   // Read the primary file into a Breakpad Module.
160   Module* module = NULL;
161   if (!dump_symbols.ReadSymbolData(&module))
162     return false;
163   scoped_ptr<Module> scoped_module(module);
164 
165   // If this is a split module, read the secondary Mach-O file, from which the
166   // CFI data will be extracted.
167   if (split_module && primary_file == options.dsymPath) {
168     if (!dump_symbols.Read(options.srcPath))
169       return false;
170 
171     Module* cfi_module = NULL;
172     if (!dump_symbols.ReadSymbolData(&cfi_module))
173       return false;
174     scoped_ptr<Module> scoped_cfi_module(cfi_module);
175 
176     // Ensure that the modules are for the same debug code file.
177     if (cfi_module->name() != module->name() ||
178         cfi_module->os() != module->os() ||
179         cfi_module->architecture() != module->architecture() ||
180         cfi_module->identifier() != module->identifier()) {
181       fprintf(stderr, "Cannot generate a symbol file from split sources that do"
182                       " not match.\n");
183       return false;
184     }
185 
186     CopyCFIDataBetweenModules(module, cfi_module);
187   }
188 
189   return module->Write(std::cout, symbol_data);
190 }
191 
192 //=============================================================================
Usage(int argc,const char * argv[])193 static void Usage(int argc, const char *argv[]) {
194   fprintf(stderr, "Output a Breakpad symbol file from a Mach-o file.\n");
195   fprintf(stderr, "Usage: %s [-a ARCHITECTURE] [-c] [-g dSYM path] "
196                   "<Mach-o file>\n", argv[0]);
197   fprintf(stderr, "\t-i: Output module header information only.\n");
198   fprintf(stderr, "\t-a: Architecture type [default: native, or whatever is\n");
199   fprintf(stderr, "\t    in the file, if it contains only one architecture]\n");
200   fprintf(stderr, "\t-g: Debug symbol file (dSYM) to dump in addition to the "
201                   "Mach-o file\n");
202   fprintf(stderr, "\t-c: Do not generate CFI section\n");
203   fprintf(stderr, "\t-r: Do not handle inter-compilation unit references\n");
204   fprintf(stderr, "\t-h: Usage\n");
205   fprintf(stderr, "\t-?: Usage\n");
206 }
207 
208 //=============================================================================
SetupOptions(int argc,const char * argv[],Options * options)209 static void SetupOptions(int argc, const char *argv[], Options *options) {
210   extern int optind;
211   signed char ch;
212 
213   while ((ch = getopt(argc, (char * const *)argv, "ia:g:chr?")) != -1) {
214     switch (ch) {
215       case 'i':
216         options->header_only = true;
217         break;
218       case 'a': {
219         const NXArchInfo *arch_info =
220             google_breakpad::BreakpadGetArchInfoFromName(optarg);
221         if (!arch_info) {
222           fprintf(stderr, "%s: Invalid architecture: %s\n", argv[0], optarg);
223           Usage(argc, argv);
224           exit(1);
225         }
226         options->arch = arch_info;
227         break;
228       }
229       case 'g':
230         options->dsymPath = optarg;
231         break;
232       case 'c':
233         options->cfi = false;
234         break;
235       case 'r':
236         options->handle_inter_cu_refs = false;
237         break;
238       case '?':
239       case 'h':
240         Usage(argc, argv);
241         exit(0);
242         break;
243     }
244   }
245 
246   if ((argc - optind) != 1) {
247     fprintf(stderr, "Must specify Mach-o file\n");
248     Usage(argc, argv);
249     exit(1);
250   }
251 
252   options->srcPath = argv[optind];
253 }
254 
255 //=============================================================================
main(int argc,const char * argv[])256 int main (int argc, const char * argv[]) {
257   Options options;
258   bool result;
259 
260   SetupOptions(argc, argv, &options);
261   result = Start(options);
262 
263   return !result;
264 }
265