blob: 03dbd53c42c82e1433ed651fd9721088d4dfcf38 [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>
Elliott Hughes71363a82012-05-18 11:56:17 -070036#include <elf.h>
Jeff Brown501edd22011-10-19 20:35:35 -070037
Elliott Hughes71363a82012-05-18 11:56:17 -070038#define __USE_GNU // For dladdr(3) in glibc.
Jeff Brown501edd22011-10-19 20:35:35 -070039#include <dlfcn.h>
Jeff Brown501edd22011-10-19 20:35:35 -070040
Elliott Hughes71363a82012-05-18 11:56:17 -070041#if defined(__BIONIC__)
42
43// Bionic implements and exports gettid but only implements tgkill.
44extern int tgkill(int tgid, int tid, int sig);
45
46#else
47
48// glibc doesn't implement or export either gettid or tgkill.
49
50#include <unistd.h>
51#include <sys/syscall.h>
52
53static pid_t gettid() {
54 return syscall(__NR_gettid);
55}
56
57static int tgkill(int tgid, int tid, int sig) {
58 return syscall(__NR_tgkill, tgid, tid, sig);
59}
60
61#endif
62
Jeff Brown501edd22011-10-19 20:35:35 -070063typedef struct {
64 backtrace_frame_t* backtrace;
65 size_t ignore_depth;
66 size_t max_depth;
67 size_t ignored_frames;
68 size_t returned_frames;
Jeff Brownf0c58722011-11-03 17:58:44 -070069 memory_t memory;
Jeff Brown501edd22011-10-19 20:35:35 -070070} backtrace_state_t;
71
72static _Unwind_Reason_Code unwind_backtrace_callback(struct _Unwind_Context* context, void* arg) {
73 backtrace_state_t* state = (backtrace_state_t*)arg;
74 uintptr_t pc = _Unwind_GetIP(context);
75 if (pc) {
76 // TODO: Get information about the stack layout from the _Unwind_Context.
77 // This will require a new architecture-specific function to query
78 // the appropriate registers. Current callers of unwind_backtrace
79 // don't need this information, so we won't bother collecting it just yet.
Jeff Brownf0c58722011-11-03 17:58:44 -070080 add_backtrace_entry(rewind_pc_arch(&state->memory, pc), state->backtrace,
Jeff Brown501edd22011-10-19 20:35:35 -070081 state->ignore_depth, state->max_depth,
82 &state->ignored_frames, &state->returned_frames);
83 }
84 return state->returned_frames < state->max_depth ? _URC_NO_REASON : _URC_END_OF_STACK;
85}
86
87ssize_t unwind_backtrace(backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) {
Jeff Brownf0c58722011-11-03 17:58:44 -070088 ALOGV("Unwinding current thread %d.", gettid());
89
90 map_info_t* milist = acquire_my_map_info_list();
91
Jeff Brown501edd22011-10-19 20:35:35 -070092 backtrace_state_t state;
93 state.backtrace = backtrace;
94 state.ignore_depth = ignore_depth;
95 state.max_depth = max_depth;
96 state.ignored_frames = 0;
97 state.returned_frames = 0;
Jeff Brownf0c58722011-11-03 17:58:44 -070098 init_memory(&state.memory, milist);
Jeff Brown501edd22011-10-19 20:35:35 -070099
100 _Unwind_Reason_Code rc =_Unwind_Backtrace(unwind_backtrace_callback, &state);
Jeff Brownf0c58722011-11-03 17:58:44 -0700101
102 release_my_map_info_list(milist);
103
Jeff Brown501edd22011-10-19 20:35:35 -0700104 if (state.returned_frames) {
105 return state.returned_frames;
106 }
107 return rc == _URC_END_OF_STACK ? 0 : -1;
108}
109
110#ifdef CORKSCREW_HAVE_ARCH
Jeff Brown67754562011-11-18 15:34:35 -0800111static const int32_t STATE_DUMPING = -1;
112static const int32_t STATE_DONE = -2;
113static const int32_t STATE_CANCEL = -3;
114
Jeff Brown501edd22011-10-19 20:35:35 -0700115static pthread_mutex_t g_unwind_signal_mutex = PTHREAD_MUTEX_INITIALIZER;
116static volatile struct {
Jeff Brown67754562011-11-18 15:34:35 -0800117 int32_t tid_state;
Jeff Brownf0c58722011-11-03 17:58:44 -0700118 const map_info_t* map_info_list;
Jeff Brown501edd22011-10-19 20:35:35 -0700119 backtrace_frame_t* backtrace;
120 size_t ignore_depth;
121 size_t max_depth;
122 size_t returned_frames;
Jeff Brown501edd22011-10-19 20:35:35 -0700123} g_unwind_signal_state;
124
Edwin Vane46beebe2012-07-26 14:18:23 -0400125static void unwind_backtrace_thread_signal_handler(int n __attribute__((unused)), siginfo_t* siginfo, void* sigcontext) {
Jeff Brown67754562011-11-18 15:34:35 -0800126 if (!android_atomic_acquire_cas(gettid(), STATE_DUMPING, &g_unwind_signal_state.tid_state)) {
Jeff Brown501edd22011-10-19 20:35:35 -0700127 g_unwind_signal_state.returned_frames = unwind_backtrace_signal_arch(
Jeff Brownf0c58722011-11-03 17:58:44 -0700128 siginfo, sigcontext,
129 g_unwind_signal_state.map_info_list,
130 g_unwind_signal_state.backtrace,
Jeff Brown501edd22011-10-19 20:35:35 -0700131 g_unwind_signal_state.ignore_depth,
132 g_unwind_signal_state.max_depth);
Jeff Brown67754562011-11-18 15:34:35 -0800133 android_atomic_release_store(STATE_DONE, &g_unwind_signal_state.tid_state);
Jeff Brownf0c58722011-11-03 17:58:44 -0700134 } else {
135 ALOGV("Received spurious SIGURG on thread %d that was intended for thread %d.",
Jeff Brown67754562011-11-18 15:34:35 -0800136 gettid(), android_atomic_acquire_load(&g_unwind_signal_state.tid_state));
Jeff Brown501edd22011-10-19 20:35:35 -0700137 }
138}
139#endif
140
141ssize_t unwind_backtrace_thread(pid_t tid, backtrace_frame_t* backtrace,
142 size_t ignore_depth, size_t max_depth) {
Jeff Brownf0c58722011-11-03 17:58:44 -0700143 if (tid == gettid()) {
144 return unwind_backtrace(backtrace, ignore_depth + 1, max_depth);
145 }
146
147 ALOGV("Unwinding thread %d from thread %d.", tid, gettid());
148
Jeff Brown501edd22011-10-19 20:35:35 -0700149#ifdef CORKSCREW_HAVE_ARCH
150 struct sigaction act;
151 struct sigaction oact;
152 memset(&act, 0, sizeof(act));
153 act.sa_sigaction = unwind_backtrace_thread_signal_handler;
Jeff Brown67754562011-11-18 15:34:35 -0800154 act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
Jeff Brown501edd22011-10-19 20:35:35 -0700155 sigemptyset(&act.sa_mask);
156
157 pthread_mutex_lock(&g_unwind_signal_mutex);
Jeff Brownf0c58722011-11-03 17:58:44 -0700158 map_info_t* milist = acquire_my_map_info_list();
Jeff Brown501edd22011-10-19 20:35:35 -0700159
160 ssize_t frames = -1;
161 if (!sigaction(SIGURG, &act, &oact)) {
Jeff Brownf0c58722011-11-03 17:58:44 -0700162 g_unwind_signal_state.map_info_list = milist;
163 g_unwind_signal_state.backtrace = backtrace;
164 g_unwind_signal_state.ignore_depth = ignore_depth;
165 g_unwind_signal_state.max_depth = max_depth;
166 g_unwind_signal_state.returned_frames = 0;
Jeff Brown67754562011-11-18 15:34:35 -0800167 android_atomic_release_store(tid, &g_unwind_signal_state.tid_state);
Jeff Brownf0c58722011-11-03 17:58:44 -0700168
Jeff Brown67754562011-11-18 15:34:35 -0800169 // Signal the specific thread that we want to dump.
170 int32_t tid_state = tid;
171 if (tgkill(getpid(), tid, SIGURG)) {
Jeff Brownf0c58722011-11-03 17:58:44 -0700172 ALOGV("Failed to send SIGURG to thread %d.", tid);
Jeff Brownf0c58722011-11-03 17:58:44 -0700173 } else {
Jeff Brown67754562011-11-18 15:34:35 -0800174 // Wait for the other thread to start dumping the stack, or time out.
175 int wait_millis = 250;
176 for (;;) {
177 tid_state = android_atomic_acquire_load(&g_unwind_signal_state.tid_state);
178 if (tid_state != tid) {
179 break;
180 }
181 if (wait_millis--) {
182 ALOGV("Waiting for thread %d to start dumping the stack...", tid);
183 usleep(1000);
184 } else {
185 ALOGV("Timed out waiting for thread %d to start dumping the stack.", tid);
186 break;
187 }
Jeff Brown501edd22011-10-19 20:35:35 -0700188 }
Jeff Brown67754562011-11-18 15:34:35 -0800189 }
190
191 // Try to cancel the dump if it has not started yet.
192 if (tid_state == tid) {
193 if (!android_atomic_acquire_cas(tid, STATE_CANCEL, &g_unwind_signal_state.tid_state)) {
194 ALOGV("Canceled thread %d stack dump.", tid);
195 tid_state = STATE_CANCEL;
196 } else {
197 tid_state = android_atomic_acquire_load(&g_unwind_signal_state.tid_state);
198 }
199 }
200
201 // Wait indefinitely for the dump to finish or be canceled.
202 // We cannot apply a timeout here because the other thread is accessing state that
203 // is owned by this thread, such as milist. It should not take very
204 // long to take the dump once started.
205 while (tid_state == STATE_DUMPING) {
206 ALOGV("Waiting for thread %d to finish dumping the stack...", tid);
207 usleep(1000);
208 tid_state = android_atomic_acquire_load(&g_unwind_signal_state.tid_state);
209 }
210
211 if (tid_state == STATE_DONE) {
Jeff Brown501edd22011-10-19 20:35:35 -0700212 frames = g_unwind_signal_state.returned_frames;
213 }
Jeff Brownf0c58722011-11-03 17:58:44 -0700214
Jeff Brown501edd22011-10-19 20:35:35 -0700215 sigaction(SIGURG, &oact, NULL);
216 }
217
Jeff Brownf0c58722011-11-03 17:58:44 -0700218 release_my_map_info_list(milist);
Jeff Brown501edd22011-10-19 20:35:35 -0700219 pthread_mutex_unlock(&g_unwind_signal_mutex);
220 return frames;
221#else
222 return -1;
223#endif
224}
225
226ssize_t unwind_backtrace_ptrace(pid_t tid, const ptrace_context_t* context,
227 backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) {
228#ifdef CORKSCREW_HAVE_ARCH
229 return unwind_backtrace_ptrace_arch(tid, context, backtrace, ignore_depth, max_depth);
230#else
231 return -1;
232#endif
233}
234
235static void init_backtrace_symbol(backtrace_symbol_t* symbol, uintptr_t pc) {
236 symbol->relative_pc = pc;
Jeff Brown19b39f32011-11-21 21:10:00 -0800237 symbol->relative_symbol_addr = 0;
Jeff Brownf0c58722011-11-03 17:58:44 -0700238 symbol->map_name = NULL;
Jeff Brown19b39f32011-11-21 21:10:00 -0800239 symbol->symbol_name = NULL;
Jeff Brown501edd22011-10-19 20:35:35 -0700240 symbol->demangled_name = NULL;
241}
242
243void get_backtrace_symbols(const backtrace_frame_t* backtrace, size_t frames,
244 backtrace_symbol_t* backtrace_symbols) {
Jeff Brownf0c58722011-11-03 17:58:44 -0700245 map_info_t* milist = acquire_my_map_info_list();
Jeff Brown501edd22011-10-19 20:35:35 -0700246 for (size_t i = 0; i < frames; i++) {
247 const backtrace_frame_t* frame = &backtrace[i];
248 backtrace_symbol_t* symbol = &backtrace_symbols[i];
249 init_backtrace_symbol(symbol, frame->absolute_pc);
250
251 const map_info_t* mi = find_map_info(milist, frame->absolute_pc);
252 if (mi) {
253 symbol->relative_pc = frame->absolute_pc - mi->start;
Jeff Brownf0c58722011-11-03 17:58:44 -0700254 if (mi->name[0]) {
255 symbol->map_name = strdup(mi->name);
256 }
Jeff Brown501edd22011-10-19 20:35:35 -0700257 Dl_info info;
258 if (dladdr((const void*)frame->absolute_pc, &info) && info.dli_sname) {
Jeff Brown19b39f32011-11-21 21:10:00 -0800259 symbol->relative_symbol_addr = (uintptr_t)info.dli_saddr
260 - (uintptr_t)info.dli_fbase;
261 symbol->symbol_name = strdup(info.dli_sname);
262 symbol->demangled_name = demangle_symbol_name(symbol->symbol_name);
Jeff Brown501edd22011-10-19 20:35:35 -0700263 }
Jeff Brown501edd22011-10-19 20:35:35 -0700264 }
265 }
Jeff Brownf0c58722011-11-03 17:58:44 -0700266 release_my_map_info_list(milist);
Jeff Brown501edd22011-10-19 20:35:35 -0700267}
268
269void get_backtrace_symbols_ptrace(const ptrace_context_t* context,
270 const backtrace_frame_t* backtrace, size_t frames,
271 backtrace_symbol_t* backtrace_symbols) {
272 for (size_t i = 0; i < frames; i++) {
273 const backtrace_frame_t* frame = &backtrace[i];
274 backtrace_symbol_t* symbol = &backtrace_symbols[i];
275 init_backtrace_symbol(symbol, frame->absolute_pc);
276
277 const map_info_t* mi;
278 const symbol_t* s;
279 find_symbol_ptrace(context, frame->absolute_pc, &mi, &s);
280 if (mi) {
281 symbol->relative_pc = frame->absolute_pc - mi->start;
Jeff Brownf0c58722011-11-03 17:58:44 -0700282 if (mi->name[0]) {
283 symbol->map_name = strdup(mi->name);
284 }
Jeff Brown501edd22011-10-19 20:35:35 -0700285 }
286 if (s) {
Jeff Brown19b39f32011-11-21 21:10:00 -0800287 symbol->relative_symbol_addr = s->start;
288 symbol->symbol_name = strdup(s->name);
289 symbol->demangled_name = demangle_symbol_name(symbol->symbol_name);
Jeff Brown501edd22011-10-19 20:35:35 -0700290 }
291 }
292}
293
294void free_backtrace_symbols(backtrace_symbol_t* backtrace_symbols, size_t frames) {
295 for (size_t i = 0; i < frames; i++) {
296 backtrace_symbol_t* symbol = &backtrace_symbols[i];
Jeff Brownf0c58722011-11-03 17:58:44 -0700297 free(symbol->map_name);
Jeff Brown19b39f32011-11-21 21:10:00 -0800298 free(symbol->symbol_name);
Jeff Brown501edd22011-10-19 20:35:35 -0700299 free(symbol->demangled_name);
300 init_backtrace_symbol(symbol, 0);
301 }
302}
Jeff Brown19b39f32011-11-21 21:10:00 -0800303
Edwin Vane46beebe2012-07-26 14:18:23 -0400304void format_backtrace_line(unsigned frameNumber, const backtrace_frame_t* frame __attribute__((unused)),
Jeff Brown19b39f32011-11-21 21:10:00 -0800305 const backtrace_symbol_t* symbol, char* buffer, size_t bufferSize) {
306 const char* mapName = symbol->map_name ? symbol->map_name : "<unknown>";
307 const char* symbolName = symbol->demangled_name ? symbol->demangled_name : symbol->symbol_name;
308 size_t fieldWidth = (bufferSize - 80) / 2;
309 if (symbolName) {
310 uint32_t pc_offset = symbol->relative_pc - symbol->relative_symbol_addr;
311 if (pc_offset) {
312 snprintf(buffer, bufferSize, "#%02d pc %08x %.*s (%.*s+%u)",
313 frameNumber, symbol->relative_pc, fieldWidth, mapName,
314 fieldWidth, symbolName, pc_offset);
315 } else {
316 snprintf(buffer, bufferSize, "#%02d pc %08x %.*s (%.*s)",
317 frameNumber, symbol->relative_pc, fieldWidth, mapName,
318 fieldWidth, symbolName);
319 }
320 } else {
321 snprintf(buffer, bufferSize, "#%02d pc %08x %.*s",
322 frameNumber, symbol->relative_pc, fieldWidth, mapName);
323 }
324}