blob: 39296b43b83d01390e16777ffb33585b23b3e49d [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>
20#include <sys/ptrace.h>
21#include <sys/types.h>
22#include <unistd.h>
23
24#define __STDC_FORMAT_MACROS
25#include <inttypes.h>
26
27#include <string>
28
29#include <backtrace/Backtrace.h>
Christopher Ferris46756822014-01-14 20:16:30 -080030#include <backtrace/BacktraceMap.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070031
Christopher Ferrisdf290612014-01-22 19:21:07 -080032#include "BacktraceImpl.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
60bool Backtrace::Unwind(size_t num_ignore_frames) {
61 return impl_->Unwind(num_ignore_frames);
62}
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
85bool Backtrace::VerifyReadWordArgs(uintptr_t ptr, uint32_t* out_value) {
86 if (ptr & 3) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070087 BACK_LOGW("invalid pointer %p", (void*)ptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -070088 *out_value = (uint32_t)-1;
89 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 Ferris46756822014-01-14 20:16:30 -0800103 if (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 Ferris46756822014-01-14 20:16:30 -0800110 if (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 Ferris46756822014-01-14 20:16:30 -0800132const backtrace_map_t* Backtrace::FindMap(uintptr_t pc) {
Christopher Ferris46756822014-01-14 20:16:30 -0800133 return map_->Find(pc);
134}
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
146bool BacktraceCurrent::ReadWord(uintptr_t ptr, uint32_t* out_value) {
147 if (!VerifyReadWordArgs(ptr, out_value)) {
148 return false;
149 }
150
Christopher Ferris46756822014-01-14 20:16:30 -0800151 const backtrace_map_t* map = FindMap(ptr);
152 if (map && map->flags & PROT_READ) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700153 *out_value = *reinterpret_cast<uint32_t*>(ptr);
154 return true;
155 } else {
Christopher Ferris8ed46272013-10-29 15:44:25 -0700156 BACK_LOGW("pointer %p not in a readable map", reinterpret_cast<void*>(ptr));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700157 *out_value = static_cast<uint32_t>(-1);
158 return false;
159 }
160}
161
162//-------------------------------------------------------------------------
163// BacktracePtrace functions.
164//-------------------------------------------------------------------------
Christopher Ferris98464972014-01-06 19:16:33 -0800165BacktracePtrace::BacktracePtrace(
Christopher Ferris46756822014-01-14 20:16:30 -0800166 BacktraceImpl* impl, pid_t pid, pid_t tid, BacktraceMap* map)
167 : Backtrace(impl, pid, map) {
168 tid_ = tid;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700169}
170
171BacktracePtrace::~BacktracePtrace() {
172}
173
174bool BacktracePtrace::ReadWord(uintptr_t ptr, uint32_t* out_value) {
175 if (!VerifyReadWordArgs(ptr, out_value)) {
176 return false;
177 }
178
179#if defined(__APPLE__)
Christopher Ferris8ed46272013-10-29 15:44:25 -0700180 BACK_LOGW("MacOS does not support reading from another pid.");
Christopher Ferris17e91d42013-10-21 13:30:52 -0700181 return false;
182#else
183 // ptrace() returns -1 and sets errno when the operation fails.
184 // To disambiguate -1 from a valid result, we clear errno beforehand.
185 errno = 0;
186 *out_value = ptrace(PTRACE_PEEKTEXT, Tid(), reinterpret_cast<void*>(ptr), NULL);
187 if (*out_value == static_cast<uint32_t>(-1) && errno) {
Christopher Ferris8ed46272013-10-29 15:44:25 -0700188 BACK_LOGW("invalid pointer %p reading from tid %d, ptrace() strerror(errno)=%s",
189 reinterpret_cast<void*>(ptr), Tid(), strerror(errno));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700190 return false;
191 }
192 return true;
193#endif
194}
195
Christopher Ferris46756822014-01-14 20:16:30 -0800196Backtrace* Backtrace::Create(pid_t pid, pid_t tid, BacktraceMap* map) {
Christopher Ferriscbfc7302013-11-05 11:04:12 -0800197 if (pid == BACKTRACE_CURRENT_PROCESS || pid == getpid()) {
Christopher Ferrisbc12d632013-11-12 10:54:16 -0800198 if (tid == BACKTRACE_CURRENT_THREAD || tid == gettid()) {
Christopher Ferris46756822014-01-14 20:16:30 -0800199 return CreateCurrentObj(map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700200 } else {
Christopher Ferris46756822014-01-14 20:16:30 -0800201 return CreateThreadObj(tid, map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700202 }
Christopher Ferrisbc12d632013-11-12 10:54:16 -0800203 } else if (tid == BACKTRACE_CURRENT_THREAD) {
Christopher Ferris46756822014-01-14 20:16:30 -0800204 return CreatePtraceObj(pid, pid, map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700205 } else {
Christopher Ferris46756822014-01-14 20:16:30 -0800206 return CreatePtraceObj(pid, tid, map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700207 }
208}