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