1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <elf.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <inttypes.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/mman.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28
29 #include <string>
30
31 #include <unwindstack/Demangle.h>
32 #include <unwindstack/DwarfSection.h>
33 #include <unwindstack/DwarfStructs.h>
34 #include <unwindstack/Elf.h>
35 #include <unwindstack/ElfInterface.h>
36 #include <unwindstack/Log.h>
37 #include <unwindstack/Memory.h>
38
39 #include "ArmExidx.h"
40 #include "ElfInterfaceArm.h"
41
42 namespace unwindstack {
43
DumpArm(Elf * elf,ElfInterfaceArm * interface)44 void DumpArm(Elf* elf, ElfInterfaceArm* interface) {
45 if (interface == nullptr) {
46 printf("No ARM Unwind Information.\n\n");
47 return;
48 }
49
50 printf("ARM Unwind Information:\n");
51 uint64_t load_bias = elf->GetLoadBias();
52 for (const auto& entry : interface->pt_loads()) {
53 printf(" PC Range 0x%" PRIx64 " - 0x%" PRIx64 "\n", entry.second.offset + load_bias,
54 entry.second.offset + entry.second.table_size + load_bias);
55 for (auto pc : *interface) {
56 SharedString name;
57 printf(" PC 0x%" PRIx64, pc + load_bias);
58 uint64_t func_offset;
59 if (elf->GetFunctionName(pc + load_bias, &name, &func_offset) && !name.empty()) {
60 printf(" <%s>", DemangleNameIfNeeded(name).c_str());
61 }
62 printf("\n");
63 uint64_t entry;
64 if (!interface->FindEntry(pc, &entry)) {
65 printf(" Cannot find entry for address.\n");
66 continue;
67 }
68 ArmExidx arm(nullptr, interface->memory().get(), nullptr);
69 arm.set_log(ARM_LOG_FULL);
70 arm.set_log_skip_execution(true);
71 arm.set_log_indent(2);
72 if (!arm.ExtractEntryData(entry)) {
73 if (arm.status() != ARM_STATUS_NO_UNWIND) {
74 printf(" Error trying to extract data.\n");
75 }
76 continue;
77 }
78 if (arm.data()->size() > 0) {
79 if (!arm.Eval() && arm.status() != ARM_STATUS_NO_UNWIND) {
80 printf(" Error trying to evaluate dwarf data.\n");
81 }
82 }
83 }
84 }
85 printf("\n");
86 }
87
DumpDwarfSection(Elf * elf,DwarfSection * section,uint64_t)88 void DumpDwarfSection(Elf* elf, DwarfSection* section, uint64_t) {
89 for (const DwarfFde* fde : *section) {
90 // Sometimes there are entries that have empty length, skip those since
91 // they don't contain any interesting information.
92 if (fde == nullptr || fde->pc_start == fde->pc_end) {
93 continue;
94 }
95 printf("\n PC 0x%" PRIx64 "-0x%" PRIx64, fde->pc_start, fde->pc_end);
96 SharedString name;
97 uint64_t func_offset;
98 if (elf->GetFunctionName(fde->pc_start, &name, &func_offset) && !name.empty()) {
99 printf(" <%s>", DemangleNameIfNeeded(name).c_str());
100 }
101 printf("\n");
102 if (!section->Log(2, UINT64_MAX, fde, elf->arch())) {
103 printf("Failed to process cfa information for entry at 0x%" PRIx64 "\n", fde->pc_start);
104 }
105 }
106 }
107
GetElfInfo(const char * file,uint64_t offset)108 int GetElfInfo(const char* file, uint64_t offset) {
109 auto elf_memory = Memory::CreateFileMemory(file, offset);
110 Elf elf(elf_memory);
111 if (!elf.Init() || !elf.valid()) {
112 printf("%s is not a valid elf file.\n", file);
113 return 1;
114 }
115
116 std::string soname(elf.GetSoname());
117 if (!soname.empty()) {
118 printf("Soname: %s\n", soname.c_str());
119 }
120
121 std::string build_id = elf.GetBuildID();
122 if (!build_id.empty()) {
123 printf("Build ID: ");
124 for (size_t i = 0; i < build_id.size(); ++i) {
125 printf("%02hhx", build_id[i]);
126 }
127 printf("\n");
128 }
129
130 ElfInterface* interface = elf.interface();
131 if (elf.machine_type() == EM_ARM) {
132 DumpArm(&elf, reinterpret_cast<ElfInterfaceArm*>(interface));
133 printf("\n");
134 }
135
136 if (interface->eh_frame() != nullptr) {
137 printf("eh_frame information:\n");
138 DumpDwarfSection(&elf, interface->eh_frame(), elf.GetLoadBias());
139 printf("\n");
140 } else {
141 printf("\nno eh_frame information\n");
142 }
143
144 if (interface->debug_frame() != nullptr) {
145 printf("\ndebug_frame information:\n");
146 DumpDwarfSection(&elf, interface->debug_frame(), elf.GetLoadBias());
147 printf("\n");
148 } else {
149 printf("\nno debug_frame information\n");
150 }
151
152 // If there is a gnu_debugdata interface, dump the information for that.
153 ElfInterface* gnu_debugdata_interface = elf.gnu_debugdata_interface();
154 if (gnu_debugdata_interface != nullptr) {
155 if (gnu_debugdata_interface->eh_frame() != nullptr) {
156 printf("\ngnu_debugdata (eh_frame):\n");
157 DumpDwarfSection(&elf, gnu_debugdata_interface->eh_frame(), 0);
158 printf("\n");
159 }
160 if (gnu_debugdata_interface->debug_frame() != nullptr) {
161 printf("\ngnu_debugdata (debug_frame):\n");
162 DumpDwarfSection(&elf, gnu_debugdata_interface->debug_frame(), 0);
163 printf("\n");
164 }
165 } else {
166 printf("\nno valid gnu_debugdata information\n");
167 }
168
169 return 0;
170 }
171
172 } // namespace unwindstack
173
main(int argc,char ** argv)174 int main(int argc, char** argv) {
175 if (argc != 2 && argc != 3) {
176 printf("Usage: unwind_info ELF_FILE [OFFSET]\n");
177 printf(" ELF_FILE\n");
178 printf(" The path to an elf file.\n");
179 printf(" OFFSET\n");
180 printf(" Use the offset into the ELF file as the beginning of the elf.\n");
181 return 1;
182 }
183
184 struct stat st;
185 if (stat(argv[1], &st) == -1) {
186 printf("Cannot stat %s: %s\n", argv[1], strerror(errno));
187 return 1;
188 }
189 if (!S_ISREG(st.st_mode)) {
190 printf("%s is not a regular file.\n", argv[1]);
191 return 1;
192 }
193
194 uint64_t offset = 0;
195 if (argc == 3) {
196 char* end;
197 offset = strtoull(argv[2], &end, 16);
198 if (*end != '\0') {
199 printf("Malformed OFFSET value: %s\n", argv[2]);
200 return 1;
201 }
202 }
203
204 return unwindstack::GetElfInfo(argv[1], offset);
205 }
206