| 1 |
#include <lyra/lyra.hpp> |
| 2 |
#include <fmt/format.h> |
| 3 |
#include <fmt/std.h> |
| 4 |
#include <fmt/ostream.h> |
| 5 |
#include <cpptrace/cpptrace.hpp> |
| 6 |
#include <cpptrace/from_current.hpp> |
| 7 |
|
| 8 |
#include <filesystem> |
| 9 |
|
| 10 |
#include "binary/elf.hpp" |
| 11 |
#include "binary/mach-o.hpp" |
| 12 |
#include "cpptrace/utils.hpp" |
| 13 |
|
| 14 |
using namespace std::literals; |
| 15 |
using namespace cpptrace::detail; |
| 16 |
|
| 17 |
template<> struct fmt::formatter<lyra::cli> : ostream_formatter {}; |
| 18 |
|
| 19 |
struct options { |
| 20 |
std::filesystem::path path; |
| 21 |
bool demangle = false; |
| 22 |
bool prune = false; |
| 23 |
}; |
| 24 |
|
| 25 |
#if IS_LINUX |
| 26 |
void dump_symtab_result( |
| 27 |
const Result<optional<std::vector<elf::symbol_entry>>, internal_error>& res, |
| 28 |
const options& options |
| 29 |
) { |
| 30 |
if(!res) { |
| 31 |
fmt::println(stderr, "Error loading: {}", res.unwrap_error().what()); |
| 32 |
} |
| 33 |
const auto& entries_ = res.unwrap_value(); |
| 34 |
if(!entries_) { |
| 35 |
fmt::println("Empty symbol table"); |
| 36 |
} |
| 37 |
const auto& entries = entries_.unwrap(); |
| 38 |
fmt::println("{:16} {:16} {:4} {}", "value", "size", "shdx", "symbol"); |
| 39 |
for(const auto& entry : entries) { |
| 40 |
std::string name; |
| 41 |
if(options.demangle) { |
| 42 |
name = cpptrace::demangle(entry.st_name); |
| 43 |
if(options.prune) { |
| 44 |
name = cpptrace::prune_symbol(name); |
| 45 |
} |
| 46 |
} else { |
| 47 |
name = entry.st_name; |
| 48 |
} |
| 49 |
fmt::println("{:016x} {:016x} {:04x} {}", entry.st_value, entry.st_size, entry.st_shndx, name); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
auto get_elf(const std::filesystem::path& path) { |
| 54 |
auto elf_ = elf::open(path.native()); |
| 55 |
if(!elf_) { |
| 56 |
fmt::println(stderr, "Error reading file: {}", elf_.unwrap_error().what()); |
| 57 |
} |
| 58 |
return std::move(elf_).unwrap_value(); |
| 59 |
} |
| 60 |
|
| 61 |
void dump_symbols(const options& options) { |
| 62 |
auto elf = get_elf(options.path); |
| 63 |
fmt::println("Symtab:"); |
| 64 |
dump_symtab_result(elf.get_symtab_entries(), options); |
| 65 |
fmt::println("Dynamic symtab:"); |
| 66 |
dump_symtab_result(elf.get_dynamic_symtab_entries(), options); |
| 67 |
} |
| 68 |
void lookup_symbol(const options& options, cpptrace::frame_ptr address) { |
| 69 |
auto elf = get_elf(options.path); |
| 70 |
if(auto symbol = elf.lookup_symbol(address)) { |
| 71 |
fmt::println("Symbol: {}", options.demangle ? cpptrace::demangle(symbol.unwrap()) : symbol.unwrap()); |
| 72 |
} else { |
| 73 |
fmt::println("Could not find symbol"); |
| 74 |
} |
| 75 |
} |
| 76 |
#elif IS_APPLE |
| 77 |
void dump_symbols(const options&) { |
| 78 |
fmt::println("Not implemented yet (TODO)"); |
| 79 |
} |
| 80 |
void lookup_symbol(const options&, cpptrace::frame_ptr) { |
| 81 |
fmt::println("Not implemented yet (TODO)"); |
| 82 |
} |
| 83 |
#else |
| 84 |
void dump_symbols(const std::filesystem::path&) { |
| 85 |
fmt::println("Not implemented yet (TODO)"); |
| 86 |
} |
| 87 |
void lookup_symbol(const std::filesystem::path&, cpptrace::frame_ptr) { |
| 88 |
fmt::println("Not implemented yet (TODO)"); |
| 89 |
} |
| 90 |
#endif |
| 91 |
|
| 92 |
int symbol_tables(int argc, char** argv) { |
| 93 |
bool show_help = false; |
| 94 |
std::optional<std::string> lookup; |
| 95 |
options options; |
| 96 |
auto cli = lyra::cli() |
| 97 |
| lyra::help(show_help) |
| 98 |
| lyra::opt(lookup, "address")["--lookup"]("address in hex to lookup") |
| 99 |
| lyra::opt(options.demangle)["--demangle"]("demangle the symbol") |
| 100 |
| lyra::opt(options.prune)["--prune"]("prune the symbol") |
| 101 |
| lyra::arg(options.path, "binary path")("binary to dump symbol tables for").required(); |
| 102 |
if(auto result = cli.parse({ argc, argv }); !result) { |
| 103 |
fmt::println(stderr, "Error in command line: {}", result.message()); |
| 104 |
fmt::println("{}", cli); |
| 105 |
return 1; |
| 106 |
} |
| 107 |
if(show_help) { |
| 108 |
fmt::println("{}", cli); |
| 109 |
return 0; |
| 110 |
} |
| 111 |
if(!std::filesystem::exists(options.path)) { |
| 112 |
fmt::println(stderr, "Error: Path doesn't exist {}", options.path); |
| 113 |
return 1; |
| 114 |
} |
| 115 |
if(!std::filesystem::is_regular_file(options.path)) { |
| 116 |
fmt::println(stderr, "Error: Path isn't a regular file {}", options.path); |
| 117 |
return 1; |
| 118 |
} |
| 119 |
if(lookup) { |
| 120 |
auto address = std::stoull(*lookup, nullptr, 16); |
| 121 |
fmt::println(stderr, "Looking up address {:016x}", address); |
| 122 |
lookup_symbol(options, address); |
| 123 |
return 0; |
| 124 |
} |
| 125 |
dump_symbols(options); |
| 126 |
return 0; |
| 127 |
} |
| 128 |
|
| 129 |
int main(int argc, char** argv) { |
| 130 |
int ret = 0; |
| 131 |
CPPTRACE_TRY { |
| 132 |
ret = symbol_tables(argc, argv); |
| 133 |
} CPPTRACE_CATCH(const std::exception& e) { |
| 134 |
fmt::println(stderr, "Caught exception {}: {}", cpptrace::demangle(typeid(e).name()), e.what()); |
| 135 |
cpptrace::from_current_exception().print(); |
| 136 |
ret = 1; |
| 137 |
} |
| 138 |
return ret; |
| 139 |
} |