blob: eca1c3d778d008762a06669e2714cd9bb29e0e02 [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//-------------------------------------------------------------------------
45Backtrace::Backtrace(BacktraceImpl* impl) : impl_(impl), map_info_(NULL) {
46 impl_->SetParent(this);
47 backtrace_.num_frames = 0;
48 backtrace_.pid = -1;
49 backtrace_.tid = -1;
50}
51
52Backtrace::~Backtrace() {
53 for (size_t i = 0; i < NumFrames(); i++) {
54 if (backtrace_.frames[i].func_name) {
55 free(backtrace_.frames[i].func_name);
56 backtrace_.frames[i].func_name = NULL;
57 }
58 }
59
60 if (map_info_) {
61 backtrace_destroy_map_info_list(map_info_);
62 map_info_ = NULL;
63 }
64
65 if (impl_) {
66 delete impl_;
67 impl_ = NULL;
68 }
69}
70
71bool Backtrace::Unwind(size_t num_ignore_frames) {
72 return impl_->Unwind(num_ignore_frames);
73}
74
75__BEGIN_DECLS
76extern char* __cxa_demangle (const char* mangled, char* buf, size_t* len,
77 int* status);
78__END_DECLS
79
80std::string Backtrace::GetFunctionName(uintptr_t pc, uintptr_t* offset) {
81 std::string func_name = impl_->GetFunctionNameRaw(pc, offset);
82 if (!func_name.empty()) {
83#if defined(__APPLE__)
84 // Mac OS' __cxa_demangle demangles "f" as "float"; last tested on 10.7.
85 if (symbol_name[0] != '_') {
86 return func_name;
87 }
88#endif
89 char* name = __cxa_demangle(func_name.c_str(), 0, 0, 0);
90 if (name) {
91 func_name = name;
92 free(name);
93 }
94 }
95 return func_name;
96}
97
98bool Backtrace::VerifyReadWordArgs(uintptr_t ptr, uint32_t* out_value) {
99 if (ptr & 3) {
100 ALOGW("Backtrace::verifyReadWordArgs: invalid pointer %p", (void*)ptr);
101 *out_value = (uint32_t)-1;
102 return false;
103 }
104 return true;
105}
106
107const char* Backtrace::GetMapName(uintptr_t pc, uintptr_t* map_start) {
108 const backtrace_map_info_t* map_info = FindMapInfo(pc);
109 if (map_info) {
110 if (map_start) {
111 *map_start = map_info->start;
112 }
113 return map_info->name;
114 }
115 return NULL;
116}
117
118const backtrace_map_info_t* Backtrace::FindMapInfo(uintptr_t ptr) {
119 return backtrace_find_map_info(map_info_, ptr);
120}
121
122std::string Backtrace::FormatFrameData(size_t frame_num) {
123 backtrace_frame_data_t* frame = &backtrace_.frames[frame_num];
124 const char* map_name;
125 if (frame->map_name) {
126 map_name = frame->map_name;
127 } else {
128 map_name = "<unknown>";
129 }
130 uintptr_t relative_pc;
131 if (frame->map_offset) {
132 relative_pc = frame->map_offset;
133 } else {
134 relative_pc = frame->pc;
135 }
136
137 char buf[512];
138 if (frame->func_name && frame->func_offset) {
139 snprintf(buf, sizeof(buf), "#%02zu pc %0*" PRIxPTR " %s (%s+%" PRIuPTR ")",
140 frame_num, (int)sizeof(uintptr_t)*2, relative_pc, map_name,
141 frame->func_name, frame->func_offset);
142 } else if (frame->func_name) {
143 snprintf(buf, sizeof(buf), "#%02zu pc %0*" PRIxPTR " %s (%s)", frame_num,
144 (int)sizeof(uintptr_t)*2, relative_pc, map_name, frame->func_name);
145 } else {
146 snprintf(buf, sizeof(buf), "#%02zu pc %0*" PRIxPTR " %s", frame_num,
147 (int)sizeof(uintptr_t)*2, relative_pc, map_name);
148 }
149
150 return buf;
151}
152
153//-------------------------------------------------------------------------
154// BacktraceCurrent functions.
155//-------------------------------------------------------------------------
156BacktraceCurrent::BacktraceCurrent(BacktraceImpl* impl) : Backtrace(impl) {
157 map_info_ = backtrace_create_map_info_list(-1);
158
159 backtrace_.pid = getpid();
160}
161
162BacktraceCurrent::~BacktraceCurrent() {
163}
164
165bool BacktraceCurrent::ReadWord(uintptr_t ptr, uint32_t* out_value) {
166 if (!VerifyReadWordArgs(ptr, out_value)) {
167 return false;
168 }
169
170 const backtrace_map_info_t* map_info = FindMapInfo(ptr);
171 if (map_info && map_info->is_readable) {
172 *out_value = *reinterpret_cast<uint32_t*>(ptr);
173 return true;
174 } else {
175 ALOGW("BacktraceCurrent::readWord: pointer %p not in a readbale map", reinterpret_cast<void*>(ptr));
176 *out_value = static_cast<uint32_t>(-1);
177 return false;
178 }
179}
180
181//-------------------------------------------------------------------------
182// BacktracePtrace functions.
183//-------------------------------------------------------------------------
184BacktracePtrace::BacktracePtrace(BacktraceImpl* impl, pid_t pid, pid_t tid)
185 : Backtrace(impl) {
186 map_info_ = backtrace_create_map_info_list(tid);
187
188 backtrace_.pid = pid;
189 backtrace_.tid = tid;
190}
191
192BacktracePtrace::~BacktracePtrace() {
193}
194
195bool BacktracePtrace::ReadWord(uintptr_t ptr, uint32_t* out_value) {
196 if (!VerifyReadWordArgs(ptr, out_value)) {
197 return false;
198 }
199
200#if defined(__APPLE__)
201 ALOGW("BacktracePtrace::readWord: MacOS does not support reading from another pid.\n");
202 return false;
203#else
204 // ptrace() returns -1 and sets errno when the operation fails.
205 // To disambiguate -1 from a valid result, we clear errno beforehand.
206 errno = 0;
207 *out_value = ptrace(PTRACE_PEEKTEXT, Tid(), reinterpret_cast<void*>(ptr), NULL);
208 if (*out_value == static_cast<uint32_t>(-1) && errno) {
209 ALOGW("BacktracePtrace::readWord: invalid pointer 0x%08x reading from tid %d, "
210 "ptrace() errno=%d", ptr, Tid(), errno);
211 return false;
212 }
213 return true;
214#endif
215}
216
217Backtrace* Backtrace::Create(pid_t pid, pid_t tid) {
218 if (pid < 0 || pid == getpid()) {
219 if (tid < 0 || tid == gettid()) {
220 return CreateCurrentObj();
221 } else {
222 return CreateThreadObj(tid);
223 }
224 } else if (tid < 0) {
225 return CreatePtraceObj(pid, pid);
226 } else {
227 return CreatePtraceObj(pid, tid);
228 }
229}
230
231//-------------------------------------------------------------------------
232// Common interface functions.
233//-------------------------------------------------------------------------
234bool backtrace_create_context(
235 backtrace_context_t* context, pid_t pid, pid_t tid, size_t num_ignore_frames) {
236 Backtrace* backtrace = Backtrace::Create(pid, tid);
237 if (!backtrace) {
238 return false;
239 }
240 if (!backtrace->Unwind(num_ignore_frames)) {
241 delete backtrace;
242 return false;
243 }
244
245 context->data = backtrace;
246 context->backtrace = backtrace->GetBacktrace();
247 return true;
248}
249
250void backtrace_destroy_context(backtrace_context_t* context) {
251 if (context->data) {
252 Backtrace* backtrace = reinterpret_cast<Backtrace*>(context->data);
253 delete backtrace;
254 context->data = NULL;
255 }
256 context->backtrace = NULL;
257}
258
259const backtrace_t* backtrace_get_data(backtrace_context_t* context) {
260 if (context && context->data) {
261 Backtrace* backtrace = reinterpret_cast<Backtrace*>(context->data);
262 return backtrace->GetBacktrace();
263 }
264 return NULL;
265}
266
267bool backtrace_read_word(const backtrace_context_t* context, uintptr_t ptr, uint32_t* value) {
268 if (context->data) {
269 Backtrace* backtrace = reinterpret_cast<Backtrace*>(context->data);
270 return backtrace->ReadWord(ptr, value);
271 }
272 return true;
273}
274
275const char* backtrace_get_map_name(const backtrace_context_t* context, uintptr_t pc, uintptr_t* map_start) {
276 if (context->data) {
277 Backtrace* backtrace = reinterpret_cast<Backtrace*>(context->data);
278 return backtrace->GetMapName(pc, map_start);
279 }
280 return NULL;
281}
282
283char* backtrace_get_func_name(const backtrace_context_t* context, uintptr_t pc, uintptr_t* func_offset) {
284 if (context->data) {
285 Backtrace* backtrace = reinterpret_cast<Backtrace*>(context->data);
286 std::string func_name = backtrace->GetFunctionName(pc, func_offset);
287 if (!func_name.empty()) {
288 return strdup(func_name.c_str());
289 }
290 }
291 return NULL;
292}
293
294void backtrace_format_frame_data(
295 const backtrace_context_t* context, size_t frame_num, char* buf,
296 size_t buf_size) {
297 if (buf_size == 0 || buf == NULL) {
298 ALOGW("backtrace_format_frame_data: bad call buf %p buf_size %zu\n",
299 buf, buf_size);
300 return;
301 }
302 if (context->data) {
303 Backtrace* backtrace = reinterpret_cast<Backtrace*>(context->data);
304 std::string line = backtrace->FormatFrameData(frame_num);
305 if (line.size() > buf_size) {
306 memcpy(buf, line.c_str(), buf_size-1);
307 buf[buf_size] = '\0';
308 } else {
309 memcpy(buf, line.c_str(), line.size()+1);
310 }
311 }
312}