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 | |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 17 | #include <stdint.h> |
Christopher Ferris | c58287d | 2014-05-09 16:59:06 -0700 | [diff] [blame] | 18 | #include <ucontext.h> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 19 | |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 20 | #include <memory> |
| 21 | #include <string> |
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 | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 26 | #include <android-base/logging.h> |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 27 | #include <backtrace/Backtrace.h> |
| 28 | |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 29 | #include "BacktraceLog.h" |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 30 | #include "UnwindCurrent.h" |
| 31 | |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 32 | std::string UnwindCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) { |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 33 | if (!initialized_) { |
| 34 | // If init local is not called, then trying to get a function name will |
| 35 | // fail, so try to initialize first. |
| 36 | std::unique_ptr<unw_cursor_t> cursor(new unw_cursor_t); |
| 37 | if (unw_init_local(cursor.get(), &context_) < 0) { |
| 38 | return ""; |
| 39 | } |
| 40 | initialized_ = true; |
| 41 | } |
| 42 | |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 43 | *offset = 0; |
| 44 | char buf[512]; |
| 45 | unw_word_t value; |
| 46 | if (unw_get_proc_name_by_ip(unw_local_addr_space, pc, buf, sizeof(buf), |
| 47 | &value, &context_) >= 0 && buf[0] != '\0') { |
| 48 | *offset = static_cast<uintptr_t>(value); |
| 49 | return buf; |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 50 | } |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 51 | return ""; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 54 | void UnwindCurrent::GetUnwContextFromUcontext(const ucontext_t* ucontext) { |
| 55 | unw_tdep_context_t* unw_context = reinterpret_cast<unw_tdep_context_t*>(&context_); |
| 56 | |
| 57 | #if defined(__arm__) |
| 58 | unw_context->regs[0] = ucontext->uc_mcontext.arm_r0; |
| 59 | unw_context->regs[1] = ucontext->uc_mcontext.arm_r1; |
| 60 | unw_context->regs[2] = ucontext->uc_mcontext.arm_r2; |
| 61 | unw_context->regs[3] = ucontext->uc_mcontext.arm_r3; |
| 62 | unw_context->regs[4] = ucontext->uc_mcontext.arm_r4; |
| 63 | unw_context->regs[5] = ucontext->uc_mcontext.arm_r5; |
| 64 | unw_context->regs[6] = ucontext->uc_mcontext.arm_r6; |
| 65 | unw_context->regs[7] = ucontext->uc_mcontext.arm_r7; |
| 66 | unw_context->regs[8] = ucontext->uc_mcontext.arm_r8; |
| 67 | unw_context->regs[9] = ucontext->uc_mcontext.arm_r9; |
| 68 | unw_context->regs[10] = ucontext->uc_mcontext.arm_r10; |
| 69 | unw_context->regs[11] = ucontext->uc_mcontext.arm_fp; |
| 70 | unw_context->regs[12] = ucontext->uc_mcontext.arm_ip; |
| 71 | unw_context->regs[13] = ucontext->uc_mcontext.arm_sp; |
| 72 | unw_context->regs[14] = ucontext->uc_mcontext.arm_lr; |
| 73 | unw_context->regs[15] = ucontext->uc_mcontext.arm_pc; |
| 74 | #else |
| 75 | unw_context->uc_mcontext = ucontext->uc_mcontext; |
| 76 | #endif |
| 77 | } |
| 78 | |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 79 | bool UnwindCurrent::UnwindFromContext(size_t num_ignore_frames, ucontext_t* ucontext) { |
| 80 | if (ucontext == nullptr) { |
| 81 | int ret = unw_getcontext(&context_); |
| 82 | if (ret < 0) { |
| 83 | BACK_LOGW("unw_getcontext failed %d", ret); |
Christopher Ferris | c463ba4 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 84 | error_ = BACKTRACE_UNWIND_ERROR_SETUP_FAILED; |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 85 | return false; |
Christopher Ferris | 17224d5 | 2014-04-11 13:05:07 -0700 | [diff] [blame] | 86 | } |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 87 | } else { |
| 88 | GetUnwContextFromUcontext(ucontext); |
| 89 | } |
| 90 | |
| 91 | // The cursor structure is pretty large, do not put it on the stack. |
| 92 | std::unique_ptr<unw_cursor_t> cursor(new unw_cursor_t); |
| 93 | int ret = unw_init_local(cursor.get(), &context_); |
| 94 | if (ret < 0) { |
| 95 | BACK_LOGW("unw_init_local failed %d", ret); |
Christopher Ferris | c463ba4 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 96 | error_ = BACKTRACE_UNWIND_ERROR_SETUP_FAILED; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 97 | return false; |
| 98 | } |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 99 | initialized_ = true; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 100 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 101 | size_t num_frames = 0; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 102 | do { |
| 103 | unw_word_t pc; |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 104 | ret = unw_get_reg(cursor.get(), UNW_REG_IP, &pc); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 105 | if (ret < 0) { |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 106 | BACK_LOGW("Failed to read IP %d", ret); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 107 | break; |
| 108 | } |
| 109 | unw_word_t sp; |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 110 | ret = unw_get_reg(cursor.get(), UNW_REG_SP, &sp); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 111 | if (ret < 0) { |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 112 | BACK_LOGW("Failed to read SP %d", ret); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 113 | break; |
| 114 | } |
| 115 | |
Christopher Ferris | ca09ce9 | 2015-03-31 17:28:22 -0700 | [diff] [blame] | 116 | frames_.resize(num_frames+1); |
| 117 | backtrace_frame_data_t* frame = &frames_.at(num_frames); |
| 118 | frame->num = num_frames; |
| 119 | frame->pc = static_cast<uintptr_t>(pc); |
| 120 | frame->sp = static_cast<uintptr_t>(sp); |
| 121 | frame->stack_size = 0; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 122 | |
Christopher Ferris | ca09ce9 | 2015-03-31 17:28:22 -0700 | [diff] [blame] | 123 | FillInMap(frame->pc, &frame->map); |
| 124 | // Check to see if we should skip this frame because it's coming |
| 125 | // from within the library, and we are doing a local unwind. |
| 126 | if (ucontext != nullptr || num_frames != 0 || !DiscardFrame(*frame)) { |
| 127 | if (num_ignore_frames == 0) { |
| 128 | // GetFunctionName is an expensive call, only do it if we are |
| 129 | // keeping the frame. |
Christopher Ferris | f5e568e | 2017-03-22 13:18:31 -0700 | [diff] [blame] | 130 | frame->func_name = GetFunctionName(frame->pc, &frame->func_offset, &frame->map); |
Christopher Ferris | ca09ce9 | 2015-03-31 17:28:22 -0700 | [diff] [blame] | 131 | if (num_frames > 0) { |
| 132 | // Set the stack size for the previous frame. |
| 133 | backtrace_frame_data_t* prev = &frames_.at(num_frames-1); |
| 134 | prev->stack_size = frame->sp - prev->sp; |
| 135 | } |
| 136 | num_frames++; |
| 137 | } else { |
| 138 | num_ignore_frames--; |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 139 | // Set the number of frames to zero to remove the frame added |
| 140 | // above. By definition, if we still have frames to ignore |
| 141 | // there should only be one frame in the vector. |
| 142 | CHECK(num_frames == 0); |
| 143 | frames_.resize(0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 144 | } |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 145 | } |
Christopher Ferris | f5e568e | 2017-03-22 13:18:31 -0700 | [diff] [blame] | 146 | // If the pc is in a device map, then don't try to step. |
| 147 | if (frame->map.flags & PROT_DEVICE_MAP) { |
| 148 | break; |
| 149 | } |
| 150 | // Verify the sp is not in a device map too. |
| 151 | backtrace_map_t map; |
| 152 | FillInMap(frame->sp, &map); |
| 153 | if (map.flags & PROT_DEVICE_MAP) { |
| 154 | break; |
| 155 | } |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 156 | ret = unw_step (cursor.get()); |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 157 | } while (ret > 0 && num_frames < MAX_BACKTRACE_FRAMES); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 158 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 159 | return true; |
| 160 | } |