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