blob: d7b40ab7e4a4245233282aff0fbdef2bad684fe3 [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
32#include "Backtrace.h"
33#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) {
43 // The map will be created when needed.
44 map_shared_ = false;
Christopher Ferris98464972014-01-06 19:16:33 -080045 }
Christopher Ferris17e91d42013-10-21 13:30:52 -070046}
47
48Backtrace::~Backtrace() {
Christopher Ferris46756822014-01-14 20:16:30 -080049 if (map_ && !map_shared_) {
50 delete map_;
51 map_ = NULL;
Christopher Ferris17e91d42013-10-21 13:30:52 -070052 }
53
54 if (impl_) {
55 delete impl_;
56 impl_ = NULL;
57 }
58}
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 -0800132bool Backtrace::BuildMap() {
133 map_ = impl_->CreateBacktraceMap(pid_);
134 if (!map_->Build()) {
135 BACK_LOGW("Failed to build map for process %d", pid_);
136 return false;
137 }
138 return true;
139}
140
141const backtrace_map_t* Backtrace::FindMap(uintptr_t pc) {
142 if (map_ == NULL) {
143 // Lazy eval, time to build the map.
144 if (!BuildMap()) {
145 return NULL;
146 }
147 }
148 return map_->Find(pc);
149}
150
151BacktraceMap* Backtrace::TakeMapOwnership() {
152 map_shared_ = true;
153 return map_;
154}
155
Christopher Ferris17e91d42013-10-21 13:30:52 -0700156//-------------------------------------------------------------------------
157// BacktraceCurrent functions.
158//-------------------------------------------------------------------------
Christopher Ferris98464972014-01-06 19:16:33 -0800159BacktraceCurrent::BacktraceCurrent(
Christopher Ferris46756822014-01-14 20:16:30 -0800160 BacktraceImpl* impl, BacktraceMap* map) : Backtrace(impl, getpid(), map) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700161}
162
163BacktraceCurrent::~BacktraceCurrent() {
164}
165
166bool BacktraceCurrent::ReadWord(uintptr_t ptr, uint32_t* out_value) {
167 if (!VerifyReadWordArgs(ptr, out_value)) {
168 return false;
169 }
170
Christopher Ferris46756822014-01-14 20:16:30 -0800171 const backtrace_map_t* map = FindMap(ptr);
172 if (map && map->flags & PROT_READ) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700173 *out_value = *reinterpret_cast<uint32_t*>(ptr);
174 return true;
175 } else {
Christopher Ferris8ed46272013-10-29 15:44:25 -0700176 BACK_LOGW("pointer %p not in a readable map", reinterpret_cast<void*>(ptr));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700177 *out_value = static_cast<uint32_t>(-1);
178 return false;
179 }
180}
181
182//-------------------------------------------------------------------------
183// BacktracePtrace functions.
184//-------------------------------------------------------------------------
Christopher Ferris98464972014-01-06 19:16:33 -0800185BacktracePtrace::BacktracePtrace(
Christopher Ferris46756822014-01-14 20:16:30 -0800186 BacktraceImpl* impl, pid_t pid, pid_t tid, BacktraceMap* map)
187 : Backtrace(impl, pid, map) {
188 tid_ = tid;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700189}
190
191BacktracePtrace::~BacktracePtrace() {
192}
193
194bool BacktracePtrace::ReadWord(uintptr_t ptr, uint32_t* out_value) {
195 if (!VerifyReadWordArgs(ptr, out_value)) {
196 return false;
197 }
198
199#if defined(__APPLE__)
Christopher Ferris8ed46272013-10-29 15:44:25 -0700200 BACK_LOGW("MacOS does not support reading from another pid.");
Christopher Ferris17e91d42013-10-21 13:30:52 -0700201 return false;
202#else
203 // ptrace() returns -1 and sets errno when the operation fails.
204 // To disambiguate -1 from a valid result, we clear errno beforehand.
205 errno = 0;
206 *out_value = ptrace(PTRACE_PEEKTEXT, Tid(), reinterpret_cast<void*>(ptr), NULL);
207 if (*out_value == static_cast<uint32_t>(-1) && errno) {
Christopher Ferris8ed46272013-10-29 15:44:25 -0700208 BACK_LOGW("invalid pointer %p reading from tid %d, ptrace() strerror(errno)=%s",
209 reinterpret_cast<void*>(ptr), Tid(), strerror(errno));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700210 return false;
211 }
212 return true;
213#endif
214}
215
Christopher Ferris46756822014-01-14 20:16:30 -0800216Backtrace* Backtrace::Create(pid_t pid, pid_t tid, BacktraceMap* map) {
Christopher Ferriscbfc7302013-11-05 11:04:12 -0800217 if (pid == BACKTRACE_CURRENT_PROCESS || pid == getpid()) {
Christopher Ferrisbc12d632013-11-12 10:54:16 -0800218 if (tid == BACKTRACE_CURRENT_THREAD || tid == gettid()) {
Christopher Ferris46756822014-01-14 20:16:30 -0800219 return CreateCurrentObj(map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700220 } else {
Christopher Ferris46756822014-01-14 20:16:30 -0800221 return CreateThreadObj(tid, map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700222 }
Christopher Ferrisbc12d632013-11-12 10:54:16 -0800223 } else if (tid == BACKTRACE_CURRENT_THREAD) {
Christopher Ferris46756822014-01-14 20:16:30 -0800224 return CreatePtraceObj(pid, pid, map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700225 } else {
Christopher Ferris46756822014-01-14 20:16:30 -0800226 return CreatePtraceObj(pid, tid, map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700227 }
228}