blob: 20667e0cc24a1d070096dc8441a8875e0e333004 [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>
30#include <cutils/log.h>
31
32#include "Backtrace.h"
33#include "thread_utils.h"
34
35//-------------------------------------------------------------------------
36// BacktraceImpl functions.
37//-------------------------------------------------------------------------
38backtrace_t* BacktraceImpl::GetBacktraceData() {
39 return &backtrace_obj_->backtrace_;
40}
41
42//-------------------------------------------------------------------------
43// Backtrace functions.
44//-------------------------------------------------------------------------
Christopher Ferris98464972014-01-06 19:16:33 -080045Backtrace::Backtrace(BacktraceImpl* impl, pid_t pid, backtrace_map_info_t* map_info)
46 : impl_(impl), map_info_(map_info), map_info_requires_delete_(false) {
Christopher Ferris17e91d42013-10-21 13:30:52 -070047 impl_->SetParent(this);
48 backtrace_.num_frames = 0;
Christopher Ferris98464972014-01-06 19:16:33 -080049 backtrace_.pid = pid;
Christopher Ferris17e91d42013-10-21 13:30:52 -070050 backtrace_.tid = -1;
Christopher Ferris98464972014-01-06 19:16:33 -080051
52 if (map_info_ == NULL) {
53 // Create the map and manage it internally.
54 map_info_ = backtrace_create_map_info_list(pid);
55 map_info_requires_delete_ = true;
56 }
Christopher Ferris17e91d42013-10-21 13:30:52 -070057}
58
59Backtrace::~Backtrace() {
60 for (size_t i = 0; i < NumFrames(); i++) {
61 if (backtrace_.frames[i].func_name) {
62 free(backtrace_.frames[i].func_name);
63 backtrace_.frames[i].func_name = NULL;
64 }
65 }
66
Christopher Ferris98464972014-01-06 19:16:33 -080067 if (map_info_ && map_info_requires_delete_) {
Christopher Ferris17e91d42013-10-21 13:30:52 -070068 backtrace_destroy_map_info_list(map_info_);
69 map_info_ = NULL;
70 }
71
72 if (impl_) {
73 delete impl_;
74 impl_ = NULL;
75 }
76}
77
78bool Backtrace::Unwind(size_t num_ignore_frames) {
79 return impl_->Unwind(num_ignore_frames);
80}
81
Christopher Ferris8ed46272013-10-29 15:44:25 -070082extern "C" char* __cxa_demangle(const char* mangled, char* buf, size_t* len,
83 int* status);
Christopher Ferris17e91d42013-10-21 13:30:52 -070084
85std::string Backtrace::GetFunctionName(uintptr_t pc, uintptr_t* offset) {
86 std::string func_name = impl_->GetFunctionNameRaw(pc, offset);
87 if (!func_name.empty()) {
88#if defined(__APPLE__)
89 // Mac OS' __cxa_demangle demangles "f" as "float"; last tested on 10.7.
Christopher Ferrisf67c6412014-01-10 00:43:54 -080090 if (func_name[0] != '_') {
Christopher Ferris17e91d42013-10-21 13:30:52 -070091 return func_name;
92 }
93#endif
94 char* name = __cxa_demangle(func_name.c_str(), 0, 0, 0);
95 if (name) {
96 func_name = name;
97 free(name);
98 }
99 }
100 return func_name;
101}
102
103bool Backtrace::VerifyReadWordArgs(uintptr_t ptr, uint32_t* out_value) {
104 if (ptr & 3) {
Christopher Ferris8ed46272013-10-29 15:44:25 -0700105 BACK_LOGW("invalid pointer %p", (void*)ptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700106 *out_value = (uint32_t)-1;
107 return false;
108 }
109 return true;
110}
111
112const char* Backtrace::GetMapName(uintptr_t pc, uintptr_t* map_start) {
113 const backtrace_map_info_t* map_info = FindMapInfo(pc);
114 if (map_info) {
115 if (map_start) {
116 *map_start = map_info->start;
117 }
118 return map_info->name;
119 }
120 return NULL;
121}
122
123const backtrace_map_info_t* Backtrace::FindMapInfo(uintptr_t ptr) {
124 return backtrace_find_map_info(map_info_, ptr);
125}
126
127std::string Backtrace::FormatFrameData(size_t frame_num) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800128 return FormatFrameData(&backtrace_.frames[frame_num]);
129}
130
131std::string Backtrace::FormatFrameData(const backtrace_frame_data_t* frame) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700132 const char* map_name;
133 if (frame->map_name) {
134 map_name = frame->map_name;
135 } else {
136 map_name = "<unknown>";
137 }
138 uintptr_t relative_pc;
139 if (frame->map_offset) {
140 relative_pc = frame->map_offset;
141 } else {
142 relative_pc = frame->pc;
143 }
144
145 char buf[512];
146 if (frame->func_name && frame->func_offset) {
147 snprintf(buf, sizeof(buf), "#%02zu pc %0*" PRIxPTR " %s (%s+%" PRIuPTR ")",
Christopher Ferris20303f82014-01-10 16:33:16 -0800148 frame->num, (int)sizeof(uintptr_t)*2, relative_pc, map_name,
Christopher Ferris17e91d42013-10-21 13:30:52 -0700149 frame->func_name, frame->func_offset);
150 } else if (frame->func_name) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800151 snprintf(buf, sizeof(buf), "#%02zu pc %0*" PRIxPTR " %s (%s)", frame->num,
Christopher Ferris17e91d42013-10-21 13:30:52 -0700152 (int)sizeof(uintptr_t)*2, relative_pc, map_name, frame->func_name);
153 } else {
Christopher Ferris20303f82014-01-10 16:33:16 -0800154 snprintf(buf, sizeof(buf), "#%02zu pc %0*" PRIxPTR " %s", frame->num,
Christopher Ferris17e91d42013-10-21 13:30:52 -0700155 (int)sizeof(uintptr_t)*2, relative_pc, map_name);
156 }
157
158 return buf;
159}
160
161//-------------------------------------------------------------------------
162// BacktraceCurrent functions.
163//-------------------------------------------------------------------------
Christopher Ferris98464972014-01-06 19:16:33 -0800164BacktraceCurrent::BacktraceCurrent(
165 BacktraceImpl* impl, backtrace_map_info_t *map_info) : Backtrace(impl, getpid(), map_info) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700166
167 backtrace_.pid = getpid();
168}
169
170BacktraceCurrent::~BacktraceCurrent() {
171}
172
173bool BacktraceCurrent::ReadWord(uintptr_t ptr, uint32_t* out_value) {
174 if (!VerifyReadWordArgs(ptr, out_value)) {
175 return false;
176 }
177
178 const backtrace_map_info_t* map_info = FindMapInfo(ptr);
179 if (map_info && map_info->is_readable) {
180 *out_value = *reinterpret_cast<uint32_t*>(ptr);
181 return true;
182 } else {
Christopher Ferris8ed46272013-10-29 15:44:25 -0700183 BACK_LOGW("pointer %p not in a readable map", reinterpret_cast<void*>(ptr));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700184 *out_value = static_cast<uint32_t>(-1);
185 return false;
186 }
187}
188
189//-------------------------------------------------------------------------
190// BacktracePtrace functions.
191//-------------------------------------------------------------------------
Christopher Ferris98464972014-01-06 19:16:33 -0800192BacktracePtrace::BacktracePtrace(
193 BacktraceImpl* impl, pid_t pid, pid_t tid, backtrace_map_info_t* map_info)
194 : Backtrace(impl, pid, map_info) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700195 backtrace_.tid = tid;
196}
197
198BacktracePtrace::~BacktracePtrace() {
199}
200
201bool BacktracePtrace::ReadWord(uintptr_t ptr, uint32_t* out_value) {
202 if (!VerifyReadWordArgs(ptr, out_value)) {
203 return false;
204 }
205
206#if defined(__APPLE__)
Christopher Ferris8ed46272013-10-29 15:44:25 -0700207 BACK_LOGW("MacOS does not support reading from another pid.");
Christopher Ferris17e91d42013-10-21 13:30:52 -0700208 return false;
209#else
210 // ptrace() returns -1 and sets errno when the operation fails.
211 // To disambiguate -1 from a valid result, we clear errno beforehand.
212 errno = 0;
213 *out_value = ptrace(PTRACE_PEEKTEXT, Tid(), reinterpret_cast<void*>(ptr), NULL);
214 if (*out_value == static_cast<uint32_t>(-1) && errno) {
Christopher Ferris8ed46272013-10-29 15:44:25 -0700215 BACK_LOGW("invalid pointer %p reading from tid %d, ptrace() strerror(errno)=%s",
216 reinterpret_cast<void*>(ptr), Tid(), strerror(errno));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700217 return false;
218 }
219 return true;
220#endif
221}
222
Christopher Ferris98464972014-01-06 19:16:33 -0800223Backtrace* Backtrace::Create(pid_t pid, pid_t tid, backtrace_map_info_t* map_info) {
Christopher Ferriscbfc7302013-11-05 11:04:12 -0800224 if (pid == BACKTRACE_CURRENT_PROCESS || pid == getpid()) {
Christopher Ferrisbc12d632013-11-12 10:54:16 -0800225 if (tid == BACKTRACE_CURRENT_THREAD || tid == gettid()) {
Christopher Ferris98464972014-01-06 19:16:33 -0800226 return CreateCurrentObj(map_info);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700227 } else {
Christopher Ferris98464972014-01-06 19:16:33 -0800228 return CreateThreadObj(tid, map_info);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700229 }
Christopher Ferrisbc12d632013-11-12 10:54:16 -0800230 } else if (tid == BACKTRACE_CURRENT_THREAD) {
Christopher Ferris98464972014-01-06 19:16:33 -0800231 return CreatePtraceObj(pid, pid, map_info);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700232 } else {
Christopher Ferris98464972014-01-06 19:16:33 -0800233 return CreatePtraceObj(pid, tid, map_info);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700234 }
235}
236
237//-------------------------------------------------------------------------
238// Common interface functions.
239//-------------------------------------------------------------------------
Christopher Ferris98464972014-01-06 19:16:33 -0800240bool backtrace_create_context_with_map(
241 backtrace_context_t* context, pid_t pid, pid_t tid, size_t num_ignore_frames,
242 backtrace_map_info_t* map_info) {
243 Backtrace* backtrace = Backtrace::Create(pid, tid, map_info);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700244 if (!backtrace) {
245 return false;
246 }
247 if (!backtrace->Unwind(num_ignore_frames)) {
248 delete backtrace;
249 return false;
250 }
251
252 context->data = backtrace;
253 context->backtrace = backtrace->GetBacktrace();
254 return true;
255}
256
Christopher Ferris98464972014-01-06 19:16:33 -0800257bool backtrace_create_context(
258 backtrace_context_t* context, pid_t pid, pid_t tid, size_t num_ignore_frames) {
259 return backtrace_create_context_with_map(context, pid, tid, num_ignore_frames, NULL);
260}
261
262
Christopher Ferris17e91d42013-10-21 13:30:52 -0700263void backtrace_destroy_context(backtrace_context_t* context) {
264 if (context->data) {
265 Backtrace* backtrace = reinterpret_cast<Backtrace*>(context->data);
266 delete backtrace;
267 context->data = NULL;
268 }
269 context->backtrace = NULL;
270}
271
272const backtrace_t* backtrace_get_data(backtrace_context_t* context) {
273 if (context && context->data) {
274 Backtrace* backtrace = reinterpret_cast<Backtrace*>(context->data);
275 return backtrace->GetBacktrace();
276 }
277 return NULL;
278}
279
280bool backtrace_read_word(const backtrace_context_t* context, uintptr_t ptr, uint32_t* value) {
281 if (context->data) {
282 Backtrace* backtrace = reinterpret_cast<Backtrace*>(context->data);
283 return backtrace->ReadWord(ptr, value);
284 }
285 return true;
286}
287
288const char* backtrace_get_map_name(const backtrace_context_t* context, uintptr_t pc, uintptr_t* map_start) {
289 if (context->data) {
290 Backtrace* backtrace = reinterpret_cast<Backtrace*>(context->data);
291 return backtrace->GetMapName(pc, map_start);
292 }
293 return NULL;
294}
295
296char* backtrace_get_func_name(const backtrace_context_t* context, uintptr_t pc, uintptr_t* func_offset) {
297 if (context->data) {
298 Backtrace* backtrace = reinterpret_cast<Backtrace*>(context->data);
299 std::string func_name = backtrace->GetFunctionName(pc, func_offset);
300 if (!func_name.empty()) {
301 return strdup(func_name.c_str());
302 }
303 }
304 return NULL;
305}
306
307void backtrace_format_frame_data(
308 const backtrace_context_t* context, size_t frame_num, char* buf,
309 size_t buf_size) {
310 if (buf_size == 0 || buf == NULL) {
Christopher Ferris8ed46272013-10-29 15:44:25 -0700311 BACK_LOGW("bad call buf %p buf_size %zu", buf, buf_size);
312 } else if (context->data) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700313 Backtrace* backtrace = reinterpret_cast<Backtrace*>(context->data);
314 std::string line = backtrace->FormatFrameData(frame_num);
315 if (line.size() > buf_size) {
316 memcpy(buf, line.c_str(), buf_size-1);
317 buf[buf_size] = '\0';
318 } else {
319 memcpy(buf, line.c_str(), line.size()+1);
320 }
321 }
322}