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