From b0e5f262334e9a4d9e083d0daf430be2202f0152 Mon Sep 17 00:00:00 2001 From: David Yen Date: Fri, 8 Apr 2016 04:37:45 +0200 Subject: [PATCH] Added an option (-i) to have dump_syms output header information only. It is often helpful to check if a particular symbol file dumped by dump_syms actually matches a version of a binary file we have. The symbol output contains an ID which can be used to see if it matches the binary file. Unfortunately, this ID is internally calculated and not a standard hash of the binary file. Being able to output the header information only will allow users to determine whether their symbol file is up to date or not. R=jochen@chromium.org BUG=561447 Review URL: https://codereview.chromium.org/1864823002 . Patch from David Yen . --- src/common/linux/dump_symbols.cc | 89 +++++++++++---- src/common/linux/dump_symbols.h | 6 ++ src/common/mac/dump_syms.cc | 126 ++++++++++++---------- src/common/mac/dump_syms.h | 9 ++ src/tools/linux/dump_syms/dump_syms.cc | 28 +++-- src/tools/mac/dump_syms/dump_syms_tool.cc | 13 ++- 6 files changed, 188 insertions(+), 83 deletions(-) diff --git a/src/common/linux/dump_symbols.cc b/src/common/linux/dump_symbols.cc index d31e2c2d..0000add6 100644 --- a/src/common/linux/dump_symbols.cc +++ b/src/common/linux/dump_symbols.cc @@ -901,19 +901,12 @@ bool SanitizeDebugFile(const typename ElfClass::Ehdr* debug_elf_header, } template -bool ReadSymbolDataElfClass(const typename ElfClass::Ehdr* elf_header, - const string& obj_filename, - const std::vector& debug_dirs, - const DumpOptions& options, - Module** out_module) { - typedef typename ElfClass::Ehdr Ehdr; - - *out_module = NULL; - +bool InitModuleForElfClass(const typename ElfClass::Ehdr* elf_header, + const string& obj_filename, + scoped_ptr& module) { PageAllocator allocator; wasteful_vector identifier(&allocator, kDefaultBuildIdSize); - if (!FileID::ElfFileIdentifierFromMappedFile(elf_header, - identifier)) { + if (!FileID::ElfFileIdentifierFromMappedFile(elf_header, identifier)) { fprintf(stderr, "%s: unable to generate file identifier\n", obj_filename.c_str()); return false; @@ -926,11 +919,6 @@ bool ReadSymbolDataElfClass(const typename ElfClass::Ehdr* elf_header, return false; } - // Figure out what endianness this file is. - bool big_endian; - if (!ElfEndianness(elf_header, &big_endian)) - return false; - string name = BaseFileName(obj_filename); string os = "Linux"; // Add an extra "0" at the end. PDB files on Windows have an 'age' @@ -938,8 +926,32 @@ bool ReadSymbolDataElfClass(const typename ElfClass::Ehdr* elf_header, // really used or necessary on other platforms, but be consistent. string id = FileID::ConvertIdentifierToUUIDString(identifier) + "0"; + module.reset(new Module(name, os, architecture, id)); + + return true; +} + +template +bool ReadSymbolDataElfClass(const typename ElfClass::Ehdr* elf_header, + const string& obj_filename, + const std::vector& debug_dirs, + const DumpOptions& options, + Module** out_module) { + typedef typename ElfClass::Ehdr Ehdr; + + *out_module = NULL; + + scoped_ptr module; + if (!InitModuleForElfClass(elf_header, obj_filename, module)) { + return false; + } + + // Figure out what endianness this file is. + bool big_endian; + if (!ElfEndianness(elf_header, &big_endian)) + return false; + LoadSymbolsInfo info(debug_dirs); - scoped_ptr module(new Module(name, os, architecture, id)); if (!LoadSymbols(obj_filename, big_endian, elf_header, !debug_dirs.empty(), &info, options, module.get())) { @@ -954,7 +966,9 @@ bool ReadSymbolDataElfClass(const typename ElfClass::Ehdr* elf_header, if (!LoadELF(debuglink_file, &debug_map_wrapper, reinterpret_cast(&debug_elf_header)) || !SanitizeDebugFile(debug_elf_header, debuglink_file, - obj_filename, architecture, big_endian)) { + obj_filename, + module->architecture().c_str(), + big_endian)) { return false; } @@ -1012,6 +1026,45 @@ bool WriteSymbolFile(const string &obj_file, return result; } +// Read the selected object file's debugging information, and write out the +// header only to |stream|. Return true on success; if an error occurs, report +// it and return false. +bool WriteSymbolFileHeader(const string& obj_file, + std::ostream &sym_stream) { + MmapWrapper map_wrapper; + void* elf_header = NULL; + if (!LoadELF(obj_file, &map_wrapper, &elf_header)) { + fprintf(stderr, "Could not load ELF file: %s\n", obj_file.c_str()); + return false; + } + + if (!IsValidElf(elf_header)) { + fprintf(stderr, "Not a valid ELF file: %s\n", obj_file.c_str()); + return false; + } + + int elfclass = ElfClass(elf_header); + scoped_ptr module; + if (elfclass == ELFCLASS32) { + if (!InitModuleForElfClass( + reinterpret_cast(elf_header), obj_file, module)) { + fprintf(stderr, "Failed to load ELF module: %s\n", obj_file.c_str()); + return false; + } + } else if (elfclass == ELFCLASS64) { + if (!InitModuleForElfClass( + reinterpret_cast(elf_header), obj_file, module)) { + fprintf(stderr, "Failed to load ELF module: %s\n", obj_file.c_str()); + return false; + } + } else { + fprintf(stderr, "Unsupported module file: %s\n", obj_file.c_str()); + return false; + } + + return module->Write(sym_stream, ALL_SYMBOL_DATA); +} + bool ReadSymbolData(const string& obj_file, const std::vector& debug_dirs, const DumpOptions& options, diff --git a/src/common/linux/dump_symbols.h b/src/common/linux/dump_symbols.h index 636bb72f..1f204cba 100644 --- a/src/common/linux/dump_symbols.h +++ b/src/common/linux/dump_symbols.h @@ -67,6 +67,12 @@ bool WriteSymbolFile(const string &obj_file, const DumpOptions& options, std::ostream &sym_stream); +// Read the selected object file's debugging information, and write out the +// header only to |stream|. Return true on success; if an error occurs, report +// it and return false. +bool WriteSymbolFileHeader(const string& obj_file, + std::ostream &sym_stream); + // As above, but simply return the debugging information in MODULE // instead of writing it to a stream. The caller owns the resulting // Module object and must delete it when finished. diff --git a/src/common/mac/dump_syms.cc b/src/common/mac/dump_syms.cc index b86a7c26..97c3e06e 100644 --- a/src/common/mac/dump_syms.cc +++ b/src/common/mac/dump_syms.cc @@ -328,6 +328,65 @@ class DumpSymbols::DumperLineToModule: dwarf2reader::ByteReader *byte_reader_; // WEAK }; +bool DumpSymbols::CreateEmptyModule(scoped_ptr& module) { + // Select an object file, if SetArchitecture hasn't been called to set one + // explicitly. + if (!selected_object_file_) { + // If there's only one architecture, that's the one. + if (object_files_.size() == 1) + selected_object_file_ = &object_files_[0]; + else { + // Look for an object file whose architecture matches our own. + const NXArchInfo *local_arch = NXGetLocalArchInfo(); + if (!SetArchitecture(local_arch->cputype, local_arch->cpusubtype)) { + fprintf(stderr, "%s: object file contains more than one" + " architecture, none of which match the current" + " architecture; specify an architecture explicitly" + " with '-a ARCH' to resolve the ambiguity\n", + object_filename_.c_str()); + return false; + } + } + } + + assert(selected_object_file_); + + // Find the name of the selected file's architecture, to appear in + // the MODULE record and in error messages. + const NXArchInfo *selected_arch_info = + google_breakpad::BreakpadGetArchInfoFromCpuType( + selected_object_file_->cputype, selected_object_file_->cpusubtype); + + const char *selected_arch_name = selected_arch_info->name; + if (strcmp(selected_arch_name, "i386") == 0) + selected_arch_name = "x86"; + + // Produce a name to use in error messages that includes the + // filename, and the architecture, if there is more than one. + selected_object_name_ = object_filename_; + if (object_files_.size() > 1) { + selected_object_name_ += ", architecture "; + selected_object_name_ + selected_arch_name; + } + + // Compute a module name, to appear in the MODULE record. + string module_name = object_filename_; + module_name = basename(&module_name[0]); + + // Choose an identifier string, to appear in the MODULE record. + string identifier = Identifier(); + if (identifier.empty()) + return false; + identifier += "0"; + + // Create a module to hold the debugging information. + module.reset(new Module(module_name, + "mac", + selected_arch_name, + identifier)); + return true; +} + bool DumpSymbols::ReadDwarf(google_breakpad::Module *module, const mach_o::Reader &macho_reader, const mach_o::SectionMap &dwarf_sections, @@ -535,61 +594,9 @@ bool DumpSymbols::LoadCommandDumper::SymtabCommand(const ByteBuffer &entries, } bool DumpSymbols::ReadSymbolData(Module** out_module) { - // Select an object file, if SetArchitecture hasn't been called to set one - // explicitly. - if (!selected_object_file_) { - // If there's only one architecture, that's the one. - if (object_files_.size() == 1) - selected_object_file_ = &object_files_[0]; - else { - // Look for an object file whose architecture matches our own. - const NXArchInfo *local_arch = NXGetLocalArchInfo(); - if (!SetArchitecture(local_arch->cputype, local_arch->cpusubtype)) { - fprintf(stderr, "%s: object file contains more than one" - " architecture, none of which match the current" - " architecture; specify an architecture explicitly" - " with '-a ARCH' to resolve the ambiguity\n", - object_filename_.c_str()); - return false; - } - } - } - - assert(selected_object_file_); - - // Find the name of the selected file's architecture, to appear in - // the MODULE record and in error messages. - const NXArchInfo *selected_arch_info = - google_breakpad::BreakpadGetArchInfoFromCpuType( - selected_object_file_->cputype, selected_object_file_->cpusubtype); - - const char *selected_arch_name = selected_arch_info->name; - if (strcmp(selected_arch_name, "i386") == 0) - selected_arch_name = "x86"; - - // Produce a name to use in error messages that includes the - // filename, and the architecture, if there is more than one. - selected_object_name_ = object_filename_; - if (object_files_.size() > 1) { - selected_object_name_ += ", architecture "; - selected_object_name_ + selected_arch_name; - } - - // Compute a module name, to appear in the MODULE record. - string module_name = object_filename_; - module_name = basename(&module_name[0]); - - // Choose an identifier string, to appear in the MODULE record. - string identifier = Identifier(); - if (identifier.empty()) + scoped_ptr module; + if (!CreateEmptyModule(module)) return false; - identifier += "0"; - - // Create a module to hold the debugging information. - scoped_ptr module(new Module(module_name, - "mac", - selected_arch_name, - identifier)); // Parse the selected object file. mach_o::Reader::Reporter reporter(selected_object_name_); @@ -624,4 +631,15 @@ bool DumpSymbols::WriteSymbolFile(std::ostream &stream) { return false; } +// Read the selected object file's debugging information, and write out the +// header only to |stream|. Return true on success; if an error occurs, report +// it and return false. +bool DumpSymbols::WriteSymbolFileHeader(std::ostream &stream) { + scoped_ptr module; + if (!CreateEmptyModule(module)) + return false; + + return module->Write(stream, symbol_data_); +} + } // namespace google_breakpad diff --git a/src/common/mac/dump_syms.h b/src/common/mac/dump_syms.h index b09928c9..9463f7dc 100644 --- a/src/common/mac/dump_syms.h +++ b/src/common/mac/dump_syms.h @@ -112,6 +112,11 @@ class DumpSymbols { // return false. bool WriteSymbolFile(std::ostream &stream); + // Read the selected object file's debugging information, and write out the + // header only to |stream|. Return true on success; if an error occurs, report + // it and return false. + bool WriteSymbolFileHeader(std::ostream &stream); + // As above, but simply return the debugging information in module // instead of writing it to a stream. The caller owns the resulting // module object and must delete it when finished. @@ -130,6 +135,10 @@ class DumpSymbols { // Return an identifier string for the file this DumpSymbols is dumping. std::string Identifier(); + + // Creates an empty module object. + bool CreateEmptyModule(scoped_ptr& module); + // Read debugging information from |dwarf_sections|, which was taken from // |macho_reader|, and add it to |module|. On success, return true; // on failure, report the problem and return false. diff --git a/src/tools/linux/dump_syms/dump_syms.cc b/src/tools/linux/dump_syms/dump_syms.cc index c51ae8cd..84953172 100644 --- a/src/tools/linux/dump_syms/dump_syms.cc +++ b/src/tools/linux/dump_syms/dump_syms.cc @@ -39,11 +39,13 @@ #include "common/linux/dump_symbols.h" using google_breakpad::WriteSymbolFile; +using google_breakpad::WriteSymbolFileHeader; int usage(const char* self) { fprintf(stderr, "Usage: %s [OPTION] " "[directories-for-debug-file]\n\n", self); fprintf(stderr, "Options:\n"); + fprintf(stderr, " -i: Output module header information only.\n"); fprintf(stderr, " -c Do not generate CFI section\n"); fprintf(stderr, " -r Do not handle inter-compilation unit references\n"); fprintf(stderr, " -v Print all warnings to stderr\n"); @@ -53,27 +55,29 @@ int usage(const char* self) { int main(int argc, char **argv) { if (argc < 2) return usage(argv[0]); - + bool header_only = false; bool cfi = true; bool handle_inter_cu_refs = true; bool log_to_stderr = false; int arg_index = 1; while (arg_index < argc && strlen(argv[arg_index]) > 0 && argv[arg_index][0] == '-') { - if (strcmp("-c", argv[arg_index]) == 0) { + if (strcmp("-i", argv[arg_index]) == 0) { + header_only = true; + } else if (strcmp("-c", argv[arg_index]) == 0) { cfi = false; } else if (strcmp("-r", argv[arg_index]) == 0) { handle_inter_cu_refs = false; } else if (strcmp("-v", argv[arg_index]) == 0) { log_to_stderr = true; } else { + printf("2.4 %s\n", argv[arg_index]); return usage(argv[0]); } ++arg_index; } if (arg_index == argc) return usage(argv[0]); - // Save stderr so it can be used below. FILE* saved_stderr = fdopen(dup(fileno(stderr)), "w"); if (!log_to_stderr) { @@ -82,7 +86,6 @@ int main(int argc, char **argv) { // Add this brace section to silence gcc warnings. } } - const char* binary; std::vector debug_dirs; binary = argv[arg_index]; @@ -92,11 +95,18 @@ int main(int argc, char **argv) { debug_dirs.push_back(argv[debug_dir_index]); } - SymbolData symbol_data = cfi ? ALL_SYMBOL_DATA : NO_CFI; - google_breakpad::DumpOptions options(symbol_data, handle_inter_cu_refs); - if (!WriteSymbolFile(binary, debug_dirs, options, std::cout)) { - fprintf(saved_stderr, "Failed to write symbol file.\n"); - return 1; + if (header_only) { + if (!WriteSymbolFileHeader(binary, std::cout)) { + fprintf(saved_stderr, "Failed to process file.\n"); + return 1; + } + } else { + SymbolData symbol_data = cfi ? ALL_SYMBOL_DATA : NO_CFI; + google_breakpad::DumpOptions options(symbol_data, handle_inter_cu_refs); + if (!WriteSymbolFile(binary, debug_dirs, options, std::cout)) { + fprintf(saved_stderr, "Failed to write symbol file.\n"); + return 1; + } } return 0; diff --git a/src/tools/mac/dump_syms/dump_syms_tool.cc b/src/tools/mac/dump_syms/dump_syms_tool.cc index 54f29226..6f68457b 100644 --- a/src/tools/mac/dump_syms/dump_syms_tool.cc +++ b/src/tools/mac/dump_syms/dump_syms_tool.cc @@ -51,11 +51,13 @@ using std::vector; struct Options { Options() - : srcPath(), dsymPath(), arch(), cfi(true), handle_inter_cu_refs(true) {} + : srcPath(), dsymPath(), arch(), header_only(false), + cfi(true), handle_inter_cu_refs(true) {} string srcPath; string dsymPath; const NXArchInfo *arch; + bool header_only; bool cfi; bool handle_inter_cu_refs; }; @@ -151,6 +153,9 @@ static bool Start(const Options &options) { } } + if (options.header_only) + return dump_symbols.WriteSymbolFileHeader(std::cout); + // Read the primary file into a Breakpad Module. Module* module = NULL; if (!dump_symbols.ReadSymbolData(&module)) @@ -189,6 +194,7 @@ 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] [-g dSYM path] " "\n", argv[0]); + fprintf(stderr, "\t-i: Output module header information only.\n"); 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 " @@ -204,8 +210,11 @@ static void SetupOptions(int argc, const char *argv[], Options *options) { extern int optind; signed char ch; - while ((ch = getopt(argc, (char * const *)argv, "a:g:chr?")) != -1) { + while ((ch = getopt(argc, (char * const *)argv, "ia:g:chr?")) != -1) { switch (ch) { + case 'i': + options->header_only = true; + break; case 'a': { const NXArchInfo *arch_info = google_breakpad::BreakpadGetArchInfoFromName(optarg);