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 <vapier@chromium.org>
This commit is contained in:
Jake Ehrlich 2019-10-04 13:29:26 -07:00 committed by Mike Frysinger
parent 3e56ef9d4e
commit 17958ef62c
4 changed files with 73 additions and 26 deletions

View File

@ -921,6 +921,7 @@ bool SanitizeDebugFile(const typename ElfClass::Ehdr* debug_elf_header,
template<typename ElfClass> template<typename ElfClass>
bool InitModuleForElfClass(const typename ElfClass::Ehdr* elf_header, bool InitModuleForElfClass(const typename ElfClass::Ehdr* elf_header,
const string& obj_filename, const string& obj_filename,
const string& obj_os,
scoped_ptr<Module>& module) { scoped_ptr<Module>& module) {
PageAllocator allocator; PageAllocator allocator;
wasteful_vector<uint8_t> identifier(&allocator, kDefaultBuildIdSize); wasteful_vector<uint8_t> identifier(&allocator, kDefaultBuildIdSize);
@ -945,7 +946,6 @@ bool InitModuleForElfClass(const typename ElfClass::Ehdr* elf_header,
? name_buf ? name_buf
: google_breakpad::BaseName(obj_filename); : google_breakpad::BaseName(obj_filename);
string os = "Linux";
// Add an extra "0" at the end. PDB files on Windows have an 'age' // 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 // number appended to the end of the file identifier; this isn't
// really used or necessary on other platforms, but be consistent. // 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. // This is just the raw Build ID in hex.
string code_id = FileID::ConvertIdentifierToString(identifier); 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; return true;
} }
@ -961,6 +961,7 @@ bool InitModuleForElfClass(const typename ElfClass::Ehdr* elf_header,
template<typename ElfClass> template<typename ElfClass>
bool ReadSymbolDataElfClass(const typename ElfClass::Ehdr* elf_header, bool ReadSymbolDataElfClass(const typename ElfClass::Ehdr* elf_header,
const string& obj_filename, const string& obj_filename,
const string& obj_os,
const std::vector<string>& debug_dirs, const std::vector<string>& debug_dirs,
const DumpOptions& options, const DumpOptions& options,
Module** out_module) { Module** out_module) {
@ -969,7 +970,8 @@ bool ReadSymbolDataElfClass(const typename ElfClass::Ehdr* elf_header,
*out_module = NULL; *out_module = NULL;
scoped_ptr<Module> module; scoped_ptr<Module> module;
if (!InitModuleForElfClass<ElfClass>(elf_header, obj_filename, module)) { if (!InitModuleForElfClass<ElfClass>(elf_header, obj_filename, obj_os,
module)) {
return false; return false;
} }
@ -1017,6 +1019,7 @@ namespace google_breakpad {
// Not explicitly exported, but not static so it can be used in unit tests. // Not explicitly exported, but not static so it can be used in unit tests.
bool ReadSymbolDataInternal(const uint8_t* obj_file, bool ReadSymbolDataInternal(const uint8_t* obj_file,
const string& obj_filename, const string& obj_filename,
const string& obj_os,
const std::vector<string>& debug_dirs, const std::vector<string>& debug_dirs,
const DumpOptions& options, const DumpOptions& options,
Module** module) { Module** module) {
@ -1028,24 +1031,27 @@ bool ReadSymbolDataInternal(const uint8_t* obj_file,
int elfclass = ElfClass(obj_file); int elfclass = ElfClass(obj_file);
if (elfclass == ELFCLASS32) { if (elfclass == ELFCLASS32) {
return ReadSymbolDataElfClass<ElfClass32>( return ReadSymbolDataElfClass<ElfClass32>(
reinterpret_cast<const Elf32_Ehdr*>(obj_file), obj_filename, debug_dirs, reinterpret_cast<const Elf32_Ehdr*>(obj_file), obj_filename, obj_os,
options, module); debug_dirs, options, module);
} }
if (elfclass == ELFCLASS64) { if (elfclass == ELFCLASS64) {
return ReadSymbolDataElfClass<ElfClass64>( return ReadSymbolDataElfClass<ElfClass64>(
reinterpret_cast<const Elf64_Ehdr*>(obj_file), obj_filename, debug_dirs, reinterpret_cast<const Elf64_Ehdr*>(obj_file), obj_filename, obj_os,
options, module); debug_dirs, options, module);
} }
return false; 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<string>& debug_dirs, const std::vector<string>& debug_dirs,
const DumpOptions& options, const DumpOptions& options,
std::ostream &sym_stream) { std::ostream &sym_stream) {
Module* module; Module* module;
if (!ReadSymbolData(obj_file, debug_dirs, options, &module)) if (!ReadSymbolData(load_path, obj_file, obj_os, debug_dirs, options,
&module))
return false; return false;
bool result = module->Write(sym_stream, options.symbol_data); 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 // 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 // header only to |stream|. Return true on success; if an error occurs, report
// it and return false. // 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) { std::ostream &sym_stream) {
MmapWrapper map_wrapper; MmapWrapper map_wrapper;
void* elf_header = NULL; 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()); fprintf(stderr, "Could not load ELF file: %s\n", obj_file.c_str());
return false; return false;
} }
@ -1074,13 +1082,15 @@ bool WriteSymbolFileHeader(const string& obj_file,
scoped_ptr<Module> module; scoped_ptr<Module> module;
if (elfclass == ELFCLASS32) { if (elfclass == ELFCLASS32) {
if (!InitModuleForElfClass<ElfClass32>( if (!InitModuleForElfClass<ElfClass32>(
reinterpret_cast<const Elf32_Ehdr*>(elf_header), obj_file, module)) { reinterpret_cast<const Elf32_Ehdr*>(elf_header), obj_file, obj_os,
module)) {
fprintf(stderr, "Failed to load ELF module: %s\n", obj_file.c_str()); fprintf(stderr, "Failed to load ELF module: %s\n", obj_file.c_str());
return false; return false;
} }
} else if (elfclass == ELFCLASS64) { } else if (elfclass == ELFCLASS64) {
if (!InitModuleForElfClass<ElfClass64>( if (!InitModuleForElfClass<ElfClass64>(
reinterpret_cast<const Elf64_Ehdr*>(elf_header), obj_file, module)) { reinterpret_cast<const Elf64_Ehdr*>(elf_header), obj_file, obj_os,
module)) {
fprintf(stderr, "Failed to load ELF module: %s\n", obj_file.c_str()); fprintf(stderr, "Failed to load ELF module: %s\n", obj_file.c_str());
return false; return false;
} }
@ -1092,17 +1102,19 @@ bool WriteSymbolFileHeader(const string& obj_file,
return module->Write(sym_stream, ALL_SYMBOL_DATA); 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<string>& debug_dirs, const std::vector<string>& debug_dirs,
const DumpOptions& options, const DumpOptions& options,
Module** module) { Module** module) {
MmapWrapper map_wrapper; MmapWrapper map_wrapper;
void* elf_header = NULL; void* elf_header = NULL;
if (!LoadELF(obj_file, &map_wrapper, &elf_header)) if (!LoadELF(load_path, &map_wrapper, &elf_header))
return false; return false;
return ReadSymbolDataInternal(reinterpret_cast<uint8_t*>(elf_header), return ReadSymbolDataInternal(reinterpret_cast<uint8_t*>(elf_header),
obj_file, debug_dirs, options, module); obj_file, obj_os, debug_dirs, options, module);
} }
} // namespace google_breakpad } // namespace google_breakpad

View File

@ -62,21 +62,28 @@ struct DumpOptions {
// If OBJ_FILE has been stripped but contains a .gnu_debuglink section, // If OBJ_FILE has been stripped but contains a .gnu_debuglink section,
// then look for the debug file in DEBUG_DIRS. // then look for the debug file in DEBUG_DIRS.
// SYMBOL_DATA allows limiting the type of symbol data written. // 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<string>& debug_dirs, const std::vector<string>& debug_dirs,
const DumpOptions& options, const DumpOptions& options,
std::ostream &sym_stream); std::ostream &sym_stream);
// Read the selected object file's debugging information, and write out the // 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 // header only to |stream|. Return true on success; if an error occurs, report
// it and return false. // it and return false. |obj_file| becomes the MODULE file name and |obj_os|
bool WriteSymbolFileHeader(const string& obj_file, // becomes the MODULE operating system.
bool WriteSymbolFileHeader(const string& load_path,
const string& obj_file,
const string& obj_os,
std::ostream &sym_stream); std::ostream &sym_stream);
// As above, but simply return the debugging information in MODULE // As above, but simply return the debugging information in MODULE
// instead of writing it to a stream. The caller owns the resulting // instead of writing it to a stream. The caller owns the resulting
// Module object and must delete it when finished. // 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<string>& debug_dirs, const std::vector<string>& debug_dirs,
const DumpOptions& options, const DumpOptions& options,
Module** module); Module** module);

View File

@ -51,6 +51,7 @@ namespace google_breakpad {
bool ReadSymbolDataInternal(const uint8_t* obj_file, bool ReadSymbolDataInternal(const uint8_t* obj_file,
const string& obj_filename, const string& obj_filename,
const string& obj_os,
const std::vector<string>& debug_dir, const std::vector<string>& debug_dir,
const DumpOptions& options, const DumpOptions& options,
Module** module); Module** module);
@ -94,6 +95,7 @@ TYPED_TEST(DumpSymbols, Invalid) {
DumpOptions options(ALL_SYMBOL_DATA, true); DumpOptions options(ALL_SYMBOL_DATA, true);
EXPECT_FALSE(ReadSymbolDataInternal(reinterpret_cast<uint8_t*>(&header), EXPECT_FALSE(ReadSymbolDataInternal(reinterpret_cast<uint8_t*>(&header),
"foo", "foo",
"Linux",
vector<string>(), vector<string>(),
options, options,
&module)); &module));
@ -130,6 +132,7 @@ TYPED_TEST(DumpSymbols, SimplePublic) {
DumpOptions options(ALL_SYMBOL_DATA, true); DumpOptions options(ALL_SYMBOL_DATA, true);
EXPECT_TRUE(ReadSymbolDataInternal(this->elfdata, EXPECT_TRUE(ReadSymbolDataInternal(this->elfdata,
"foo", "foo",
"Linux",
vector<string>(), vector<string>(),
options, options,
&module)); &module));
@ -186,6 +189,7 @@ TYPED_TEST(DumpSymbols, SimpleBuildID) {
DumpOptions options(ALL_SYMBOL_DATA, true); DumpOptions options(ALL_SYMBOL_DATA, true);
EXPECT_TRUE(ReadSymbolDataInternal(this->elfdata, EXPECT_TRUE(ReadSymbolDataInternal(this->elfdata,
"foo", "foo",
"Linux",
vector<string>(), vector<string>(),
options, options,
&module)); &module));

View File

@ -45,10 +45,14 @@ int usage(const char* self) {
fprintf(stderr, "Usage: %s [OPTION] <binary-with-debugging-info> " fprintf(stderr, "Usage: %s [OPTION] <binary-with-debugging-info> "
"[directories-for-debug-file]\n\n", self); "[directories-for-debug-file]\n\n", self);
fprintf(stderr, "Options:\n"); fprintf(stderr, "Options:\n");
fprintf(stderr, " -i: Output module header information only.\n"); fprintf(stderr, " -i: Output module header information only.\n");
fprintf(stderr, " -c Do not generate CFI section\n"); fprintf(stderr, " -c Do not generate CFI section\n");
fprintf(stderr, " -r Do not handle inter-compilation unit references\n"); fprintf(stderr, " -r Do not handle inter-compilation "
fprintf(stderr, " -v Print all warnings to stderr\n"); "unit references\n");
fprintf(stderr, " -v Print all warnings to stderr\n");
fprintf(stderr, " -n <name> Use specified name for name of the object\n");
fprintf(stderr, " -o <os> Use specified name for the "
"operating system\n");
return 1; return 1;
} }
@ -59,6 +63,8 @@ int main(int argc, char **argv) {
bool cfi = true; bool cfi = true;
bool handle_inter_cu_refs = true; bool handle_inter_cu_refs = true;
bool log_to_stderr = false; bool log_to_stderr = false;
std::string obj_name;
const char* obj_os = "Linux";
int arg_index = 1; int arg_index = 1;
while (arg_index < argc && strlen(argv[arg_index]) > 0 && while (arg_index < argc && strlen(argv[arg_index]) > 0 &&
argv[arg_index][0] == '-') { argv[arg_index][0] == '-') {
@ -70,6 +76,20 @@ int main(int argc, char **argv) {
handle_inter_cu_refs = false; handle_inter_cu_refs = false;
} else if (strcmp("-v", argv[arg_index]) == 0) { } else if (strcmp("-v", argv[arg_index]) == 0) {
log_to_stderr = true; 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 { } else {
printf("2.4 %s\n", argv[arg_index]); printf("2.4 %s\n", argv[arg_index]);
return usage(argv[0]); return usage(argv[0]);
@ -95,15 +115,19 @@ int main(int argc, char **argv) {
debug_dirs.push_back(argv[debug_dir_index]); debug_dirs.push_back(argv[debug_dir_index]);
} }
if (obj_name.empty())
obj_name = binary;
if (header_only) { 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"); fprintf(saved_stderr, "Failed to process file.\n");
return 1; return 1;
} }
} else { } else {
SymbolData symbol_data = cfi ? ALL_SYMBOL_DATA : NO_CFI; SymbolData symbol_data = cfi ? ALL_SYMBOL_DATA : NO_CFI;
google_breakpad::DumpOptions options(symbol_data, handle_inter_cu_refs); 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"); fprintf(saved_stderr, "Failed to write symbol file.\n");
return 1; return 1;
} }