2017-09-20 22:37:24 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 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.
|
|
|
|
*/
|
|
|
|
|
2017-09-26 04:23:07 +02:00
|
|
|
#define _GNU_SOURCE 1
|
2017-09-20 22:37:24 +02:00
|
|
|
#include <elf.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdint.h>
|
2017-09-26 04:23:07 +02:00
|
|
|
#include <string.h>
|
2017-09-20 22:37:24 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2017-10-25 00:36:00 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
2017-09-20 22:37:24 +02:00
|
|
|
#include <android-base/stringprintf.h>
|
|
|
|
|
|
|
|
#include <unwindstack/Elf.h>
|
2017-12-21 03:49:01 +01:00
|
|
|
#include <unwindstack/JitDebug.h>
|
2017-09-20 22:37:24 +02:00
|
|
|
#include <unwindstack/MapInfo.h>
|
|
|
|
#include <unwindstack/Unwinder.h>
|
|
|
|
|
2018-01-31 04:47:24 +01:00
|
|
|
#if !defined(NO_LIBDEXFILE_SUPPORT)
|
|
|
|
#include <unwindstack/DexFiles.h>
|
|
|
|
#endif
|
|
|
|
|
2017-09-20 22:37:24 +02:00
|
|
|
namespace unwindstack {
|
|
|
|
|
2018-01-31 04:47:24 +01:00
|
|
|
// Inject extra 'virtual' frame that represents the dex pc data.
|
|
|
|
// The dex pc is a magic register defined in the Mterp interpreter,
|
|
|
|
// and thus it will be restored/observed in the frame after it.
|
|
|
|
// Adding the dex frame first here will create something like:
|
|
|
|
// #7 pc 0015fa20 core.vdex java.util.Arrays.binarySearch+8
|
|
|
|
// #8 pc 006b1ba1 libartd.so ExecuteMterpImpl+14625
|
|
|
|
// #9 pc 0039a1ef libartd.so art::interpreter::Execute+719
|
|
|
|
void Unwinder::FillInDexFrame() {
|
|
|
|
size_t frame_num = frames_.size();
|
|
|
|
frames_.resize(frame_num + 1);
|
|
|
|
FrameData* frame = &frames_.at(frame_num);
|
|
|
|
|
|
|
|
uint64_t dex_pc = regs_->dex_pc();
|
|
|
|
frame->pc = dex_pc;
|
|
|
|
frame->sp = regs_->sp();
|
|
|
|
|
2018-02-01 04:05:19 +01:00
|
|
|
MapInfo* info = maps_->Find(dex_pc);
|
2018-02-06 23:51:48 +01:00
|
|
|
if (info != nullptr) {
|
|
|
|
frame->map_start = info->start;
|
|
|
|
frame->map_end = info->end;
|
|
|
|
frame->map_offset = info->offset;
|
|
|
|
frame->map_load_bias = info->load_bias;
|
|
|
|
frame->map_flags = info->flags;
|
|
|
|
frame->map_name = info->name;
|
|
|
|
frame->rel_pc = dex_pc - info->start;
|
|
|
|
} else {
|
|
|
|
frame->rel_pc = dex_pc;
|
|
|
|
return;
|
|
|
|
}
|
2018-01-31 04:47:24 +01:00
|
|
|
|
2018-02-01 04:05:19 +01:00
|
|
|
#if !defined(NO_LIBDEXFILE_SUPPORT)
|
|
|
|
if (dex_files_ == nullptr) {
|
2018-01-31 04:47:24 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-01 04:05:19 +01:00
|
|
|
// dex_files_->GetMethodInformation(dex_pc - dex_offset, dex_offset, info, &frame->function_name,
|
|
|
|
dex_files_->GetMethodInformation(maps_, info, dex_pc, &frame->function_name,
|
2018-02-01 15:49:14 +01:00
|
|
|
&frame->function_offset);
|
2018-01-31 04:47:24 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-12-21 03:49:01 +01:00
|
|
|
void Unwinder::FillInFrame(MapInfo* map_info, Elf* elf, uint64_t adjusted_rel_pc, uint64_t func_pc) {
|
2017-09-20 22:37:24 +02:00
|
|
|
size_t frame_num = frames_.size();
|
|
|
|
frames_.resize(frame_num + 1);
|
|
|
|
FrameData* frame = &frames_.at(frame_num);
|
|
|
|
frame->num = frame_num;
|
|
|
|
frame->sp = regs_->sp();
|
2017-11-29 04:14:54 +01:00
|
|
|
frame->rel_pc = adjusted_rel_pc;
|
2017-09-20 22:37:24 +02:00
|
|
|
|
|
|
|
if (map_info == nullptr) {
|
2017-11-29 04:14:54 +01:00
|
|
|
frame->pc = regs_->pc();
|
2017-09-20 22:37:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-12 22:24:18 +01:00
|
|
|
frame->pc = map_info->start + adjusted_rel_pc - elf->GetLoadBias() - map_info->elf_offset;
|
2017-09-20 22:37:24 +02:00
|
|
|
frame->map_name = map_info->name;
|
2017-10-18 03:42:03 +02:00
|
|
|
frame->map_offset = map_info->offset;
|
2017-09-20 22:37:24 +02:00
|
|
|
frame->map_start = map_info->start;
|
|
|
|
frame->map_end = map_info->end;
|
2017-09-26 04:23:07 +02:00
|
|
|
frame->map_flags = map_info->flags;
|
|
|
|
frame->map_load_bias = elf->GetLoadBias();
|
2017-09-20 22:37:24 +02:00
|
|
|
|
2017-12-21 03:49:01 +01:00
|
|
|
if (!elf->GetFunctionName(func_pc, &frame->function_name, &frame->function_offset)) {
|
2017-09-20 22:37:24 +02:00
|
|
|
frame->function_name = "";
|
|
|
|
frame->function_offset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-25 00:36:00 +02:00
|
|
|
static bool ShouldStop(const std::vector<std::string>* map_suffixes_to_ignore,
|
|
|
|
std::string& map_name) {
|
2017-10-20 01:08:58 +02:00
|
|
|
if (map_suffixes_to_ignore == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto pos = map_name.find_last_of('.');
|
|
|
|
if (pos == std::string::npos) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-25 00:36:00 +02:00
|
|
|
|
|
|
|
return std::find(map_suffixes_to_ignore->begin(), map_suffixes_to_ignore->end(),
|
|
|
|
map_name.substr(pos + 1)) != map_suffixes_to_ignore->end();
|
2017-10-20 01:08:58 +02:00
|
|
|
}
|
|
|
|
|
2017-10-25 00:36:00 +02:00
|
|
|
void Unwinder::Unwind(const std::vector<std::string>* initial_map_names_to_skip,
|
|
|
|
const std::vector<std::string>* map_suffixes_to_ignore) {
|
2017-09-20 22:37:24 +02:00
|
|
|
frames_.clear();
|
2018-01-24 02:52:23 +01:00
|
|
|
last_error_.code = ERROR_NONE;
|
|
|
|
last_error_.address = 0;
|
2017-09-20 22:37:24 +02:00
|
|
|
|
|
|
|
bool return_address_attempt = false;
|
2017-09-26 04:23:07 +02:00
|
|
|
bool adjust_pc = false;
|
2017-12-21 03:49:01 +01:00
|
|
|
std::unique_ptr<JitDebug> jit_debug;
|
2017-09-20 22:37:24 +02:00
|
|
|
for (; frames_.size() < max_frames_;) {
|
2017-11-01 00:10:42 +01:00
|
|
|
uint64_t cur_pc = regs_->pc();
|
|
|
|
uint64_t cur_sp = regs_->sp();
|
2017-09-20 22:37:24 +02:00
|
|
|
|
2017-11-01 00:10:42 +01:00
|
|
|
MapInfo* map_info = maps_->Find(regs_->pc());
|
2017-09-20 22:37:24 +02:00
|
|
|
uint64_t rel_pc;
|
2017-12-21 03:49:01 +01:00
|
|
|
uint64_t adjusted_pc;
|
2017-11-29 04:14:54 +01:00
|
|
|
uint64_t adjusted_rel_pc;
|
2017-09-26 04:23:07 +02:00
|
|
|
Elf* elf;
|
|
|
|
if (map_info == nullptr) {
|
|
|
|
rel_pc = regs_->pc();
|
2017-11-29 04:14:54 +01:00
|
|
|
adjusted_rel_pc = rel_pc;
|
2017-12-21 03:49:01 +01:00
|
|
|
adjusted_pc = rel_pc;
|
2018-01-24 02:52:23 +01:00
|
|
|
last_error_.code = ERROR_INVALID_MAP;
|
2017-09-26 04:23:07 +02:00
|
|
|
} else {
|
2017-10-20 01:08:58 +02:00
|
|
|
if (ShouldStop(map_suffixes_to_ignore, map_info->name)) {
|
|
|
|
break;
|
|
|
|
}
|
2017-09-26 04:23:07 +02:00
|
|
|
elf = map_info->GetElf(process_memory_, true);
|
|
|
|
rel_pc = elf->GetRelPc(regs_->pc(), map_info);
|
2017-11-29 04:14:54 +01:00
|
|
|
if (adjust_pc) {
|
2017-12-21 03:49:01 +01:00
|
|
|
adjusted_pc = regs_->GetAdjustedPc(rel_pc, elf);
|
2017-11-29 04:14:54 +01:00
|
|
|
} else {
|
2017-12-21 03:49:01 +01:00
|
|
|
adjusted_pc = rel_pc;
|
|
|
|
}
|
|
|
|
adjusted_rel_pc = adjusted_pc;
|
|
|
|
|
|
|
|
// If the pc is in an invalid elf file, try and get an Elf object
|
|
|
|
// using the jit debug information.
|
|
|
|
if (!elf->valid() && jit_debug_ != nullptr) {
|
|
|
|
uint64_t adjusted_jit_pc = regs_->pc() - (rel_pc - adjusted_pc);
|
|
|
|
Elf* jit_elf = jit_debug_->GetElf(maps_, adjusted_jit_pc);
|
|
|
|
if (jit_elf != nullptr) {
|
|
|
|
// The jit debug information requires a non relative adjusted pc.
|
|
|
|
adjusted_pc = adjusted_jit_pc;
|
|
|
|
adjusted_rel_pc = adjusted_pc - map_info->start;
|
|
|
|
elf = jit_elf;
|
|
|
|
}
|
2017-11-29 04:14:54 +01:00
|
|
|
}
|
2017-09-26 04:23:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (map_info == nullptr || initial_map_names_to_skip == nullptr ||
|
2017-10-25 00:36:00 +02:00
|
|
|
std::find(initial_map_names_to_skip->begin(), initial_map_names_to_skip->end(),
|
|
|
|
basename(map_info->name.c_str())) == initial_map_names_to_skip->end()) {
|
2018-01-31 04:47:24 +01:00
|
|
|
if (regs_->dex_pc() != 0) {
|
|
|
|
// Add a frame to represent the dex file.
|
|
|
|
FillInDexFrame();
|
2018-02-12 17:46:19 +01:00
|
|
|
// Clear the dex pc so that we don't repeat this frame later.
|
|
|
|
regs_->set_dex_pc(0);
|
2018-01-31 04:47:24 +01:00
|
|
|
}
|
|
|
|
|
2017-12-21 03:49:01 +01:00
|
|
|
FillInFrame(map_info, elf, adjusted_rel_pc, adjusted_pc);
|
2017-11-29 04:14:54 +01:00
|
|
|
|
2017-09-26 04:23:07 +02:00
|
|
|
// Once a frame is added, stop skipping frames.
|
|
|
|
initial_map_names_to_skip = nullptr;
|
|
|
|
}
|
|
|
|
adjust_pc = true;
|
2017-09-20 22:37:24 +02:00
|
|
|
|
|
|
|
bool stepped;
|
2017-09-26 04:23:07 +02:00
|
|
|
bool in_device_map = false;
|
2017-09-20 22:37:24 +02:00
|
|
|
if (map_info == nullptr) {
|
|
|
|
stepped = false;
|
|
|
|
} else {
|
2017-09-26 04:23:07 +02:00
|
|
|
if (map_info->flags & MAPS_FLAGS_DEVICE_MAP) {
|
|
|
|
// Do not stop here, fall through in case we are
|
|
|
|
// in the speculative unwind path and need to remove
|
|
|
|
// some of the speculative frames.
|
|
|
|
stepped = false;
|
|
|
|
in_device_map = true;
|
|
|
|
} else {
|
|
|
|
MapInfo* sp_info = maps_->Find(regs_->sp());
|
|
|
|
if (sp_info != nullptr && sp_info->flags & MAPS_FLAGS_DEVICE_MAP) {
|
|
|
|
// Do not stop here, fall through in case we are
|
|
|
|
// in the speculative unwind path and need to remove
|
|
|
|
// some of the speculative frames.
|
|
|
|
stepped = false;
|
|
|
|
in_device_map = true;
|
|
|
|
} else {
|
|
|
|
bool finished;
|
2017-12-21 03:49:01 +01:00
|
|
|
stepped = elf->Step(rel_pc, adjusted_pc, map_info->elf_offset, regs_,
|
2017-11-29 04:14:54 +01:00
|
|
|
process_memory_.get(), &finished);
|
2018-01-24 02:52:23 +01:00
|
|
|
elf->GetLastError(&last_error_);
|
2017-09-26 04:23:07 +02:00
|
|
|
if (stepped && finished) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-09-20 22:37:24 +02:00
|
|
|
}
|
|
|
|
}
|
2017-11-01 00:10:42 +01:00
|
|
|
|
2017-09-20 22:37:24 +02:00
|
|
|
if (!stepped) {
|
|
|
|
if (return_address_attempt) {
|
|
|
|
// Remove the speculative frame.
|
|
|
|
frames_.pop_back();
|
|
|
|
break;
|
2017-09-26 04:23:07 +02:00
|
|
|
} else if (in_device_map) {
|
|
|
|
// Do not attempt any other unwinding, pc or sp is in a device
|
|
|
|
// map.
|
|
|
|
break;
|
2017-09-20 22:37:24 +02:00
|
|
|
} else {
|
|
|
|
// Steping didn't work, try this secondary method.
|
|
|
|
if (!regs_->SetPcFromReturnAddress(process_memory_.get())) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return_address_attempt = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return_address_attempt = false;
|
2018-01-24 02:52:23 +01:00
|
|
|
if (max_frames_ == frames_.size()) {
|
|
|
|
last_error_.code = ERROR_MAX_FRAMES_EXCEEDED;
|
|
|
|
}
|
2017-09-20 22:37:24 +02:00
|
|
|
}
|
2017-11-01 00:10:42 +01:00
|
|
|
|
|
|
|
// If the pc and sp didn't change, then consider everything stopped.
|
|
|
|
if (cur_pc == regs_->pc() && cur_sp == regs_->sp()) {
|
2018-01-24 02:52:23 +01:00
|
|
|
last_error_.code = ERROR_REPEATED_FRAME;
|
2017-11-01 00:10:42 +01:00
|
|
|
break;
|
|
|
|
}
|
2017-09-20 22:37:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Unwinder::FormatFrame(size_t frame_num) {
|
|
|
|
if (frame_num >= frames_.size()) {
|
|
|
|
return "";
|
|
|
|
}
|
2017-12-21 03:49:01 +01:00
|
|
|
return FormatFrame(frames_[frame_num], regs_->Is32Bit());
|
2017-09-20 22:37:24 +02:00
|
|
|
}
|
|
|
|
|
2017-12-21 03:49:01 +01:00
|
|
|
std::string Unwinder::FormatFrame(const FrameData& frame, bool is32bit) {
|
2017-09-20 22:37:24 +02:00
|
|
|
std::string data;
|
|
|
|
|
2017-12-21 03:49:01 +01:00
|
|
|
if (is32bit) {
|
2017-09-20 22:37:24 +02:00
|
|
|
data += android::base::StringPrintf(" #%02zu pc %08" PRIx64, frame.num, frame.rel_pc);
|
|
|
|
} else {
|
|
|
|
data += android::base::StringPrintf(" #%02zu pc %016" PRIx64, frame.num, frame.rel_pc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame.map_offset != 0) {
|
|
|
|
data += android::base::StringPrintf(" (offset 0x%" PRIx64 ")", frame.map_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame.map_start == frame.map_end) {
|
|
|
|
// No valid map associated with this frame.
|
|
|
|
data += " <unknown>";
|
|
|
|
} else if (!frame.map_name.empty()) {
|
|
|
|
data += " " + frame.map_name;
|
|
|
|
} else {
|
|
|
|
data += android::base::StringPrintf(" <anonymous:%" PRIx64 ">", frame.map_start);
|
|
|
|
}
|
|
|
|
if (!frame.function_name.empty()) {
|
|
|
|
data += " (" + frame.function_name;
|
|
|
|
if (frame.function_offset != 0) {
|
|
|
|
data += android::base::StringPrintf("+%" PRId64, frame.function_offset);
|
|
|
|
}
|
|
|
|
data += ')';
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2017-12-21 03:49:01 +01:00
|
|
|
void Unwinder::SetJitDebug(JitDebug* jit_debug, ArchEnum arch) {
|
|
|
|
jit_debug->SetArch(arch);
|
|
|
|
jit_debug_ = jit_debug;
|
|
|
|
}
|
|
|
|
|
2018-01-31 04:47:24 +01:00
|
|
|
#if !defined(NO_LIBDEXFILE_SUPPORT)
|
|
|
|
void Unwinder::SetDexFiles(DexFiles* dex_files, ArchEnum arch) {
|
|
|
|
dex_files->SetArch(arch);
|
|
|
|
dex_files_ = dex_files;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-09-20 22:37:24 +02:00
|
|
|
} // namespace unwindstack
|