2017-07-27 18:29:18 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define _GNU_SOURCE 1
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <memory>
|
2017-09-26 04:23:07 +02:00
|
|
|
#include <set>
|
2017-07-27 18:29:18 +02:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <backtrace/Backtrace.h>
|
|
|
|
#include <unwindstack/Elf.h>
|
|
|
|
#include <unwindstack/MapInfo.h>
|
|
|
|
#include <unwindstack/Maps.h>
|
|
|
|
#include <unwindstack/Memory.h>
|
|
|
|
#include <unwindstack/Regs.h>
|
|
|
|
#include <unwindstack/RegsGetLocal.h>
|
|
|
|
|
2019-04-05 20:23:32 +02:00
|
|
|
#if !defined(NO_LIBDEXFILE_SUPPORT)
|
|
|
|
#include <unwindstack/DexFiles.h>
|
|
|
|
#endif
|
2017-09-26 04:23:07 +02:00
|
|
|
#include <unwindstack/Unwinder.h>
|
|
|
|
|
2017-07-27 18:29:18 +02:00
|
|
|
#include "BacktraceLog.h"
|
|
|
|
#include "UnwindStack.h"
|
|
|
|
#include "UnwindStackMap.h"
|
|
|
|
|
2019-07-18 23:11:07 +02:00
|
|
|
extern "C" char* __cxa_demangle(const char*, char*, size_t*, int*);
|
|
|
|
|
2017-09-06 23:35:35 +02:00
|
|
|
bool Backtrace::Unwind(unwindstack::Regs* regs, BacktraceMap* back_map,
|
2017-11-03 00:19:56 +01:00
|
|
|
std::vector<backtrace_frame_data_t>* frames, size_t num_ignore_frames,
|
2017-12-12 02:44:33 +01:00
|
|
|
std::vector<std::string>* skip_names, BacktraceUnwindError* error) {
|
2017-09-01 20:17:16 +02:00
|
|
|
UnwindStackMap* stack_map = reinterpret_cast<UnwindStackMap*>(back_map);
|
2017-09-20 22:37:24 +02:00
|
|
|
auto process_memory = stack_map->process_memory();
|
2017-09-26 04:23:07 +02:00
|
|
|
unwindstack::Unwinder unwinder(MAX_BACKTRACE_FRAMES + num_ignore_frames, stack_map->stack_maps(),
|
|
|
|
regs, stack_map->process_memory());
|
2018-02-20 22:58:40 +01:00
|
|
|
unwinder.SetResolveNames(stack_map->ResolveNames());
|
2018-10-24 02:42:41 +02:00
|
|
|
stack_map->SetArch(regs->Arch());
|
2019-04-05 20:23:32 +02:00
|
|
|
if (stack_map->GetJitDebug() != nullptr) {
|
|
|
|
unwinder.SetJitDebug(stack_map->GetJitDebug(), regs->Arch());
|
|
|
|
}
|
|
|
|
#if !defined(NO_LIBDEXFILE_SUPPORT)
|
|
|
|
if (stack_map->GetDexFiles() != nullptr) {
|
|
|
|
unwinder.SetDexFiles(stack_map->GetDexFiles(), regs->Arch());
|
|
|
|
}
|
|
|
|
#endif
|
2017-11-03 00:19:56 +01:00
|
|
|
unwinder.Unwind(skip_names, &stack_map->GetSuffixesToIgnore());
|
2017-12-12 02:44:33 +01:00
|
|
|
if (error != nullptr) {
|
|
|
|
switch (unwinder.LastErrorCode()) {
|
|
|
|
case unwindstack::ERROR_NONE:
|
|
|
|
error->error_code = BACKTRACE_UNWIND_NO_ERROR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case unwindstack::ERROR_MEMORY_INVALID:
|
|
|
|
error->error_code = BACKTRACE_UNWIND_ERROR_ACCESS_MEM_FAILED;
|
|
|
|
error->error_info.addr = unwinder.LastErrorAddress();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case unwindstack::ERROR_UNWIND_INFO:
|
|
|
|
error->error_code = BACKTRACE_UNWIND_ERROR_UNWIND_INFO;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case unwindstack::ERROR_UNSUPPORTED:
|
|
|
|
error->error_code = BACKTRACE_UNWIND_ERROR_UNSUPPORTED_OPERATION;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case unwindstack::ERROR_INVALID_MAP:
|
|
|
|
error->error_code = BACKTRACE_UNWIND_ERROR_MAP_MISSING;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case unwindstack::ERROR_MAX_FRAMES_EXCEEDED:
|
|
|
|
error->error_code = BACKTRACE_UNWIND_ERROR_EXCEED_MAX_FRAMES_LIMIT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case unwindstack::ERROR_REPEATED_FRAME:
|
|
|
|
error->error_code = BACKTRACE_UNWIND_ERROR_REPEATED_FRAME;
|
|
|
|
break;
|
2019-04-12 04:45:35 +02:00
|
|
|
|
|
|
|
case unwindstack::ERROR_INVALID_ELF:
|
|
|
|
error->error_code = BACKTRACE_UNWIND_ERROR_INVALID_ELF;
|
|
|
|
break;
|
2017-12-12 02:44:33 +01:00
|
|
|
}
|
|
|
|
}
|
2017-09-26 04:23:07 +02:00
|
|
|
|
|
|
|
if (num_ignore_frames >= unwinder.NumFrames()) {
|
|
|
|
frames->resize(0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto unwinder_frames = unwinder.frames();
|
2018-01-31 04:47:24 +01:00
|
|
|
frames->resize(unwinder.NumFrames() - num_ignore_frames);
|
2017-09-26 04:23:07 +02:00
|
|
|
size_t cur_frame = 0;
|
2018-01-25 13:21:59 +01:00
|
|
|
for (size_t i = num_ignore_frames; i < unwinder.NumFrames(); i++) {
|
2017-09-26 04:23:07 +02:00
|
|
|
auto frame = &unwinder_frames[i];
|
2018-01-25 13:21:59 +01:00
|
|
|
|
2018-01-26 23:26:13 +01:00
|
|
|
backtrace_frame_data_t* back_frame = &frames->at(cur_frame);
|
2017-09-26 04:23:07 +02:00
|
|
|
|
2018-01-26 23:26:13 +01:00
|
|
|
back_frame->num = cur_frame++;
|
2017-09-26 04:23:07 +02:00
|
|
|
|
|
|
|
back_frame->rel_pc = frame->rel_pc;
|
|
|
|
back_frame->pc = frame->pc;
|
|
|
|
back_frame->sp = frame->sp;
|
|
|
|
|
2019-07-18 23:11:07 +02:00
|
|
|
char* demangled_name = __cxa_demangle(frame->function_name.c_str(), nullptr, nullptr, nullptr);
|
|
|
|
if (demangled_name != nullptr) {
|
|
|
|
back_frame->func_name = demangled_name;
|
|
|
|
free(demangled_name);
|
|
|
|
} else {
|
|
|
|
back_frame->func_name = frame->function_name;
|
|
|
|
}
|
2017-09-26 04:23:07 +02:00
|
|
|
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;
|
2018-12-14 01:08:50 +01:00
|
|
|
back_frame->map.offset = frame->map_elf_start_offset;
|
2017-09-26 04:23:07 +02:00
|
|
|
back_frame->map.load_bias = frame->map_load_bias;
|
|
|
|
back_frame->map.flags = frame->map_flags;
|
2017-07-27 18:29:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnwindStackCurrent::UnwindStackCurrent(pid_t pid, pid_t tid, BacktraceMap* map)
|
2017-09-01 20:17:16 +02:00
|
|
|
: BacktraceCurrent(pid, tid, map) {}
|
2017-07-27 18:29:18 +02:00
|
|
|
|
2018-01-18 20:15:49 +01:00
|
|
|
std::string UnwindStackCurrent::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) {
|
2017-09-07 07:16:09 +02:00
|
|
|
return GetMap()->GetFunctionName(pc, offset);
|
2017-07-27 18:29:18 +02:00
|
|
|
}
|
|
|
|
|
2017-12-12 02:44:33 +01:00
|
|
|
bool UnwindStackCurrent::UnwindFromContext(size_t num_ignore_frames, void* ucontext) {
|
2017-07-27 18:29:18 +02:00
|
|
|
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-12-01 03:56:01 +01:00
|
|
|
regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), ucontext));
|
2017-07-27 18:29:18 +02:00
|
|
|
}
|
|
|
|
|
2017-11-03 00:19:56 +01:00
|
|
|
std::vector<std::string> skip_names{"libunwindstack.so", "libbacktrace.so"};
|
2018-03-23 20:51:43 +01:00
|
|
|
if (!skip_frames_) {
|
|
|
|
skip_names.clear();
|
|
|
|
}
|
2017-12-12 02:44:33 +01:00
|
|
|
return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, &skip_names, &error_);
|
2017-07-27 18:29:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
UnwindStackPtrace::UnwindStackPtrace(pid_t pid, pid_t tid, BacktraceMap* map)
|
2019-03-13 01:50:52 +01:00
|
|
|
: BacktracePtrace(pid, tid, map), memory_(unwindstack::Memory::CreateProcessMemory(pid)) {}
|
2017-07-27 18:29:18 +02:00
|
|
|
|
2018-01-18 20:15:49 +01:00
|
|
|
std::string UnwindStackPtrace::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) {
|
2017-09-07 07:16:09 +02:00
|
|
|
return GetMap()->GetFunctionName(pc, offset);
|
2017-07-27 18:29:18 +02:00
|
|
|
}
|
|
|
|
|
2017-12-12 02:44:33 +01:00
|
|
|
bool UnwindStackPtrace::Unwind(size_t num_ignore_frames, void* context) {
|
2017-07-27 18:29:18 +02:00
|
|
|
std::unique_ptr<unwindstack::Regs> regs;
|
|
|
|
if (context == nullptr) {
|
2017-08-25 22:55:06 +02:00
|
|
|
regs.reset(unwindstack::Regs::RemoteGet(Tid()));
|
2017-07-27 18:29:18 +02:00
|
|
|
} else {
|
2017-12-01 03:56:01 +01:00
|
|
|
regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), context));
|
2017-07-27 18:29:18 +02:00
|
|
|
}
|
|
|
|
|
2017-12-12 02:44:33 +01:00
|
|
|
return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, nullptr, &error_);
|
2017-07-27 18:29:18 +02:00
|
|
|
}
|
2017-12-08 02:41:18 +01:00
|
|
|
|
2018-01-18 20:15:49 +01:00
|
|
|
size_t UnwindStackPtrace::Read(uint64_t addr, uint8_t* buffer, size_t bytes) {
|
2019-03-13 01:50:52 +01:00
|
|
|
return memory_->Read(addr, buffer, bytes);
|
2017-12-12 02:44:33 +01:00
|
|
|
}
|