blob: afe518c5b1b00e9c43486888353a1980a9912a32 [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
Christopher Ferris9a6b3e32017-10-18 09:08:51 -070017#include <assert.h>
Christopher Ferris2c43cff2015-03-26 19:18:36 -070018#include <inttypes.h>
19#include <stdint.h>
20#include <stdlib.h>
21#include <sys/types.h>
22#include <ucontext.h>
23
24#include <string>
25
Elliott Hughes4f713192015-12-04 22:00:26 -080026#include <android-base/stringprintf.h>
Christopher Ferris2c43cff2015-03-26 19:18:36 -070027
28#include <backtrace/Backtrace.h>
29#include <backtrace/BacktraceMap.h>
30
Christopher Ferris7d0aea92017-06-01 14:15:09 -070031#include <demangle.h>
32
Christopher Ferris2c43cff2015-03-26 19:18:36 -070033#include "BacktraceLog.h"
Christopher Ferris2c43cff2015-03-26 19:18:36 -070034#include "UnwindCurrent.h"
35#include "UnwindPtrace.h"
Christopher Ferris9a6b3e32017-10-18 09:08:51 -070036#include "UnwindStack.h"
37#include "thread_utils.h"
Christopher Ferris2c43cff2015-03-26 19:18:36 -070038
39using android::base::StringPrintf;
40
41//-------------------------------------------------------------------------
42// Backtrace functions.
43//-------------------------------------------------------------------------
44Backtrace::Backtrace(pid_t pid, pid_t tid, BacktraceMap* map)
45 : pid_(pid), tid_(tid), map_(map), map_shared_(true) {
46 if (map_ == nullptr) {
47 map_ = BacktraceMap::Create(pid);
48 map_shared_ = false;
49 }
50}
51
52Backtrace::~Backtrace() {
53 if (map_ && !map_shared_) {
54 delete map_;
55 map_ = nullptr;
56 }
57}
58
Christopher Ferrisf5e568e2017-03-22 13:18:31 -070059std::string Backtrace::GetFunctionName(uintptr_t pc, uintptr_t* offset, const backtrace_map_t* map) {
60 backtrace_map_t map_value;
61 if (map == nullptr) {
62 FillInMap(pc, &map_value);
63 map = &map_value;
64 }
65 // If no map is found, or this map is backed by a device, then return nothing.
66 if (map->start == 0 || (map->flags & PROT_DEVICE_MAP)) {
67 return "";
68 }
Christopher Ferris7d0aea92017-06-01 14:15:09 -070069 return demangle(GetFunctionNameRaw(pc, offset).c_str());
Christopher Ferris2c43cff2015-03-26 19:18:36 -070070}
71
72bool Backtrace::VerifyReadWordArgs(uintptr_t ptr, word_t* out_value) {
73 if (ptr & (sizeof(word_t)-1)) {
74 BACK_LOGW("invalid pointer %p", reinterpret_cast<void*>(ptr));
75 *out_value = static_cast<word_t>(-1);
76 return false;
77 }
78 return true;
79}
80
81std::string Backtrace::FormatFrameData(size_t frame_num) {
82 if (frame_num >= frames_.size()) {
83 return "";
84 }
85 return FormatFrameData(&frames_[frame_num]);
86}
87
88std::string Backtrace::FormatFrameData(const backtrace_frame_data_t* frame) {
Christopher Ferrisda750a72015-11-30 13:36:08 -080089 std::string map_name;
90 if (BacktraceMap::IsValid(frame->map)) {
Christopher Ferrisda750a72015-11-30 13:36:08 -080091 if (!frame->map.name.empty()) {
92 map_name = frame->map.name.c_str();
93 if (map_name[0] == '[' && map_name[map_name.size() - 1] == ']') {
94 map_name.resize(map_name.size() - 1);
95 map_name += StringPrintf(":%" PRIPTR "]", frame->map.start);
96 }
97 } else {
98 map_name = StringPrintf("<anonymous:%" PRIPTR ">", frame->map.start);
99 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700100 } else {
101 map_name = "<unknown>";
102 }
103
Christopher Ferris96722b02017-07-19 14:20:46 -0700104 std::string line(StringPrintf("#%02zu pc %" PRIPTR " ", frame->num, frame->rel_pc));
Christopher Ferrisda750a72015-11-30 13:36:08 -0800105 line += map_name;
Christopher Ferris60001732015-08-20 11:16:54 -0700106 // Special handling for non-zero offset maps, we need to print that
107 // information.
108 if (frame->map.offset != 0) {
109 line += " (offset " + StringPrintf("0x%" PRIxPTR, frame->map.offset) + ")";
110 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700111 if (!frame->func_name.empty()) {
112 line += " (" + frame->func_name;
113 if (frame->func_offset) {
114 line += StringPrintf("+%" PRIuPTR, frame->func_offset);
115 }
116 line += ')';
117 }
118
119 return line;
120}
121
122void Backtrace::FillInMap(uintptr_t pc, backtrace_map_t* map) {
Christopher Ferris30c942c2015-05-14 15:39:52 -0700123 if (map_ != nullptr) {
124 map_->FillIn(pc, map);
125 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700126}
127
128Backtrace* Backtrace::Create(pid_t pid, pid_t tid, BacktraceMap* map) {
129 if (pid == BACKTRACE_CURRENT_PROCESS) {
130 pid = getpid();
131 if (tid == BACKTRACE_CURRENT_THREAD) {
132 tid = gettid();
133 }
134 } else if (tid == BACKTRACE_CURRENT_THREAD) {
135 tid = pid;
136 }
137
138 if (pid == getpid()) {
139 return new UnwindCurrent(pid, tid, map);
140 } else {
141 return new UnwindPtrace(pid, tid, map);
142 }
143}
Christopher Ferrisc463ba42016-03-09 14:35:54 -0800144
Christopher Ferris9a6b3e32017-10-18 09:08:51 -0700145Backtrace* Backtrace::CreateNew(pid_t pid, pid_t tid, BacktraceMap* map) {
146 if (pid == BACKTRACE_CURRENT_PROCESS) {
147 pid = getpid();
148 if (tid == BACKTRACE_CURRENT_THREAD) {
149 tid = gettid();
150 }
151 } else if (tid == BACKTRACE_CURRENT_THREAD) {
152 tid = pid;
153 }
154
155 if (map == nullptr) {
156// This would cause the wrong type of map object to be created, so disallow.
157#if defined(__ANDROID__)
158 __assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__,
159 "Backtrace::CreateNew() must be called with a real map pointer.");
160#else
161 BACK_LOGE("Backtrace::CreateNew() must be called with a real map pointer.");
162 abort();
163#endif
164 }
165
166 if (pid == getpid()) {
167 return new UnwindStackCurrent(pid, tid, map);
168 } else {
169 return new UnwindStackPtrace(pid, tid, map);
170 }
171}
172
Christopher Ferrisc463ba42016-03-09 14:35:54 -0800173std::string Backtrace::GetErrorString(BacktraceUnwindError error) {
174 switch (error) {
175 case BACKTRACE_UNWIND_NO_ERROR:
176 return "No error";
177 case BACKTRACE_UNWIND_ERROR_SETUP_FAILED:
178 return "Setup failed";
179 case BACKTRACE_UNWIND_ERROR_MAP_MISSING:
180 return "No map found";
181 case BACKTRACE_UNWIND_ERROR_INTERNAL:
182 return "Internal libbacktrace error, please submit a bugreport";
183 case BACKTRACE_UNWIND_ERROR_THREAD_DOESNT_EXIST:
184 return "Thread doesn't exist";
185 case BACKTRACE_UNWIND_ERROR_THREAD_TIMEOUT:
Brian Carlstrom6d1da7c2017-03-23 11:24:33 -0700186 return "Thread has not responded to signal in time";
Christopher Ferrisc463ba42016-03-09 14:35:54 -0800187 case BACKTRACE_UNWIND_ERROR_UNSUPPORTED_OPERATION:
188 return "Attempt to use an unsupported feature";
189 case BACKTRACE_UNWIND_ERROR_NO_CONTEXT:
190 return "Attempt to do an offline unwind without a context";
191 }
192}