blob: dec241c3c5604d45ff5c68eca82866554c8f05d4 [file] [log] [blame]
Christopher Ferris2c43cff2015-03-26 19:18:36 -07001/*
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 Hughes4f713192015-12-04 22:00:26 -080025#include <android-base/stringprintf.h>
Christopher Ferris2c43cff2015-03-26 19:18:36 -070026
27#include <backtrace/Backtrace.h>
28#include <backtrace/BacktraceMap.h>
29
Christopher Ferris7d0aea92017-06-01 14:15:09 -070030#include <demangle.h>
31
Christopher Ferris2c43cff2015-03-26 19:18:36 -070032#include "BacktraceLog.h"
Christopher Ferris9a6b3e32017-10-18 09:08:51 -070033#include "UnwindStack.h"
34#include "thread_utils.h"
Christopher Ferris2c43cff2015-03-26 19:18:36 -070035
36using android::base::StringPrintf;
37
38//-------------------------------------------------------------------------
39// Backtrace functions.
40//-------------------------------------------------------------------------
41Backtrace::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
49Backtrace::~Backtrace() {
50 if (map_ && !map_shared_) {
51 delete map_;
52 map_ = nullptr;
53 }
54}
55
Christopher Ferris7937a362018-01-18 11:15:49 -080056std::string Backtrace::GetFunctionName(uint64_t pc, uint64_t* offset, const backtrace_map_t* map) {
Christopher Ferrisf5e568e2017-03-22 13:18:31 -070057 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 Ferris7d0aea92017-06-01 14:15:09 -070066 return demangle(GetFunctionNameRaw(pc, offset).c_str());
Christopher Ferris2c43cff2015-03-26 19:18:36 -070067}
68
Christopher Ferris7937a362018-01-18 11:15:49 -080069bool Backtrace::VerifyReadWordArgs(uint64_t ptr, word_t* out_value) {
Christopher Ferris2c43cff2015-03-26 19:18:36 -070070 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
78std::string Backtrace::FormatFrameData(size_t frame_num) {
79 if (frame_num >= frames_.size()) {
80 return "";
81 }
82 return FormatFrameData(&frames_[frame_num]);
83}
84
85std::string Backtrace::FormatFrameData(const backtrace_frame_data_t* frame) {
Christopher Ferrisda750a72015-11-30 13:36:08 -080086 std::string map_name;
87 if (BacktraceMap::IsValid(frame->map)) {
Christopher Ferrisda750a72015-11-30 13:36:08 -080088 if (!frame->map.name.empty()) {
89 map_name = frame->map.name.c_str();
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 } else {
95 map_name = StringPrintf("<anonymous:%" PRIPTR ">", frame->map.start);
96 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -070097 } else {
98 map_name = "<unknown>";
99 }
100
Christopher Ferris96722b02017-07-19 14:20:46 -0700101 std::string line(StringPrintf("#%02zu pc %" PRIPTR " ", frame->num, frame->rel_pc));
Christopher Ferrisda750a72015-11-30 13:36:08 -0800102 line += map_name;
Christopher Ferris60001732015-08-20 11:16:54 -0700103 // Special handling for non-zero offset maps, we need to print that
104 // information.
105 if (frame->map.offset != 0) {
Christopher Ferris7937a362018-01-18 11:15:49 -0800106 line += " (offset " + StringPrintf("0x%" PRIx64, frame->map.offset) + ")";
Christopher Ferris60001732015-08-20 11:16:54 -0700107 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700108 if (!frame->func_name.empty()) {
109 line += " (" + frame->func_name;
110 if (frame->func_offset) {
Christopher Ferris7937a362018-01-18 11:15:49 -0800111 line += StringPrintf("+%" PRIu64, frame->func_offset);
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700112 }
113 line += ')';
114 }
115
116 return line;
117}
118
Christopher Ferris7937a362018-01-18 11:15:49 -0800119void Backtrace::FillInMap(uint64_t pc, backtrace_map_t* map) {
Christopher Ferris30c942c2015-05-14 15:39:52 -0700120 if (map_ != nullptr) {
121 map_->FillIn(pc, map);
122 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700123}
124
125Backtrace* Backtrace::Create(pid_t pid, pid_t tid, BacktraceMap* map) {
126 if (pid == BACKTRACE_CURRENT_PROCESS) {
127 pid = getpid();
128 if (tid == BACKTRACE_CURRENT_THREAD) {
129 tid = gettid();
130 }
131 } else if (tid == BACKTRACE_CURRENT_THREAD) {
132 tid = pid;
133 }
134
135 if (pid == getpid()) {
Christopher Ferris9a6b3e32017-10-18 09:08:51 -0700136 return new UnwindStackCurrent(pid, tid, map);
137 } else {
138 return new UnwindStackPtrace(pid, tid, map);
139 }
140}
141
Christopher Ferrisc463ba42016-03-09 14:35:54 -0800142std::string Backtrace::GetErrorString(BacktraceUnwindError error) {
Yabin Cuif8808282017-12-12 18:04:10 -0800143 switch (error.error_code) {
144 case BACKTRACE_UNWIND_NO_ERROR:
145 return "No error";
146 case BACKTRACE_UNWIND_ERROR_SETUP_FAILED:
147 return "Setup failed";
148 case BACKTRACE_UNWIND_ERROR_MAP_MISSING:
149 return "No map found";
150 case BACKTRACE_UNWIND_ERROR_INTERNAL:
151 return "Internal libbacktrace error, please submit a bugreport";
152 case BACKTRACE_UNWIND_ERROR_THREAD_DOESNT_EXIST:
153 return "Thread doesn't exist";
154 case BACKTRACE_UNWIND_ERROR_THREAD_TIMEOUT:
155 return "Thread has not responded to signal in time";
156 case BACKTRACE_UNWIND_ERROR_UNSUPPORTED_OPERATION:
157 return "Attempt to use an unsupported feature";
158 case BACKTRACE_UNWIND_ERROR_NO_CONTEXT:
159 return "Attempt to do an offline unwind without a context";
160 case BACKTRACE_UNWIND_ERROR_EXCEED_MAX_FRAMES_LIMIT:
161 return "Exceed MAX_BACKTRACE_FRAMES limit";
162 case BACKTRACE_UNWIND_ERROR_ACCESS_MEM_FAILED:
163 return android::base::StringPrintf("Failed to read memory at addr 0x%" PRIx64,
164 error.error_info.addr);
165 case BACKTRACE_UNWIND_ERROR_ACCESS_REG_FAILED:
166 return android::base::StringPrintf("Failed to read register %" PRIu64, error.error_info.regno);
167 case BACKTRACE_UNWIND_ERROR_FIND_PROC_INFO_FAILED:
168 return "Failed to find a function in debug sections";
169 case BACKTRACE_UNWIND_ERROR_EXECUTE_DWARF_INSTRUCTION_FAILED:
170 return "Failed to execute dwarf instructions in debug sections";
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800171 case BACKTRACE_UNWIND_ERROR_UNWIND_INFO:
172 return "Failed to unwind due to invalid unwind information";
173 case BACKTRACE_UNWIND_ERROR_REPEATED_FRAME:
174 return "Failed to unwind due to same sp/pc repeating";
Christopher Ferrisc463ba42016-03-09 14:35:54 -0800175 }
176}