blob: 5a8edfdf3831b73b0d4acb641c8068b7e85096f2 [file] [log] [blame]
Christopher Ferris3958f802017-02-01 15:44:40 -08001/*
2 * Copyright (C) 2016 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>
Christopher Ferrisa1417092017-11-28 10:59:33 -080022#include <stdlib.h>
Christopher Ferris3958f802017-02-01 15:44:40 -080023#include <string.h>
24#include <sys/mman.h>
Christopher Ferris3958f802017-02-01 15:44:40 -080025#include <sys/stat.h>
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070026#include <sys/types.h>
Christopher Ferris3958f802017-02-01 15:44:40 -080027#include <unistd.h>
28
Christopher Ferrisd226a512017-07-14 10:37:19 -070029#include <unwindstack/DwarfSection.h>
30#include <unwindstack/DwarfStructs.h>
31#include <unwindstack/Elf.h>
32#include <unwindstack/ElfInterface.h>
33#include <unwindstack/Log.h>
34
Christopher Ferris3958f802017-02-01 15:44:40 -080035#include "ArmExidx.h"
Christopher Ferris3958f802017-02-01 15:44:40 -080036#include "ElfInterfaceArm.h"
Christopher Ferrisd226a512017-07-14 10:37:19 -070037
38namespace unwindstack {
Christopher Ferris3958f802017-02-01 15:44:40 -080039
40void DumpArm(ElfInterfaceArm* interface) {
41 if (interface == nullptr) {
42 printf("No ARM Unwind Information.\n\n");
43 return;
44 }
45
46 printf("ARM Unwind Information:\n");
47 for (const auto& entry : interface->pt_loads()) {
48 uint64_t load_bias = entry.second.table_offset;
49 printf(" PC Range 0x%" PRIx64 " - 0x%" PRIx64 "\n", entry.second.offset + load_bias,
50 entry.second.table_size + load_bias);
51 for (auto addr : *interface) {
52 std::string name;
53 printf(" PC 0x%" PRIx64, addr + load_bias);
54 uint64_t func_offset;
Christopher Ferrisbae69f12017-06-28 14:51:54 -070055 uint64_t pc = addr + load_bias;
Christopher Ferrisb70b4a52018-03-15 14:35:01 -070056 if (interface->GetFunctionName(pc, load_bias, &name, &func_offset) && !name.empty()) {
Christopher Ferris3958f802017-02-01 15:44:40 -080057 printf(" <%s>", name.c_str());
58 }
59 printf("\n");
60 uint64_t entry;
Christopher Ferrisbae69f12017-06-28 14:51:54 -070061 if (!interface->FindEntry(pc, &entry)) {
Christopher Ferris3958f802017-02-01 15:44:40 -080062 printf(" Cannot find entry for address.\n");
63 continue;
64 }
65 ArmExidx arm(nullptr, interface->memory(), nullptr);
66 arm.set_log(true);
67 arm.set_log_skip_execution(true);
68 arm.set_log_indent(2);
69 if (!arm.ExtractEntryData(entry)) {
70 if (arm.status() != ARM_STATUS_NO_UNWIND) {
71 printf(" Error trying to extract data.\n");
72 }
73 continue;
74 }
75 if (arm.data()->size() > 0) {
76 if (!arm.Eval() && arm.status() != ARM_STATUS_NO_UNWIND) {
77 printf(" Error trying to evaluate dwarf data.\n");
78 }
79 }
80 }
81 }
82 printf("\n");
83}
84
Christopher Ferrisbae69f12017-06-28 14:51:54 -070085void DumpDwarfSection(ElfInterface* interface, DwarfSection* section, uint64_t load_bias) {
86 for (const DwarfFde* fde : *section) {
87 // Sometimes there are entries that have empty length, skip those since
88 // they don't contain any interesting information.
Christopher Ferrise7b66242017-12-15 11:17:45 -080089 if (fde == nullptr || fde->pc_start == fde->pc_end) {
Christopher Ferrisbae69f12017-06-28 14:51:54 -070090 continue;
91 }
92 printf("\n PC 0x%" PRIx64, fde->pc_start + load_bias);
93 std::string name;
94 uint64_t func_offset;
Christopher Ferrise69f4702017-10-19 16:08:58 -070095 if (interface->GetFunctionName(fde->pc_start, load_bias, &name, &func_offset) && !name.empty()) {
Christopher Ferrisbae69f12017-06-28 14:51:54 -070096 printf(" <%s>", name.c_str());
97 }
98 printf("\n");
99 if (!section->Log(2, UINT64_MAX, load_bias, fde)) {
100 printf("Failed to process cfa information for entry at 0x%" PRIx64 "\n", fde->pc_start);
101 }
102 }
103}
104
Christopher Ferrisa1417092017-11-28 10:59:33 -0800105int GetElfInfo(const char* file, uint64_t offset) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800106 // Send all log messages to stdout.
107 log_to_stdout(true);
108
109 MemoryFileAtOffset* memory = new MemoryFileAtOffset;
Christopher Ferrisa1417092017-11-28 10:59:33 -0800110 if (!memory->Init(file, offset)) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800111 // Initializatation failed.
112 printf("Failed to init\n");
113 return 1;
114 }
115
116 Elf elf(memory);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700117 if (!elf.Init(true) || !elf.valid()) {
Christopher Ferrisd226a512017-07-14 10:37:19 -0700118 printf("%s is not a valid elf file.\n", file);
Christopher Ferris3958f802017-02-01 15:44:40 -0800119 return 1;
120 }
121
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800122 std::string soname;
123 if (elf.GetSoname(&soname)) {
124 printf("Soname: %s\n", soname.c_str());
125 }
126
Christopher Ferris3958f802017-02-01 15:44:40 -0800127 ElfInterface* interface = elf.interface();
128 if (elf.machine_type() == EM_ARM) {
129 DumpArm(reinterpret_cast<ElfInterfaceArm*>(interface));
130 printf("\n");
131 }
132
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700133 if (interface->eh_frame() != nullptr) {
134 printf("eh_frame information:\n");
Christopher Ferrise69f4702017-10-19 16:08:58 -0700135 DumpDwarfSection(interface, interface->eh_frame(), elf.GetLoadBias());
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700136 printf("\n");
137 } else {
138 printf("\nno eh_frame information\n");
139 }
140
141 if (interface->debug_frame() != nullptr) {
142 printf("\ndebug_frame information:\n");
Christopher Ferrise69f4702017-10-19 16:08:58 -0700143 DumpDwarfSection(interface, interface->debug_frame(), elf.GetLoadBias());
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700144 printf("\n");
145 } else {
146 printf("\nno debug_frame information\n");
147 }
148
149 // If there is a gnu_debugdata interface, dump the information for that.
150 ElfInterface* gnu_debugdata_interface = elf.gnu_debugdata_interface();
151 if (gnu_debugdata_interface != nullptr) {
152 if (gnu_debugdata_interface->eh_frame() != nullptr) {
153 printf("\ngnu_debugdata (eh_frame):\n");
154 DumpDwarfSection(gnu_debugdata_interface, gnu_debugdata_interface->eh_frame(), 0);
155 printf("\n");
156 }
157 if (gnu_debugdata_interface->debug_frame() != nullptr) {
158 printf("\ngnu_debugdata (debug_frame):\n");
159 DumpDwarfSection(gnu_debugdata_interface, gnu_debugdata_interface->debug_frame(), 0);
160 printf("\n");
161 }
162 } else {
163 printf("\nno valid gnu_debugdata information\n");
164 }
165
Christopher Ferris3958f802017-02-01 15:44:40 -0800166 return 0;
167}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700168
169} // namespace unwindstack
170
171int main(int argc, char** argv) {
Christopher Ferrisa1417092017-11-28 10:59:33 -0800172 if (argc != 2 && argc != 3) {
173 printf("Usage: unwind_info ELF_FILE [OFFSET]\n");
174 printf(" ELF_FILE\n");
175 printf(" The path to an elf file.\n");
176 printf(" OFFSET\n");
177 printf(" Use the offset into the ELF file as the beginning of the elf.\n");
Christopher Ferrisd226a512017-07-14 10:37:19 -0700178 return 1;
179 }
180
181 struct stat st;
182 if (stat(argv[1], &st) == -1) {
183 printf("Cannot stat %s: %s\n", argv[1], strerror(errno));
184 return 1;
185 }
186 if (!S_ISREG(st.st_mode)) {
187 printf("%s is not a regular file.\n", argv[1]);
188 return 1;
189 }
190
Christopher Ferrisa1417092017-11-28 10:59:33 -0800191 uint64_t offset = 0;
192 if (argc == 3) {
193 char* end;
194 offset = strtoull(argv[2], &end, 16);
195 if (*end != '\0') {
196 printf("Malformed OFFSET value: %s\n", argv[2]);
197 return 1;
198 }
199 }
200
201 return unwindstack::GetElfInfo(argv[1], offset);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700202}