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 | #include <errno.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <sys/ptrace.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <unistd.h> |
| 23 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 24 | #include <inttypes.h> |
| 25 | |
| 26 | #include <string> |
| 27 | |
| 28 | #include <backtrace/Backtrace.h> |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 29 | #include <backtrace/BacktraceMap.h> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 30 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 31 | #include "BacktraceImpl.h" |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 32 | #include "thread_utils.h" |
| 33 | |
| 34 | //------------------------------------------------------------------------- |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 35 | // Backtrace functions. |
| 36 | //------------------------------------------------------------------------- |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 37 | Backtrace::Backtrace(BacktraceImpl* impl, pid_t pid, BacktraceMap* map) |
| 38 | : pid_(pid), tid_(-1), map_(map), map_shared_(true), impl_(impl) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 39 | impl_->SetParent(this); |
Christopher Ferris | 9846497 | 2014-01-06 19:16:33 -0800 | [diff] [blame] | 40 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 41 | if (map_ == NULL) { |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 42 | map_ = BacktraceMap::Create(pid); |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 43 | map_shared_ = false; |
Christopher Ferris | 9846497 | 2014-01-06 19:16:33 -0800 | [diff] [blame] | 44 | } |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | Backtrace::~Backtrace() { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 48 | if (impl_) { |
| 49 | delete impl_; |
| 50 | impl_ = NULL; |
| 51 | } |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 52 | |
| 53 | if (map_ && !map_shared_) { |
| 54 | delete map_; |
| 55 | map_ = NULL; |
| 56 | } |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | bool Backtrace::Unwind(size_t num_ignore_frames) { |
| 60 | return impl_->Unwind(num_ignore_frames); |
| 61 | } |
| 62 | |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 63 | extern "C" char* __cxa_demangle(const char* mangled, char* buf, size_t* len, |
| 64 | int* status); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 65 | |
| 66 | std::string Backtrace::GetFunctionName(uintptr_t pc, uintptr_t* offset) { |
| 67 | std::string func_name = impl_->GetFunctionNameRaw(pc, offset); |
| 68 | if (!func_name.empty()) { |
| 69 | #if defined(__APPLE__) |
| 70 | // Mac OS' __cxa_demangle demangles "f" as "float"; last tested on 10.7. |
Christopher Ferris | f67c641 | 2014-01-10 00:43:54 -0800 | [diff] [blame] | 71 | if (func_name[0] != '_') { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 72 | return func_name; |
| 73 | } |
| 74 | #endif |
| 75 | char* name = __cxa_demangle(func_name.c_str(), 0, 0, 0); |
| 76 | if (name) { |
| 77 | func_name = name; |
| 78 | free(name); |
| 79 | } |
| 80 | } |
| 81 | return func_name; |
| 82 | } |
| 83 | |
| 84 | bool Backtrace::VerifyReadWordArgs(uintptr_t ptr, uint32_t* out_value) { |
| 85 | if (ptr & 3) { |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 86 | BACK_LOGW("invalid pointer %p", (void*)ptr); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 87 | *out_value = (uint32_t)-1; |
| 88 | return false; |
| 89 | } |
| 90 | return true; |
| 91 | } |
| 92 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 93 | std::string Backtrace::FormatFrameData(size_t frame_num) { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 94 | if (frame_num >= frames_.size()) { |
| 95 | return ""; |
| 96 | } |
| 97 | return FormatFrameData(&frames_[frame_num]); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | std::string Backtrace::FormatFrameData(const backtrace_frame_data_t* frame) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 101 | const char* map_name; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 102 | if (frame->map && !frame->map->name.empty()) { |
| 103 | map_name = frame->map->name.c_str(); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 104 | } else { |
| 105 | map_name = "<unknown>"; |
| 106 | } |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 107 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 108 | uintptr_t relative_pc; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 109 | if (frame->map) { |
| 110 | relative_pc = frame->pc - frame->map->start; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 111 | } else { |
| 112 | relative_pc = frame->pc; |
| 113 | } |
| 114 | |
| 115 | char buf[512]; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 116 | if (!frame->func_name.empty() && frame->func_offset) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 117 | snprintf(buf, sizeof(buf), "#%02zu pc %0*" PRIxPTR " %s (%s+%" PRIuPTR ")", |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 118 | frame->num, (int)sizeof(uintptr_t)*2, relative_pc, map_name, |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 119 | frame->func_name.c_str(), frame->func_offset); |
| 120 | } else if (!frame->func_name.empty()) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 121 | snprintf(buf, sizeof(buf), "#%02zu pc %0*" PRIxPTR " %s (%s)", frame->num, |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 122 | (int)sizeof(uintptr_t)*2, relative_pc, map_name, frame->func_name.c_str()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 123 | } else { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 124 | snprintf(buf, sizeof(buf), "#%02zu pc %0*" PRIxPTR " %s", frame->num, |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 125 | (int)sizeof(uintptr_t)*2, relative_pc, map_name); |
| 126 | } |
| 127 | |
| 128 | return buf; |
| 129 | } |
| 130 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 131 | const backtrace_map_t* Backtrace::FindMap(uintptr_t pc) { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 132 | return map_->Find(pc); |
| 133 | } |
| 134 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 135 | //------------------------------------------------------------------------- |
| 136 | // BacktraceCurrent functions. |
| 137 | //------------------------------------------------------------------------- |
Christopher Ferris | 9846497 | 2014-01-06 19:16:33 -0800 | [diff] [blame] | 138 | BacktraceCurrent::BacktraceCurrent( |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 139 | BacktraceImpl* impl, BacktraceMap* map) : Backtrace(impl, getpid(), map) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | BacktraceCurrent::~BacktraceCurrent() { |
| 143 | } |
| 144 | |
| 145 | bool BacktraceCurrent::ReadWord(uintptr_t ptr, uint32_t* out_value) { |
| 146 | if (!VerifyReadWordArgs(ptr, out_value)) { |
| 147 | return false; |
| 148 | } |
| 149 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 150 | const backtrace_map_t* map = FindMap(ptr); |
| 151 | if (map && map->flags & PROT_READ) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 152 | *out_value = *reinterpret_cast<uint32_t*>(ptr); |
| 153 | return true; |
| 154 | } else { |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 155 | BACK_LOGW("pointer %p not in a readable map", reinterpret_cast<void*>(ptr)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 156 | *out_value = static_cast<uint32_t>(-1); |
| 157 | return false; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | //------------------------------------------------------------------------- |
| 162 | // BacktracePtrace functions. |
| 163 | //------------------------------------------------------------------------- |
Christopher Ferris | 9846497 | 2014-01-06 19:16:33 -0800 | [diff] [blame] | 164 | BacktracePtrace::BacktracePtrace( |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 165 | BacktraceImpl* impl, pid_t pid, pid_t tid, BacktraceMap* map) |
| 166 | : Backtrace(impl, pid, map) { |
| 167 | tid_ = tid; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | BacktracePtrace::~BacktracePtrace() { |
| 171 | } |
| 172 | |
| 173 | bool BacktracePtrace::ReadWord(uintptr_t ptr, uint32_t* out_value) { |
| 174 | if (!VerifyReadWordArgs(ptr, out_value)) { |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | #if defined(__APPLE__) |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 179 | BACK_LOGW("MacOS does not support reading from another pid."); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 180 | return false; |
| 181 | #else |
| 182 | // ptrace() returns -1 and sets errno when the operation fails. |
| 183 | // To disambiguate -1 from a valid result, we clear errno beforehand. |
| 184 | errno = 0; |
| 185 | *out_value = ptrace(PTRACE_PEEKTEXT, Tid(), reinterpret_cast<void*>(ptr), NULL); |
| 186 | if (*out_value == static_cast<uint32_t>(-1) && errno) { |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 187 | BACK_LOGW("invalid pointer %p reading from tid %d, ptrace() strerror(errno)=%s", |
| 188 | reinterpret_cast<void*>(ptr), Tid(), strerror(errno)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 189 | return false; |
| 190 | } |
| 191 | return true; |
| 192 | #endif |
| 193 | } |
| 194 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 195 | Backtrace* Backtrace::Create(pid_t pid, pid_t tid, BacktraceMap* map) { |
Christopher Ferris | cbfc730 | 2013-11-05 11:04:12 -0800 | [diff] [blame] | 196 | if (pid == BACKTRACE_CURRENT_PROCESS || pid == getpid()) { |
Christopher Ferris | bc12d63 | 2013-11-12 10:54:16 -0800 | [diff] [blame] | 197 | if (tid == BACKTRACE_CURRENT_THREAD || tid == gettid()) { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 198 | return CreateCurrentObj(map); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 199 | } else { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 200 | return CreateThreadObj(tid, map); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 201 | } |
Christopher Ferris | bc12d63 | 2013-11-12 10:54:16 -0800 | [diff] [blame] | 202 | } else if (tid == BACKTRACE_CURRENT_THREAD) { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 203 | return CreatePtraceObj(pid, pid, map); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 204 | } else { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 205 | return CreatePtraceObj(pid, tid, map); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 206 | } |
| 207 | } |