blob: da1522b3ddc6052c304019a951c80b1576435915 [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>
22#include <string.h>
23#include <sys/mman.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <unistd.h>
27
28#include "ArmExidx.h"
Christopher Ferrisbae69f12017-06-28 14:51:54 -070029#include "DwarfSection.h"
30#include "DwarfStructs.h"
Christopher Ferris3958f802017-02-01 15:44:40 -080031#include "Elf.h"
32#include "ElfInterface.h"
33#include "ElfInterfaceArm.h"
34#include "Log.h"
35
36void DumpArm(ElfInterfaceArm* interface) {
37 if (interface == nullptr) {
38 printf("No ARM Unwind Information.\n\n");
39 return;
40 }
41
42 printf("ARM Unwind Information:\n");
43 for (const auto& entry : interface->pt_loads()) {
44 uint64_t load_bias = entry.second.table_offset;
45 printf(" PC Range 0x%" PRIx64 " - 0x%" PRIx64 "\n", entry.second.offset + load_bias,
46 entry.second.table_size + load_bias);
47 for (auto addr : *interface) {
48 std::string name;
49 printf(" PC 0x%" PRIx64, addr + load_bias);
50 uint64_t func_offset;
Christopher Ferrisbae69f12017-06-28 14:51:54 -070051 uint64_t pc = addr + load_bias;
52 // This might be a thumb function, so set the low bit.
53 if (interface->GetFunctionName(pc | 1, &name, &func_offset) && !name.empty()) {
Christopher Ferris3958f802017-02-01 15:44:40 -080054 printf(" <%s>", name.c_str());
55 }
56 printf("\n");
57 uint64_t entry;
Christopher Ferrisbae69f12017-06-28 14:51:54 -070058 if (!interface->FindEntry(pc, &entry)) {
Christopher Ferris3958f802017-02-01 15:44:40 -080059 printf(" Cannot find entry for address.\n");
60 continue;
61 }
62 ArmExidx arm(nullptr, interface->memory(), nullptr);
63 arm.set_log(true);
64 arm.set_log_skip_execution(true);
65 arm.set_log_indent(2);
66 if (!arm.ExtractEntryData(entry)) {
67 if (arm.status() != ARM_STATUS_NO_UNWIND) {
68 printf(" Error trying to extract data.\n");
69 }
70 continue;
71 }
72 if (arm.data()->size() > 0) {
73 if (!arm.Eval() && arm.status() != ARM_STATUS_NO_UNWIND) {
74 printf(" Error trying to evaluate dwarf data.\n");
75 }
76 }
77 }
78 }
79 printf("\n");
80}
81
Christopher Ferrisbae69f12017-06-28 14:51:54 -070082void DumpDwarfSection(ElfInterface* interface, DwarfSection* section, uint64_t load_bias) {
83 for (const DwarfFde* fde : *section) {
84 // Sometimes there are entries that have empty length, skip those since
85 // they don't contain any interesting information.
86 if (fde->pc_start == fde->pc_end) {
87 continue;
88 }
89 printf("\n PC 0x%" PRIx64, fde->pc_start + load_bias);
90 std::string name;
91 uint64_t func_offset;
92 if (interface->GetFunctionName(fde->pc_start + load_bias, &name, &func_offset) &&
93 !name.empty()) {
94 printf(" <%s>", name.c_str());
95 }
96 printf("\n");
97 if (!section->Log(2, UINT64_MAX, load_bias, fde)) {
98 printf("Failed to process cfa information for entry at 0x%" PRIx64 "\n", fde->pc_start);
99 }
100 }
101}
102
Christopher Ferris3958f802017-02-01 15:44:40 -0800103int main(int argc, char** argv) {
104 if (argc != 2) {
105 printf("Need to pass the name of an elf file to the program.\n");
106 return 1;
107 }
108
109 struct stat st;
110 if (stat(argv[1], &st) == -1) {
111 printf("Cannot stat %s: %s\n", argv[1], strerror(errno));
112 return 1;
113 }
114 if (!S_ISREG(st.st_mode)) {
115 printf("%s is not a regular file.\n", argv[1]);
116 return 1;
117 }
118 if (S_ISDIR(st.st_mode)) {
119 printf("%s is a directory.\n", argv[1]);
120 return 1;
121 }
122
123 // Send all log messages to stdout.
124 log_to_stdout(true);
125
126 MemoryFileAtOffset* memory = new MemoryFileAtOffset;
127 if (!memory->Init(argv[1], 0)) {
128 // Initializatation failed.
129 printf("Failed to init\n");
130 return 1;
131 }
132
133 Elf elf(memory);
134 if (!elf.Init() || !elf.valid()) {
135 printf("%s is not a valid elf file.\n", argv[1]);
136 return 1;
137 }
138
139 ElfInterface* interface = elf.interface();
140 if (elf.machine_type() == EM_ARM) {
141 DumpArm(reinterpret_cast<ElfInterfaceArm*>(interface));
142 printf("\n");
143 }
144
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700145 if (interface->eh_frame() != nullptr) {
146 printf("eh_frame information:\n");
147 DumpDwarfSection(interface, interface->eh_frame(), interface->load_bias());
148 printf("\n");
149 } else {
150 printf("\nno eh_frame information\n");
151 }
152
153 if (interface->debug_frame() != nullptr) {
154 printf("\ndebug_frame information:\n");
155 DumpDwarfSection(interface, interface->debug_frame(), interface->load_bias());
156 printf("\n");
157 } else {
158 printf("\nno debug_frame information\n");
159 }
160
161 // If there is a gnu_debugdata interface, dump the information for that.
162 ElfInterface* gnu_debugdata_interface = elf.gnu_debugdata_interface();
163 if (gnu_debugdata_interface != nullptr) {
164 if (gnu_debugdata_interface->eh_frame() != nullptr) {
165 printf("\ngnu_debugdata (eh_frame):\n");
166 DumpDwarfSection(gnu_debugdata_interface, gnu_debugdata_interface->eh_frame(), 0);
167 printf("\n");
168 }
169 if (gnu_debugdata_interface->debug_frame() != nullptr) {
170 printf("\ngnu_debugdata (debug_frame):\n");
171 DumpDwarfSection(gnu_debugdata_interface, gnu_debugdata_interface->debug_frame(), 0);
172 printf("\n");
173 }
174 } else {
175 printf("\nno valid gnu_debugdata information\n");
176 }
177
Christopher Ferris3958f802017-02-01 15:44:40 -0800178 return 0;
179}