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 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 25 | #include <android-base/stringprintf.h> |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 26 | |
| 27 | #include <backtrace/Backtrace.h> |
| 28 | #include <backtrace/BacktraceMap.h> |
| 29 | |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 30 | #include "BacktraceLog.h" |
| 31 | #include "thread_utils.h" |
| 32 | #include "UnwindCurrent.h" |
| 33 | #include "UnwindPtrace.h" |
| 34 | |
| 35 | using android::base::StringPrintf; |
| 36 | |
| 37 | //------------------------------------------------------------------------- |
| 38 | // Backtrace functions. |
| 39 | //------------------------------------------------------------------------- |
| 40 | Backtrace::Backtrace(pid_t pid, pid_t tid, BacktraceMap* map) |
| 41 | : pid_(pid), tid_(tid), map_(map), map_shared_(true) { |
| 42 | if (map_ == nullptr) { |
| 43 | map_ = BacktraceMap::Create(pid); |
| 44 | map_shared_ = false; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | Backtrace::~Backtrace() { |
| 49 | if (map_ && !map_shared_) { |
| 50 | delete map_; |
| 51 | map_ = nullptr; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | extern "C" char* __cxa_demangle(const char* mangled, char* buf, size_t* len, |
| 56 | int* status); |
| 57 | |
| 58 | std::string Backtrace::GetFunctionName(uintptr_t pc, uintptr_t* offset) { |
| 59 | std::string func_name = GetFunctionNameRaw(pc, offset); |
| 60 | if (!func_name.empty()) { |
| 61 | #if defined(__APPLE__) |
| 62 | // Mac OS' __cxa_demangle demangles "f" as "float"; last tested on 10.7. |
| 63 | if (func_name[0] != '_') { |
| 64 | return func_name; |
| 65 | } |
| 66 | #endif |
| 67 | char* name = __cxa_demangle(func_name.c_str(), 0, 0, 0); |
| 68 | if (name) { |
| 69 | func_name = name; |
| 70 | free(name); |
| 71 | } |
| 72 | } |
| 73 | return func_name; |
| 74 | } |
| 75 | |
| 76 | bool Backtrace::VerifyReadWordArgs(uintptr_t ptr, word_t* out_value) { |
| 77 | if (ptr & (sizeof(word_t)-1)) { |
| 78 | BACK_LOGW("invalid pointer %p", reinterpret_cast<void*>(ptr)); |
| 79 | *out_value = static_cast<word_t>(-1); |
| 80 | return false; |
| 81 | } |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | std::string Backtrace::FormatFrameData(size_t frame_num) { |
| 86 | if (frame_num >= frames_.size()) { |
| 87 | return ""; |
| 88 | } |
| 89 | return FormatFrameData(&frames_[frame_num]); |
| 90 | } |
| 91 | |
| 92 | std::string Backtrace::FormatFrameData(const backtrace_frame_data_t* frame) { |
Christopher Ferris | da750a7 | 2015-11-30 13:36:08 -0800 | [diff] [blame] | 93 | uintptr_t relative_pc; |
| 94 | std::string map_name; |
| 95 | if (BacktraceMap::IsValid(frame->map)) { |
| 96 | relative_pc = BacktraceMap::GetRelativePc(frame->map, frame->pc); |
| 97 | if (!frame->map.name.empty()) { |
| 98 | map_name = frame->map.name.c_str(); |
| 99 | if (map_name[0] == '[' && map_name[map_name.size() - 1] == ']') { |
| 100 | map_name.resize(map_name.size() - 1); |
| 101 | map_name += StringPrintf(":%" PRIPTR "]", frame->map.start); |
| 102 | } |
| 103 | } else { |
| 104 | map_name = StringPrintf("<anonymous:%" PRIPTR ">", frame->map.start); |
| 105 | } |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 106 | } else { |
| 107 | map_name = "<unknown>"; |
Christopher Ferris | da750a7 | 2015-11-30 13:36:08 -0800 | [diff] [blame] | 108 | relative_pc = frame->pc; |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Christopher Ferris | da750a7 | 2015-11-30 13:36:08 -0800 | [diff] [blame] | 111 | std::string line(StringPrintf("#%02zu pc %" PRIPTR " ", frame->num, relative_pc)); |
| 112 | line += map_name; |
Christopher Ferris | 6000173 | 2015-08-20 11:16:54 -0700 | [diff] [blame] | 113 | // Special handling for non-zero offset maps, we need to print that |
| 114 | // information. |
| 115 | if (frame->map.offset != 0) { |
| 116 | line += " (offset " + StringPrintf("0x%" PRIxPTR, frame->map.offset) + ")"; |
| 117 | } |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 118 | if (!frame->func_name.empty()) { |
| 119 | line += " (" + frame->func_name; |
| 120 | if (frame->func_offset) { |
| 121 | line += StringPrintf("+%" PRIuPTR, frame->func_offset); |
| 122 | } |
| 123 | line += ')'; |
| 124 | } |
| 125 | |
| 126 | return line; |
| 127 | } |
| 128 | |
| 129 | void Backtrace::FillInMap(uintptr_t pc, backtrace_map_t* map) { |
Christopher Ferris | 30c942c | 2015-05-14 15:39:52 -0700 | [diff] [blame] | 130 | if (map_ != nullptr) { |
| 131 | map_->FillIn(pc, map); |
| 132 | } |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | Backtrace* Backtrace::Create(pid_t pid, pid_t tid, BacktraceMap* map) { |
| 136 | if (pid == BACKTRACE_CURRENT_PROCESS) { |
| 137 | pid = getpid(); |
| 138 | if (tid == BACKTRACE_CURRENT_THREAD) { |
| 139 | tid = gettid(); |
| 140 | } |
| 141 | } else if (tid == BACKTRACE_CURRENT_THREAD) { |
| 142 | tid = pid; |
| 143 | } |
| 144 | |
| 145 | if (pid == getpid()) { |
| 146 | return new UnwindCurrent(pid, tid, map); |
| 147 | } else { |
| 148 | return new UnwindPtrace(pid, tid, map); |
| 149 | } |
| 150 | } |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 151 | |
| 152 | std::string Backtrace::GetErrorString(BacktraceUnwindError error) { |
| 153 | switch (error) { |
| 154 | case BACKTRACE_UNWIND_NO_ERROR: |
| 155 | return "No error"; |
| 156 | case BACKTRACE_UNWIND_ERROR_SETUP_FAILED: |
| 157 | return "Setup failed"; |
| 158 | case BACKTRACE_UNWIND_ERROR_MAP_MISSING: |
| 159 | return "No map found"; |
| 160 | case BACKTRACE_UNWIND_ERROR_INTERNAL: |
| 161 | return "Internal libbacktrace error, please submit a bugreport"; |
| 162 | case BACKTRACE_UNWIND_ERROR_THREAD_DOESNT_EXIST: |
| 163 | return "Thread doesn't exist"; |
| 164 | case BACKTRACE_UNWIND_ERROR_THREAD_TIMEOUT: |
| 165 | return "Thread has not repsonded to signal in time"; |
| 166 | case BACKTRACE_UNWIND_ERROR_UNSUPPORTED_OPERATION: |
| 167 | return "Attempt to use an unsupported feature"; |
| 168 | case BACKTRACE_UNWIND_ERROR_NO_CONTEXT: |
| 169 | return "Attempt to do an offline unwind without a context"; |
| 170 | } |
| 171 | } |