2017-07-27 16:29:18 +00: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define _GNU_SOURCE 1
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ucontext.h>
|
|
|
|
|
|
|
|
#include <memory>
|
2017-09-26 02:23:07 +00:00
|
|
|
#include <set>
|
2017-07-27 16:29:18 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#if !defined(__ANDROID__)
|
|
|
|
#include <cutils/threads.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <backtrace/Backtrace.h>
|
2017-08-11 22:17:46 +00:00
|
|
|
#include <demangle.h>
|
2017-07-27 16:29:18 +00:00
|
|
|
#include <unwindstack/Elf.h>
|
|
|
|
#include <unwindstack/MapInfo.h>
|
|
|
|
#include <unwindstack/Maps.h>
|
|
|
|
#include <unwindstack/Memory.h>
|
|
|
|
#include <unwindstack/Regs.h>
|
|
|
|
#include <unwindstack/RegsGetLocal.h>
|
|
|
|
|
2017-09-26 02:23:07 +00:00
|
|
|
#include <unwindstack/Unwinder.h>
|
|
|
|
|
2017-07-27 16:29:18 +00:00
|
|
|
#include "BacktraceLog.h"
|
|
|
|
#include "UnwindStack.h"
|
|
|
|
#include "UnwindStackMap.h"
|
|
|
|
|
2017-09-01 18:17:16 +00:00
|
|
|
static std::string GetFunctionName(BacktraceMap* back_map, uintptr_t pc, uintptr_t* offset) {
|
2017-07-27 16:29:18 +00:00
|
|
|
*offset = 0;
|
|
|
|
unwindstack::Maps* maps = reinterpret_cast<UnwindStackMap*>(back_map)->stack_maps();
|
|
|
|
|
|
|
|
// Get the map for this
|
|
|
|
unwindstack::MapInfo* map_info = maps->Find(pc);
|
|
|
|
if (map_info == nullptr || map_info->flags & PROT_DEVICE_MAP) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2017-09-01 18:17:16 +00:00
|
|
|
UnwindStackMap* stack_map = reinterpret_cast<UnwindStackMap*>(back_map);
|
|
|
|
unwindstack::Elf* elf = map_info->GetElf(stack_map->process_memory(), true);
|
2017-07-27 16:29:18 +00:00
|
|
|
|
|
|
|
std::string name;
|
|
|
|
uint64_t func_offset;
|
|
|
|
if (!elf->GetFunctionName(elf->GetRelPc(pc, map_info), &name, &func_offset)) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
*offset = func_offset;
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2017-09-06 21:35:35 +00:00
|
|
|
bool Backtrace::Unwind(unwindstack::Regs* regs, BacktraceMap* back_map,
|
|
|
|
std::vector<backtrace_frame_data_t>* frames, size_t num_ignore_frames) {
|
2017-09-26 02:23:07 +00:00
|
|
|
static std::set<std::string> skip_names{"libunwindstack.so", "libbacktrace.so"};
|
2017-09-01 18:17:16 +00:00
|
|
|
UnwindStackMap* stack_map = reinterpret_cast<UnwindStackMap*>(back_map);
|
2017-09-20 20:37:24 +00:00
|
|
|
auto process_memory = stack_map->process_memory();
|
2017-09-26 02:23:07 +00:00
|
|
|
unwindstack::Unwinder unwinder(MAX_BACKTRACE_FRAMES + num_ignore_frames, stack_map->stack_maps(),
|
|
|
|
regs, stack_map->process_memory());
|
|
|
|
unwinder.Unwind(&skip_names);
|
|
|
|
|
|
|
|
if (num_ignore_frames >= unwinder.NumFrames()) {
|
|
|
|
frames->resize(0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
frames->resize(unwinder.NumFrames() - num_ignore_frames);
|
|
|
|
auto unwinder_frames = unwinder.frames();
|
|
|
|
size_t cur_frame = 0;
|
|
|
|
for (size_t i = num_ignore_frames; i < unwinder.NumFrames(); i++, cur_frame++) {
|
|
|
|
auto frame = &unwinder_frames[i];
|
|
|
|
backtrace_frame_data_t* back_frame = &frames->at(cur_frame);
|
|
|
|
|
|
|
|
back_frame->num = frame->num;
|
|
|
|
|
|
|
|
back_frame->rel_pc = frame->rel_pc;
|
|
|
|
back_frame->pc = frame->pc;
|
|
|
|
back_frame->sp = frame->sp;
|
|
|
|
|
|
|
|
back_frame->func_name = frame->function_name;
|
|
|
|
back_frame->func_offset = frame->function_offset;
|
|
|
|
|
|
|
|
back_frame->map.name = frame->map_name;
|
|
|
|
back_frame->map.start = frame->map_start;
|
|
|
|
back_frame->map.end = frame->map_end;
|
|
|
|
back_frame->map.offset = frame->map_offset;
|
|
|
|
back_frame->map.load_bias = frame->map_load_bias;
|
|
|
|
back_frame->map.flags = frame->map_flags;
|
2017-07-27 16:29:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnwindStackCurrent::UnwindStackCurrent(pid_t pid, pid_t tid, BacktraceMap* map)
|
2017-09-01 18:17:16 +00:00
|
|
|
: BacktraceCurrent(pid, tid, map) {}
|
2017-07-27 16:29:18 +00:00
|
|
|
|
|
|
|
std::string UnwindStackCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
|
2017-09-01 18:17:16 +00:00
|
|
|
return ::GetFunctionName(GetMap(), pc, offset);
|
2017-07-27 16:29:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool UnwindStackCurrent::UnwindFromContext(size_t num_ignore_frames, ucontext_t* ucontext) {
|
|
|
|
std::unique_ptr<unwindstack::Regs> regs;
|
|
|
|
if (ucontext == nullptr) {
|
|
|
|
regs.reset(unwindstack::Regs::CreateFromLocal());
|
|
|
|
// Fill in the registers from this function. Do it here to avoid
|
|
|
|
// one extra function call appearing in the unwind.
|
|
|
|
unwindstack::RegsGetLocal(regs.get());
|
|
|
|
} else {
|
2017-08-25 20:55:06 +00:00
|
|
|
regs.reset(
|
|
|
|
unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentMachineType(), ucontext));
|
2017-07-27 16:29:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
error_ = BACKTRACE_UNWIND_NO_ERROR;
|
2017-09-06 21:35:35 +00:00
|
|
|
return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames);
|
2017-07-27 16:29:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnwindStackPtrace::UnwindStackPtrace(pid_t pid, pid_t tid, BacktraceMap* map)
|
2017-09-01 18:17:16 +00:00
|
|
|
: BacktracePtrace(pid, tid, map) {}
|
2017-07-27 16:29:18 +00:00
|
|
|
|
|
|
|
std::string UnwindStackPtrace::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
|
2017-09-01 18:17:16 +00:00
|
|
|
return ::GetFunctionName(GetMap(), pc, offset);
|
2017-07-27 16:29:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool UnwindStackPtrace::Unwind(size_t num_ignore_frames, ucontext_t* context) {
|
|
|
|
std::unique_ptr<unwindstack::Regs> regs;
|
|
|
|
if (context == nullptr) {
|
2017-08-25 20:55:06 +00:00
|
|
|
regs.reset(unwindstack::Regs::RemoteGet(Tid()));
|
2017-07-27 16:29:18 +00:00
|
|
|
} else {
|
2017-08-25 20:55:06 +00:00
|
|
|
regs.reset(
|
|
|
|
unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentMachineType(), context));
|
2017-07-27 16:29:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
error_ = BACKTRACE_UNWIND_NO_ERROR;
|
2017-09-06 21:35:35 +00:00
|
|
|
return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames);
|
2017-07-27 16:29:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Backtrace* Backtrace::CreateNew(pid_t pid, pid_t tid, BacktraceMap* map) {
|
|
|
|
if (pid == BACKTRACE_CURRENT_PROCESS) {
|
|
|
|
pid = getpid();
|
|
|
|
if (tid == BACKTRACE_CURRENT_THREAD) {
|
|
|
|
tid = gettid();
|
|
|
|
}
|
|
|
|
} else if (tid == BACKTRACE_CURRENT_THREAD) {
|
|
|
|
tid = pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (map == nullptr) {
|
|
|
|
// This would cause the wrong type of map object to be created, so disallow.
|
|
|
|
#if defined(__ANDROID__)
|
|
|
|
__assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__,
|
|
|
|
"Backtrace::CreateNew() must be called with a real map pointer.");
|
|
|
|
#else
|
|
|
|
BACK_LOGE("Backtrace::CreateNew() must be called with a real map pointer.");
|
|
|
|
abort();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid == getpid()) {
|
|
|
|
return new UnwindStackCurrent(pid, tid, map);
|
|
|
|
} else {
|
|
|
|
return new UnwindStackPtrace(pid, tid, map);
|
|
|
|
}
|
|
|
|
}
|