blob: 4650b6aaf7d022df17f329ef9ef418e9a4942020 [file] [log] [blame]
Christopher Ferris17e91d42013-10-21 13:30:52 -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 <errno.h>
18#include <stdlib.h>
19#include <string.h>
Christopher Ferris2b4a63f2015-03-17 14:42:03 -070020#include <sys/param.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070021#include <sys/ptrace.h>
22#include <sys/types.h>
Christopher Ferrisc58287d2014-05-09 16:59:06 -070023#include <ucontext.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070024#include <unistd.h>
25
Christopher Ferris17e91d42013-10-21 13:30:52 -070026#include <string>
27
28#include <backtrace/Backtrace.h>
Christopher Ferris46756822014-01-14 20:16:30 -080029#include <backtrace/BacktraceMap.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070030
Christopher Ferrisdf290612014-01-22 19:21:07 -080031#include "BacktraceImpl.h"
Christopher Ferrise2960912014-03-07 19:42:19 -080032#include "BacktraceLog.h"
Christopher Ferris17e91d42013-10-21 13:30:52 -070033#include "thread_utils.h"
34
35//-------------------------------------------------------------------------
Christopher Ferris17e91d42013-10-21 13:30:52 -070036// Backtrace functions.
37//-------------------------------------------------------------------------
Christopher Ferris46756822014-01-14 20:16:30 -080038Backtrace::Backtrace(BacktraceImpl* impl, pid_t pid, BacktraceMap* map)
39 : pid_(pid), tid_(-1), map_(map), map_shared_(true), impl_(impl) {
Christopher Ferris17e91d42013-10-21 13:30:52 -070040 impl_->SetParent(this);
Christopher Ferris98464972014-01-06 19:16:33 -080041
Christopher Ferris46756822014-01-14 20:16:30 -080042 if (map_ == NULL) {
Christopher Ferrisdf290612014-01-22 19:21:07 -080043 map_ = BacktraceMap::Create(pid);
Christopher Ferris46756822014-01-14 20:16:30 -080044 map_shared_ = false;
Christopher Ferris98464972014-01-06 19:16:33 -080045 }
Christopher Ferris17e91d42013-10-21 13:30:52 -070046}
47
48Backtrace::~Backtrace() {
Christopher Ferris17e91d42013-10-21 13:30:52 -070049 if (impl_) {
50 delete impl_;
51 impl_ = NULL;
52 }
Christopher Ferrisdf290612014-01-22 19:21:07 -080053
54 if (map_ && !map_shared_) {
55 delete map_;
56 map_ = NULL;
57 }
Christopher Ferris17e91d42013-10-21 13:30:52 -070058}
59
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070060bool Backtrace::Unwind(size_t num_ignore_frames, ucontext_t* ucontext) {
61 return impl_->Unwind(num_ignore_frames, ucontext);
Christopher Ferris17e91d42013-10-21 13:30:52 -070062}
63
Christopher Ferris8ed46272013-10-29 15:44:25 -070064extern "C" char* __cxa_demangle(const char* mangled, char* buf, size_t* len,
65 int* status);
Christopher Ferris17e91d42013-10-21 13:30:52 -070066
67std::string Backtrace::GetFunctionName(uintptr_t pc, uintptr_t* offset) {
68 std::string func_name = impl_->GetFunctionNameRaw(pc, offset);
69 if (!func_name.empty()) {
70#if defined(__APPLE__)
71 // Mac OS' __cxa_demangle demangles "f" as "float"; last tested on 10.7.
Christopher Ferrisf67c6412014-01-10 00:43:54 -080072 if (func_name[0] != '_') {
Christopher Ferris17e91d42013-10-21 13:30:52 -070073 return func_name;
74 }
75#endif
76 char* name = __cxa_demangle(func_name.c_str(), 0, 0, 0);
77 if (name) {
78 func_name = name;
79 free(name);
80 }
81 }
82 return func_name;
83}
84
Pavel Chupinc6c194c2013-11-21 23:17:20 +040085bool Backtrace::VerifyReadWordArgs(uintptr_t ptr, word_t* out_value) {
86 if (ptr & (sizeof(word_t)-1)) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070087 BACK_LOGW("invalid pointer %p", (void*)ptr);
Pavel Chupinc6c194c2013-11-21 23:17:20 +040088 *out_value = (word_t)-1;
Christopher Ferris17e91d42013-10-21 13:30:52 -070089 return false;
90 }
91 return true;
92}
93
Christopher Ferris17e91d42013-10-21 13:30:52 -070094std::string Backtrace::FormatFrameData(size_t frame_num) {
Christopher Ferris46756822014-01-14 20:16:30 -080095 if (frame_num >= frames_.size()) {
96 return "";
97 }
98 return FormatFrameData(&frames_[frame_num]);
Christopher Ferris20303f82014-01-10 16:33:16 -080099}
100
101std::string Backtrace::FormatFrameData(const backtrace_frame_data_t* frame) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700102 const char* map_name;
Christopher Ferris12385e32015-02-06 13:22:01 -0800103 if (BacktraceMap::IsValid(frame->map) && !frame->map.name.empty()) {
104 map_name = frame->map.name.c_str();
Christopher Ferris17e91d42013-10-21 13:30:52 -0700105 } else {
106 map_name = "<unknown>";
107 }
Christopher Ferris46756822014-01-14 20:16:30 -0800108
Christopher Ferris17e91d42013-10-21 13:30:52 -0700109 uintptr_t relative_pc;
Christopher Ferris12385e32015-02-06 13:22:01 -0800110 if (BacktraceMap::IsValid(frame->map)) {
111 relative_pc = frame->pc - frame->map.start;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700112 } else {
113 relative_pc = frame->pc;
114 }
115
116 char buf[512];
Christopher Ferris46756822014-01-14 20:16:30 -0800117 if (!frame->func_name.empty() && frame->func_offset) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700118 snprintf(buf, sizeof(buf), "#%02zu pc %0*" PRIxPTR " %s (%s+%" PRIuPTR ")",
Christopher Ferris20303f82014-01-10 16:33:16 -0800119 frame->num, (int)sizeof(uintptr_t)*2, relative_pc, map_name,
Christopher Ferris46756822014-01-14 20:16:30 -0800120 frame->func_name.c_str(), frame->func_offset);
121 } else if (!frame->func_name.empty()) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800122 snprintf(buf, sizeof(buf), "#%02zu pc %0*" PRIxPTR " %s (%s)", frame->num,
Christopher Ferris46756822014-01-14 20:16:30 -0800123 (int)sizeof(uintptr_t)*2, relative_pc, map_name, frame->func_name.c_str());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700124 } else {
Christopher Ferris20303f82014-01-10 16:33:16 -0800125 snprintf(buf, sizeof(buf), "#%02zu pc %0*" PRIxPTR " %s", frame->num,
Christopher Ferris17e91d42013-10-21 13:30:52 -0700126 (int)sizeof(uintptr_t)*2, relative_pc, map_name);
127 }
128
129 return buf;
130}
131
Christopher Ferris12385e32015-02-06 13:22:01 -0800132void Backtrace::FillInMap(uintptr_t pc, backtrace_map_t* map) {
133 map_->FillIn(pc, map);
Christopher Ferris46756822014-01-14 20:16:30 -0800134}
135
Christopher Ferris17e91d42013-10-21 13:30:52 -0700136//-------------------------------------------------------------------------
137// BacktraceCurrent functions.
138//-------------------------------------------------------------------------
Christopher Ferris98464972014-01-06 19:16:33 -0800139BacktraceCurrent::BacktraceCurrent(
Christopher Ferris46756822014-01-14 20:16:30 -0800140 BacktraceImpl* impl, BacktraceMap* map) : Backtrace(impl, getpid(), map) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700141}
142
143BacktraceCurrent::~BacktraceCurrent() {
144}
145
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400146bool BacktraceCurrent::ReadWord(uintptr_t ptr, word_t* out_value) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700147 if (!VerifyReadWordArgs(ptr, out_value)) {
148 return false;
149 }
150
Christopher Ferris12385e32015-02-06 13:22:01 -0800151 backtrace_map_t map;
152 FillInMap(ptr, &map);
153 if (BacktraceMap::IsValid(map) && map.flags & PROT_READ) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400154 *out_value = *reinterpret_cast<word_t*>(ptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700155 return true;
156 } else {
Christopher Ferris8ed46272013-10-29 15:44:25 -0700157 BACK_LOGW("pointer %p not in a readable map", reinterpret_cast<void*>(ptr));
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400158 *out_value = static_cast<word_t>(-1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700159 return false;
160 }
161}
162
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700163size_t BacktraceCurrent::Read(uintptr_t addr, uint8_t* buffer, size_t bytes) {
164 backtrace_map_t map;
165 FillInMap(addr, &map);
166 if (!BacktraceMap::IsValid(map) || !(map.flags & PROT_READ)) {
167 return 0;
168 }
169 bytes = MIN(map.end - addr, bytes);
170 memcpy(buffer, reinterpret_cast<uint8_t*>(addr), bytes);
171 return bytes;
172}
173
Christopher Ferris17e91d42013-10-21 13:30:52 -0700174//-------------------------------------------------------------------------
175// BacktracePtrace functions.
176//-------------------------------------------------------------------------
Christopher Ferris98464972014-01-06 19:16:33 -0800177BacktracePtrace::BacktracePtrace(
Christopher Ferris46756822014-01-14 20:16:30 -0800178 BacktraceImpl* impl, pid_t pid, pid_t tid, BacktraceMap* map)
179 : Backtrace(impl, pid, map) {
180 tid_ = tid;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700181}
182
183BacktracePtrace::~BacktracePtrace() {
184}
185
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700186#if !defined(__APPLE__)
187static bool PtraceRead(pid_t tid, uintptr_t addr, word_t* out_value) {
188 // ptrace() returns -1 and sets errno when the operation fails.
189 // To disambiguate -1 from a valid result, we clear errno beforehand.
190 errno = 0;
191 *out_value = ptrace(PTRACE_PEEKTEXT, tid, reinterpret_cast<void*>(addr), NULL);
192 if (*out_value == static_cast<word_t>(-1) && errno) {
193 BACK_LOGW("invalid pointer %p reading from tid %d, ptrace() strerror(errno)=%s",
194 reinterpret_cast<void*>(addr), tid, strerror(errno));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700195 return false;
196 }
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700197 return true;
198}
199#endif
Christopher Ferris17e91d42013-10-21 13:30:52 -0700200
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700201bool BacktracePtrace::ReadWord(uintptr_t ptr, word_t* out_value) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700202#if defined(__APPLE__)
Christopher Ferris8ed46272013-10-29 15:44:25 -0700203 BACK_LOGW("MacOS does not support reading from another pid.");
Christopher Ferris17e91d42013-10-21 13:30:52 -0700204 return false;
205#else
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700206 if (!VerifyReadWordArgs(ptr, out_value)) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700207 return false;
208 }
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700209
210 backtrace_map_t map;
211 FillInMap(ptr, &map);
212 if (!BacktraceMap::IsValid(map) || !(map.flags & PROT_READ)) {
213 return false;
214 }
215
216 return PtraceRead(Tid(), ptr, out_value);
217#endif
218}
219
220size_t BacktracePtrace::Read(uintptr_t addr, uint8_t* buffer, size_t bytes) {
221#if defined(__APPLE__)
222 BACK_LOGW("MacOS does not support reading from another pid.");
223 return 0;
224#else
225 backtrace_map_t map;
226 FillInMap(addr, &map);
227 if (!BacktraceMap::IsValid(map) || !(map.flags & PROT_READ)) {
228 return 0;
229 }
230
231 bytes = MIN(map.end - addr, bytes);
232 size_t bytes_read = 0;
233 word_t data_word;
234 size_t align_bytes = addr & (sizeof(word_t) - 1);
235 if (align_bytes != 0) {
236 if (!PtraceRead(Tid(), addr & ~(sizeof(word_t) - 1), &data_word)) {
237 return 0;
238 }
239 align_bytes = sizeof(word_t) - align_bytes;
240 memcpy(buffer, reinterpret_cast<uint8_t*>(&data_word) + sizeof(word_t) - align_bytes,
241 align_bytes);
242 addr += align_bytes;
243 buffer += align_bytes;
244 bytes -= align_bytes;
245 bytes_read += align_bytes;
246 }
247
248 size_t num_words = bytes / sizeof(word_t);
249 for (size_t i = 0; i < num_words; i++) {
250 if (!PtraceRead(Tid(), addr, &data_word)) {
251 return bytes_read;
252 }
253 memcpy(buffer, &data_word, sizeof(word_t));
254 buffer += sizeof(word_t);
255 addr += sizeof(word_t);
256 bytes_read += sizeof(word_t);
257 }
258
259 size_t left_over = bytes & (sizeof(word_t) - 1);
260 if (left_over) {
261 if (!PtraceRead(Tid(), addr, &data_word)) {
262 return bytes_read;
263 }
264 memcpy(buffer, &data_word, left_over);
265 bytes_read += left_over;
266 }
267 return bytes_read;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700268#endif
269}
270
Christopher Ferris46756822014-01-14 20:16:30 -0800271Backtrace* Backtrace::Create(pid_t pid, pid_t tid, BacktraceMap* map) {
Christopher Ferriscbfc7302013-11-05 11:04:12 -0800272 if (pid == BACKTRACE_CURRENT_PROCESS || pid == getpid()) {
Christopher Ferrisbc12d632013-11-12 10:54:16 -0800273 if (tid == BACKTRACE_CURRENT_THREAD || tid == gettid()) {
Christopher Ferris46756822014-01-14 20:16:30 -0800274 return CreateCurrentObj(map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700275 } else {
Christopher Ferris46756822014-01-14 20:16:30 -0800276 return CreateThreadObj(tid, map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700277 }
Christopher Ferrisbc12d632013-11-12 10:54:16 -0800278 } else if (tid == BACKTRACE_CURRENT_THREAD) {
Christopher Ferris46756822014-01-14 20:16:30 -0800279 return CreatePtraceObj(pid, pid, map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700280 } else {
Christopher Ferris46756822014-01-14 20:16:30 -0800281 return CreatePtraceObj(pid, tid, map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700282 }
283}