Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -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 <inttypes.h> |
| 18 | #include <stdint.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <ucontext.h> |
| 22 | |
| 23 | #include <string> |
| 24 | |
| 25 | #include <base/stringprintf.h> |
| 26 | |
| 27 | #include <backtrace/Backtrace.h> |
| 28 | #include <backtrace/BacktraceMap.h> |
| 29 | |
Dan Albert | 23f750b | 2015-04-30 12:52:21 -0700 | [diff] [blame] | 30 | #include <cutils/threads.h> |
| 31 | |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 32 | #include "BacktraceLog.h" |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 33 | #include "BacktraceOffline.h" |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 34 | #include "thread_utils.h" |
| 35 | #include "UnwindCurrent.h" |
| 36 | #include "UnwindPtrace.h" |
| 37 | |
| 38 | using android::base::StringPrintf; |
| 39 | |
| 40 | //------------------------------------------------------------------------- |
| 41 | // Backtrace functions. |
| 42 | //------------------------------------------------------------------------- |
| 43 | Backtrace::Backtrace(pid_t pid, pid_t tid, BacktraceMap* map) |
| 44 | : pid_(pid), tid_(tid), map_(map), map_shared_(true) { |
| 45 | if (map_ == nullptr) { |
| 46 | map_ = BacktraceMap::Create(pid); |
| 47 | map_shared_ = false; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | Backtrace::~Backtrace() { |
| 52 | if (map_ && !map_shared_) { |
| 53 | delete map_; |
| 54 | map_ = nullptr; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | extern "C" char* __cxa_demangle(const char* mangled, char* buf, size_t* len, |
| 59 | int* status); |
| 60 | |
| 61 | std::string Backtrace::GetFunctionName(uintptr_t pc, uintptr_t* offset) { |
| 62 | std::string func_name = GetFunctionNameRaw(pc, offset); |
| 63 | if (!func_name.empty()) { |
| 64 | #if defined(__APPLE__) |
| 65 | // Mac OS' __cxa_demangle demangles "f" as "float"; last tested on 10.7. |
| 66 | if (func_name[0] != '_') { |
| 67 | return func_name; |
| 68 | } |
| 69 | #endif |
| 70 | char* name = __cxa_demangle(func_name.c_str(), 0, 0, 0); |
| 71 | if (name) { |
| 72 | func_name = name; |
| 73 | free(name); |
| 74 | } |
| 75 | } |
| 76 | return func_name; |
| 77 | } |
| 78 | |
| 79 | bool Backtrace::VerifyReadWordArgs(uintptr_t ptr, word_t* out_value) { |
| 80 | if (ptr & (sizeof(word_t)-1)) { |
| 81 | BACK_LOGW("invalid pointer %p", reinterpret_cast<void*>(ptr)); |
| 82 | *out_value = static_cast<word_t>(-1); |
| 83 | return false; |
| 84 | } |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | std::string Backtrace::FormatFrameData(size_t frame_num) { |
| 89 | if (frame_num >= frames_.size()) { |
| 90 | return ""; |
| 91 | } |
| 92 | return FormatFrameData(&frames_[frame_num]); |
| 93 | } |
| 94 | |
| 95 | std::string Backtrace::FormatFrameData(const backtrace_frame_data_t* frame) { |
| 96 | const char* map_name; |
| 97 | if (BacktraceMap::IsValid(frame->map) && !frame->map.name.empty()) { |
| 98 | map_name = frame->map.name.c_str(); |
| 99 | } else { |
| 100 | map_name = "<unknown>"; |
| 101 | } |
| 102 | |
Christopher Ferris | 2106f4b | 2015-05-01 15:02:03 -0700 | [diff] [blame] | 103 | uintptr_t relative_pc = BacktraceMap::GetRelativePc(frame->map, frame->pc); |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 104 | |
| 105 | std::string line(StringPrintf("#%02zu pc %" PRIPTR " %s", frame->num, relative_pc, map_name)); |
Christopher Ferris | 6000173 | 2015-08-20 11:16:54 -0700 | [diff] [blame] | 106 | // Special handling for non-zero offset maps, we need to print that |
| 107 | // information. |
| 108 | if (frame->map.offset != 0) { |
| 109 | line += " (offset " + StringPrintf("0x%" PRIxPTR, frame->map.offset) + ")"; |
| 110 | } |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 111 | if (!frame->func_name.empty()) { |
| 112 | line += " (" + frame->func_name; |
| 113 | if (frame->func_offset) { |
| 114 | line += StringPrintf("+%" PRIuPTR, frame->func_offset); |
| 115 | } |
| 116 | line += ')'; |
| 117 | } |
| 118 | |
| 119 | return line; |
| 120 | } |
| 121 | |
| 122 | void Backtrace::FillInMap(uintptr_t pc, backtrace_map_t* map) { |
Christopher Ferris | 30c942c | 2015-05-14 15:39:52 -0700 | [diff] [blame] | 123 | if (map_ != nullptr) { |
| 124 | map_->FillIn(pc, map); |
| 125 | } |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | Backtrace* Backtrace::Create(pid_t pid, pid_t tid, BacktraceMap* map) { |
| 129 | if (pid == BACKTRACE_CURRENT_PROCESS) { |
| 130 | pid = getpid(); |
| 131 | if (tid == BACKTRACE_CURRENT_THREAD) { |
| 132 | tid = gettid(); |
| 133 | } |
| 134 | } else if (tid == BACKTRACE_CURRENT_THREAD) { |
| 135 | tid = pid; |
| 136 | } |
| 137 | |
| 138 | if (pid == getpid()) { |
| 139 | return new UnwindCurrent(pid, tid, map); |
| 140 | } else { |
| 141 | return new UnwindPtrace(pid, tid, map); |
| 142 | } |
| 143 | } |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 144 | |
| 145 | Backtrace* Backtrace::CreateOffline(pid_t pid, pid_t tid, BacktraceMap* map, |
| 146 | const backtrace_stackinfo_t& stack, bool cache_file) { |
| 147 | return new BacktraceOffline(pid, tid, map, stack, cache_file); |
| 148 | } |