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 | |
| 24 | #define __STDC_FORMAT_MACROS |
| 25 | #include <inttypes.h> |
| 26 | |
| 27 | #include <string> |
| 28 | |
| 29 | #include <backtrace/Backtrace.h> |
| 30 | #include <cutils/log.h> |
| 31 | |
| 32 | #include "Backtrace.h" |
| 33 | #include "thread_utils.h" |
| 34 | |
| 35 | //------------------------------------------------------------------------- |
| 36 | // BacktraceImpl functions. |
| 37 | //------------------------------------------------------------------------- |
| 38 | backtrace_t* BacktraceImpl::GetBacktraceData() { |
| 39 | return &backtrace_obj_->backtrace_; |
| 40 | } |
| 41 | |
| 42 | //------------------------------------------------------------------------- |
| 43 | // Backtrace functions. |
| 44 | //------------------------------------------------------------------------- |
| 45 | Backtrace::Backtrace(BacktraceImpl* impl) : impl_(impl), map_info_(NULL) { |
| 46 | impl_->SetParent(this); |
| 47 | backtrace_.num_frames = 0; |
| 48 | backtrace_.pid = -1; |
| 49 | backtrace_.tid = -1; |
| 50 | } |
| 51 | |
| 52 | Backtrace::~Backtrace() { |
| 53 | for (size_t i = 0; i < NumFrames(); i++) { |
| 54 | if (backtrace_.frames[i].func_name) { |
| 55 | free(backtrace_.frames[i].func_name); |
| 56 | backtrace_.frames[i].func_name = NULL; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | if (map_info_) { |
| 61 | backtrace_destroy_map_info_list(map_info_); |
| 62 | map_info_ = NULL; |
| 63 | } |
| 64 | |
| 65 | if (impl_) { |
| 66 | delete impl_; |
| 67 | impl_ = NULL; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | bool Backtrace::Unwind(size_t num_ignore_frames) { |
| 72 | return impl_->Unwind(num_ignore_frames); |
| 73 | } |
| 74 | |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 75 | extern "C" char* __cxa_demangle(const char* mangled, char* buf, size_t* len, |
| 76 | int* status); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 77 | |
| 78 | std::string Backtrace::GetFunctionName(uintptr_t pc, uintptr_t* offset) { |
| 79 | std::string func_name = impl_->GetFunctionNameRaw(pc, offset); |
| 80 | if (!func_name.empty()) { |
| 81 | #if defined(__APPLE__) |
| 82 | // Mac OS' __cxa_demangle demangles "f" as "float"; last tested on 10.7. |
| 83 | if (symbol_name[0] != '_') { |
| 84 | return func_name; |
| 85 | } |
| 86 | #endif |
| 87 | char* name = __cxa_demangle(func_name.c_str(), 0, 0, 0); |
| 88 | if (name) { |
| 89 | func_name = name; |
| 90 | free(name); |
| 91 | } |
| 92 | } |
| 93 | return func_name; |
| 94 | } |
| 95 | |
| 96 | bool Backtrace::VerifyReadWordArgs(uintptr_t ptr, uint32_t* out_value) { |
| 97 | if (ptr & 3) { |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 98 | BACK_LOGW("invalid pointer %p", (void*)ptr); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 99 | *out_value = (uint32_t)-1; |
| 100 | return false; |
| 101 | } |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | const char* Backtrace::GetMapName(uintptr_t pc, uintptr_t* map_start) { |
| 106 | const backtrace_map_info_t* map_info = FindMapInfo(pc); |
| 107 | if (map_info) { |
| 108 | if (map_start) { |
| 109 | *map_start = map_info->start; |
| 110 | } |
| 111 | return map_info->name; |
| 112 | } |
| 113 | return NULL; |
| 114 | } |
| 115 | |
| 116 | const backtrace_map_info_t* Backtrace::FindMapInfo(uintptr_t ptr) { |
| 117 | return backtrace_find_map_info(map_info_, ptr); |
| 118 | } |
| 119 | |
| 120 | std::string Backtrace::FormatFrameData(size_t frame_num) { |
| 121 | backtrace_frame_data_t* frame = &backtrace_.frames[frame_num]; |
| 122 | const char* map_name; |
| 123 | if (frame->map_name) { |
| 124 | map_name = frame->map_name; |
| 125 | } else { |
| 126 | map_name = "<unknown>"; |
| 127 | } |
| 128 | uintptr_t relative_pc; |
| 129 | if (frame->map_offset) { |
| 130 | relative_pc = frame->map_offset; |
| 131 | } else { |
| 132 | relative_pc = frame->pc; |
| 133 | } |
| 134 | |
| 135 | char buf[512]; |
| 136 | if (frame->func_name && frame->func_offset) { |
| 137 | snprintf(buf, sizeof(buf), "#%02zu pc %0*" PRIxPTR " %s (%s+%" PRIuPTR ")", |
| 138 | frame_num, (int)sizeof(uintptr_t)*2, relative_pc, map_name, |
| 139 | frame->func_name, frame->func_offset); |
| 140 | } else if (frame->func_name) { |
| 141 | snprintf(buf, sizeof(buf), "#%02zu pc %0*" PRIxPTR " %s (%s)", frame_num, |
| 142 | (int)sizeof(uintptr_t)*2, relative_pc, map_name, frame->func_name); |
| 143 | } else { |
| 144 | snprintf(buf, sizeof(buf), "#%02zu pc %0*" PRIxPTR " %s", frame_num, |
| 145 | (int)sizeof(uintptr_t)*2, relative_pc, map_name); |
| 146 | } |
| 147 | |
| 148 | return buf; |
| 149 | } |
| 150 | |
| 151 | //------------------------------------------------------------------------- |
| 152 | // BacktraceCurrent functions. |
| 153 | //------------------------------------------------------------------------- |
| 154 | BacktraceCurrent::BacktraceCurrent(BacktraceImpl* impl) : Backtrace(impl) { |
| 155 | map_info_ = backtrace_create_map_info_list(-1); |
| 156 | |
| 157 | backtrace_.pid = getpid(); |
| 158 | } |
| 159 | |
| 160 | BacktraceCurrent::~BacktraceCurrent() { |
| 161 | } |
| 162 | |
| 163 | bool BacktraceCurrent::ReadWord(uintptr_t ptr, uint32_t* out_value) { |
| 164 | if (!VerifyReadWordArgs(ptr, out_value)) { |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | const backtrace_map_info_t* map_info = FindMapInfo(ptr); |
| 169 | if (map_info && map_info->is_readable) { |
| 170 | *out_value = *reinterpret_cast<uint32_t*>(ptr); |
| 171 | return true; |
| 172 | } else { |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 173 | 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] | 174 | *out_value = static_cast<uint32_t>(-1); |
| 175 | return false; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | //------------------------------------------------------------------------- |
| 180 | // BacktracePtrace functions. |
| 181 | //------------------------------------------------------------------------- |
| 182 | BacktracePtrace::BacktracePtrace(BacktraceImpl* impl, pid_t pid, pid_t tid) |
| 183 | : Backtrace(impl) { |
| 184 | map_info_ = backtrace_create_map_info_list(tid); |
| 185 | |
| 186 | backtrace_.pid = pid; |
| 187 | backtrace_.tid = tid; |
| 188 | } |
| 189 | |
| 190 | BacktracePtrace::~BacktracePtrace() { |
| 191 | } |
| 192 | |
| 193 | bool BacktracePtrace::ReadWord(uintptr_t ptr, uint32_t* out_value) { |
| 194 | if (!VerifyReadWordArgs(ptr, out_value)) { |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | #if defined(__APPLE__) |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 199 | BACK_LOGW("MacOS does not support reading from another pid."); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 200 | return false; |
| 201 | #else |
| 202 | // ptrace() returns -1 and sets errno when the operation fails. |
| 203 | // To disambiguate -1 from a valid result, we clear errno beforehand. |
| 204 | errno = 0; |
| 205 | *out_value = ptrace(PTRACE_PEEKTEXT, Tid(), reinterpret_cast<void*>(ptr), NULL); |
| 206 | if (*out_value == static_cast<uint32_t>(-1) && errno) { |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 207 | BACK_LOGW("invalid pointer %p reading from tid %d, ptrace() strerror(errno)=%s", |
| 208 | reinterpret_cast<void*>(ptr), Tid(), strerror(errno)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 209 | return false; |
| 210 | } |
| 211 | return true; |
| 212 | #endif |
| 213 | } |
| 214 | |
| 215 | Backtrace* Backtrace::Create(pid_t pid, pid_t tid) { |
| 216 | if (pid < 0 || pid == getpid()) { |
| 217 | if (tid < 0 || tid == gettid()) { |
| 218 | return CreateCurrentObj(); |
| 219 | } else { |
| 220 | return CreateThreadObj(tid); |
| 221 | } |
| 222 | } else if (tid < 0) { |
| 223 | return CreatePtraceObj(pid, pid); |
| 224 | } else { |
| 225 | return CreatePtraceObj(pid, tid); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | //------------------------------------------------------------------------- |
| 230 | // Common interface functions. |
| 231 | //------------------------------------------------------------------------- |
| 232 | bool backtrace_create_context( |
| 233 | backtrace_context_t* context, pid_t pid, pid_t tid, size_t num_ignore_frames) { |
| 234 | Backtrace* backtrace = Backtrace::Create(pid, tid); |
| 235 | if (!backtrace) { |
| 236 | return false; |
| 237 | } |
| 238 | if (!backtrace->Unwind(num_ignore_frames)) { |
| 239 | delete backtrace; |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | context->data = backtrace; |
| 244 | context->backtrace = backtrace->GetBacktrace(); |
| 245 | return true; |
| 246 | } |
| 247 | |
| 248 | void backtrace_destroy_context(backtrace_context_t* context) { |
| 249 | if (context->data) { |
| 250 | Backtrace* backtrace = reinterpret_cast<Backtrace*>(context->data); |
| 251 | delete backtrace; |
| 252 | context->data = NULL; |
| 253 | } |
| 254 | context->backtrace = NULL; |
| 255 | } |
| 256 | |
| 257 | const backtrace_t* backtrace_get_data(backtrace_context_t* context) { |
| 258 | if (context && context->data) { |
| 259 | Backtrace* backtrace = reinterpret_cast<Backtrace*>(context->data); |
| 260 | return backtrace->GetBacktrace(); |
| 261 | } |
| 262 | return NULL; |
| 263 | } |
| 264 | |
| 265 | bool backtrace_read_word(const backtrace_context_t* context, uintptr_t ptr, uint32_t* value) { |
| 266 | if (context->data) { |
| 267 | Backtrace* backtrace = reinterpret_cast<Backtrace*>(context->data); |
| 268 | return backtrace->ReadWord(ptr, value); |
| 269 | } |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | const char* backtrace_get_map_name(const backtrace_context_t* context, uintptr_t pc, uintptr_t* map_start) { |
| 274 | if (context->data) { |
| 275 | Backtrace* backtrace = reinterpret_cast<Backtrace*>(context->data); |
| 276 | return backtrace->GetMapName(pc, map_start); |
| 277 | } |
| 278 | return NULL; |
| 279 | } |
| 280 | |
| 281 | char* backtrace_get_func_name(const backtrace_context_t* context, uintptr_t pc, uintptr_t* func_offset) { |
| 282 | if (context->data) { |
| 283 | Backtrace* backtrace = reinterpret_cast<Backtrace*>(context->data); |
| 284 | std::string func_name = backtrace->GetFunctionName(pc, func_offset); |
| 285 | if (!func_name.empty()) { |
| 286 | return strdup(func_name.c_str()); |
| 287 | } |
| 288 | } |
| 289 | return NULL; |
| 290 | } |
| 291 | |
| 292 | void backtrace_format_frame_data( |
| 293 | const backtrace_context_t* context, size_t frame_num, char* buf, |
| 294 | size_t buf_size) { |
| 295 | if (buf_size == 0 || buf == NULL) { |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 296 | BACK_LOGW("bad call buf %p buf_size %zu", buf, buf_size); |
| 297 | } else if (context->data) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 298 | Backtrace* backtrace = reinterpret_cast<Backtrace*>(context->data); |
| 299 | std::string line = backtrace->FormatFrameData(frame_num); |
| 300 | if (line.size() > buf_size) { |
| 301 | memcpy(buf, line.c_str(), buf_size-1); |
| 302 | buf[buf_size] = '\0'; |
| 303 | } else { |
| 304 | memcpy(buf, line.c_str(), line.size()+1); |
| 305 | } |
| 306 | } |
| 307 | } |