From 17958ef62cc5fe8208a965d50f41fc70c5fc116f Mon Sep 17 00:00:00 2001 From: Jake Ehrlich Date: Fri, 4 Oct 2019 13:29:26 -0700 Subject: Add options to set OS and filename This allows Fuchsia to use dump_syms directly without a postprocessing step. Change-Id: I84507f8bedddfcdcdb237119457c8ddf8ac354d5 Reviewed-on: https://chromium-review.googlesource.com/c/breakpad/breakpad/+/1850718 Reviewed-by: Mike Frysinger --- src/common/linux/dump_symbols.cc | 44 ++++++++++++++++++++----------- src/common/linux/dump_symbols.h | 15 ++++++++--- src/common/linux/dump_symbols_unittest.cc | 4 +++ src/tools/linux/dump_syms/dump_syms.cc | 36 ++++++++++++++++++++----- 4 files changed, 73 insertions(+), 26 deletions(-) diff --git a/src/common/linux/dump_symbols.cc b/src/common/linux/dump_symbols.cc index 40bb3ff2..660f133e 100644 --- a/src/common/linux/dump_symbols.cc +++ b/src/common/linux/dump_symbols.cc @@ -921,6 +921,7 @@ bool SanitizeDebugFile(const typename ElfClass::Ehdr* debug_elf_header, template bool InitModuleForElfClass(const typename ElfClass::Ehdr* elf_header, const string& obj_filename, + const string& obj_os, scoped_ptr& module) { PageAllocator allocator; wasteful_vector identifier(&allocator, kDefaultBuildIdSize); @@ -945,7 +946,6 @@ bool InitModuleForElfClass(const typename ElfClass::Ehdr* elf_header, ? name_buf : google_breakpad::BaseName(obj_filename); - string os = "Linux"; // Add an extra "0" at the end. PDB files on Windows have an 'age' // number appended to the end of the file identifier; this isn't // really used or necessary on other platforms, but be consistent. @@ -953,7 +953,7 @@ bool InitModuleForElfClass(const typename ElfClass::Ehdr* elf_header, // This is just the raw Build ID in hex. string code_id = FileID::ConvertIdentifierToString(identifier); - module.reset(new Module(name, os, architecture, id, code_id)); + module.reset(new Module(name, obj_os, architecture, id, code_id)); return true; } @@ -961,6 +961,7 @@ bool InitModuleForElfClass(const typename ElfClass::Ehdr* elf_header, template bool ReadSymbolDataElfClass(const typename ElfClass::Ehdr* elf_header, const string& obj_filename, + const string& obj_os, const std::vector& debug_dirs, const DumpOptions& options, Module** out_module) { @@ -969,7 +970,8 @@ bool ReadSymbolDataElfClass(const typename ElfClass::Ehdr* elf_header, *out_module = NULL; scoped_ptr module; - if (!InitModuleForElfClass(elf_header, obj_filename, module)) { + if (!InitModuleForElfClass(elf_header, obj_filename, obj_os, + module)) { return false; } @@ -1017,6 +1019,7 @@ namespace google_breakpad { // Not explicitly exported, but not static so it can be used in unit tests. bool ReadSymbolDataInternal(const uint8_t* obj_file, const string& obj_filename, + const string& obj_os, const std::vector& debug_dirs, const DumpOptions& options, Module** module) { @@ -1028,24 +1031,27 @@ bool ReadSymbolDataInternal(const uint8_t* obj_file, int elfclass = ElfClass(obj_file); if (elfclass == ELFCLASS32) { return ReadSymbolDataElfClass( - reinterpret_cast(obj_file), obj_filename, debug_dirs, - options, module); + reinterpret_cast(obj_file), obj_filename, obj_os, + debug_dirs, options, module); } if (elfclass == ELFCLASS64) { return ReadSymbolDataElfClass( - reinterpret_cast(obj_file), obj_filename, debug_dirs, - options, module); + reinterpret_cast(obj_file), obj_filename, obj_os, + debug_dirs, options, module); } return false; } -bool WriteSymbolFile(const string &obj_file, +bool WriteSymbolFile(const string &load_path, + const string &obj_file, + const string &obj_os, const std::vector& debug_dirs, const DumpOptions& options, std::ostream &sym_stream) { Module* module; - if (!ReadSymbolData(obj_file, debug_dirs, options, &module)) + if (!ReadSymbolData(load_path, obj_file, obj_os, debug_dirs, options, + &module)) return false; bool result = module->Write(sym_stream, options.symbol_data); @@ -1056,11 +1062,13 @@ bool WriteSymbolFile(const string &obj_file, // 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, +bool WriteSymbolFileHeader(const string& load_path, + const string& obj_file, + const string& obj_os, std::ostream &sym_stream) { MmapWrapper map_wrapper; void* elf_header = NULL; - if (!LoadELF(obj_file, &map_wrapper, &elf_header)) { + if (!LoadELF(load_path, &map_wrapper, &elf_header)) { fprintf(stderr, "Could not load ELF file: %s\n", obj_file.c_str()); return false; } @@ -1074,13 +1082,15 @@ bool WriteSymbolFileHeader(const string& obj_file, scoped_ptr module; if (elfclass == ELFCLASS32) { if (!InitModuleForElfClass( - reinterpret_cast(elf_header), obj_file, module)) { + reinterpret_cast(elf_header), obj_file, obj_os, + 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)) { + reinterpret_cast(elf_header), obj_file, obj_os, + module)) { fprintf(stderr, "Failed to load ELF module: %s\n", obj_file.c_str()); return false; } @@ -1092,17 +1102,19 @@ bool WriteSymbolFileHeader(const string& obj_file, return module->Write(sym_stream, ALL_SYMBOL_DATA); } -bool ReadSymbolData(const string& obj_file, +bool ReadSymbolData(const string& load_path, + const string& obj_file, + const string& obj_os, const std::vector& debug_dirs, const DumpOptions& options, Module** module) { MmapWrapper map_wrapper; void* elf_header = NULL; - if (!LoadELF(obj_file, &map_wrapper, &elf_header)) + if (!LoadELF(load_path, &map_wrapper, &elf_header)) return false; return ReadSymbolDataInternal(reinterpret_cast(elf_header), - obj_file, debug_dirs, options, module); + obj_file, obj_os, debug_dirs, options, module); } } // namespace google_breakpad diff --git a/src/common/linux/dump_symbols.h b/src/common/linux/dump_symbols.h index 1f204cba..eaddd8b2 100644 --- a/src/common/linux/dump_symbols.h +++ b/src/common/linux/dump_symbols.h @@ -62,21 +62,28 @@ struct DumpOptions { // If OBJ_FILE has been stripped but contains a .gnu_debuglink section, // then look for the debug file in DEBUG_DIRS. // SYMBOL_DATA allows limiting the type of symbol data written. -bool WriteSymbolFile(const string &obj_file, +bool WriteSymbolFile(const string &load_path, + const string &obj_file, + const string &obj_os, const std::vector& debug_dirs, 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, +// it and return false. |obj_file| becomes the MODULE file name and |obj_os| +// becomes the MODULE operating system. +bool WriteSymbolFileHeader(const string& load_path, + const string& obj_file, + const string& obj_os, 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. -bool ReadSymbolData(const string& obj_file, +bool ReadSymbolData(const string& load_path, + const string& obj_file, + const string& obj_os, const std::vector& debug_dirs, const DumpOptions& options, Module** module); diff --git a/src/common/linux/dump_symbols_unittest.cc b/src/common/linux/dump_symbols_unittest.cc index f011c8a7..54c21096 100644 --- a/src/common/linux/dump_symbols_unittest.cc +++ b/src/common/linux/dump_symbols_unittest.cc @@ -51,6 +51,7 @@ namespace google_breakpad { bool ReadSymbolDataInternal(const uint8_t* obj_file, const string& obj_filename, + const string& obj_os, const std::vector& debug_dir, const DumpOptions& options, Module** module); @@ -94,6 +95,7 @@ TYPED_TEST(DumpSymbols, Invalid) { DumpOptions options(ALL_SYMBOL_DATA, true); EXPECT_FALSE(ReadSymbolDataInternal(reinterpret_cast(&header), "foo", + "Linux", vector(), options, &module)); @@ -130,6 +132,7 @@ TYPED_TEST(DumpSymbols, SimplePublic) { DumpOptions options(ALL_SYMBOL_DATA, true); EXPECT_TRUE(ReadSymbolDataInternal(this->elfdata, "foo", + "Linux", vector(), options, &module)); @@ -186,6 +189,7 @@ TYPED_TEST(DumpSymbols, SimpleBuildID) { DumpOptions options(ALL_SYMBOL_DATA, true); EXPECT_TRUE(ReadSymbolDataInternal(this->elfdata, "foo", + "Linux", vector(), options, &module)); diff --git a/src/tools/linux/dump_syms/dump_syms.cc b/src/tools/linux/dump_syms/dump_syms.cc index 84953172..ebdf2314 100644 --- a/src/tools/linux/dump_syms/dump_syms.cc +++ b/src/tools/linux/dump_syms/dump_syms.cc @@ -45,10 +45,14 @@ 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"); + 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"); + fprintf(stderr, " -n Use specified name for name of the object\n"); + fprintf(stderr, " -o Use specified name for the " + "operating system\n"); return 1; } @@ -59,6 +63,8 @@ int main(int argc, char **argv) { bool cfi = true; bool handle_inter_cu_refs = true; bool log_to_stderr = false; + std::string obj_name; + const char* obj_os = "Linux"; int arg_index = 1; while (arg_index < argc && strlen(argv[arg_index]) > 0 && argv[arg_index][0] == '-') { @@ -70,6 +76,20 @@ int main(int argc, char **argv) { handle_inter_cu_refs = false; } else if (strcmp("-v", argv[arg_index]) == 0) { log_to_stderr = true; + } else if (strcmp("-n", argv[arg_index]) == 0) { + if (arg_index + 1 >= argc) { + fprintf(stderr, "Missing argument to -n\n"); + return usage(argv[0]); + } + obj_name = argv[arg_index + 1]; + ++arg_index; + } else if (strcmp("-o", argv[arg_index]) == 0) { + if (arg_index + 1 >= argc) { + fprintf(stderr, "Missing argument to -o\n"); + return usage(argv[0]); + } + obj_os = argv[arg_index + 1]; + ++arg_index; } else { printf("2.4 %s\n", argv[arg_index]); return usage(argv[0]); @@ -95,15 +115,19 @@ int main(int argc, char **argv) { debug_dirs.push_back(argv[debug_dir_index]); } + if (obj_name.empty()) + obj_name = binary; + if (header_only) { - if (!WriteSymbolFileHeader(binary, std::cout)) { + if (!WriteSymbolFileHeader(binary, obj_name, obj_os, 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)) { + if (!WriteSymbolFile(binary, obj_name, obj_os, debug_dirs, options, + std::cout)) { fprintf(saved_stderr, "Failed to write symbol file.\n"); return 1; } -- cgit v1.2.1