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 <android-base/stringprintf.h> 26 27 #include <backtrace/Backtrace.h> 28 #include <backtrace/BacktraceMap.h> 29 30 #include <demangle.h> 31 32 #include "BacktraceLog.h" 33 #include "UnwindStack.h" 34 #include "thread_utils.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 std::string Backtrace::GetFunctionName(uint64_t pc, uint64_t* offset, const backtrace_map_t* map) { 57 backtrace_map_t map_value; 58 if (map == nullptr) { 59 FillInMap(pc, &map_value); 60 map = &map_value; 61 } 62 // If no map is found, or this map is backed by a device, then return nothing. 63 if (map->start == 0 || (map->flags & PROT_DEVICE_MAP)) { 64 return ""; 65 } 66 return demangle(GetFunctionNameRaw(pc, offset).c_str()); 67 } 68 69 bool Backtrace::VerifyReadWordArgs(uint64_t ptr, word_t* out_value) { 70 if (ptr & (sizeof(word_t)-1)) { 71 BACK_LOGW("invalid pointer %p", reinterpret_cast<void*>(ptr)); 72 *out_value = static_cast<word_t>(-1); 73 return false; 74 } 75 return true; 76 } 77 78 std::string Backtrace::FormatFrameData(size_t frame_num) { 79 if (frame_num >= frames_.size()) { 80 return ""; 81 } 82 return FormatFrameData(&frames_[frame_num]); 83 } 84 85 std::string Backtrace::FormatFrameData(const backtrace_frame_data_t* frame) { 86 std::string map_name; 87 if (BacktraceMap::IsValid(frame->map)) { 88 map_name = frame->map.Name(); 89 if (!frame->map.name.empty()) { 90 if (map_name[0] == '[' && map_name[map_name.size() - 1] == ']') { 91 map_name.resize(map_name.size() - 1); 92 map_name += StringPrintf(":%" PRIPTR "]", frame->map.start); 93 } 94 } 95 } else { 96 map_name = "<unknown>"; 97 } 98 99 std::string line(StringPrintf("#%02zu pc %" PRIPTR " ", frame->num, frame->rel_pc)); 100 line += map_name; 101 // Special handling for non-zero offset maps, we need to print that 102 // information. 103 if (frame->map.offset != 0) { 104 line += " (offset " + StringPrintf("0x%" PRIx64, frame->map.offset) + ")"; 105 } 106 if (!frame->func_name.empty()) { 107 line += " (" + frame->func_name; 108 if (frame->func_offset) { 109 line += StringPrintf("+%" PRIu64, frame->func_offset); 110 } 111 line += ')'; 112 } 113 114 return line; 115 } 116 117 void Backtrace::FillInMap(uint64_t pc, backtrace_map_t* map) { 118 if (map_ != nullptr) { 119 map_->FillIn(pc, map); 120 } 121 } 122 123 Backtrace* Backtrace::Create(pid_t pid, pid_t tid, BacktraceMap* map) { 124 if (pid == BACKTRACE_CURRENT_PROCESS) { 125 pid = getpid(); 126 if (tid == BACKTRACE_CURRENT_THREAD) { 127 tid = gettid(); 128 } 129 } else if (tid == BACKTRACE_CURRENT_THREAD) { 130 tid = pid; 131 } 132 133 if (pid == getpid()) { 134 return new UnwindStackCurrent(pid, tid, map); 135 } else { 136 return new UnwindStackPtrace(pid, tid, map); 137 } 138 } 139 140 std::string Backtrace::GetErrorString(BacktraceUnwindError error) { 141 switch (error.error_code) { 142 case BACKTRACE_UNWIND_NO_ERROR: 143 return "No error"; 144 case BACKTRACE_UNWIND_ERROR_SETUP_FAILED: 145 return "Setup failed"; 146 case BACKTRACE_UNWIND_ERROR_MAP_MISSING: 147 return "No map found"; 148 case BACKTRACE_UNWIND_ERROR_INTERNAL: 149 return "Internal libbacktrace error, please submit a bugreport"; 150 case BACKTRACE_UNWIND_ERROR_THREAD_DOESNT_EXIST: 151 return "Thread doesn't exist"; 152 case BACKTRACE_UNWIND_ERROR_THREAD_TIMEOUT: 153 return "Thread has not responded to signal in time"; 154 case BACKTRACE_UNWIND_ERROR_UNSUPPORTED_OPERATION: 155 return "Attempt to use an unsupported feature"; 156 case BACKTRACE_UNWIND_ERROR_NO_CONTEXT: 157 return "Attempt to do an offline unwind without a context"; 158 case BACKTRACE_UNWIND_ERROR_EXCEED_MAX_FRAMES_LIMIT: 159 return "Exceed MAX_BACKTRACE_FRAMES limit"; 160 case BACKTRACE_UNWIND_ERROR_ACCESS_MEM_FAILED: 161 return android::base::StringPrintf("Failed to read memory at addr 0x%" PRIx64, 162 error.error_info.addr); 163 case BACKTRACE_UNWIND_ERROR_ACCESS_REG_FAILED: 164 return android::base::StringPrintf("Failed to read register %" PRIu64, error.error_info.regno); 165 case BACKTRACE_UNWIND_ERROR_FIND_PROC_INFO_FAILED: 166 return "Failed to find a function in debug sections"; 167 case BACKTRACE_UNWIND_ERROR_EXECUTE_DWARF_INSTRUCTION_FAILED: 168 return "Failed to execute dwarf instructions in debug sections"; 169 case BACKTRACE_UNWIND_ERROR_UNWIND_INFO: 170 return "Failed to unwind due to invalid unwind information"; 171 case BACKTRACE_UNWIND_ERROR_REPEATED_FRAME: 172 return "Failed to unwind due to same sp/pc repeating"; 173 } 174 } 175