blob: 8027d869ae827d5ba5707338bb3e5808b4657c5c [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
Christopher Ferris17e91d42013-10-21 13:30:52 -070024#include <inttypes.h>
25
26#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 Ferris17e91d42013-10-21 13:30:52 -070032#include "thread_utils.h"
33
34//-------------------------------------------------------------------------
Christopher Ferris17e91d42013-10-21 13:30:52 -070035// Backtrace functions.
36//-------------------------------------------------------------------------
Christopher Ferris46756822014-01-14 20:16:30 -080037Backtrace::Backtrace(BacktraceImpl* impl, pid_t pid, BacktraceMap* map)
38 : pid_(pid), tid_(-1), map_(map), map_shared_(true), impl_(impl) {
Christopher Ferris17e91d42013-10-21 13:30:52 -070039 impl_->SetParent(this);
Christopher Ferris98464972014-01-06 19:16:33 -080040
Christopher Ferris46756822014-01-14 20:16:30 -080041 if (map_ == NULL) {
Christopher Ferrisdf290612014-01-22 19:21:07 -080042 map_ = BacktraceMap::Create(pid);
Christopher Ferris46756822014-01-14 20:16:30 -080043 map_shared_ = false;
Christopher Ferris98464972014-01-06 19:16:33 -080044 }
Christopher Ferris17e91d42013-10-21 13:30:52 -070045}
46
47Backtrace::~Backtrace() {
Christopher Ferris17e91d42013-10-21 13:30:52 -070048 if (impl_) {
49 delete impl_;
50 impl_ = NULL;
51 }
Christopher Ferrisdf290612014-01-22 19:21:07 -080052
53 if (map_ && !map_shared_) {
54 delete map_;
55 map_ = NULL;
56 }
Christopher Ferris17e91d42013-10-21 13:30:52 -070057}
58
59bool Backtrace::Unwind(size_t num_ignore_frames) {
60 return impl_->Unwind(num_ignore_frames);
61}
62
Christopher Ferris8ed46272013-10-29 15:44:25 -070063extern "C" char* __cxa_demangle(const char* mangled, char* buf, size_t* len,
64 int* status);
Christopher Ferris17e91d42013-10-21 13:30:52 -070065
66std::string Backtrace::GetFunctionName(uintptr_t pc, uintptr_t* offset) {
67 std::string func_name = impl_->GetFunctionNameRaw(pc, offset);
68 if (!func_name.empty()) {
69#if defined(__APPLE__)
70 // Mac OS' __cxa_demangle demangles "f" as "float"; last tested on 10.7.
Christopher Ferrisf67c6412014-01-10 00:43:54 -080071 if (func_name[0] != '_') {
Christopher Ferris17e91d42013-10-21 13:30:52 -070072 return func_name;
73 }
74#endif
75 char* name = __cxa_demangle(func_name.c_str(), 0, 0, 0);
76 if (name) {
77 func_name = name;
78 free(name);
79 }
80 }
81 return func_name;
82}
83
84bool Backtrace::VerifyReadWordArgs(uintptr_t ptr, uint32_t* out_value) {
85 if (ptr & 3) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070086 BACK_LOGW("invalid pointer %p", (void*)ptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -070087 *out_value = (uint32_t)-1;
88 return false;
89 }
90 return true;
91}
92
Christopher Ferris17e91d42013-10-21 13:30:52 -070093std::string Backtrace::FormatFrameData(size_t frame_num) {
Christopher Ferris46756822014-01-14 20:16:30 -080094 if (frame_num >= frames_.size()) {
95 return "";
96 }
97 return FormatFrameData(&frames_[frame_num]);
Christopher Ferris20303f82014-01-10 16:33:16 -080098}
99
100std::string Backtrace::FormatFrameData(const backtrace_frame_data_t* frame) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700101 const char* map_name;
Christopher Ferris46756822014-01-14 20:16:30 -0800102 if (frame->map && !frame->map->name.empty()) {
103 map_name = frame->map->name.c_str();
Christopher Ferris17e91d42013-10-21 13:30:52 -0700104 } else {
105 map_name = "<unknown>";
106 }
Christopher Ferris46756822014-01-14 20:16:30 -0800107
Christopher Ferris17e91d42013-10-21 13:30:52 -0700108 uintptr_t relative_pc;
Christopher Ferris46756822014-01-14 20:16:30 -0800109 if (frame->map) {
110 relative_pc = frame->pc - frame->map->start;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700111 } else {
112 relative_pc = frame->pc;
113 }
114
115 char buf[512];
Christopher Ferris46756822014-01-14 20:16:30 -0800116 if (!frame->func_name.empty() && frame->func_offset) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700117 snprintf(buf, sizeof(buf), "#%02zu pc %0*" PRIxPTR " %s (%s+%" PRIuPTR ")",
Christopher Ferris20303f82014-01-10 16:33:16 -0800118 frame->num, (int)sizeof(uintptr_t)*2, relative_pc, map_name,
Christopher Ferris46756822014-01-14 20:16:30 -0800119 frame->func_name.c_str(), frame->func_offset);
120 } else if (!frame->func_name.empty()) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800121 snprintf(buf, sizeof(buf), "#%02zu pc %0*" PRIxPTR " %s (%s)", frame->num,
Christopher Ferris46756822014-01-14 20:16:30 -0800122 (int)sizeof(uintptr_t)*2, relative_pc, map_name, frame->func_name.c_str());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700123 } else {
Christopher Ferris20303f82014-01-10 16:33:16 -0800124 snprintf(buf, sizeof(buf), "#%02zu pc %0*" PRIxPTR " %s", frame->num,
Christopher Ferris17e91d42013-10-21 13:30:52 -0700125 (int)sizeof(uintptr_t)*2, relative_pc, map_name);
126 }
127
128 return buf;
129}
130
Christopher Ferris46756822014-01-14 20:16:30 -0800131const backtrace_map_t* Backtrace::FindMap(uintptr_t pc) {
Christopher Ferris46756822014-01-14 20:16:30 -0800132 return map_->Find(pc);
133}
134
Christopher Ferris17e91d42013-10-21 13:30:52 -0700135//-------------------------------------------------------------------------
136// BacktraceCurrent functions.
137//-------------------------------------------------------------------------
Christopher Ferris98464972014-01-06 19:16:33 -0800138BacktraceCurrent::BacktraceCurrent(
Christopher Ferris46756822014-01-14 20:16:30 -0800139 BacktraceImpl* impl, BacktraceMap* map) : Backtrace(impl, getpid(), map) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700140}
141
142BacktraceCurrent::~BacktraceCurrent() {
143}
144
145bool BacktraceCurrent::ReadWord(uintptr_t ptr, uint32_t* out_value) {
146 if (!VerifyReadWordArgs(ptr, out_value)) {
147 return false;
148 }
149
Christopher Ferris46756822014-01-14 20:16:30 -0800150 const backtrace_map_t* map = FindMap(ptr);
151 if (map && map->flags & PROT_READ) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700152 *out_value = *reinterpret_cast<uint32_t*>(ptr);
153 return true;
154 } else {
Christopher Ferris8ed46272013-10-29 15:44:25 -0700155 BACK_LOGW("pointer %p not in a readable map", reinterpret_cast<void*>(ptr));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700156 *out_value = static_cast<uint32_t>(-1);
157 return false;
158 }
159}
160
161//-------------------------------------------------------------------------
162// BacktracePtrace functions.
163//-------------------------------------------------------------------------
Christopher Ferris98464972014-01-06 19:16:33 -0800164BacktracePtrace::BacktracePtrace(
Christopher Ferris46756822014-01-14 20:16:30 -0800165 BacktraceImpl* impl, pid_t pid, pid_t tid, BacktraceMap* map)
166 : Backtrace(impl, pid, map) {
167 tid_ = tid;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700168}
169
170BacktracePtrace::~BacktracePtrace() {
171}
172
173bool BacktracePtrace::ReadWord(uintptr_t ptr, uint32_t* out_value) {
174 if (!VerifyReadWordArgs(ptr, out_value)) {
175 return false;
176 }
177
178#if defined(__APPLE__)
Christopher Ferris8ed46272013-10-29 15:44:25 -0700179 BACK_LOGW("MacOS does not support reading from another pid.");
Christopher Ferris17e91d42013-10-21 13:30:52 -0700180 return false;
181#else
182 // ptrace() returns -1 and sets errno when the operation fails.
183 // To disambiguate -1 from a valid result, we clear errno beforehand.
184 errno = 0;
185 *out_value = ptrace(PTRACE_PEEKTEXT, Tid(), reinterpret_cast<void*>(ptr), NULL);
186 if (*out_value == static_cast<uint32_t>(-1) && errno) {
Christopher Ferris8ed46272013-10-29 15:44:25 -0700187 BACK_LOGW("invalid pointer %p reading from tid %d, ptrace() strerror(errno)=%s",
188 reinterpret_cast<void*>(ptr), Tid(), strerror(errno));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700189 return false;
190 }
191 return true;
192#endif
193}
194
Christopher Ferris46756822014-01-14 20:16:30 -0800195Backtrace* Backtrace::Create(pid_t pid, pid_t tid, BacktraceMap* map) {
Christopher Ferriscbfc7302013-11-05 11:04:12 -0800196 if (pid == BACKTRACE_CURRENT_PROCESS || pid == getpid()) {
Christopher Ferrisbc12d632013-11-12 10:54:16 -0800197 if (tid == BACKTRACE_CURRENT_THREAD || tid == gettid()) {
Christopher Ferris46756822014-01-14 20:16:30 -0800198 return CreateCurrentObj(map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700199 } else {
Christopher Ferris46756822014-01-14 20:16:30 -0800200 return CreateThreadObj(tid, map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700201 }
Christopher Ferrisbc12d632013-11-12 10:54:16 -0800202 } else if (tid == BACKTRACE_CURRENT_THREAD) {
Christopher Ferris46756822014-01-14 20:16:30 -0800203 return CreatePtraceObj(pid, pid, map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700204 } else {
Christopher Ferris46756822014-01-14 20:16:30 -0800205 return CreatePtraceObj(pid, tid, map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700206 }
207}