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>
|
2014-05-10 01:59:06 +02:00
|
|
|
#include <ucontext.h>
|
2013-10-21 22:30:52 +02:00
|
|
|
|
2015-03-27 03:18:36 +01:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2013-10-21 22:30:52 +02:00
|
|
|
|
|
|
|
#define UNW_LOCAL_ONLY
|
|
|
|
#include <libunwind.h>
|
|
|
|
|
2017-03-14 23:22:26 +01:00
|
|
|
#include <android-base/logging.h>
|
2015-03-27 03:18:36 +01:00
|
|
|
#include <backtrace/Backtrace.h>
|
|
|
|
|
2014-03-08 04:42:19 +01:00
|
|
|
#include "BacktraceLog.h"
|
2013-10-21 22:30:52 +02:00
|
|
|
#include "UnwindCurrent.h"
|
|
|
|
|
2015-03-27 03:18:36 +01:00
|
|
|
std::string UnwindCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
|
2017-03-14 23:22:26 +01:00
|
|
|
if (!initialized_) {
|
|
|
|
// If init local is not called, then trying to get a function name will
|
|
|
|
// fail, so try to initialize first.
|
|
|
|
std::unique_ptr<unw_cursor_t> cursor(new unw_cursor_t);
|
|
|
|
if (unw_init_local(cursor.get(), &context_) < 0) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
initialized_ = true;
|
|
|
|
}
|
|
|
|
|
2015-03-27 03:18:36 +01:00
|
|
|
*offset = 0;
|
|
|
|
char buf[512];
|
|
|
|
unw_word_t value;
|
|
|
|
if (unw_get_proc_name_by_ip(unw_local_addr_space, pc, buf, sizeof(buf),
|
|
|
|
&value, &context_) >= 0 && buf[0] != '\0') {
|
|
|
|
*offset = static_cast<uintptr_t>(value);
|
|
|
|
return buf;
|
2013-10-21 22:30:52 +02:00
|
|
|
}
|
2015-03-27 03:18:36 +01:00
|
|
|
return "";
|
2013-10-21 22:30:52 +02:00
|
|
|
}
|
|
|
|
|
2014-05-07 00:23:59 +02:00
|
|
|
void UnwindCurrent::GetUnwContextFromUcontext(const ucontext_t* ucontext) {
|
|
|
|
unw_tdep_context_t* unw_context = reinterpret_cast<unw_tdep_context_t*>(&context_);
|
|
|
|
|
|
|
|
#if defined(__arm__)
|
|
|
|
unw_context->regs[0] = ucontext->uc_mcontext.arm_r0;
|
|
|
|
unw_context->regs[1] = ucontext->uc_mcontext.arm_r1;
|
|
|
|
unw_context->regs[2] = ucontext->uc_mcontext.arm_r2;
|
|
|
|
unw_context->regs[3] = ucontext->uc_mcontext.arm_r3;
|
|
|
|
unw_context->regs[4] = ucontext->uc_mcontext.arm_r4;
|
|
|
|
unw_context->regs[5] = ucontext->uc_mcontext.arm_r5;
|
|
|
|
unw_context->regs[6] = ucontext->uc_mcontext.arm_r6;
|
|
|
|
unw_context->regs[7] = ucontext->uc_mcontext.arm_r7;
|
|
|
|
unw_context->regs[8] = ucontext->uc_mcontext.arm_r8;
|
|
|
|
unw_context->regs[9] = ucontext->uc_mcontext.arm_r9;
|
|
|
|
unw_context->regs[10] = ucontext->uc_mcontext.arm_r10;
|
|
|
|
unw_context->regs[11] = ucontext->uc_mcontext.arm_fp;
|
|
|
|
unw_context->regs[12] = ucontext->uc_mcontext.arm_ip;
|
|
|
|
unw_context->regs[13] = ucontext->uc_mcontext.arm_sp;
|
|
|
|
unw_context->regs[14] = ucontext->uc_mcontext.arm_lr;
|
|
|
|
unw_context->regs[15] = ucontext->uc_mcontext.arm_pc;
|
|
|
|
#else
|
|
|
|
unw_context->uc_mcontext = ucontext->uc_mcontext;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-03-27 03:18:36 +01:00
|
|
|
bool UnwindCurrent::UnwindFromContext(size_t num_ignore_frames, ucontext_t* ucontext) {
|
|
|
|
if (ucontext == nullptr) {
|
|
|
|
int ret = unw_getcontext(&context_);
|
|
|
|
if (ret < 0) {
|
|
|
|
BACK_LOGW("unw_getcontext failed %d", ret);
|
2016-03-09 23:35:54 +01:00
|
|
|
error_ = BACKTRACE_UNWIND_ERROR_SETUP_FAILED;
|
2015-03-27 03:18:36 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
GetUnwContextFromUcontext(ucontext);
|
2013-10-21 22:30:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// The cursor structure is pretty large, do not put it on the stack.
|
2015-03-27 03:18:36 +01:00
|
|
|
std::unique_ptr<unw_cursor_t> cursor(new unw_cursor_t);
|
|
|
|
int ret = unw_init_local(cursor.get(), &context_);
|
2013-10-21 22:30:52 +02:00
|
|
|
if (ret < 0) {
|
2015-03-27 03:18:36 +01:00
|
|
|
BACK_LOGW("unw_init_local 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;
|
|
|
|
}
|
2017-03-14 23:22:26 +01:00
|
|
|
initialized_ = true;
|
2013-10-21 22:30:52 +02:00
|
|
|
|
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;
|
2015-03-27 03:18:36 +01:00
|
|
|
ret = unw_get_reg(cursor.get(), UNW_REG_IP, &pc);
|
2013-10-21 22:30:52 +02:00
|
|
|
if (ret < 0) {
|
2015-03-27 03:18:36 +01:00
|
|
|
BACK_LOGW("Failed to read IP %d", ret);
|
2013-10-21 22:30:52 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
unw_word_t sp;
|
2015-03-27 03:18:36 +01:00
|
|
|
ret = unw_get_reg(cursor.get(), UNW_REG_SP, &sp);
|
2013-10-21 22:30:52 +02:00
|
|
|
if (ret < 0) {
|
2015-03-27 03:18:36 +01:00
|
|
|
BACK_LOGW("Failed to read SP %d", ret);
|
2013-10-21 22:30:52 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-04-01 02:28:22 +02:00
|
|
|
frames_.resize(num_frames+1);
|
|
|
|
backtrace_frame_data_t* frame = &frames_.at(num_frames);
|
|
|
|
frame->num = num_frames;
|
|
|
|
frame->pc = static_cast<uintptr_t>(pc);
|
|
|
|
frame->sp = static_cast<uintptr_t>(sp);
|
|
|
|
frame->stack_size = 0;
|
2013-10-21 22:30:52 +02:00
|
|
|
|
2015-04-01 02:28:22 +02:00
|
|
|
FillInMap(frame->pc, &frame->map);
|
|
|
|
// Check to see if we should skip this frame because it's coming
|
|
|
|
// from within the library, and we are doing a local unwind.
|
|
|
|
if (ucontext != nullptr || num_frames != 0 || !DiscardFrame(*frame)) {
|
|
|
|
if (num_ignore_frames == 0) {
|
|
|
|
// GetFunctionName is an expensive call, only do it if we are
|
|
|
|
// keeping the frame.
|
|
|
|
frame->func_name = GetFunctionName(frame->pc, &frame->func_offset);
|
|
|
|
if (num_frames > 0) {
|
|
|
|
// Set the stack size for the previous frame.
|
|
|
|
backtrace_frame_data_t* prev = &frames_.at(num_frames-1);
|
|
|
|
prev->stack_size = frame->sp - prev->sp;
|
|
|
|
}
|
|
|
|
num_frames++;
|
|
|
|
} else {
|
|
|
|
num_ignore_frames--;
|
2017-03-14 23:22:26 +01:00
|
|
|
// Set the number of frames to zero to remove the frame added
|
|
|
|
// above. By definition, if we still have frames to ignore
|
|
|
|
// there should only be one frame in the vector.
|
|
|
|
CHECK(num_frames == 0);
|
|
|
|
frames_.resize(0);
|
2013-10-21 22:30:52 +02:00
|
|
|
}
|
|
|
|
}
|
2015-03-27 03:18:36 +01:00
|
|
|
ret = unw_step (cursor.get());
|
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;
|
|
|
|
}
|