1 /*
2 * Copyright (C) 2018 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/DwarfLocation.h>
30 #include <unwindstack/DwarfMemory.h>
31 #include <unwindstack/DwarfSection.h>
32 #include <unwindstack/DwarfStructs.h>
33 #include <unwindstack/Elf.h>
34 #include <unwindstack/ElfInterface.h>
35 #include <unwindstack/Log.h>
36 #include <unwindstack/Memory.h>
37
38 #include "ArmExidx.h"
39 #include "DwarfOp.h"
40 #include "ElfInterfaceArm.h"
41
42 namespace unwindstack {
43
PrintSignedValue(int64_t value)44 void PrintSignedValue(int64_t value) {
45 if (value < 0) {
46 printf("- %" PRId64, -value);
47 } else if (value > 0) {
48 printf("+ %" PRId64, value);
49 }
50 }
51
PrintExpression(Memory * memory,uint8_t class_type,uint64_t end,uint64_t length)52 void PrintExpression(Memory* memory, uint8_t class_type, uint64_t end, uint64_t length) {
53 std::vector<std::string> lines;
54 DwarfMemory dwarf_memory(memory);
55 if (class_type == ELFCLASS32) {
56 DwarfOp<uint32_t> op(&dwarf_memory, nullptr);
57 op.GetLogInfo(end - length, end, &lines);
58 } else {
59 DwarfOp<uint64_t> op(&dwarf_memory, nullptr);
60 op.GetLogInfo(end - length, end, &lines);
61 }
62 for (auto& line : lines) {
63 printf(" %s\n", line.c_str());
64 }
65 }
66
PrintRegInformation(DwarfSection * section,Memory * memory,uint64_t pc,uint8_t class_type,ArchEnum arch)67 void PrintRegInformation(DwarfSection* section, Memory* memory, uint64_t pc, uint8_t class_type,
68 ArchEnum arch) {
69 const DwarfFde* fde = section->GetFdeFromPc(pc);
70 if (fde == nullptr) {
71 printf(" No fde found.\n");
72 return;
73 }
74
75 DwarfLocations regs;
76 if (!section->GetCfaLocationInfo(pc, fde, ®s, arch)) {
77 printf(" Cannot get location information.\n");
78 return;
79 }
80
81 std::vector<std::pair<uint32_t, DwarfLocation>> loc_regs;
82 for (auto& loc : regs) {
83 loc_regs.push_back(loc);
84 }
85 std::sort(loc_regs.begin(), loc_regs.end(), [](auto a, auto b) {
86 if (a.first == CFA_REG) {
87 return true;
88 } else if (b.first == CFA_REG) {
89 return false;
90 }
91 return a.first < b.first;
92 });
93
94 for (auto& entry : loc_regs) {
95 const DwarfLocation* loc = &entry.second;
96 if (entry.first == CFA_REG) {
97 printf(" cfa = ");
98 } else {
99 printf(" r%d = ", entry.first);
100 }
101 switch (loc->type) {
102 case DWARF_LOCATION_OFFSET:
103 printf("[cfa ");
104 PrintSignedValue(loc->values[0]);
105 printf("]\n");
106 break;
107
108 case DWARF_LOCATION_VAL_OFFSET:
109 printf("cfa ");
110 PrintSignedValue(loc->values[0]);
111 printf("\n");
112 break;
113
114 case DWARF_LOCATION_REGISTER:
115 printf("r%" PRId64 " ", loc->values[0]);
116 PrintSignedValue(loc->values[1]);
117 printf("\n");
118 break;
119
120 case DWARF_LOCATION_EXPRESSION: {
121 printf("EXPRESSION\n");
122 PrintExpression(memory, class_type, loc->values[1], loc->values[0]);
123 break;
124 }
125
126 case DWARF_LOCATION_VAL_EXPRESSION: {
127 printf("VAL EXPRESSION\n");
128 PrintExpression(memory, class_type, loc->values[1], loc->values[0]);
129 break;
130 }
131
132 case DWARF_LOCATION_PSEUDO_REGISTER: {
133 printf("%" PRId64 " (pseudo)\n", loc->values[0]);
134 break;
135 }
136
137 case DWARF_LOCATION_UNDEFINED:
138 printf("undefine\n");
139 break;
140
141 case DWARF_LOCATION_INVALID:
142 printf("INVALID\n");
143 break;
144 }
145 }
146 }
147
PrintArmRegInformation(ElfInterfaceArm * interface,uint64_t pc)148 void PrintArmRegInformation(ElfInterfaceArm* interface, uint64_t pc) {
149 printf("\nArm exidx:\n");
150 uint64_t entry_offset;
151 if (!interface->FindEntry(pc, &entry_offset)) {
152 return;
153 }
154
155 ArmExidx arm(nullptr, interface->memory(), nullptr);
156
157 arm.set_log(ARM_LOG_BY_REG);
158 arm.set_log_skip_execution(true);
159 arm.set_log_indent(1);
160 if (!arm.ExtractEntryData(entry_offset)) {
161 if (arm.status() != ARM_STATUS_NO_UNWIND) {
162 printf(" Error trying to extract data.\n");
163 }
164 return;
165 }
166 if (arm.data()->size() != 0 && arm.Eval()) {
167 arm.LogByReg();
168 } else {
169 printf(" Error tring to evaluate exidx data.\n");
170 }
171 }
172
GetInfo(const char * file,uint64_t offset,uint64_t pc)173 int GetInfo(const char* file, uint64_t offset, uint64_t pc) {
174 Elf elf(Memory::CreateFileMemory(file, offset).release());
175 if (!elf.Init() || !elf.valid()) {
176 printf("%s is not a valid elf file.\n", file);
177 return 1;
178 }
179
180 ElfInterface* interface = elf.interface();
181 uint64_t load_bias = elf.GetLoadBias();
182 if (pc < load_bias) {
183 printf("PC is less than load bias.\n");
184 return 1;
185 }
186
187 std::string soname(elf.GetSoname());
188 if (!soname.empty()) {
189 printf("Soname: %s\n\n", soname.c_str());
190 }
191
192 printf("PC 0x%" PRIx64, pc);
193 SharedString function_name;
194 uint64_t function_offset;
195 if (elf.GetFunctionName(pc, &function_name, &function_offset)) {
196 printf(" (%s)", function_name.c_str());
197 }
198 printf(":\n");
199
200 if (elf.machine_type() == EM_ARM) {
201 PrintArmRegInformation(reinterpret_cast<ElfInterfaceArm*>(interface), pc - load_bias);
202 }
203
204 DwarfSection* section = interface->eh_frame();
205 if (section != nullptr) {
206 printf("\neh_frame:\n");
207 PrintRegInformation(section, elf.memory(), pc, elf.class_type(), elf.arch());
208 } else {
209 printf("\nno eh_frame information\n");
210 }
211
212 section = interface->debug_frame();
213 if (section != nullptr) {
214 printf("\ndebug_frame:\n");
215 PrintRegInformation(section, elf.memory(), pc, elf.class_type(), elf.arch());
216 printf("\n");
217 } else {
218 printf("\nno debug_frame information\n");
219 }
220
221 // If there is a gnu_debugdata interface, dump the information for that.
222 ElfInterface* gnu_debugdata_interface = elf.gnu_debugdata_interface();
223 if (gnu_debugdata_interface != nullptr) {
224 section = gnu_debugdata_interface->eh_frame();
225 if (section != nullptr) {
226 printf("\ngnu_debugdata (eh_frame):\n");
227 PrintRegInformation(section, gnu_debugdata_interface->memory(), pc, elf.class_type(),
228 elf.arch());
229 printf("\n");
230 } else {
231 printf("\nno gnu_debugdata (eh_frame)\n");
232 }
233
234 section = gnu_debugdata_interface->debug_frame();
235 if (section != nullptr) {
236 printf("\ngnu_debugdata (debug_frame):\n");
237 PrintRegInformation(section, gnu_debugdata_interface->memory(), pc, elf.class_type(),
238 elf.arch());
239 printf("\n");
240 } else {
241 printf("\nno gnu_debugdata (debug_frame)\n");
242 }
243 } else {
244 printf("\nno valid gnu_debugdata information\n");
245 }
246
247 return 0;
248 }
249
250 } // namespace unwindstack
251
main(int argc,char ** argv)252 int main(int argc, char** argv) {
253 if (argc != 3 && argc != 4) {
254 printf("Usage: unwind_reg_info ELF_FILE PC [OFFSET]\n");
255 printf(" ELF_FILE\n");
256 printf(" The path to an elf file.\n");
257 printf(" PC\n");
258 printf(" The pc for which the register information should be obtained.\n");
259 printf(" OFFSET\n");
260 printf(" Use the offset into the ELF file as the beginning of the elf.\n");
261 return 1;
262 }
263
264 struct stat st;
265 if (stat(argv[1], &st) == -1) {
266 printf("Cannot stat %s: %s\n", argv[1], strerror(errno));
267 return 1;
268 }
269 if (!S_ISREG(st.st_mode)) {
270 printf("%s is not a regular file.\n", argv[1]);
271 return 1;
272 }
273
274 uint64_t pc = 0;
275 char* end;
276 pc = strtoull(argv[2], &end, 16);
277 if (*end != '\0') {
278 printf("Malformed OFFSET value: %s\n", argv[2]);
279 return 1;
280 }
281
282 uint64_t offset = 0;
283 if (argc == 4) {
284 char* end;
285 offset = strtoull(argv[3], &end, 16);
286 if (*end != '\0') {
287 printf("Malformed OFFSET value: %s\n", argv[3]);
288 return 1;
289 }
290 }
291
292 return unwindstack::GetInfo(argv[1], offset, pc);
293 }
294