Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 1 | /* |
| 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 Ferris | a141709 | 2017-11-28 10:59:33 -0800 | [diff] [blame] | 22 | #include <stdlib.h> |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 23 | #include <string.h> |
| 24 | #include <sys/mman.h> |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 25 | #include <sys/stat.h> |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 26 | #include <sys/types.h> |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 27 | #include <unistd.h> |
| 28 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 29 | #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 Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 35 | #include "ArmExidx.h" |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 36 | #include "ElfInterfaceArm.h" |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 37 | |
| 38 | namespace unwindstack { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 39 | |
| 40 | void 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 Ferris | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 55 | uint64_t pc = addr + load_bias; |
Christopher Ferris | b70b4a5 | 2018-03-15 14:35:01 -0700 | [diff] [blame] | 56 | if (interface->GetFunctionName(pc, load_bias, &name, &func_offset) && !name.empty()) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 57 | printf(" <%s>", name.c_str()); |
| 58 | } |
| 59 | printf("\n"); |
| 60 | uint64_t entry; |
Christopher Ferris | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 61 | if (!interface->FindEntry(pc, &entry)) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 62 | 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 Ferris | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 85 | void 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 Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 89 | if (fde == nullptr || fde->pc_start == fde->pc_end) { |
Christopher Ferris | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 90 | continue; |
| 91 | } |
| 92 | printf("\n PC 0x%" PRIx64, fde->pc_start + load_bias); |
| 93 | std::string name; |
| 94 | uint64_t func_offset; |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 95 | if (interface->GetFunctionName(fde->pc_start, load_bias, &name, &func_offset) && !name.empty()) { |
Christopher Ferris | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 96 | 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 Ferris | a141709 | 2017-11-28 10:59:33 -0800 | [diff] [blame] | 105 | int GetElfInfo(const char* file, uint64_t offset) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 106 | // Send all log messages to stdout. |
| 107 | log_to_stdout(true); |
| 108 | |
| 109 | MemoryFileAtOffset* memory = new MemoryFileAtOffset; |
Christopher Ferris | a141709 | 2017-11-28 10:59:33 -0800 | [diff] [blame] | 110 | if (!memory->Init(file, offset)) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 111 | // Initializatation failed. |
| 112 | printf("Failed to init\n"); |
| 113 | return 1; |
| 114 | } |
| 115 | |
| 116 | Elf elf(memory); |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 117 | if (!elf.Init(true) || !elf.valid()) { |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 118 | printf("%s is not a valid elf file.\n", file); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 119 | return 1; |
| 120 | } |
| 121 | |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 122 | std::string soname; |
| 123 | if (elf.GetSoname(&soname)) { |
| 124 | printf("Soname: %s\n", soname.c_str()); |
| 125 | } |
| 126 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 127 | ElfInterface* interface = elf.interface(); |
| 128 | if (elf.machine_type() == EM_ARM) { |
| 129 | DumpArm(reinterpret_cast<ElfInterfaceArm*>(interface)); |
| 130 | printf("\n"); |
| 131 | } |
| 132 | |
Christopher Ferris | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 133 | if (interface->eh_frame() != nullptr) { |
| 134 | printf("eh_frame information:\n"); |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 135 | DumpDwarfSection(interface, interface->eh_frame(), elf.GetLoadBias()); |
Christopher Ferris | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 136 | 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 Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 143 | DumpDwarfSection(interface, interface->debug_frame(), elf.GetLoadBias()); |
Christopher Ferris | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 144 | 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 Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 166 | return 0; |
| 167 | } |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 168 | |
| 169 | } // namespace unwindstack |
| 170 | |
| 171 | int main(int argc, char** argv) { |
Christopher Ferris | a141709 | 2017-11-28 10:59:33 -0800 | [diff] [blame] | 172 | 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 Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 178 | 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 Ferris | a141709 | 2017-11-28 10:59:33 -0800 | [diff] [blame] | 191 | 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 Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 202 | } |