blob: f1dd61d745c6bc9ba47feeb082f6053414d77c55 [file] [log] [blame]
Jeff Brown501edd22011-10-19 20:35:35 -07001/*
2 * Copyright (C) 2011 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#define LOG_TAG "Corkscrew"
18//#define LOG_NDEBUG 0
19
20#include "backtrace-arch.h"
21#include "backtrace-helper.h"
22#include "ptrace-arch.h"
23#include <corkscrew/map_info.h>
24#include <corkscrew/symbol_table.h>
25#include <corkscrew/ptrace.h>
26#include <corkscrew/demangle.h>
27
28#include <unistd.h>
29#include <signal.h>
Elliott Hughes71363a82012-05-18 11:56:17 -070030#include <stdlib.h>
31#include <string.h>
Jeff Brown501edd22011-10-19 20:35:35 -070032#include <pthread.h>
33#include <unwind.h>
Jeff Brown501edd22011-10-19 20:35:35 -070034#include <cutils/log.h>
Jeff Brownf0c58722011-11-03 17:58:44 -070035#include <cutils/atomic.h>
Jeff Brown501edd22011-10-19 20:35:35 -070036
Elliott Hughes71363a82012-05-18 11:56:17 -070037#define __USE_GNU // For dladdr(3) in glibc.
Jeff Brown501edd22011-10-19 20:35:35 -070038#include <dlfcn.h>
Jeff Brown501edd22011-10-19 20:35:35 -070039
Elliott Hughes71363a82012-05-18 11:56:17 -070040#if defined(__BIONIC__)
41
42// Bionic implements and exports gettid but only implements tgkill.
43extern int tgkill(int tgid, int tid, int sig);
44
Elliott Hughesbfec3a32012-05-24 19:03:07 -070045#elif defined(__APPLE__)
46
47#include <sys/syscall.h>
48
49// Mac OS >= 10.6 has a system call equivalent to Linux's gettid().
50static pid_t gettid() {
51 return syscall(SYS_thread_selfid);
52}
53
Elliott Hughes71363a82012-05-18 11:56:17 -070054#else
55
56// glibc doesn't implement or export either gettid or tgkill.
57
58#include <unistd.h>
59#include <sys/syscall.h>
60
61static pid_t gettid() {
62 return syscall(__NR_gettid);
63}
64
65static int tgkill(int tgid, int tid, int sig) {
66 return syscall(__NR_tgkill, tgid, tid, sig);
67}
68
69#endif
70
Jeff Brown501edd22011-10-19 20:35:35 -070071typedef struct {
72 backtrace_frame_t* backtrace;
73 size_t ignore_depth;
74 size_t max_depth;
75 size_t ignored_frames;
76 size_t returned_frames;
Jeff Brownf0c58722011-11-03 17:58:44 -070077 memory_t memory;
Jeff Brown501edd22011-10-19 20:35:35 -070078} backtrace_state_t;
79
80static _Unwind_Reason_Code unwind_backtrace_callback(struct _Unwind_Context* context, void* arg) {
81 backtrace_state_t* state = (backtrace_state_t*)arg;
82 uintptr_t pc = _Unwind_GetIP(context);
83 if (pc) {
84 // TODO: Get information about the stack layout from the _Unwind_Context.
85 // This will require a new architecture-specific function to query
86 // the appropriate registers. Current callers of unwind_backtrace
87 // don't need this information, so we won't bother collecting it just yet.
Jeff Brownf0c58722011-11-03 17:58:44 -070088 add_backtrace_entry(rewind_pc_arch(&state->memory, pc), state->backtrace,
Jeff Brown501edd22011-10-19 20:35:35 -070089 state->ignore_depth, state->max_depth,
90 &state->ignored_frames, &state->returned_frames);
91 }
92 return state->returned_frames < state->max_depth ? _URC_NO_REASON : _URC_END_OF_STACK;
93}
94
95ssize_t unwind_backtrace(backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) {
Jeff Brownf0c58722011-11-03 17:58:44 -070096 ALOGV("Unwinding current thread %d.", gettid());
97
98 map_info_t* milist = acquire_my_map_info_list();
99
Jeff Brown501edd22011-10-19 20:35:35 -0700100 backtrace_state_t state;
101 state.backtrace = backtrace;
102 state.ignore_depth = ignore_depth;
103 state.max_depth = max_depth;
104 state.ignored_frames = 0;
105 state.returned_frames = 0;
Jeff Brownf0c58722011-11-03 17:58:44 -0700106 init_memory(&state.memory, milist);
Jeff Brown501edd22011-10-19 20:35:35 -0700107
Elliott Hughesbfec3a32012-05-24 19:03:07 -0700108 _Unwind_Reason_Code rc = _Unwind_Backtrace(unwind_backtrace_callback, &state);
Jeff Brownf0c58722011-11-03 17:58:44 -0700109
110 release_my_map_info_list(milist);
111
Jeff Brown501edd22011-10-19 20:35:35 -0700112 if (state.returned_frames) {
113 return state.returned_frames;
114 }
115 return rc == _URC_END_OF_STACK ? 0 : -1;
116}
117
118#ifdef CORKSCREW_HAVE_ARCH
Jeff Brown67754562011-11-18 15:34:35 -0800119static const int32_t STATE_DUMPING = -1;
120static const int32_t STATE_DONE = -2;
121static const int32_t STATE_CANCEL = -3;
122
Jeff Brown501edd22011-10-19 20:35:35 -0700123static pthread_mutex_t g_unwind_signal_mutex = PTHREAD_MUTEX_INITIALIZER;
124static volatile struct {
Jeff Brown67754562011-11-18 15:34:35 -0800125 int32_t tid_state;
Jeff Brownf0c58722011-11-03 17:58:44 -0700126 const map_info_t* map_info_list;
Jeff Brown501edd22011-10-19 20:35:35 -0700127 backtrace_frame_t* backtrace;
128 size_t ignore_depth;
129 size_t max_depth;
130 size_t returned_frames;
Jeff Brown501edd22011-10-19 20:35:35 -0700131} g_unwind_signal_state;
132
Edwin Vane46beebe2012-07-26 14:18:23 -0400133static void unwind_backtrace_thread_signal_handler(int n __attribute__((unused)), siginfo_t* siginfo, void* sigcontext) {
Jeff Brown67754562011-11-18 15:34:35 -0800134 if (!android_atomic_acquire_cas(gettid(), STATE_DUMPING, &g_unwind_signal_state.tid_state)) {
Jeff Brown501edd22011-10-19 20:35:35 -0700135 g_unwind_signal_state.returned_frames = unwind_backtrace_signal_arch(
Jeff Brownf0c58722011-11-03 17:58:44 -0700136 siginfo, sigcontext,
137 g_unwind_signal_state.map_info_list,
138 g_unwind_signal_state.backtrace,
Jeff Brown501edd22011-10-19 20:35:35 -0700139 g_unwind_signal_state.ignore_depth,
140 g_unwind_signal_state.max_depth);
Jeff Brown67754562011-11-18 15:34:35 -0800141 android_atomic_release_store(STATE_DONE, &g_unwind_signal_state.tid_state);
Jeff Brownf0c58722011-11-03 17:58:44 -0700142 } else {
143 ALOGV("Received spurious SIGURG on thread %d that was intended for thread %d.",
Jeff Brown67754562011-11-18 15:34:35 -0800144 gettid(), android_atomic_acquire_load(&g_unwind_signal_state.tid_state));
Jeff Brown501edd22011-10-19 20:35:35 -0700145 }
146}
147#endif
148
149ssize_t unwind_backtrace_thread(pid_t tid, backtrace_frame_t* backtrace,
150 size_t ignore_depth, size_t max_depth) {
Jeff Brownf0c58722011-11-03 17:58:44 -0700151 if (tid == gettid()) {
152 return unwind_backtrace(backtrace, ignore_depth + 1, max_depth);
153 }
154
155 ALOGV("Unwinding thread %d from thread %d.", tid, gettid());
156
Elliott Hughesbfec3a32012-05-24 19:03:07 -0700157 // TODO: there's no tgkill(2) on Mac OS, so we'd either need the
158 // mach_port_t or the pthread_t rather than the tid.
159#if defined(CORKSCREW_HAVE_ARCH) && !defined(__APPLE__)
Jeff Brown501edd22011-10-19 20:35:35 -0700160 struct sigaction act;
161 struct sigaction oact;
162 memset(&act, 0, sizeof(act));
163 act.sa_sigaction = unwind_backtrace_thread_signal_handler;
Jeff Brown67754562011-11-18 15:34:35 -0800164 act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
Jeff Brown501edd22011-10-19 20:35:35 -0700165 sigemptyset(&act.sa_mask);
166
167 pthread_mutex_lock(&g_unwind_signal_mutex);
Jeff Brownf0c58722011-11-03 17:58:44 -0700168 map_info_t* milist = acquire_my_map_info_list();
Jeff Brown501edd22011-10-19 20:35:35 -0700169
170 ssize_t frames = -1;
171 if (!sigaction(SIGURG, &act, &oact)) {
Jeff Brownf0c58722011-11-03 17:58:44 -0700172 g_unwind_signal_state.map_info_list = milist;
173 g_unwind_signal_state.backtrace = backtrace;
174 g_unwind_signal_state.ignore_depth = ignore_depth;
175 g_unwind_signal_state.max_depth = max_depth;
176 g_unwind_signal_state.returned_frames = 0;
Jeff Brown67754562011-11-18 15:34:35 -0800177 android_atomic_release_store(tid, &g_unwind_signal_state.tid_state);
Jeff Brownf0c58722011-11-03 17:58:44 -0700178
Jeff Brown67754562011-11-18 15:34:35 -0800179 // Signal the specific thread that we want to dump.
180 int32_t tid_state = tid;
181 if (tgkill(getpid(), tid, SIGURG)) {
Jeff Brownf0c58722011-11-03 17:58:44 -0700182 ALOGV("Failed to send SIGURG to thread %d.", tid);
Jeff Brownf0c58722011-11-03 17:58:44 -0700183 } else {
Jeff Brown67754562011-11-18 15:34:35 -0800184 // Wait for the other thread to start dumping the stack, or time out.
185 int wait_millis = 250;
186 for (;;) {
187 tid_state = android_atomic_acquire_load(&g_unwind_signal_state.tid_state);
188 if (tid_state != tid) {
189 break;
190 }
191 if (wait_millis--) {
192 ALOGV("Waiting for thread %d to start dumping the stack...", tid);
193 usleep(1000);
194 } else {
195 ALOGV("Timed out waiting for thread %d to start dumping the stack.", tid);
196 break;
197 }
Jeff Brown501edd22011-10-19 20:35:35 -0700198 }
Jeff Brown67754562011-11-18 15:34:35 -0800199 }
200
201 // Try to cancel the dump if it has not started yet.
202 if (tid_state == tid) {
203 if (!android_atomic_acquire_cas(tid, STATE_CANCEL, &g_unwind_signal_state.tid_state)) {
204 ALOGV("Canceled thread %d stack dump.", tid);
205 tid_state = STATE_CANCEL;
206 } else {
207 tid_state = android_atomic_acquire_load(&g_unwind_signal_state.tid_state);
208 }
209 }
210
211 // Wait indefinitely for the dump to finish or be canceled.
212 // We cannot apply a timeout here because the other thread is accessing state that
213 // is owned by this thread, such as milist. It should not take very
214 // long to take the dump once started.
215 while (tid_state == STATE_DUMPING) {
216 ALOGV("Waiting for thread %d to finish dumping the stack...", tid);
217 usleep(1000);
218 tid_state = android_atomic_acquire_load(&g_unwind_signal_state.tid_state);
219 }
220
221 if (tid_state == STATE_DONE) {
Jeff Brown501edd22011-10-19 20:35:35 -0700222 frames = g_unwind_signal_state.returned_frames;
223 }
Jeff Brownf0c58722011-11-03 17:58:44 -0700224
Jeff Brown501edd22011-10-19 20:35:35 -0700225 sigaction(SIGURG, &oact, NULL);
226 }
227
Jeff Brownf0c58722011-11-03 17:58:44 -0700228 release_my_map_info_list(milist);
Jeff Brown501edd22011-10-19 20:35:35 -0700229 pthread_mutex_unlock(&g_unwind_signal_mutex);
230 return frames;
231#else
232 return -1;
233#endif
234}
235
236ssize_t unwind_backtrace_ptrace(pid_t tid, const ptrace_context_t* context,
237 backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) {
238#ifdef CORKSCREW_HAVE_ARCH
239 return unwind_backtrace_ptrace_arch(tid, context, backtrace, ignore_depth, max_depth);
240#else
241 return -1;
242#endif
243}
244
245static void init_backtrace_symbol(backtrace_symbol_t* symbol, uintptr_t pc) {
246 symbol->relative_pc = pc;
Jeff Brown19b39f32011-11-21 21:10:00 -0800247 symbol->relative_symbol_addr = 0;
Jeff Brownf0c58722011-11-03 17:58:44 -0700248 symbol->map_name = NULL;
Jeff Brown19b39f32011-11-21 21:10:00 -0800249 symbol->symbol_name = NULL;
Jeff Brown501edd22011-10-19 20:35:35 -0700250 symbol->demangled_name = NULL;
251}
252
253void get_backtrace_symbols(const backtrace_frame_t* backtrace, size_t frames,
254 backtrace_symbol_t* backtrace_symbols) {
Jeff Brownf0c58722011-11-03 17:58:44 -0700255 map_info_t* milist = acquire_my_map_info_list();
Jeff Brown501edd22011-10-19 20:35:35 -0700256 for (size_t i = 0; i < frames; i++) {
257 const backtrace_frame_t* frame = &backtrace[i];
258 backtrace_symbol_t* symbol = &backtrace_symbols[i];
259 init_backtrace_symbol(symbol, frame->absolute_pc);
260
261 const map_info_t* mi = find_map_info(milist, frame->absolute_pc);
262 if (mi) {
263 symbol->relative_pc = frame->absolute_pc - mi->start;
Jeff Brownf0c58722011-11-03 17:58:44 -0700264 if (mi->name[0]) {
265 symbol->map_name = strdup(mi->name);
266 }
Jeff Brown501edd22011-10-19 20:35:35 -0700267 Dl_info info;
268 if (dladdr((const void*)frame->absolute_pc, &info) && info.dli_sname) {
Jeff Brown19b39f32011-11-21 21:10:00 -0800269 symbol->relative_symbol_addr = (uintptr_t)info.dli_saddr
270 - (uintptr_t)info.dli_fbase;
271 symbol->symbol_name = strdup(info.dli_sname);
272 symbol->demangled_name = demangle_symbol_name(symbol->symbol_name);
Jeff Brown501edd22011-10-19 20:35:35 -0700273 }
Jeff Brown501edd22011-10-19 20:35:35 -0700274 }
275 }
Jeff Brownf0c58722011-11-03 17:58:44 -0700276 release_my_map_info_list(milist);
Jeff Brown501edd22011-10-19 20:35:35 -0700277}
278
279void get_backtrace_symbols_ptrace(const ptrace_context_t* context,
280 const backtrace_frame_t* backtrace, size_t frames,
281 backtrace_symbol_t* backtrace_symbols) {
282 for (size_t i = 0; i < frames; i++) {
283 const backtrace_frame_t* frame = &backtrace[i];
284 backtrace_symbol_t* symbol = &backtrace_symbols[i];
285 init_backtrace_symbol(symbol, frame->absolute_pc);
286
287 const map_info_t* mi;
288 const symbol_t* s;
289 find_symbol_ptrace(context, frame->absolute_pc, &mi, &s);
290 if (mi) {
291 symbol->relative_pc = frame->absolute_pc - mi->start;
Jeff Brownf0c58722011-11-03 17:58:44 -0700292 if (mi->name[0]) {
293 symbol->map_name = strdup(mi->name);
294 }
Jeff Brown501edd22011-10-19 20:35:35 -0700295 }
296 if (s) {
Jeff Brown19b39f32011-11-21 21:10:00 -0800297 symbol->relative_symbol_addr = s->start;
298 symbol->symbol_name = strdup(s->name);
299 symbol->demangled_name = demangle_symbol_name(symbol->symbol_name);
Jeff Brown501edd22011-10-19 20:35:35 -0700300 }
301 }
302}
303
304void free_backtrace_symbols(backtrace_symbol_t* backtrace_symbols, size_t frames) {
305 for (size_t i = 0; i < frames; i++) {
306 backtrace_symbol_t* symbol = &backtrace_symbols[i];
Jeff Brownf0c58722011-11-03 17:58:44 -0700307 free(symbol->map_name);
Jeff Brown19b39f32011-11-21 21:10:00 -0800308 free(symbol->symbol_name);
Jeff Brown501edd22011-10-19 20:35:35 -0700309 free(symbol->demangled_name);
310 init_backtrace_symbol(symbol, 0);
311 }
312}
Jeff Brown19b39f32011-11-21 21:10:00 -0800313
Edwin Vane46beebe2012-07-26 14:18:23 -0400314void format_backtrace_line(unsigned frameNumber, const backtrace_frame_t* frame __attribute__((unused)),
Jeff Brown19b39f32011-11-21 21:10:00 -0800315 const backtrace_symbol_t* symbol, char* buffer, size_t bufferSize) {
316 const char* mapName = symbol->map_name ? symbol->map_name : "<unknown>";
317 const char* symbolName = symbol->demangled_name ? symbol->demangled_name : symbol->symbol_name;
Elliott Hughesbfec3a32012-05-24 19:03:07 -0700318 int fieldWidth = (bufferSize - 80) / 2;
Jeff Brown19b39f32011-11-21 21:10:00 -0800319 if (symbolName) {
320 uint32_t pc_offset = symbol->relative_pc - symbol->relative_symbol_addr;
321 if (pc_offset) {
Ben Chengddc50e62013-05-21 10:55:03 -0700322 snprintf(buffer, bufferSize, "#%02u pc %08x %.*s (%.*s+%u)",
323 frameNumber, (unsigned int) symbol->relative_pc,
324 fieldWidth, mapName, fieldWidth, symbolName, pc_offset);
Jeff Brown19b39f32011-11-21 21:10:00 -0800325 } else {
Ben Chengddc50e62013-05-21 10:55:03 -0700326 snprintf(buffer, bufferSize, "#%02u pc %08x %.*s (%.*s)",
327 frameNumber, (unsigned int) symbol->relative_pc,
328 fieldWidth, mapName, fieldWidth, symbolName);
Jeff Brown19b39f32011-11-21 21:10:00 -0800329 }
330 } else {
Ben Chengddc50e62013-05-21 10:55:03 -0700331 snprintf(buffer, bufferSize, "#%02u pc %08x %.*s",
332 frameNumber, (unsigned int) symbol->relative_pc,
333 fieldWidth, mapName);
Jeff Brown19b39f32011-11-21 21:10:00 -0800334 }
335}