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 | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 18 | #include <sys/types.h> |
Christopher Ferris | c58287d | 2014-05-09 16:59:06 -0700 | [diff] [blame] | 19 | #include <ucontext.h> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 20 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 21 | #include <libunwind.h> |
| 22 | #include <libunwind-ptrace.h> |
| 23 | |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 24 | #include <backtrace/Backtrace.h> |
| 25 | #include <backtrace/BacktraceMap.h> |
| 26 | |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 27 | #include "BacktraceLog.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 | #include "UnwindPtrace.h" |
| 30 | |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 31 | UnwindPtrace::UnwindPtrace(pid_t pid, pid_t tid, BacktraceMap* map) |
| 32 | : BacktracePtrace(pid, tid, map), addr_space_(nullptr), upt_info_(nullptr) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | UnwindPtrace::~UnwindPtrace() { |
| 36 | if (upt_info_) { |
| 37 | _UPT_destroy(upt_info_); |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 38 | upt_info_ = nullptr; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 39 | } |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 40 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 41 | if (addr_space_) { |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 42 | // Remove the map from the address space before destroying it. |
| 43 | // It will be freed in the UnwindMap destructor. |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 44 | unw_map_set(addr_space_, nullptr); |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 45 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 46 | unw_destroy_addr_space(addr_space_); |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 47 | addr_space_ = nullptr; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 51 | bool UnwindPtrace::Init() { |
| 52 | if (upt_info_) { |
| 53 | return true; |
Christopher Ferris | 30c942c | 2015-05-14 15:39:52 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 56 | if (addr_space_) { |
| 57 | // If somehow the addr_space_ gets initialized but upt_info_ doesn't, |
| 58 | // then that indicates there is some kind of failure. |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 59 | return false; |
| 60 | } |
| 61 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 62 | addr_space_ = unw_create_addr_space(&_UPT_accessors, 0); |
| 63 | if (!addr_space_) { |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 64 | BACK_LOGW("unw_create_addr_space failed."); |
Christopher Ferris | c463ba4 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 65 | error_ = BACKTRACE_UNWIND_ERROR_SETUP_FAILED; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 66 | return false; |
| 67 | } |
| 68 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 69 | UnwindMap* map = static_cast<UnwindMap*>(GetMap()); |
| 70 | unw_map_set(addr_space_, map->GetMapCursor()); |
| 71 | |
| 72 | upt_info_ = reinterpret_cast<struct UPT_info*>(_UPT_create(Tid())); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 73 | if (!upt_info_) { |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 74 | BACK_LOGW("Failed to create upt info."); |
Christopher Ferris | c463ba4 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 75 | error_ = BACKTRACE_UNWIND_ERROR_SETUP_FAILED; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 76 | return false; |
| 77 | } |
| 78 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 79 | return true; |
| 80 | } |
| 81 | |
| 82 | bool UnwindPtrace::Unwind(size_t num_ignore_frames, ucontext_t* ucontext) { |
| 83 | if (GetMap() == nullptr) { |
| 84 | // Without a map object, we can't do anything. |
| 85 | error_ = BACKTRACE_UNWIND_ERROR_MAP_MISSING; |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | error_ = BACKTRACE_UNWIND_NO_ERROR; |
| 90 | |
| 91 | if (ucontext) { |
| 92 | BACK_LOGW("Unwinding from a specified context not supported yet."); |
| 93 | error_ = BACKTRACE_UNWIND_ERROR_UNSUPPORTED_OPERATION; |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | if (!Init()) { |
| 98 | return false; |
| 99 | } |
| 100 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 101 | unw_cursor_t cursor; |
| 102 | int ret = unw_init_remote(&cursor, addr_space_, upt_info_); |
| 103 | if (ret < 0) { |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 104 | BACK_LOGW("unw_init_remote failed %d", ret); |
Christopher Ferris | c463ba4 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 105 | error_ = BACKTRACE_UNWIND_ERROR_SETUP_FAILED; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 106 | return false; |
| 107 | } |
| 108 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 109 | size_t num_frames = 0; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 110 | do { |
| 111 | unw_word_t pc; |
| 112 | ret = unw_get_reg(&cursor, UNW_REG_IP, &pc); |
| 113 | if (ret < 0) { |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 114 | BACK_LOGW("Failed to read IP %d", ret); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 115 | break; |
| 116 | } |
| 117 | unw_word_t sp; |
| 118 | ret = unw_get_reg(&cursor, UNW_REG_SP, &sp); |
| 119 | if (ret < 0) { |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 120 | BACK_LOGW("Failed to read SP %d", ret); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 121 | break; |
| 122 | } |
| 123 | |
| 124 | if (num_ignore_frames == 0) { |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 125 | frames_.resize(num_frames+1); |
| 126 | backtrace_frame_data_t* frame = &frames_.at(num_frames); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 127 | frame->num = num_frames; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 128 | frame->pc = static_cast<uintptr_t>(pc); |
| 129 | frame->sp = static_cast<uintptr_t>(sp); |
| 130 | frame->stack_size = 0; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 131 | |
| 132 | if (num_frames > 0) { |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 133 | backtrace_frame_data_t* prev = &frames_.at(num_frames-1); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 134 | prev->stack_size = frame->sp - prev->sp; |
| 135 | } |
| 136 | |
Christopher Ferris | 12385e3 | 2015-02-06 13:22:01 -0800 | [diff] [blame] | 137 | FillInMap(frame->pc, &frame->map); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 138 | |
Christopher Ferris | f5e568e | 2017-03-22 13:18:31 -0700 | [diff] [blame] | 139 | frame->func_name = GetFunctionName(frame->pc, &frame->func_offset, &frame->map); |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 140 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 141 | num_frames++; |
Christopher Ferris | f5e568e | 2017-03-22 13:18:31 -0700 | [diff] [blame] | 142 | // If the pc is in a device map, then don't try to step. |
| 143 | if (frame->map.flags & PROT_DEVICE_MAP) { |
| 144 | break; |
| 145 | } |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 146 | } else { |
Christopher Ferris | f5e568e | 2017-03-22 13:18:31 -0700 | [diff] [blame] | 147 | // If the pc is in a device map, then don't try to step. |
| 148 | backtrace_map_t map; |
| 149 | FillInMap(pc, &map); |
| 150 | if (map.flags & PROT_DEVICE_MAP) { |
| 151 | break; |
| 152 | } |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 153 | num_ignore_frames--; |
| 154 | } |
Christopher Ferris | f5e568e | 2017-03-22 13:18:31 -0700 | [diff] [blame] | 155 | // Verify the sp is not in a device map. |
| 156 | backtrace_map_t map; |
| 157 | FillInMap(sp, &map); |
| 158 | if (map.flags & PROT_DEVICE_MAP) { |
| 159 | break; |
| 160 | } |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 161 | ret = unw_step (&cursor); |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 162 | } while (ret > 0 && num_frames < MAX_BACKTRACE_FRAMES); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 163 | |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | std::string UnwindPtrace::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) { |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 168 | if (!Init()) { |
| 169 | return ""; |
| 170 | } |
| 171 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 172 | *offset = 0; |
| 173 | char buf[512]; |
| 174 | unw_word_t value; |
| 175 | if (unw_get_proc_name_by_ip(addr_space_, pc, buf, sizeof(buf), &value, |
| 176 | upt_info_) >= 0 && buf[0] != '\0') { |
| 177 | *offset = static_cast<uintptr_t>(value); |
| 178 | return buf; |
| 179 | } |
| 180 | return ""; |
| 181 | } |