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 | |
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) { |
Christopher Ferris | da750a7 | 2015-11-30 13:36:08 -0800 | [diff] [blame] | 96 | uintptr_t relative_pc; |
| 97 | std::string map_name; |
| 98 | if (BacktraceMap::IsValid(frame->map)) { |
| 99 | relative_pc = BacktraceMap::GetRelativePc(frame->map, frame->pc); |
| 100 | if (!frame->map.name.empty()) { |
| 101 | map_name = frame->map.name.c_str(); |
| 102 | if (map_name[0] == '[' && map_name[map_name.size() - 1] == ']') { |
| 103 | map_name.resize(map_name.size() - 1); |
| 104 | map_name += StringPrintf(":%" PRIPTR "]", frame->map.start); |
| 105 | } |
| 106 | } else { |
| 107 | map_name = StringPrintf("<anonymous:%" PRIPTR ">", frame->map.start); |
| 108 | } |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 109 | } else { |
| 110 | map_name = "<unknown>"; |
Christopher Ferris | da750a7 | 2015-11-30 13:36:08 -0800 | [diff] [blame] | 111 | relative_pc = frame->pc; |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Christopher Ferris | da750a7 | 2015-11-30 13:36:08 -0800 | [diff] [blame] | 114 | std::string line(StringPrintf("#%02zu pc %" PRIPTR " ", frame->num, relative_pc)); |
| 115 | line += map_name; |
Christopher Ferris | 6000173 | 2015-08-20 11:16:54 -0700 | [diff] [blame] | 116 | // Special handling for non-zero offset maps, we need to print that |
| 117 | // information. |
| 118 | if (frame->map.offset != 0) { |
| 119 | line += " (offset " + StringPrintf("0x%" PRIxPTR, frame->map.offset) + ")"; |
| 120 | } |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 121 | if (!frame->func_name.empty()) { |
| 122 | line += " (" + frame->func_name; |
| 123 | if (frame->func_offset) { |
| 124 | line += StringPrintf("+%" PRIuPTR, frame->func_offset); |
| 125 | } |
| 126 | line += ')'; |
| 127 | } |
| 128 | |
| 129 | return line; |
| 130 | } |
| 131 | |
| 132 | void Backtrace::FillInMap(uintptr_t pc, backtrace_map_t* map) { |
Christopher Ferris | 30c942c | 2015-05-14 15:39:52 -0700 | [diff] [blame] | 133 | if (map_ != nullptr) { |
| 134 | map_->FillIn(pc, map); |
| 135 | } |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | Backtrace* Backtrace::Create(pid_t pid, pid_t tid, BacktraceMap* map) { |
| 139 | if (pid == BACKTRACE_CURRENT_PROCESS) { |
| 140 | pid = getpid(); |
| 141 | if (tid == BACKTRACE_CURRENT_THREAD) { |
| 142 | tid = gettid(); |
| 143 | } |
| 144 | } else if (tid == BACKTRACE_CURRENT_THREAD) { |
| 145 | tid = pid; |
| 146 | } |
| 147 | |
| 148 | if (pid == getpid()) { |
| 149 | return new UnwindCurrent(pid, tid, map); |
| 150 | } else { |
| 151 | return new UnwindPtrace(pid, tid, map); |
| 152 | } |
| 153 | } |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 154 | |
| 155 | Backtrace* Backtrace::CreateOffline(pid_t pid, pid_t tid, BacktraceMap* map, |
| 156 | const backtrace_stackinfo_t& stack, bool cache_file) { |
| 157 | return new BacktraceOffline(pid, tid, map, stack, cache_file); |
| 158 | } |