| 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> | 
| Elliott Hughes | 3848890 | 2018-07-11 11:13:16 -0700 | [diff] [blame] | 26 | #include <android-base/threads.h> | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 27 |  | 
|  | 28 | #include <backtrace/Backtrace.h> | 
|  | 29 | #include <backtrace/BacktraceMap.h> | 
|  | 30 |  | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 31 | #include "BacktraceLog.h" | 
| Christopher Ferris | 9a6b3e3 | 2017-10-18 09:08:51 -0700 | [diff] [blame] | 32 | #include "UnwindStack.h" | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 33 |  | 
|  | 34 | using android::base::StringPrintf; | 
|  | 35 |  | 
| Christopher Ferris | 4ec93a7 | 2019-07-18 14:11:07 -0700 | [diff] [blame] | 36 | extern "C" char* __cxa_demangle(const char*, char*, size_t*, int*); | 
|  | 37 |  | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 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 |  | 
| Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 56 | std::string Backtrace::GetFunctionName(uint64_t pc, uint64_t* offset, const backtrace_map_t* map) { | 
| Christopher Ferris | f5e568e | 2017-03-22 13:18:31 -0700 | [diff] [blame] | 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 | } | 
| Christopher Ferris | 4ec93a7 | 2019-07-18 14:11:07 -0700 | [diff] [blame] | 66 | std::string name(GetFunctionNameRaw(pc, offset)); | 
|  | 67 | char* demangled_name = __cxa_demangle(name.c_str(), nullptr, nullptr, nullptr); | 
|  | 68 | if (demangled_name != nullptr) { | 
|  | 69 | name = demangled_name; | 
|  | 70 | free(demangled_name); | 
|  | 71 | return name; | 
|  | 72 | } | 
|  | 73 | return name; | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 74 | } | 
|  | 75 |  | 
| Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 76 | bool Backtrace::VerifyReadWordArgs(uint64_t ptr, word_t* out_value) { | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 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 | std::string map_name; | 
|  | 94 | if (BacktraceMap::IsValid(frame->map)) { | 
| Elliott Hughes | e1415a5 | 2018-02-15 09:18:21 -0800 | [diff] [blame] | 95 | map_name = frame->map.Name(); | 
| Christopher Ferris | da750a7 | 2015-11-30 13:36:08 -0800 | [diff] [blame] | 96 | if (!frame->map.name.empty()) { | 
| Christopher Ferris | da750a7 | 2015-11-30 13:36:08 -0800 | [diff] [blame] | 97 | if (map_name[0] == '[' && map_name[map_name.size() - 1] == ']') { | 
|  | 98 | map_name.resize(map_name.size() - 1); | 
|  | 99 | map_name += StringPrintf(":%" PRIPTR "]", frame->map.start); | 
|  | 100 | } | 
| Christopher Ferris | da750a7 | 2015-11-30 13:36:08 -0800 | [diff] [blame] | 101 | } | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 102 | } else { | 
|  | 103 | map_name = "<unknown>"; | 
|  | 104 | } | 
|  | 105 |  | 
| Christopher Ferris | 96722b0 | 2017-07-19 14:20:46 -0700 | [diff] [blame] | 106 | std::string line(StringPrintf("#%02zu pc %" PRIPTR "  ", frame->num, frame->rel_pc)); | 
| Christopher Ferris | da750a7 | 2015-11-30 13:36:08 -0800 | [diff] [blame] | 107 | line += map_name; | 
| Christopher Ferris | 6000173 | 2015-08-20 11:16:54 -0700 | [diff] [blame] | 108 | // Special handling for non-zero offset maps, we need to print that | 
|  | 109 | // information. | 
|  | 110 | if (frame->map.offset != 0) { | 
| Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 111 | line += " (offset " + StringPrintf("0x%" PRIx64, frame->map.offset) + ")"; | 
| Christopher Ferris | 6000173 | 2015-08-20 11:16:54 -0700 | [diff] [blame] | 112 | } | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 113 | if (!frame->func_name.empty()) { | 
|  | 114 | line += " (" + frame->func_name; | 
|  | 115 | if (frame->func_offset) { | 
| Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 116 | line += StringPrintf("+%" PRIu64, frame->func_offset); | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 117 | } | 
|  | 118 | line += ')'; | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | return line; | 
|  | 122 | } | 
|  | 123 |  | 
| Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 124 | void Backtrace::FillInMap(uint64_t pc, backtrace_map_t* map) { | 
| Christopher Ferris | 30c942c | 2015-05-14 15:39:52 -0700 | [diff] [blame] | 125 | if (map_ != nullptr) { | 
|  | 126 | map_->FillIn(pc, map); | 
|  | 127 | } | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 128 | } | 
|  | 129 |  | 
|  | 130 | Backtrace* Backtrace::Create(pid_t pid, pid_t tid, BacktraceMap* map) { | 
|  | 131 | if (pid == BACKTRACE_CURRENT_PROCESS) { | 
|  | 132 | pid = getpid(); | 
|  | 133 | if (tid == BACKTRACE_CURRENT_THREAD) { | 
| Elliott Hughes | 3848890 | 2018-07-11 11:13:16 -0700 | [diff] [blame] | 134 | tid = android::base::GetThreadId(); | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 135 | } | 
|  | 136 | } else if (tid == BACKTRACE_CURRENT_THREAD) { | 
|  | 137 | tid = pid; | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | if (pid == getpid()) { | 
| Christopher Ferris | 9a6b3e3 | 2017-10-18 09:08:51 -0700 | [diff] [blame] | 141 | return new UnwindStackCurrent(pid, tid, map); | 
|  | 142 | } else { | 
|  | 143 | return new UnwindStackPtrace(pid, tid, map); | 
|  | 144 | } | 
|  | 145 | } | 
|  | 146 |  | 
| Christopher Ferris | c463ba4 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 147 | std::string Backtrace::GetErrorString(BacktraceUnwindError error) { | 
| Yabin Cui | f880828 | 2017-12-12 18:04:10 -0800 | [diff] [blame] | 148 | switch (error.error_code) { | 
|  | 149 | case BACKTRACE_UNWIND_NO_ERROR: | 
|  | 150 | return "No error"; | 
|  | 151 | case BACKTRACE_UNWIND_ERROR_SETUP_FAILED: | 
|  | 152 | return "Setup failed"; | 
|  | 153 | case BACKTRACE_UNWIND_ERROR_MAP_MISSING: | 
|  | 154 | return "No map found"; | 
|  | 155 | case BACKTRACE_UNWIND_ERROR_INTERNAL: | 
|  | 156 | return "Internal libbacktrace error, please submit a bugreport"; | 
|  | 157 | case BACKTRACE_UNWIND_ERROR_THREAD_DOESNT_EXIST: | 
|  | 158 | return "Thread doesn't exist"; | 
|  | 159 | case BACKTRACE_UNWIND_ERROR_THREAD_TIMEOUT: | 
|  | 160 | return "Thread has not responded to signal in time"; | 
|  | 161 | case BACKTRACE_UNWIND_ERROR_UNSUPPORTED_OPERATION: | 
|  | 162 | return "Attempt to use an unsupported feature"; | 
|  | 163 | case BACKTRACE_UNWIND_ERROR_NO_CONTEXT: | 
|  | 164 | return "Attempt to do an offline unwind without a context"; | 
|  | 165 | case BACKTRACE_UNWIND_ERROR_EXCEED_MAX_FRAMES_LIMIT: | 
|  | 166 | return "Exceed MAX_BACKTRACE_FRAMES limit"; | 
|  | 167 | case BACKTRACE_UNWIND_ERROR_ACCESS_MEM_FAILED: | 
|  | 168 | return android::base::StringPrintf("Failed to read memory at addr 0x%" PRIx64, | 
|  | 169 | error.error_info.addr); | 
|  | 170 | case BACKTRACE_UNWIND_ERROR_ACCESS_REG_FAILED: | 
|  | 171 | return android::base::StringPrintf("Failed to read register %" PRIu64, error.error_info.regno); | 
|  | 172 | case BACKTRACE_UNWIND_ERROR_FIND_PROC_INFO_FAILED: | 
|  | 173 | return "Failed to find a function in debug sections"; | 
|  | 174 | case BACKTRACE_UNWIND_ERROR_EXECUTE_DWARF_INSTRUCTION_FAILED: | 
|  | 175 | return "Failed to execute dwarf instructions in debug sections"; | 
| Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 176 | case BACKTRACE_UNWIND_ERROR_UNWIND_INFO: | 
|  | 177 | return "Failed to unwind due to invalid unwind information"; | 
|  | 178 | case BACKTRACE_UNWIND_ERROR_REPEATED_FRAME: | 
|  | 179 | return "Failed to unwind due to same sp/pc repeating"; | 
| Christopher Ferris | d11ed86 | 2019-04-11 19:45:35 -0700 | [diff] [blame] | 180 | case BACKTRACE_UNWIND_ERROR_INVALID_ELF: | 
|  | 181 | return "Failed to unwind due to invalid elf"; | 
| Christopher Ferris | c463ba4 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 182 | } | 
|  | 183 | } |