2013-10-21 22:30:52 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 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.
|
|
|
|
*/
|
|
|
|
|
2015-03-27 03:18:36 +01:00
|
|
|
#include <stdint.h>
|
2013-10-21 22:30:52 +02:00
|
|
|
#include <sys/types.h>
|
2014-05-10 01:59:06 +02:00
|
|
|
#include <ucontext.h>
|
2013-10-21 22:30:52 +02:00
|
|
|
|
|
|
|
#include <libunwind.h>
|
|
|
|
#include <libunwind-ptrace.h>
|
|
|
|
|
2015-03-27 03:18:36 +01:00
|
|
|
#include <backtrace/Backtrace.h>
|
|
|
|
#include <backtrace/BacktraceMap.h>
|
|
|
|
|
2014-03-08 04:42:19 +01:00
|
|
|
#include "BacktraceLog.h"
|
2014-01-23 04:21:07 +01:00
|
|
|
#include "UnwindMap.h"
|
2013-10-21 22:30:52 +02:00
|
|
|
#include "UnwindPtrace.h"
|
|
|
|
|
2015-03-27 03:18:36 +01:00
|
|
|
UnwindPtrace::UnwindPtrace(pid_t pid, pid_t tid, BacktraceMap* map)
|
|
|
|
: BacktracePtrace(pid, tid, map), addr_space_(nullptr), upt_info_(nullptr) {
|
2013-10-21 22:30:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
UnwindPtrace::~UnwindPtrace() {
|
|
|
|
if (upt_info_) {
|
|
|
|
_UPT_destroy(upt_info_);
|
2015-03-27 03:18:36 +01:00
|
|
|
upt_info_ = nullptr;
|
2013-10-21 22:30:52 +02:00
|
|
|
}
|
|
|
|
if (addr_space_) {
|
2014-01-23 04:21:07 +01:00
|
|
|
// Remove the map from the address space before destroying it.
|
|
|
|
// It will be freed in the UnwindMap destructor.
|
2015-03-27 03:18:36 +01:00
|
|
|
unw_map_set(addr_space_, nullptr);
|
2014-01-23 04:21:07 +01:00
|
|
|
|
2013-10-21 22:30:52 +02:00
|
|
|
unw_destroy_addr_space(addr_space_);
|
2015-03-27 03:18:36 +01:00
|
|
|
addr_space_ = nullptr;
|
2013-10-21 22:30:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-07 00:23:59 +02:00
|
|
|
bool UnwindPtrace::Unwind(size_t num_ignore_frames, ucontext_t* ucontext) {
|
2015-05-15 00:39:52 +02:00
|
|
|
if (GetMap() == nullptr) {
|
|
|
|
// Without a map object, we can't do anything.
|
2016-03-09 23:35:54 +01:00
|
|
|
error_ = BACKTRACE_UNWIND_ERROR_MAP_MISSING;
|
2015-05-15 00:39:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-09 23:35:54 +01:00
|
|
|
error_ = BACKTRACE_UNWIND_NO_ERROR;
|
|
|
|
|
2014-05-07 00:23:59 +02:00
|
|
|
if (ucontext) {
|
|
|
|
BACK_LOGW("Unwinding from a specified context not supported yet.");
|
2016-03-09 23:35:54 +01:00
|
|
|
error_ = BACKTRACE_UNWIND_ERROR_UNSUPPORTED_OPERATION;
|
2014-05-07 00:23:59 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-21 22:30:52 +02:00
|
|
|
addr_space_ = unw_create_addr_space(&_UPT_accessors, 0);
|
|
|
|
if (!addr_space_) {
|
2013-10-29 23:44:25 +01:00
|
|
|
BACK_LOGW("unw_create_addr_space failed.");
|
2016-03-09 23:35:54 +01:00
|
|
|
error_ = BACKTRACE_UNWIND_ERROR_SETUP_FAILED;
|
2013-10-21 22:30:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-23 04:21:07 +01:00
|
|
|
UnwindMap* map = static_cast<UnwindMap*>(GetMap());
|
|
|
|
unw_map_set(addr_space_, map->GetMapCursor());
|
|
|
|
|
|
|
|
upt_info_ = reinterpret_cast<struct UPT_info*>(_UPT_create(Tid()));
|
2013-10-21 22:30:52 +02:00
|
|
|
if (!upt_info_) {
|
2013-10-29 23:44:25 +01:00
|
|
|
BACK_LOGW("Failed to create upt info.");
|
2016-03-09 23:35:54 +01:00
|
|
|
error_ = BACKTRACE_UNWIND_ERROR_SETUP_FAILED;
|
2013-10-21 22:30:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unw_cursor_t cursor;
|
|
|
|
int ret = unw_init_remote(&cursor, addr_space_, upt_info_);
|
|
|
|
if (ret < 0) {
|
2013-10-29 23:44:25 +01:00
|
|
|
BACK_LOGW("unw_init_remote failed %d", ret);
|
2016-03-09 23:35:54 +01:00
|
|
|
error_ = BACKTRACE_UNWIND_ERROR_SETUP_FAILED;
|
2013-10-21 22:30:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-15 05:16:30 +01:00
|
|
|
size_t num_frames = 0;
|
2013-10-21 22:30:52 +02:00
|
|
|
do {
|
|
|
|
unw_word_t pc;
|
|
|
|
ret = unw_get_reg(&cursor, UNW_REG_IP, &pc);
|
|
|
|
if (ret < 0) {
|
2013-10-29 23:44:25 +01:00
|
|
|
BACK_LOGW("Failed to read IP %d", ret);
|
2013-10-21 22:30:52 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
unw_word_t sp;
|
|
|
|
ret = unw_get_reg(&cursor, UNW_REG_SP, &sp);
|
|
|
|
if (ret < 0) {
|
2013-10-29 23:44:25 +01:00
|
|
|
BACK_LOGW("Failed to read SP %d", ret);
|
2013-10-21 22:30:52 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_ignore_frames == 0) {
|
2015-03-27 03:18:36 +01:00
|
|
|
frames_.resize(num_frames+1);
|
|
|
|
backtrace_frame_data_t* frame = &frames_.at(num_frames);
|
2014-01-11 01:33:16 +01:00
|
|
|
frame->num = num_frames;
|
2013-10-21 22:30:52 +02:00
|
|
|
frame->pc = static_cast<uintptr_t>(pc);
|
|
|
|
frame->sp = static_cast<uintptr_t>(sp);
|
|
|
|
frame->stack_size = 0;
|
|
|
|
|
|
|
|
if (num_frames > 0) {
|
2015-03-27 03:18:36 +01:00
|
|
|
backtrace_frame_data_t* prev = &frames_.at(num_frames-1);
|
2013-10-21 22:30:52 +02:00
|
|
|
prev->stack_size = frame->sp - prev->sp;
|
|
|
|
}
|
|
|
|
|
2014-01-23 04:21:07 +01:00
|
|
|
frame->func_name = GetFunctionName(frame->pc, &frame->func_offset);
|
2013-10-21 22:30:52 +02:00
|
|
|
|
2015-02-06 22:22:01 +01:00
|
|
|
FillInMap(frame->pc, &frame->map);
|
2013-10-21 22:30:52 +02:00
|
|
|
|
2014-01-15 05:16:30 +01:00
|
|
|
num_frames++;
|
2013-10-21 22:30:52 +02:00
|
|
|
} else {
|
|
|
|
num_ignore_frames--;
|
|
|
|
}
|
|
|
|
ret = unw_step (&cursor);
|
2014-01-15 05:16:30 +01:00
|
|
|
} while (ret > 0 && num_frames < MAX_BACKTRACE_FRAMES);
|
2013-10-21 22:30:52 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string UnwindPtrace::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
|
|
|
|
*offset = 0;
|
|
|
|
char buf[512];
|
|
|
|
unw_word_t value;
|
|
|
|
if (unw_get_proc_name_by_ip(addr_space_, pc, buf, sizeof(buf), &value,
|
|
|
|
upt_info_) >= 0 && buf[0] != '\0') {
|
|
|
|
*offset = static_cast<uintptr_t>(value);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|