Debian Codegraph @

tools/resolver/main.cpp

raw
1 #include "cpptrace/basic.hpp"
2 #include "cpptrace/formatting.hpp"
3 #include "cpptrace/forward.hpp"
4 #include <chrono>
5 #include <lyra/lyra.hpp>
6 #include <fmt/format.h>
7 #include <fmt/std.h>
8 #include <fmt/ostream.h>
9 #include <fmt/chrono.h>
10 #include <cpptrace/cpptrace.hpp>
11 #include <cpptrace/from_current.hpp>
12
13 #include <filesystem>
14 #include <stdexcept>
15 #include <string>
16 #include <thread>
17
18 #include "symbols/symbols.hpp"
19 #include "demangle/demangle.hpp"
20
21 using namespace std::literals;
22 using namespace cpptrace::detail;
23
24 template<> struct fmt::formatter<lyra::cli> : ostream_formatter {};
25
26 auto formatter = cpptrace::formatter{}.addresses(cpptrace::formatter::address_mode::object);
27
28 struct options {
29 bool show_help = false;
30 std::filesystem::path path;
31 std::vector<std::string> address_strings;
32 bool from_stdin = false;
33 bool keepalive = false;
34 bool timing = false;
35 bool disable_aranges = false;
36 cpptrace::nullable<std::size_t> line_table_cache_size;
37 };
38
39 void resolve(const options& opts, cpptrace::frame_ptr address) {
40 cpptrace::object_frame obj_frame{0, address, opts.path.string()};
41 auto start = std::chrono::high_resolution_clock::now();
42 std::vector<cpptrace::stacktrace_frame> trace = cpptrace::detail::resolve_frames({obj_frame});
43 auto end = std::chrono::high_resolution_clock::now();
44 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
45 if(trace.size() != 1) {
46 throw std::runtime_error("Something went wrong, trace vector size didn't match");
47 }
48 trace[0].symbol = cpptrace::demangle(trace[0].symbol);
49 formatter.print(trace[0]);
50 std::cout<<std::endl;
51 if(opts.timing) {
52 fmt::println("resolve time: {}", duration);
53 }
54 }
55
56 int resolver(int argc, char** argv) {
57 options opts;
58 auto cli = lyra::cli()
59 | lyra::help(opts.show_help)
60 | lyra::opt(opts.from_stdin)["--stdin"]("read addresses from stdin")
61 | lyra::opt(opts.keepalive)["--keepalive"]("keep the program alive after resolution finishes (useful for debugging)")
62 | lyra::opt(opts.timing)["--timing"]("provide timing stats")
63 | lyra::opt(opts.disable_aranges)["--disable-aranges"]("don't use the .debug_aranges accelerated address lookup table")
64 | lyra::opt(opts.line_table_cache_size.raw_value, "line table cache size")["--line-table-cache-size"]("limit the size of cpptrace's line table cache")
65 | lyra::arg(opts.path, "binary path")("binary to look in").required()
66 | lyra::arg(opts.address_strings, "addresses")("addresses");
67 if(auto result = cli.parse({ argc, argv }); !result) {
68 fmt::println(stderr, "Error in command line: {}", result.message());
69 fmt::println("{}", cli);
70 return 1;
71 }
72 if(opts.show_help) {
73 fmt::println("{}", cli);
74 return 0;
75 }
76 if(!std::filesystem::exists(opts.path)) {
77 fmt::println(stderr, "Error: Path doesn't exist {}", opts.path);
78 return 1;
79 }
80 if(!std::filesystem::is_regular_file(opts.path)) {
81 fmt::println(stderr, "Error: Path isn't a regular file {}", opts.path);
82 return 1;
83 }
84 if(opts.disable_aranges) {
85 cpptrace::experimental::set_dwarf_resolver_disable_aranges(true);
86 }
87 if(opts.line_table_cache_size.has_value()) {
88 cpptrace::experimental::set_dwarf_resolver_line_table_cache_size(opts.line_table_cache_size);
89 }
90 for(const auto& address : opts.address_strings) {
91 resolve(opts, std::stoi(address, nullptr, 16));
92 }
93 if(opts.from_stdin) {
94 std::string word;
95 while(std::cin >> word) {
96 resolve(opts, std::stoi(word, nullptr, 16));
97 }
98 }
99 if(opts.keepalive) {
100 fmt::println("Done");
101 while(true) {
102 std::this_thread::sleep_for(std::chrono::seconds(60));
103 }
104 }
105 return 0;
106 }
107
108 int main(int argc, char** argv) {
109 int ret = 0;
110 CPPTRACE_TRY {
111 ret = resolver(argc, argv);
112 } CPPTRACE_CATCH(const std::exception& e) {
113 fmt::println(stderr, "Caught exception {}: {}", cpptrace::demangle(typeid(e).name()), e.what());
114 cpptrace::from_current_exception().print();
115 ret = 1;
116 }
117 return ret;
118 }