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 log_to_stdout(true);
158 arm.set_log(ARM_LOG_BY_REG);
159 arm.set_log_skip_execution(true);
160 arm.set_log_indent(1);
161 if (!arm.ExtractEntryData(entry_offset)) {
162 if (arm.status() != ARM_STATUS_NO_UNWIND) {
163 printf(" Error trying to extract data.\n");
164 }
165 return;
166 }
167 if (arm.data()->size() != 0 && arm.Eval()) {
168 arm.LogByReg();
169 } else {
170 printf(" Error tring to evaluate exidx data.\n");
171 }
172 }
173
GetInfo(const char * file,uint64_t offset,uint64_t pc)174 int GetInfo(const char* file, uint64_t offset, uint64_t pc) {
175 Elf elf(Memory::CreateFileMemory(file, offset).release());
176 if (!elf.Init() || !elf.valid()) {
177 printf("%s is not a valid elf file.\n", file);
178 return 1;
179 }
180
181 ElfInterface* interface = elf.interface();
182 uint64_t load_bias = elf.GetLoadBias();
183 if (pc < load_bias) {
184 printf("PC is less than load bias.\n");
185 return 1;
186 }
187
188 std::string soname(elf.GetSoname());
189 if (!soname.empty()) {
190 printf("Soname: %s\n\n", soname.c_str());
191 }
192
193 printf("PC 0x%" PRIx64, pc);
194 SharedString function_name;
195 uint64_t function_offset;
196 if (elf.GetFunctionName(pc, &function_name, &function_offset)) {
197 printf(" (%s)", function_name.c_str());
198 }
199 printf(":\n");
200
201 if (elf.machine_type() == EM_ARM) {
202 PrintArmRegInformation(reinterpret_cast<ElfInterfaceArm*>(interface), pc - load_bias);
203 }
204
205 DwarfSection* section = interface->eh_frame();
206 if (section != nullptr) {
207 printf("\neh_frame:\n");
208 PrintRegInformation(section, elf.memory(), pc, elf.class_type(), elf.arch());
209 } else {
210 printf("\nno eh_frame information\n");
211 }
212
213 section = interface->debug_frame();
214 if (section != nullptr) {
215 printf("\ndebug_frame:\n");
216 PrintRegInformation(section, elf.memory(), pc, elf.class_type(), elf.arch());
217 printf("\n");
218 } else {
219 printf("\nno debug_frame information\n");
220 }
221
222 // If there is a gnu_debugdata interface, dump the information for that.
223 ElfInterface* gnu_debugdata_interface = elf.gnu_debugdata_interface();
224 if (gnu_debugdata_interface != nullptr) {
225 section = gnu_debugdata_interface->eh_frame();
226 if (section != nullptr) {
227 printf("\ngnu_debugdata (eh_frame):\n");
228 PrintRegInformation(section, gnu_debugdata_interface->memory(), pc, elf.class_type(),
229 elf.arch());
230 printf("\n");
231 } else {
232 printf("\nno gnu_debugdata (eh_frame)\n");
233 }
234
235 section = gnu_debugdata_interface->debug_frame();
236 if (section != nullptr) {
237 printf("\ngnu_debugdata (debug_frame):\n");
238 PrintRegInformation(section, gnu_debugdata_interface->memory(), pc, elf.class_type(),
239 elf.arch());
240 printf("\n");
241 } else {
242 printf("\nno gnu_debugdata (debug_frame)\n");
243 }
244 } else {
245 printf("\nno valid gnu_debugdata information\n");
246 }
247
248 return 0;
249 }
250
251 } // namespace unwindstack
252
main(int argc,char ** argv)253 int main(int argc, char** argv) {
254 if (argc != 3 && argc != 4) {
255 printf("Usage: unwind_reg_info ELF_FILE PC [OFFSET]\n");
256 printf(" ELF_FILE\n");
257 printf(" The path to an elf file.\n");
258 printf(" PC\n");
259 printf(" The pc for which the register information should be obtained.\n");
260 printf(" OFFSET\n");
261 printf(" Use the offset into the ELF file as the beginning of the elf.\n");
262 return 1;
263 }
264
265 struct stat st;
266 if (stat(argv[1], &st) == -1) {
267 printf("Cannot stat %s: %s\n", argv[1], strerror(errno));
268 return 1;
269 }
270 if (!S_ISREG(st.st_mode)) {
271 printf("%s is not a regular file.\n", argv[1]);
272 return 1;
273 }
274
275 uint64_t pc = 0;
276 char* end;
277 pc = strtoull(argv[2], &end, 16);
278 if (*end != '\0') {
279 printf("Malformed OFFSET value: %s\n", argv[2]);
280 return 1;
281 }
282
283 uint64_t offset = 0;
284 if (argc == 4) {
285 char* end;
286 offset = strtoull(argv[3], &end, 16);
287 if (*end != '\0') {
288 printf("Malformed OFFSET value: %s\n", argv[3]);
289 return 1;
290 }
291 }
292
293 return unwindstack::GetInfo(argv[1], offset, pc);
294 }
295