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