backtrace.c revision 19b39f371be5250e7b9e88016be1e5e665367b3f
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 <pthread.h>
31#include <unwind.h>
32#include <sys/exec_elf.h>
33#include <cutils/log.h>
34#include <cutils/atomic.h>
35
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;
46    memory_t memory;
47} 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.
57        add_backtrace_entry(rewind_pc_arch(&state->memory, pc), state->backtrace,
58                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) {
65    ALOGV("Unwinding current thread %d.", gettid());
66
67    map_info_t* milist = acquire_my_map_info_list();
68
69    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;
75    init_memory(&state.memory, milist);
76
77    _Unwind_Reason_Code rc =_Unwind_Backtrace(unwind_backtrace_callback, &state);
78
79    release_my_map_info_list(milist);
80
81    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
88static const int32_t STATE_DUMPING = -1;
89static const int32_t STATE_DONE = -2;
90static const int32_t STATE_CANCEL = -3;
91
92static pthread_mutex_t g_unwind_signal_mutex = PTHREAD_MUTEX_INITIALIZER;
93static volatile struct {
94    int32_t tid_state;
95    const map_info_t* map_info_list;
96    backtrace_frame_t* backtrace;
97    size_t ignore_depth;
98    size_t max_depth;
99    size_t returned_frames;
100} g_unwind_signal_state;
101
102static void unwind_backtrace_thread_signal_handler(int n, siginfo_t* siginfo, void* sigcontext) {
103    if (!android_atomic_acquire_cas(gettid(), STATE_DUMPING, &g_unwind_signal_state.tid_state)) {
104        g_unwind_signal_state.returned_frames = unwind_backtrace_signal_arch(
105                siginfo, sigcontext,
106                g_unwind_signal_state.map_info_list,
107                g_unwind_signal_state.backtrace,
108                g_unwind_signal_state.ignore_depth,
109                g_unwind_signal_state.max_depth);
110        android_atomic_release_store(STATE_DONE, &g_unwind_signal_state.tid_state);
111    } else {
112        ALOGV("Received spurious SIGURG on thread %d that was intended for thread %d.",
113                gettid(), android_atomic_acquire_load(&g_unwind_signal_state.tid_state));
114    }
115}
116#endif
117
118extern int tgkill(int tgid, int tid, int sig);
119
120ssize_t unwind_backtrace_thread(pid_t tid, backtrace_frame_t* backtrace,
121        size_t ignore_depth, size_t max_depth) {
122    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
128#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;
133    act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
134    sigemptyset(&act.sa_mask);
135
136    pthread_mutex_lock(&g_unwind_signal_mutex);
137    map_info_t* milist = acquire_my_map_info_list();
138
139    ssize_t frames = -1;
140    if (!sigaction(SIGURG, &act, &oact)) {
141        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;
146        android_atomic_release_store(tid, &g_unwind_signal_state.tid_state);
147
148        // Signal the specific thread that we want to dump.
149        int32_t tid_state = tid;
150        if (tgkill(getpid(), tid, SIGURG)) {
151            ALOGV("Failed to send SIGURG to thread %d.", tid);
152        } else {
153            // 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                }
167            }
168        }
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) {
191            frames = g_unwind_signal_state.returned_frames;
192        }
193
194        sigaction(SIGURG, &oact, NULL);
195    }
196
197    release_my_map_info_list(milist);
198    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;
216    symbol->relative_symbol_addr = 0;
217    symbol->map_name = NULL;
218    symbol->symbol_name = NULL;
219    symbol->demangled_name = NULL;
220}
221
222void get_backtrace_symbols(const backtrace_frame_t* backtrace, size_t frames,
223        backtrace_symbol_t* backtrace_symbols) {
224    map_info_t* milist = acquire_my_map_info_list();
225    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;
233            if (mi->name[0]) {
234                symbol->map_name = strdup(mi->name);
235            }
236#if HAVE_DLADDR
237            Dl_info info;
238            if (dladdr((const void*)frame->absolute_pc, &info) && info.dli_sname) {
239                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);
243            }
244#endif
245        }
246    }
247    release_my_map_info_list(milist);
248}
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;
263            if (mi->name[0]) {
264                symbol->map_name = strdup(mi->name);
265            }
266        }
267        if (s) {
268            symbol->relative_symbol_addr = s->start;
269            symbol->symbol_name = strdup(s->name);
270            symbol->demangled_name = demangle_symbol_name(symbol->symbol_name);
271        }
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];
278        free(symbol->map_name);
279        free(symbol->symbol_name);
280        free(symbol->demangled_name);
281        init_backtrace_symbol(symbol, 0);
282    }
283}
284
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}
306