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 <unwindstack/DwarfSection.h>
30 #include <unwindstack/DwarfStructs.h>
31 #include <unwindstack/Elf.h>
32 #include <unwindstack/ElfInterface.h>
33 #include <unwindstack/Log.h>
34 #include <unwindstack/Memory.h>
35
36 #include "ArmExidx.h"
37 #include "ElfInterfaceArm.h"
38
39 namespace unwindstack {
40
DumpArm(Elf * elf,ElfInterfaceArm * interface)41 void DumpArm(Elf* elf, ElfInterfaceArm* interface) {
42 if (interface == nullptr) {
43 printf("No ARM Unwind Information.\n\n");
44 return;
45 }
46
47 printf("ARM Unwind Information:\n");
48 uint64_t load_bias = elf->GetLoadBias();
49 for (const auto& entry : interface->pt_loads()) {
50 printf(" PC Range 0x%" PRIx64 " - 0x%" PRIx64 "\n", entry.second.offset + load_bias,
51 entry.second.offset + entry.second.table_size + load_bias);
52 for (auto pc : *interface) {
53 SharedString name;
54 printf(" PC 0x%" PRIx64, pc + load_bias);
55 uint64_t func_offset;
56 if (elf->GetFunctionName(pc + load_bias, &name, &func_offset) && !name.empty()) {
57 printf(" <%s>", name.c_str());
58 }
59 printf("\n");
60 uint64_t entry;
61 if (!interface->FindEntry(pc, &entry)) {
62 printf(" Cannot find entry for address.\n");
63 continue;
64 }
65 ArmExidx arm(nullptr, interface->memory(), nullptr);
66 arm.set_log(ARM_LOG_FULL);
67 arm.set_log_skip_execution(true);
68 arm.set_log_indent(2);
69 if (!arm.ExtractEntryData(entry)) {
70 if (arm.status() != ARM_STATUS_NO_UNWIND) {
71 printf(" Error trying to extract data.\n");
72 }
73 continue;
74 }
75 if (arm.data()->size() > 0) {
76 if (!arm.Eval() && arm.status() != ARM_STATUS_NO_UNWIND) {
77 printf(" Error trying to evaluate dwarf data.\n");
78 }
79 }
80 }
81 }
82 printf("\n");
83 }
84
DumpDwarfSection(Elf * elf,DwarfSection * section,uint64_t)85 void DumpDwarfSection(Elf* elf, DwarfSection* section, uint64_t) {
86 for (const DwarfFde* fde : *section) {
87 // Sometimes there are entries that have empty length, skip those since
88 // they don't contain any interesting information.
89 if (fde == nullptr || fde->pc_start == fde->pc_end) {
90 continue;
91 }
92 printf("\n PC 0x%" PRIx64 "-0x%" PRIx64, fde->pc_start, fde->pc_end);
93 SharedString name;
94 uint64_t func_offset;
95 if (elf->GetFunctionName(fde->pc_start, &name, &func_offset) && !name.empty()) {
96 printf(" <%s>", name.c_str());
97 }
98 printf("\n");
99 if (!section->Log(2, UINT64_MAX, fde, elf->arch())) {
100 printf("Failed to process cfa information for entry at 0x%" PRIx64 "\n", fde->pc_start);
101 }
102 }
103 }
104
GetElfInfo(const char * file,uint64_t offset)105 int GetElfInfo(const char* file, uint64_t offset) {
106 Elf elf(Memory::CreateFileMemory(file, offset).release());
107 if (!elf.Init() || !elf.valid()) {
108 printf("%s is not a valid elf file.\n", file);
109 return 1;
110 }
111
112 std::string soname(elf.GetSoname());
113 if (!soname.empty()) {
114 printf("Soname: %s\n", soname.c_str());
115 }
116
117 std::string build_id = elf.GetBuildID();
118 if (!build_id.empty()) {
119 printf("Build ID: ");
120 for (size_t i = 0; i < build_id.size(); ++i) {
121 printf("%02hhx", build_id[i]);
122 }
123 printf("\n");
124 }
125
126 ElfInterface* interface = elf.interface();
127 if (elf.machine_type() == EM_ARM) {
128 DumpArm(&elf, reinterpret_cast<ElfInterfaceArm*>(interface));
129 printf("\n");
130 }
131
132 if (interface->eh_frame() != nullptr) {
133 printf("eh_frame information:\n");
134 DumpDwarfSection(&elf, interface->eh_frame(), elf.GetLoadBias());
135 printf("\n");
136 } else {
137 printf("\nno eh_frame information\n");
138 }
139
140 if (interface->debug_frame() != nullptr) {
141 printf("\ndebug_frame information:\n");
142 DumpDwarfSection(&elf, interface->debug_frame(), elf.GetLoadBias());
143 printf("\n");
144 } else {
145 printf("\nno debug_frame information\n");
146 }
147
148 // If there is a gnu_debugdata interface, dump the information for that.
149 ElfInterface* gnu_debugdata_interface = elf.gnu_debugdata_interface();
150 if (gnu_debugdata_interface != nullptr) {
151 if (gnu_debugdata_interface->eh_frame() != nullptr) {
152 printf("\ngnu_debugdata (eh_frame):\n");
153 DumpDwarfSection(&elf, gnu_debugdata_interface->eh_frame(), 0);
154 printf("\n");
155 }
156 if (gnu_debugdata_interface->debug_frame() != nullptr) {
157 printf("\ngnu_debugdata (debug_frame):\n");
158 DumpDwarfSection(&elf, gnu_debugdata_interface->debug_frame(), 0);
159 printf("\n");
160 }
161 } else {
162 printf("\nno valid gnu_debugdata information\n");
163 }
164
165 return 0;
166 }
167
168 } // namespace unwindstack
169
main(int argc,char ** argv)170 int main(int argc, char** argv) {
171 if (argc != 2 && argc != 3) {
172 printf("Usage: unwind_info ELF_FILE [OFFSET]\n");
173 printf(" ELF_FILE\n");
174 printf(" The path to an elf file.\n");
175 printf(" OFFSET\n");
176 printf(" Use the offset into the ELF file as the beginning of the elf.\n");
177 return 1;
178 }
179
180 struct stat st;
181 if (stat(argv[1], &st) == -1) {
182 printf("Cannot stat %s: %s\n", argv[1], strerror(errno));
183 return 1;
184 }
185 if (!S_ISREG(st.st_mode)) {
186 printf("%s is not a regular file.\n", argv[1]);
187 return 1;
188 }
189
190 uint64_t offset = 0;
191 if (argc == 3) {
192 char* end;
193 offset = strtoull(argv[2], &end, 16);
194 if (*end != '\0') {
195 printf("Malformed OFFSET value: %s\n", argv[2]);
196 return 1;
197 }
198 }
199
200 return unwindstack::GetElfInfo(argv[1], offset);
201 }
202