Debian Codegraph @

tools/dwarfdump/main.cpp

raw
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 "symbols/dwarf/dwarf.hpp"
11
12 using namespace std::literals;
13 using namespace cpptrace::detail::libdwarf;
14
15 template<> struct fmt::formatter<lyra::cli> : ostream_formatter {};
16
17 class DwarfDumper {
18 std::string object_path;
19 Dwarf_Debug dbg = nullptr;
20
21 // Error handling helper
22 // For some reason R (*f)(Args..., void*)-style deduction isn't possible, seems like a bug in all compilers
23 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56190
24 // TODO: Duplicate
25 template<
26 typename... Args,
27 typename... Args2,
28 typename std::enable_if<
29 std::is_same<
30 decltype(
31 (void)std::declval<int(Args...)>()(std::forward<Args2>(std::declval<Args2>())..., nullptr)
32 ),
33 void
34 >::value,
35 int
36 >::type = 0
37 >
38 int wrap(int (*f)(Args...), Args2&&... args) const {
39 Dwarf_Error error = nullptr;
40 int ret = f(std::forward<Args2>(args)..., &error);
41 if(ret == DW_DLV_ERROR) {
42 handle_dwarf_error(dbg, error);
43 }
44 return ret;
45 }
46
47 // TODO: Duplicate
48 // walk all CU's in a dbg, callback is called on each die and should return true to
49 // continue traversal
50 void walk_compilation_units(const std::function<bool(const die_object&)>& fn) {
51 // libdwarf keeps track of where it is in the file, dwarf_next_cu_header_d is statefull
52 Dwarf_Unsigned next_cu_header;
53 Dwarf_Half header_cu_type;
54 while(true) {
55 int ret = wrap(
56 dwarf_next_cu_header_d,
57 dbg,
58 true,
59 nullptr,
60 nullptr,
61 nullptr,
62 nullptr,
63 nullptr,
64 nullptr,
65 nullptr,
66 nullptr,
67 &next_cu_header,
68 &header_cu_type
69 );
70 if(ret == DW_DLV_NO_ENTRY) {
71 fmt::println("End walk_dbg");
72 return;
73 }
74 if(ret != DW_DLV_OK) {
75 PANIC("Unexpected return code from dwarf_next_cu_header_d");
76 return;
77 }
78 // 0 passed as the die to the first call of dwarf_siblingof_b immediately after dwarf_next_cu_header_d
79 // to fetch the cu die
80 die_object cu_die(dbg, nullptr);
81 cu_die = cu_die.get_sibling();
82 if(!cu_die) {
83 break;
84 }
85 if(!walk_die_list(cu_die, fn)) {
86 break;
87 }
88 }
89 fmt::println("End walk_compilation_units");
90 }
91
92 void dump_die_tree(const die_object& die, int depth) {
93 walk_die_list(
94 die,
95 [this, depth] (const die_object& die) {
96 fmt::println("{:016x}{: <{}} {}", die.get_global_offset(), "", depth * 2, die.get_tag_name());
97 fmt::println("{: <16}{: <{}} name: {}", "", "", depth * 2, die.get_name());
98 fmt::println("");
99 auto child = die.get_child();
100 if(child) {
101 dump_die_tree(child, depth + 1);
102 }
103 return true;
104 }
105 );
106 }
107
108 void dump_cu(const die_object& cu_die) {
109 Dwarf_Half offset_size = 0;
110 Dwarf_Half dwversion = 0;
111 dwarf_get_version_of_die(cu_die.get(), &dwversion, &offset_size);
112 fmt::println("{:016x} Compile Unit: version = {}, unit type = {}", cu_die.get_global_offset(), dwversion, cu_die.get_tag_name());
113 dump_die_tree(cu_die, 0);
114 }
115
116 public:
117 DwarfDumper() = default;
118 ~DwarfDumper() {
119 dwarf_finish(dbg);
120 }
121
122 void dump(std::filesystem::path path) {
123 object_path = path;
124 auto ret = wrap(
125 dwarf_init_path_a,
126 object_path.c_str(),
127 nullptr,
128 0,
129 DW_GROUPNUMBER_ANY,
130 0,
131 nullptr,
132 nullptr,
133 &dbg
134 );
135 if(ret == DW_DLV_OK) {
136 // ok
137 } else if(ret == DW_DLV_NO_ENTRY) {
138 // fail, no debug info
139 fmt::println(stderr, "No debug info");
140 std::exit(1);
141 } else {
142 fmt::println(stderr, "Error: Unknown return code from dwarf_init_path {}", ret);
143 std::exit(1);
144 }
145 walk_compilation_units([this] (const die_object& cu_die) {
146 dump_cu(cu_die);
147 return true;
148 });
149 }
150 };
151
152 int main(int argc, char** argv) CPPTRACE_TRY {
153 bool show_help = false;
154 std::filesystem::path path;
155 auto cli = lyra::cli()
156 | lyra::help(show_help)
157 | lyra::arg(path, "binary path")("binary to dwarfdump").required();
158 if(auto result = cli.parse({ argc, argv }); !result) {
159 fmt::println(stderr, "Error in command line: {}", result.message());
160 fmt::println("{}", cli);
161 return 1;
162 }
163 if(show_help) {
164 fmt::println("{}", cli);
165 return 0;
166 }
167 if(!std::filesystem::exists(path)) {
168 fmt::println(stderr, "Error: Path doesn't exist {}", path);
169 return 1;
170 }
171 if(!std::filesystem::is_regular_file(path)) {
172 fmt::println(stderr, "Error: Path isn't a regular file {}", path);
173 return 1;
174 }
175 DwarfDumper{}.dump(path);
176 } CPPTRACE_CATCH(const std::exception& e) {
177 fmt::println(stderr, "Caught exception {}: {}", cpptrace::demangle(typeid(e).name()), e.what());
178 cpptrace::from_current_exception().print();
179 }