• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 <stdlib.h>
31 #include <string.h>
32 #include <pthread.h>
33 #include <unwind.h>
34 #include <cutils/log.h>
35 #include <cutils/atomic.h>
36 
37 #define __USE_GNU // For dladdr(3) in glibc.
38 #include <dlfcn.h>
39 
40 #if defined(__BIONIC__)
41 
42 // Bionic implements and exports gettid but only implements tgkill.
43 extern int tgkill(int tgid, int tid, int sig);
44 
45 #elif defined(__APPLE__)
46 
47 #include <sys/syscall.h>
48 
49 // Mac OS >= 10.6 has a system call equivalent to Linux's gettid().
gettid()50 static pid_t gettid() {
51   return syscall(SYS_thread_selfid);
52 }
53 
54 #else
55 
56 // glibc doesn't implement or export either gettid or tgkill.
57 
58 #include <unistd.h>
59 #include <sys/syscall.h>
60 
gettid()61 static pid_t gettid() {
62   return syscall(__NR_gettid);
63 }
64 
tgkill(int tgid,int tid,int sig)65 static int tgkill(int tgid, int tid, int sig) {
66   return syscall(__NR_tgkill, tgid, tid, sig);
67 }
68 
69 #endif
70 
71 typedef 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;
77     memory_t memory;
78 } backtrace_state_t;
79 
unwind_backtrace_callback(struct _Unwind_Context * context,void * arg)80 static _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.
88         add_backtrace_entry(rewind_pc_arch(&state->memory, pc), state->backtrace,
89                 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 
unwind_backtrace(backtrace_frame_t * backtrace,size_t ignore_depth,size_t max_depth)95 ssize_t unwind_backtrace(backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) {
96     ALOGV("Unwinding current thread %d.", gettid());
97 
98     map_info_t* milist = acquire_my_map_info_list();
99 
100     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;
106     init_memory(&state.memory, milist);
107 
108     _Unwind_Reason_Code rc = _Unwind_Backtrace(unwind_backtrace_callback, &state);
109 
110     release_my_map_info_list(milist);
111 
112     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
119 static const int32_t STATE_DUMPING = -1;
120 static const int32_t STATE_DONE = -2;
121 static const int32_t STATE_CANCEL = -3;
122 
123 static pthread_mutex_t g_unwind_signal_mutex = PTHREAD_MUTEX_INITIALIZER;
124 static volatile struct {
125     int32_t tid_state;
126     const map_info_t* map_info_list;
127     backtrace_frame_t* backtrace;
128     size_t ignore_depth;
129     size_t max_depth;
130     size_t returned_frames;
131 } g_unwind_signal_state;
132 
unwind_backtrace_thread_signal_handler(int n,siginfo_t * siginfo,void * sigcontext)133 static void unwind_backtrace_thread_signal_handler(int n __attribute__((unused)), siginfo_t* siginfo, void* sigcontext) {
134     if (!android_atomic_acquire_cas(gettid(), STATE_DUMPING, &g_unwind_signal_state.tid_state)) {
135         g_unwind_signal_state.returned_frames = unwind_backtrace_signal_arch(
136                 siginfo, sigcontext,
137                 g_unwind_signal_state.map_info_list,
138                 g_unwind_signal_state.backtrace,
139                 g_unwind_signal_state.ignore_depth,
140                 g_unwind_signal_state.max_depth);
141         android_atomic_release_store(STATE_DONE, &g_unwind_signal_state.tid_state);
142     } else {
143         ALOGV("Received spurious SIGURG on thread %d that was intended for thread %d.",
144                 gettid(), android_atomic_acquire_load(&g_unwind_signal_state.tid_state));
145     }
146 }
147 #endif
148 
unwind_backtrace_thread(pid_t tid,backtrace_frame_t * backtrace,size_t ignore_depth,size_t max_depth)149 ssize_t unwind_backtrace_thread(pid_t tid, backtrace_frame_t* backtrace,
150         size_t ignore_depth, size_t max_depth) {
151     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 
157     // 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__)
160     struct sigaction act;
161     struct sigaction oact;
162     memset(&act, 0, sizeof(act));
163     act.sa_sigaction = unwind_backtrace_thread_signal_handler;
164     act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
165     sigemptyset(&act.sa_mask);
166 
167     pthread_mutex_lock(&g_unwind_signal_mutex);
168     map_info_t* milist = acquire_my_map_info_list();
169 
170     ssize_t frames = -1;
171     if (!sigaction(SIGURG, &act, &oact)) {
172         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;
177         android_atomic_release_store(tid, &g_unwind_signal_state.tid_state);
178 
179         // Signal the specific thread that we want to dump.
180         int32_t tid_state = tid;
181         if (tgkill(getpid(), tid, SIGURG)) {
182             ALOGV("Failed to send SIGURG to thread %d.", tid);
183         } else {
184             // 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                 }
198             }
199         }
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) {
222             frames = g_unwind_signal_state.returned_frames;
223         }
224 
225         sigaction(SIGURG, &oact, NULL);
226     }
227 
228     release_my_map_info_list(milist);
229     pthread_mutex_unlock(&g_unwind_signal_mutex);
230     return frames;
231 #else
232     return -1;
233 #endif
234 }
235 
unwind_backtrace_ptrace(pid_t tid,const ptrace_context_t * context,backtrace_frame_t * backtrace,size_t ignore_depth,size_t max_depth)236 ssize_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 
init_backtrace_symbol(backtrace_symbol_t * symbol,uintptr_t pc)245 static void init_backtrace_symbol(backtrace_symbol_t* symbol, uintptr_t pc) {
246     symbol->relative_pc = pc;
247     symbol->relative_symbol_addr = 0;
248     symbol->map_name = NULL;
249     symbol->symbol_name = NULL;
250     symbol->demangled_name = NULL;
251 }
252 
get_backtrace_symbols(const backtrace_frame_t * backtrace,size_t frames,backtrace_symbol_t * backtrace_symbols)253 void get_backtrace_symbols(const backtrace_frame_t* backtrace, size_t frames,
254         backtrace_symbol_t* backtrace_symbols) {
255     map_info_t* milist = acquire_my_map_info_list();
256     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;
264             if (mi->name[0]) {
265                 symbol->map_name = strdup(mi->name);
266             }
267             Dl_info info;
268             if (dladdr((const void*)frame->absolute_pc, &info) && info.dli_sname) {
269                 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);
273             }
274         }
275     }
276     release_my_map_info_list(milist);
277 }
278 
get_backtrace_symbols_ptrace(const ptrace_context_t * context,const backtrace_frame_t * backtrace,size_t frames,backtrace_symbol_t * backtrace_symbols)279 void 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;
292             if (mi->name[0]) {
293                 symbol->map_name = strdup(mi->name);
294             }
295         }
296         if (s) {
297             symbol->relative_symbol_addr = s->start;
298             symbol->symbol_name = strdup(s->name);
299             symbol->demangled_name = demangle_symbol_name(symbol->symbol_name);
300         }
301     }
302 }
303 
free_backtrace_symbols(backtrace_symbol_t * backtrace_symbols,size_t frames)304 void 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];
307         free(symbol->map_name);
308         free(symbol->symbol_name);
309         free(symbol->demangled_name);
310         init_backtrace_symbol(symbol, 0);
311     }
312 }
313 
format_backtrace_line(unsigned frameNumber,const backtrace_frame_t * frame,const backtrace_symbol_t * symbol,char * buffer,size_t bufferSize)314 void format_backtrace_line(unsigned frameNumber, const backtrace_frame_t* frame __attribute__((unused)),
315         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;
318     int fieldWidth = (bufferSize - 80) / 2;
319     if (symbolName) {
320         uint32_t pc_offset = symbol->relative_pc - symbol->relative_symbol_addr;
321         if (pc_offset) {
322             snprintf(buffer, bufferSize, "#%02u  pc %08x  %.*s (%.*s+%u)",
323                     frameNumber, (unsigned int) symbol->relative_pc,
324                     fieldWidth, mapName, fieldWidth, symbolName, pc_offset);
325         } else {
326             snprintf(buffer, bufferSize, "#%02u  pc %08x  %.*s (%.*s)",
327                     frameNumber, (unsigned int) symbol->relative_pc,
328                     fieldWidth, mapName, fieldWidth, symbolName);
329         }
330     } else {
331         snprintf(buffer, bufferSize, "#%02u  pc %08x  %.*s",
332                 frameNumber, (unsigned int) symbol->relative_pc,
333                 fieldWidth, mapName);
334     }
335 }
336