Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / breakpad / src / tools / mac / dump_syms / dump_syms_tool.mm
index fd1b61e..8bade25 100644 (file)
 #include "common/mac/dump_syms.h"
 #include "common/mac/arch_utilities.h"
 #include "common/mac/macho_utilities.h"
+#include "common/scoped_ptr.h"
 
 using google_breakpad::DumpSymbols;
+using google_breakpad::Module;
+using google_breakpad::scoped_ptr;
 using std::vector;
 
 struct Options {
   Options() : srcPath(), arch(), cfi(true), handle_inter_cu_refs(true) { }
   NSString *srcPath;
+  NSString *dsymPath;
   const NXArchInfo *arch;
   bool cfi;
   bool handle_inter_cu_refs;
 };
 
-//=============================================================================
+static bool StackFrameEntryComparator(const Module::StackFrameEntry* a,
+                                      const Module::StackFrameEntry* b) {
+  return a->address < b->address;
+}
+
+// Copy the CFI data from |from_module| into |to_module|, for any non-
+// overlapping ranges.
+static void CopyCFIDataBetweenModules(Module* to_module,
+                                      const Module* from_module) {
+  typedef vector<Module::StackFrameEntry*>::const_iterator Iterator;
+
+  // Get the CFI data from both the source and destination modules and ensure
+  // it is sorted by start address.
+  vector<Module::StackFrameEntry*> from_data;
+  from_module->GetStackFrameEntries(&from_data);
+  std::sort(from_data.begin(), from_data.end(), &StackFrameEntryComparator);
+
+  vector<Module::StackFrameEntry*> to_data;
+  to_module->GetStackFrameEntries(&to_data);
+  std::sort(to_data.begin(), to_data.end(), &StackFrameEntryComparator);
+
+  Iterator to_it = to_data.begin();
+
+  for (Iterator it = from_data.begin(); it != from_data.end(); ++it) {
+    Module::StackFrameEntry* from_entry = *it;
+    Module::Address from_entry_end = from_entry->address + from_entry->size;
+
+    // Find the first CFI record in the |to_module| that does not have an
+    // address less than the entry to be copied.
+    while (to_it != to_data.end()) {
+      if (from_entry->address > (*to_it)->address)
+        ++to_it;
+      else
+        break;
+    }
+
+    // If the entry does not overlap, then it is safe to copy to |to_module|.
+    if (to_it == to_data.end() || (from_entry->address < (*to_it)->address &&
+            from_entry_end < (*to_it)->address)) {
+      to_module->AddStackFrameEntry(new Module::StackFrameEntry(*from_entry));
+    }
+  }
+}
+
 static bool Start(const Options &options) {
-  DumpSymbols dump_symbols(options.cfi ? ALL_SYMBOL_DATA : NO_CFI,
-                           options.handle_inter_cu_refs);
+  SymbolData symbol_data = options.cfi ? ALL_SYMBOL_DATA : NO_CFI;
+  DumpSymbols dump_symbols(symbol_data, options.handle_inter_cu_refs);
+
+  // For x86_64 binaries, the CFI data is in the __TEXT,__eh_frame of the
+  // Mach-O file, which is not copied into the dSYM. Whereas in i386, the CFI
+  // data is in the __DWARF,__debug_frame section, which is moved into the
+  // dSYM. Therefore, to get x86_64 CFI data, dump_syms needs to look at both
+  // the dSYM and the Mach-O file. If both paths are present and CFI was
+  // requested, then consider the Module as "split" and dump all the debug data
+  // from the primary debug info file, the dSYM, and then dump additional CFI
+  // data from the source Mach-O file.
+  bool split_module = options.dsymPath && options.srcPath && options.cfi;
+  NSString* primary_file = split_module ? options.dsymPath : options.srcPath;
 
-  if (!dump_symbols.Read(options.srcPath))
+  if (!dump_symbols.Read(primary_file))
     return false;
 
   if (options.arch) {
     if (!dump_symbols.SetArchitecture(options.arch->cputype,
                                       options.arch->cpusubtype)) {
       fprintf(stderr, "%s: no architecture '%s' is present in file.\n",
-              [options.srcPath fileSystemRepresentation], options.arch->name);
+              [primary_file fileSystemRepresentation], options.arch->name);
       size_t available_size;
       const struct fat_arch *available =
         dump_symbols.AvailableArchitectures(&available_size);
@@ -88,16 +146,48 @@ static bool Start(const Options &options) {
     }
   }
 
-  return dump_symbols.WriteSymbolFile(std::cout);
+  // Read the primary file into a Breakpad Module.
+  Module* module = NULL;
+  if (!dump_symbols.ReadSymbolData(&module))
+    return false;
+  scoped_ptr<Module> scoped_module(module);
+
+  // If this is a split module, read the secondary Mach-O file, from which the
+  // CFI data will be extracted.
+  if (split_module && primary_file == options.dsymPath) {
+    if (!dump_symbols.Read(options.srcPath))
+      return false;
+
+    Module* cfi_module = NULL;
+    if (!dump_symbols.ReadSymbolData(&cfi_module))
+      return false;
+    scoped_ptr<Module> scoped_cfi_module(cfi_module);
+
+    // Ensure that the modules are for the same debug code file.
+    if (cfi_module->name() != module->name() ||
+        cfi_module->os() != module->os() ||
+        cfi_module->architecture() != module->architecture() ||
+        cfi_module->identifier() != module->identifier()) {
+      fprintf(stderr, "Cannot generate a symbol file from split sources that do"
+                      " not match.\n");
+      return false;
+    }
+
+    CopyCFIDataBetweenModules(module, cfi_module);
+  }
+
+  return module->Write(std::cout, symbol_data);
 }
 
 //=============================================================================
 static void Usage(int argc, const char *argv[]) {
   fprintf(stderr, "Output a Breakpad symbol file from a Mach-o file.\n");
-  fprintf(stderr, "Usage: %s [-a ARCHITECTURE] [-c] <Mach-o file>\n",
-          argv[0]);
+  fprintf(stderr, "Usage: %s [-a ARCHITECTURE] [-c] [-g dSYM path] "
+                  "<Mach-o file>\n", argv[0]);
   fprintf(stderr, "\t-a: Architecture type [default: native, or whatever is\n");
   fprintf(stderr, "\t    in the file, if it contains only one architecture]\n");
+  fprintf(stderr, "\t-g: Debug symbol file (dSYM) to dump in addition to the "
+                  "Mach-o file\n");
   fprintf(stderr, "\t-c: Do not generate CFI section\n");
   fprintf(stderr, "\t-r: Do not handle inter-compilation unit references\n");
   fprintf(stderr, "\t-h: Usage\n");
@@ -109,7 +199,7 @@ static void SetupOptions(int argc, const char *argv[], Options *options) {
   extern int optind;
   signed char ch;
 
-  while ((ch = getopt(argc, (char * const *)argv, "a:chr?")) != -1) {
+  while ((ch = getopt(argc, (char * const *)argv, "a:g:chr?")) != -1) {
     switch (ch) {
       case 'a': {
         const NXArchInfo *arch_info =
@@ -122,6 +212,10 @@ static void SetupOptions(int argc, const char *argv[], Options *options) {
         options->arch = arch_info;
         break;
       }
+      case 'g':
+        options->dsymPath = [[NSFileManager defaultManager]
+            stringWithFileSystemRepresentation:optarg length:strlen(optarg)];
+        break;
       case 'c':
         options->cfi = false;
         break;