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