blob: fa215742b7afd166145704dd2fd13496e3d6e28f [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>
30#include <pthread.h>
31#include <unwind.h>
32#include <sys/exec_elf.h>
33#include <cutils/log.h>
Jeff Brownf0c58722011-11-03 17:58:44 -070034#include <cutils/atomic.h>
Jeff Brown501edd22011-10-19 20:35:35 -070035
36#if HAVE_DLADDR
37#include <dlfcn.h>
38#endif
39
40typedef struct {
41 backtrace_frame_t* backtrace;
42 size_t ignore_depth;
43 size_t max_depth;
44 size_t ignored_frames;
45 size_t returned_frames;
Jeff Brownf0c58722011-11-03 17:58:44 -070046 memory_t memory;
Jeff Brown501edd22011-10-19 20:35:35 -070047} backtrace_state_t;
48
49static _Unwind_Reason_Code unwind_backtrace_callback(struct _Unwind_Context* context, void* arg) {
50 backtrace_state_t* state = (backtrace_state_t*)arg;
51 uintptr_t pc = _Unwind_GetIP(context);
52 if (pc) {
53 // TODO: Get information about the stack layout from the _Unwind_Context.
54 // This will require a new architecture-specific function to query
55 // the appropriate registers. Current callers of unwind_backtrace
56 // don't need this information, so we won't bother collecting it just yet.
Jeff Brownf0c58722011-11-03 17:58:44 -070057 add_backtrace_entry(rewind_pc_arch(&state->memory, pc), state->backtrace,
Jeff Brown501edd22011-10-19 20:35:35 -070058 state->ignore_depth, state->max_depth,
59 &state->ignored_frames, &state->returned_frames);
60 }
61 return state->returned_frames < state->max_depth ? _URC_NO_REASON : _URC_END_OF_STACK;
62}
63
64ssize_t unwind_backtrace(backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) {
Jeff Brownf0c58722011-11-03 17:58:44 -070065 ALOGV("Unwinding current thread %d.", gettid());
66
67 map_info_t* milist = acquire_my_map_info_list();
68
Jeff Brown501edd22011-10-19 20:35:35 -070069 backtrace_state_t state;
70 state.backtrace = backtrace;
71 state.ignore_depth = ignore_depth;
72 state.max_depth = max_depth;
73 state.ignored_frames = 0;
74 state.returned_frames = 0;
Jeff Brownf0c58722011-11-03 17:58:44 -070075 init_memory(&state.memory, milist);
Jeff Brown501edd22011-10-19 20:35:35 -070076
77 _Unwind_Reason_Code rc =_Unwind_Backtrace(unwind_backtrace_callback, &state);
Jeff Brownf0c58722011-11-03 17:58:44 -070078
79 release_my_map_info_list(milist);
80
Jeff Brown501edd22011-10-19 20:35:35 -070081 if (state.returned_frames) {
82 return state.returned_frames;
83 }
84 return rc == _URC_END_OF_STACK ? 0 : -1;
85}
86
87#ifdef CORKSCREW_HAVE_ARCH
Jeff Brown67754562011-11-18 15:34:35 -080088static const int32_t STATE_DUMPING = -1;
89static const int32_t STATE_DONE = -2;
90static const int32_t STATE_CANCEL = -3;
91
Jeff Brown501edd22011-10-19 20:35:35 -070092static pthread_mutex_t g_unwind_signal_mutex = PTHREAD_MUTEX_INITIALIZER;
93static volatile struct {
Jeff Brown67754562011-11-18 15:34:35 -080094 int32_t tid_state;
Jeff Brownf0c58722011-11-03 17:58:44 -070095 const map_info_t* map_info_list;
Jeff Brown501edd22011-10-19 20:35:35 -070096 backtrace_frame_t* backtrace;
97 size_t ignore_depth;
98 size_t max_depth;
99 size_t returned_frames;
Jeff Brown501edd22011-10-19 20:35:35 -0700100} g_unwind_signal_state;
101
102static void unwind_backtrace_thread_signal_handler(int n, siginfo_t* siginfo, void* sigcontext) {
Jeff Brown67754562011-11-18 15:34:35 -0800103 if (!android_atomic_acquire_cas(gettid(), STATE_DUMPING, &g_unwind_signal_state.tid_state)) {
Jeff Brown501edd22011-10-19 20:35:35 -0700104 g_unwind_signal_state.returned_frames = unwind_backtrace_signal_arch(
Jeff Brownf0c58722011-11-03 17:58:44 -0700105 siginfo, sigcontext,
106 g_unwind_signal_state.map_info_list,
107 g_unwind_signal_state.backtrace,
Jeff Brown501edd22011-10-19 20:35:35 -0700108 g_unwind_signal_state.ignore_depth,
109 g_unwind_signal_state.max_depth);
Jeff Brown67754562011-11-18 15:34:35 -0800110 android_atomic_release_store(STATE_DONE, &g_unwind_signal_state.tid_state);
Jeff Brownf0c58722011-11-03 17:58:44 -0700111 } else {
112 ALOGV("Received spurious SIGURG on thread %d that was intended for thread %d.",
Jeff Brown67754562011-11-18 15:34:35 -0800113 gettid(), android_atomic_acquire_load(&g_unwind_signal_state.tid_state));
Jeff Brown501edd22011-10-19 20:35:35 -0700114 }
115}
116#endif
117
Jeff Brown67754562011-11-18 15:34:35 -0800118extern int tgkill(int tgid, int tid, int sig);
119
Jeff Brown501edd22011-10-19 20:35:35 -0700120ssize_t unwind_backtrace_thread(pid_t tid, backtrace_frame_t* backtrace,
121 size_t ignore_depth, size_t max_depth) {
Jeff Brownf0c58722011-11-03 17:58:44 -0700122 if (tid == gettid()) {
123 return unwind_backtrace(backtrace, ignore_depth + 1, max_depth);
124 }
125
126 ALOGV("Unwinding thread %d from thread %d.", tid, gettid());
127
Jeff Brown501edd22011-10-19 20:35:35 -0700128#ifdef CORKSCREW_HAVE_ARCH
129 struct sigaction act;
130 struct sigaction oact;
131 memset(&act, 0, sizeof(act));
132 act.sa_sigaction = unwind_backtrace_thread_signal_handler;
Jeff Brown67754562011-11-18 15:34:35 -0800133 act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
Jeff Brown501edd22011-10-19 20:35:35 -0700134 sigemptyset(&act.sa_mask);
135
136 pthread_mutex_lock(&g_unwind_signal_mutex);
Jeff Brownf0c58722011-11-03 17:58:44 -0700137 map_info_t* milist = acquire_my_map_info_list();
Jeff Brown501edd22011-10-19 20:35:35 -0700138
139 ssize_t frames = -1;
140 if (!sigaction(SIGURG, &act, &oact)) {
Jeff Brownf0c58722011-11-03 17:58:44 -0700141 g_unwind_signal_state.map_info_list = milist;
142 g_unwind_signal_state.backtrace = backtrace;
143 g_unwind_signal_state.ignore_depth = ignore_depth;
144 g_unwind_signal_state.max_depth = max_depth;
145 g_unwind_signal_state.returned_frames = 0;
Jeff Brown67754562011-11-18 15:34:35 -0800146 android_atomic_release_store(tid, &g_unwind_signal_state.tid_state);
Jeff Brownf0c58722011-11-03 17:58:44 -0700147
Jeff Brown67754562011-11-18 15:34:35 -0800148 // Signal the specific thread that we want to dump.
149 int32_t tid_state = tid;
150 if (tgkill(getpid(), tid, SIGURG)) {
Jeff Brownf0c58722011-11-03 17:58:44 -0700151 ALOGV("Failed to send SIGURG to thread %d.", tid);
Jeff Brownf0c58722011-11-03 17:58:44 -0700152 } else {
Jeff Brown67754562011-11-18 15:34:35 -0800153 // Wait for the other thread to start dumping the stack, or time out.
154 int wait_millis = 250;
155 for (;;) {
156 tid_state = android_atomic_acquire_load(&g_unwind_signal_state.tid_state);
157 if (tid_state != tid) {
158 break;
159 }
160 if (wait_millis--) {
161 ALOGV("Waiting for thread %d to start dumping the stack...", tid);
162 usleep(1000);
163 } else {
164 ALOGV("Timed out waiting for thread %d to start dumping the stack.", tid);
165 break;
166 }
Jeff Brown501edd22011-10-19 20:35:35 -0700167 }
Jeff Brown67754562011-11-18 15:34:35 -0800168 }
169
170 // Try to cancel the dump if it has not started yet.
171 if (tid_state == tid) {
172 if (!android_atomic_acquire_cas(tid, STATE_CANCEL, &g_unwind_signal_state.tid_state)) {
173 ALOGV("Canceled thread %d stack dump.", tid);
174 tid_state = STATE_CANCEL;
175 } else {
176 tid_state = android_atomic_acquire_load(&g_unwind_signal_state.tid_state);
177 }
178 }
179
180 // Wait indefinitely for the dump to finish or be canceled.
181 // We cannot apply a timeout here because the other thread is accessing state that
182 // is owned by this thread, such as milist. It should not take very
183 // long to take the dump once started.
184 while (tid_state == STATE_DUMPING) {
185 ALOGV("Waiting for thread %d to finish dumping the stack...", tid);
186 usleep(1000);
187 tid_state = android_atomic_acquire_load(&g_unwind_signal_state.tid_state);
188 }
189
190 if (tid_state == STATE_DONE) {
Jeff Brown501edd22011-10-19 20:35:35 -0700191 frames = g_unwind_signal_state.returned_frames;
192 }
Jeff Brownf0c58722011-11-03 17:58:44 -0700193
Jeff Brown501edd22011-10-19 20:35:35 -0700194 sigaction(SIGURG, &oact, NULL);
195 }
196
Jeff Brownf0c58722011-11-03 17:58:44 -0700197 release_my_map_info_list(milist);
Jeff Brown501edd22011-10-19 20:35:35 -0700198 pthread_mutex_unlock(&g_unwind_signal_mutex);
199 return frames;
200#else
201 return -1;
202#endif
203}
204
205ssize_t unwind_backtrace_ptrace(pid_t tid, const ptrace_context_t* context,
206 backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) {
207#ifdef CORKSCREW_HAVE_ARCH
208 return unwind_backtrace_ptrace_arch(tid, context, backtrace, ignore_depth, max_depth);
209#else
210 return -1;
211#endif
212}
213
214static void init_backtrace_symbol(backtrace_symbol_t* symbol, uintptr_t pc) {
215 symbol->relative_pc = pc;
Jeff Brown19b39f32011-11-21 21:10:00 -0800216 symbol->relative_symbol_addr = 0;
Jeff Brownf0c58722011-11-03 17:58:44 -0700217 symbol->map_name = NULL;
Jeff Brown19b39f32011-11-21 21:10:00 -0800218 symbol->symbol_name = NULL;
Jeff Brown501edd22011-10-19 20:35:35 -0700219 symbol->demangled_name = NULL;
220}
221
222void get_backtrace_symbols(const backtrace_frame_t* backtrace, size_t frames,
223 backtrace_symbol_t* backtrace_symbols) {
Jeff Brownf0c58722011-11-03 17:58:44 -0700224 map_info_t* milist = acquire_my_map_info_list();
Jeff Brown501edd22011-10-19 20:35:35 -0700225 for (size_t i = 0; i < frames; i++) {
226 const backtrace_frame_t* frame = &backtrace[i];
227 backtrace_symbol_t* symbol = &backtrace_symbols[i];
228 init_backtrace_symbol(symbol, frame->absolute_pc);
229
230 const map_info_t* mi = find_map_info(milist, frame->absolute_pc);
231 if (mi) {
232 symbol->relative_pc = frame->absolute_pc - mi->start;
Jeff Brownf0c58722011-11-03 17:58:44 -0700233 if (mi->name[0]) {
234 symbol->map_name = strdup(mi->name);
235 }
Jeff Brown501edd22011-10-19 20:35:35 -0700236#if HAVE_DLADDR
237 Dl_info info;
238 if (dladdr((const void*)frame->absolute_pc, &info) && info.dli_sname) {
Jeff Brown19b39f32011-11-21 21:10:00 -0800239 symbol->relative_symbol_addr = (uintptr_t)info.dli_saddr
240 - (uintptr_t)info.dli_fbase;
241 symbol->symbol_name = strdup(info.dli_sname);
242 symbol->demangled_name = demangle_symbol_name(symbol->symbol_name);
Jeff Brown501edd22011-10-19 20:35:35 -0700243 }
244#endif
245 }
246 }
Jeff Brownf0c58722011-11-03 17:58:44 -0700247 release_my_map_info_list(milist);
Jeff Brown501edd22011-10-19 20:35:35 -0700248}
249
250void get_backtrace_symbols_ptrace(const ptrace_context_t* context,
251 const backtrace_frame_t* backtrace, size_t frames,
252 backtrace_symbol_t* backtrace_symbols) {
253 for (size_t i = 0; i < frames; i++) {
254 const backtrace_frame_t* frame = &backtrace[i];
255 backtrace_symbol_t* symbol = &backtrace_symbols[i];
256 init_backtrace_symbol(symbol, frame->absolute_pc);
257
258 const map_info_t* mi;
259 const symbol_t* s;
260 find_symbol_ptrace(context, frame->absolute_pc, &mi, &s);
261 if (mi) {
262 symbol->relative_pc = frame->absolute_pc - mi->start;
Jeff Brownf0c58722011-11-03 17:58:44 -0700263 if (mi->name[0]) {
264 symbol->map_name = strdup(mi->name);
265 }
Jeff Brown501edd22011-10-19 20:35:35 -0700266 }
267 if (s) {
Jeff Brown19b39f32011-11-21 21:10:00 -0800268 symbol->relative_symbol_addr = s->start;
269 symbol->symbol_name = strdup(s->name);
270 symbol->demangled_name = demangle_symbol_name(symbol->symbol_name);
Jeff Brown501edd22011-10-19 20:35:35 -0700271 }
272 }
273}
274
275void free_backtrace_symbols(backtrace_symbol_t* backtrace_symbols, size_t frames) {
276 for (size_t i = 0; i < frames; i++) {
277 backtrace_symbol_t* symbol = &backtrace_symbols[i];
Jeff Brownf0c58722011-11-03 17:58:44 -0700278 free(symbol->map_name);
Jeff Brown19b39f32011-11-21 21:10:00 -0800279 free(symbol->symbol_name);
Jeff Brown501edd22011-10-19 20:35:35 -0700280 free(symbol->demangled_name);
281 init_backtrace_symbol(symbol, 0);
282 }
283}
Jeff Brown19b39f32011-11-21 21:10:00 -0800284
285void format_backtrace_line(unsigned frameNumber, const backtrace_frame_t* frame,
286 const backtrace_symbol_t* symbol, char* buffer, size_t bufferSize) {
287 const char* mapName = symbol->map_name ? symbol->map_name : "<unknown>";
288 const char* symbolName = symbol->demangled_name ? symbol->demangled_name : symbol->symbol_name;
289 size_t fieldWidth = (bufferSize - 80) / 2;
290 if (symbolName) {
291 uint32_t pc_offset = symbol->relative_pc - symbol->relative_symbol_addr;
292 if (pc_offset) {
293 snprintf(buffer, bufferSize, "#%02d pc %08x %.*s (%.*s+%u)",
294 frameNumber, symbol->relative_pc, fieldWidth, mapName,
295 fieldWidth, symbolName, pc_offset);
296 } else {
297 snprintf(buffer, bufferSize, "#%02d pc %08x %.*s (%.*s)",
298 frameNumber, symbol->relative_pc, fieldWidth, mapName,
299 fieldWidth, symbolName);
300 }
301 } else {
302 snprintf(buffer, bufferSize, "#%02d pc %08x %.*s",
303 frameNumber, symbol->relative_pc, fieldWidth, mapName);
304 }
305}