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