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