Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "libbacktrace" |
| 18 | |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 19 | #include <sys/ucontext.h> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 22 | #include <backtrace/Backtrace.h> |
| 23 | #include <backtrace/BacktraceMap.h> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 24 | |
| 25 | #define UNW_LOCAL_ONLY |
| 26 | #include <libunwind.h> |
| 27 | |
| 28 | #include "UnwindCurrent.h" |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 29 | #include "UnwindMap.h" |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 30 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 31 | //------------------------------------------------------------------------- |
| 32 | // UnwindCurrent functions. |
| 33 | //------------------------------------------------------------------------- |
| 34 | UnwindCurrent::UnwindCurrent() { |
| 35 | } |
| 36 | |
| 37 | UnwindCurrent::~UnwindCurrent() { |
| 38 | } |
| 39 | |
| 40 | bool UnwindCurrent::Unwind(size_t num_ignore_frames) { |
| 41 | int ret = unw_getcontext(&context_); |
| 42 | if (ret < 0) { |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 43 | BACK_LOGW("unw_getcontext failed %d", ret); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 44 | return false; |
| 45 | } |
| 46 | return UnwindFromContext(num_ignore_frames, true); |
| 47 | } |
| 48 | |
| 49 | std::string UnwindCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) { |
| 50 | *offset = 0; |
| 51 | char buf[512]; |
| 52 | unw_word_t value; |
| 53 | if (unw_get_proc_name_by_ip(unw_local_addr_space, pc, buf, sizeof(buf), |
| 54 | &value, &context_) >= 0 && buf[0] != '\0') { |
| 55 | *offset = static_cast<uintptr_t>(value); |
| 56 | return buf; |
| 57 | } |
| 58 | return ""; |
| 59 | } |
| 60 | |
| 61 | bool UnwindCurrent::UnwindFromContext(size_t num_ignore_frames, bool resolve) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 62 | // The cursor structure is pretty large, do not put it on the stack. |
| 63 | unw_cursor_t* cursor = new unw_cursor_t; |
| 64 | int ret = unw_init_local(cursor, &context_); |
| 65 | if (ret < 0) { |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 66 | BACK_LOGW("unw_init_local failed %d", ret); |
Christopher Ferris | ddc4f09 | 2014-01-07 14:31:07 -0800 | [diff] [blame] | 67 | delete cursor; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 68 | return false; |
| 69 | } |
| 70 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 71 | std::vector<backtrace_frame_data_t>* frames = GetFrames(); |
| 72 | frames->reserve(MAX_BACKTRACE_FRAMES); |
| 73 | size_t num_frames = 0; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 74 | do { |
| 75 | unw_word_t pc; |
| 76 | ret = unw_get_reg(cursor, UNW_REG_IP, &pc); |
| 77 | if (ret < 0) { |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 78 | BACK_LOGW("Failed to read IP %d", ret); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 79 | break; |
| 80 | } |
| 81 | unw_word_t sp; |
| 82 | ret = unw_get_reg(cursor, UNW_REG_SP, &sp); |
| 83 | if (ret < 0) { |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 84 | BACK_LOGW("Failed to read SP %d", ret); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 85 | break; |
| 86 | } |
| 87 | |
| 88 | if (num_ignore_frames == 0) { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 89 | frames->resize(num_frames+1); |
| 90 | backtrace_frame_data_t* frame = &frames->at(num_frames); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 91 | frame->num = num_frames; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 92 | frame->pc = static_cast<uintptr_t>(pc); |
| 93 | frame->sp = static_cast<uintptr_t>(sp); |
| 94 | frame->stack_size = 0; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 95 | |
| 96 | if (num_frames > 0) { |
| 97 | // Set the stack size for the previous frame. |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 98 | backtrace_frame_data_t* prev = &frames->at(num_frames-1); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 99 | prev->stack_size = frame->sp - prev->sp; |
| 100 | } |
| 101 | |
| 102 | if (resolve) { |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 103 | frame->func_name = GetFunctionName(frame->pc, &frame->func_offset); |
| 104 | frame->map = FindMap(frame->pc); |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 105 | } else { |
| 106 | frame->map = NULL; |
| 107 | frame->func_offset = 0; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 108 | } |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 109 | num_frames++; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 110 | } else { |
| 111 | num_ignore_frames--; |
| 112 | } |
| 113 | ret = unw_step (cursor); |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 114 | } while (ret > 0 && num_frames < MAX_BACKTRACE_FRAMES); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 115 | |
| 116 | delete cursor; |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | void UnwindCurrent::ExtractContext(void* sigcontext) { |
| 121 | unw_tdep_context_t* context = reinterpret_cast<unw_tdep_context_t*>(&context_); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 122 | const ucontext_t* uc = reinterpret_cast<const ucontext_t*>(sigcontext); |
| 123 | |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 124 | #if defined(__arm__) |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 125 | context->regs[0] = uc->uc_mcontext.arm_r0; |
| 126 | context->regs[1] = uc->uc_mcontext.arm_r1; |
| 127 | context->regs[2] = uc->uc_mcontext.arm_r2; |
| 128 | context->regs[3] = uc->uc_mcontext.arm_r3; |
| 129 | context->regs[4] = uc->uc_mcontext.arm_r4; |
| 130 | context->regs[5] = uc->uc_mcontext.arm_r5; |
| 131 | context->regs[6] = uc->uc_mcontext.arm_r6; |
| 132 | context->regs[7] = uc->uc_mcontext.arm_r7; |
| 133 | context->regs[8] = uc->uc_mcontext.arm_r8; |
| 134 | context->regs[9] = uc->uc_mcontext.arm_r9; |
| 135 | context->regs[10] = uc->uc_mcontext.arm_r10; |
| 136 | context->regs[11] = uc->uc_mcontext.arm_fp; |
| 137 | context->regs[12] = uc->uc_mcontext.arm_ip; |
| 138 | context->regs[13] = uc->uc_mcontext.arm_sp; |
| 139 | context->regs[14] = uc->uc_mcontext.arm_lr; |
| 140 | context->regs[15] = uc->uc_mcontext.arm_pc; |
Christopher Ferris | 5c40019 | 2014-01-30 10:30:36 -0800 | [diff] [blame] | 141 | #else |
Christopher Ferris | ddc4f09 | 2014-01-07 14:31:07 -0800 | [diff] [blame] | 142 | context->uc_mcontext = uc->uc_mcontext; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 143 | #endif |
| 144 | } |
| 145 | |
| 146 | //------------------------------------------------------------------------- |
| 147 | // UnwindThread functions. |
| 148 | //------------------------------------------------------------------------- |
| 149 | UnwindThread::UnwindThread() { |
| 150 | } |
| 151 | |
| 152 | UnwindThread::~UnwindThread() { |
| 153 | } |
| 154 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 155 | void UnwindThread::ThreadUnwind( |
| 156 | siginfo_t* /*siginfo*/, void* sigcontext, size_t num_ignore_frames) { |
| 157 | ExtractContext(sigcontext); |
| 158 | UnwindFromContext(num_ignore_frames, false); |
| 159 | } |
| 160 | |
| 161 | //------------------------------------------------------------------------- |
| 162 | // C++ object creation function. |
| 163 | //------------------------------------------------------------------------- |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 164 | Backtrace* CreateCurrentObj(BacktraceMap* map) { |
| 165 | return new BacktraceCurrent(new UnwindCurrent(), map); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 168 | Backtrace* CreateThreadObj(pid_t tid, BacktraceMap* map) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 169 | UnwindThread* thread_obj = new UnwindThread(); |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 170 | return new BacktraceThread(thread_obj, thread_obj, tid, map); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 171 | } |