1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <elf.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <unwindstack/DwarfLocation.h>
#include <unwindstack/DwarfMemory.h>
#include <unwindstack/DwarfSection.h>
#include <unwindstack/DwarfStructs.h>
#include <unwindstack/Elf.h>
#include <unwindstack/ElfInterface.h>
#include <unwindstack/Log.h>
#include "ArmExidx.h"
#include "DwarfOp.h"
#include "ElfInterfaceArm.h"
namespace unwindstack {
void PrintSignedValue(int64_t value) {
if (value < 0) {
printf("- %" PRId64, -value);
} else if (value > 0) {
printf("+ %" PRId64, value);
}
}
void PrintExpression(Memory* memory, uint8_t class_type, uint64_t end, uint64_t length) {
std::vector<std::string> lines;
DwarfMemory dwarf_memory(memory);
if (class_type == ELFCLASS32) {
DwarfOp<uint32_t> op(&dwarf_memory, nullptr);
op.GetLogInfo(end - length, end, &lines);
} else {
DwarfOp<uint64_t> op(&dwarf_memory, nullptr);
op.GetLogInfo(end - length, end, &lines);
}
for (auto& line : lines) {
printf(" %s\n", line.c_str());
}
}
void PrintRegInformation(DwarfSection* section, Memory* memory, uint64_t pc, uint8_t class_type) {
const DwarfFde* fde = section->GetFdeFromPc(pc);
if (fde == nullptr) {
printf(" No fde found.\n");
return;
}
dwarf_loc_regs_t regs;
if (!section->GetCfaLocationInfo(pc, fde, ®s)) {
printf(" Cannot get location information.\n");
return;
}
std::vector<std::pair<uint32_t, DwarfLocation>> loc_regs;
for (auto& loc : regs) {
loc_regs.push_back(loc);
}
std::sort(loc_regs.begin(), loc_regs.end(), [](auto a, auto b) {
if (a.first == CFA_REG) {
return true;
} else if (b.first == CFA_REG) {
return false;
}
return a.first < b.first;
});
for (auto& entry : loc_regs) {
const DwarfLocation* loc = &entry.second;
if (entry.first == CFA_REG) {
printf(" cfa = ");
} else {
printf(" r%d = ", entry.first);
}
switch (loc->type) {
case DWARF_LOCATION_OFFSET:
printf("[cfa ");
PrintSignedValue(loc->values[0]);
printf("]\n");
break;
case DWARF_LOCATION_VAL_OFFSET:
printf("cfa ");
PrintSignedValue(loc->values[0]);
printf("\n");
break;
case DWARF_LOCATION_REGISTER:
printf("r%" PRId64 " ", loc->values[0]);
PrintSignedValue(loc->values[1]);
printf("\n");
break;
case DWARF_LOCATION_EXPRESSION: {
printf("EXPRESSION\n");
PrintExpression(memory, class_type, loc->values[1], loc->values[0]);
break;
}
case DWARF_LOCATION_VAL_EXPRESSION: {
printf("VAL EXPRESSION\n");
PrintExpression(memory, class_type, loc->values[1], loc->values[0]);
break;
}
case DWARF_LOCATION_UNDEFINED:
printf("undefine\n");
break;
case DWARF_LOCATION_INVALID:
printf("INVALID\n");
break;
}
}
}
void PrintArmRegInformation(ElfInterfaceArm* interface, uint64_t pc) {
printf("\nArm exidx:\n");
uint64_t entry_offset;
if (!interface->FindEntry(pc, &entry_offset)) {
return;
}
ArmExidx arm(nullptr, interface->memory(), nullptr);
log_to_stdout(true);
arm.set_log(ARM_LOG_BY_REG);
arm.set_log_skip_execution(true);
arm.set_log_indent(1);
if (!arm.ExtractEntryData(entry_offset)) {
if (arm.status() != ARM_STATUS_NO_UNWIND) {
printf(" Error trying to extract data.\n");
}
return;
}
if (arm.data()->size() != 0 && arm.Eval()) {
arm.LogByReg();
} else {
printf(" Error tring to evaluate exidx data.\n");
}
}
int GetInfo(const char* file, uint64_t pc) {
MemoryFileAtOffset* memory = new MemoryFileAtOffset;
if (!memory->Init(file, 0)) {
// Initializatation failed.
printf("Failed to init\n");
return 1;
}
Elf elf(memory);
if (!elf.Init() || !elf.valid()) {
printf("%s is not a valid elf file.\n", file);
return 1;
}
ElfInterface* interface = elf.interface();
uint64_t load_bias = elf.GetLoadBias();
if (pc < load_bias) {
printf("PC is less than load bias.\n");
return 1;
}
std::string soname(elf.GetSoname());
if (!soname.empty()) {
printf("Soname: %s\n\n", soname.c_str());
}
printf("PC 0x%" PRIx64, pc);
std::string function_name;
uint64_t function_offset;
if (elf.GetFunctionName(pc, &function_name, &function_offset)) {
printf(" (%s)", function_name.c_str());
}
printf(":\n");
if (elf.machine_type() == EM_ARM) {
PrintArmRegInformation(reinterpret_cast<ElfInterfaceArm*>(interface), pc - load_bias);
}
DwarfSection* section = interface->eh_frame();
if (section != nullptr) {
printf("\neh_frame:\n");
PrintRegInformation(section, memory, pc, elf.class_type());
} else {
printf("\nno eh_frame information\n");
}
section = interface->debug_frame();
if (section != nullptr) {
printf("\ndebug_frame:\n");
PrintRegInformation(section, memory, pc, elf.class_type());
printf("\n");
} else {
printf("\nno debug_frame information\n");
}
// If there is a gnu_debugdata interface, dump the information for that.
ElfInterface* gnu_debugdata_interface = elf.gnu_debugdata_interface();
if (gnu_debugdata_interface != nullptr) {
section = gnu_debugdata_interface->eh_frame();
if (section != nullptr) {
printf("\ngnu_debugdata (eh_frame):\n");
PrintRegInformation(section, gnu_debugdata_interface->memory(), pc, elf.class_type());
printf("\n");
} else {
printf("\nno gnu_debugdata (eh_frame)\n");
}
section = gnu_debugdata_interface->debug_frame();
if (section != nullptr) {
printf("\ngnu_debugdata (debug_frame):\n");
PrintRegInformation(section, gnu_debugdata_interface->memory(), pc, elf.class_type());
printf("\n");
} else {
printf("\nno gnu_debugdata (debug_frame)\n");
}
} else {
printf("\nno valid gnu_debugdata information\n");
}
return 0;
}
} // namespace unwindstack
int main(int argc, char** argv) {
if (argc != 3) {
printf("Usage: unwind_reg_info ELF_FILE PC\n");
printf(" ELF_FILE\n");
printf(" The path to an elf file.\n");
printf(" PC\n");
printf(" The pc for which the register information should be obtained.\n");
return 1;
}
struct stat st;
if (stat(argv[1], &st) == -1) {
printf("Cannot stat %s: %s\n", argv[1], strerror(errno));
return 1;
}
if (!S_ISREG(st.st_mode)) {
printf("%s is not a regular file.\n", argv[1]);
return 1;
}
uint64_t pc = 0;
char* end;
pc = strtoull(argv[2], &end, 16);
if (*end != '\0') {
printf("Malformed OFFSET value: %s\n", argv[2]);
return 1;
}
return unwindstack::GetInfo(argv[1], pc);
}
|