1501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown/*
2501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown * Copyright (C) 2011 The Android Open Source Project
3501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown *
4501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown * Licensed under the Apache License, Version 2.0 (the "License");
5501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown * you may not use this file except in compliance with the License.
6501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown * You may obtain a copy of the License at
7501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown *
8501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown *      http://www.apache.org/licenses/LICENSE-2.0
9501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown *
10501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown * Unless required by applicable law or agreed to in writing, software
11501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown * distributed under the License is distributed on an "AS IS" BASIS,
12501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown * See the License for the specific language governing permissions and
14501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown * limitations under the License.
15501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown */
16501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
17501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#define LOG_TAG "Corkscrew"
18501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown//#define LOG_NDEBUG 0
19501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
20501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#include "backtrace-arch.h"
21501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#include "backtrace-helper.h"
22501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#include "ptrace-arch.h"
23501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#include <corkscrew/map_info.h>
24501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#include <corkscrew/symbol_table.h>
25501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#include <corkscrew/ptrace.h>
26501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#include <corkscrew/demangle.h>
27501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
28501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#include <unistd.h>
29501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#include <signal.h>
3071363a8075e7916c2166cbdd8a0bf5a180ed4d54Elliott Hughes#include <stdlib.h>
3171363a8075e7916c2166cbdd8a0bf5a180ed4d54Elliott Hughes#include <string.h>
32501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#include <pthread.h>
33501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#include <unwind.h>
34501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#include <cutils/log.h>
35f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown#include <cutils/atomic.h>
3671363a8075e7916c2166cbdd8a0bf5a180ed4d54Elliott Hughes#include <elf.h>
37501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
38501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#if HAVE_DLADDR
3971363a8075e7916c2166cbdd8a0bf5a180ed4d54Elliott Hughes#define __USE_GNU // For dladdr(3) in glibc.
40501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#include <dlfcn.h>
41501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#endif
42501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
4371363a8075e7916c2166cbdd8a0bf5a180ed4d54Elliott Hughes#if defined(__BIONIC__)
4471363a8075e7916c2166cbdd8a0bf5a180ed4d54Elliott Hughes
4571363a8075e7916c2166cbdd8a0bf5a180ed4d54Elliott Hughes// Bionic implements and exports gettid but only implements tgkill.
4671363a8075e7916c2166cbdd8a0bf5a180ed4d54Elliott Hughesextern int tgkill(int tgid, int tid, int sig);
4771363a8075e7916c2166cbdd8a0bf5a180ed4d54Elliott Hughes
4871363a8075e7916c2166cbdd8a0bf5a180ed4d54Elliott Hughes#else
4971363a8075e7916c2166cbdd8a0bf5a180ed4d54Elliott Hughes
5071363a8075e7916c2166cbdd8a0bf5a180ed4d54Elliott Hughes// glibc doesn't implement or export either gettid or tgkill.
5171363a8075e7916c2166cbdd8a0bf5a180ed4d54Elliott Hughes
5271363a8075e7916c2166cbdd8a0bf5a180ed4d54Elliott Hughes#include <unistd.h>
5371363a8075e7916c2166cbdd8a0bf5a180ed4d54Elliott Hughes#include <sys/syscall.h>
5471363a8075e7916c2166cbdd8a0bf5a180ed4d54Elliott Hughes
5571363a8075e7916c2166cbdd8a0bf5a180ed4d54Elliott Hughesstatic pid_t gettid() {
5671363a8075e7916c2166cbdd8a0bf5a180ed4d54Elliott Hughes  return syscall(__NR_gettid);
5771363a8075e7916c2166cbdd8a0bf5a180ed4d54Elliott Hughes}
5871363a8075e7916c2166cbdd8a0bf5a180ed4d54Elliott Hughes
5971363a8075e7916c2166cbdd8a0bf5a180ed4d54Elliott Hughesstatic int tgkill(int tgid, int tid, int sig) {
6071363a8075e7916c2166cbdd8a0bf5a180ed4d54Elliott Hughes  return syscall(__NR_tgkill, tgid, tid, sig);
6171363a8075e7916c2166cbdd8a0bf5a180ed4d54Elliott Hughes}
6271363a8075e7916c2166cbdd8a0bf5a180ed4d54Elliott Hughes
6371363a8075e7916c2166cbdd8a0bf5a180ed4d54Elliott Hughes#endif
6471363a8075e7916c2166cbdd8a0bf5a180ed4d54Elliott Hughes
65501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Browntypedef struct {
66501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    backtrace_frame_t* backtrace;
67501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    size_t ignore_depth;
68501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    size_t max_depth;
69501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    size_t ignored_frames;
70501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    size_t returned_frames;
71f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    memory_t memory;
72501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown} backtrace_state_t;
73501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
74501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brownstatic _Unwind_Reason_Code unwind_backtrace_callback(struct _Unwind_Context* context, void* arg) {
75501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    backtrace_state_t* state = (backtrace_state_t*)arg;
76501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    uintptr_t pc = _Unwind_GetIP(context);
77501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    if (pc) {
78501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        // TODO: Get information about the stack layout from the _Unwind_Context.
79501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        //       This will require a new architecture-specific function to query
80501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        //       the appropriate registers.  Current callers of unwind_backtrace
81501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        //       don't need this information, so we won't bother collecting it just yet.
82f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        add_backtrace_entry(rewind_pc_arch(&state->memory, pc), state->backtrace,
83501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown                state->ignore_depth, state->max_depth,
84501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown                &state->ignored_frames, &state->returned_frames);
85501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    }
86501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    return state->returned_frames < state->max_depth ? _URC_NO_REASON : _URC_END_OF_STACK;
87501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown}
88501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
89501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brownssize_t unwind_backtrace(backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) {
90f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    ALOGV("Unwinding current thread %d.", gettid());
91f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown
92f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    map_info_t* milist = acquire_my_map_info_list();
93f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown
94501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    backtrace_state_t state;
95501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    state.backtrace = backtrace;
96501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    state.ignore_depth = ignore_depth;
97501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    state.max_depth = max_depth;
98501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    state.ignored_frames = 0;
99501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    state.returned_frames = 0;
100f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    init_memory(&state.memory, milist);
101501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
102501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    _Unwind_Reason_Code rc =_Unwind_Backtrace(unwind_backtrace_callback, &state);
103f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown
104f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    release_my_map_info_list(milist);
105f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown
106501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    if (state.returned_frames) {
107501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        return state.returned_frames;
108501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    }
109501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    return rc == _URC_END_OF_STACK ? 0 : -1;
110501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown}
111501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
112501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#ifdef CORKSCREW_HAVE_ARCH
113677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brownstatic const int32_t STATE_DUMPING = -1;
114677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brownstatic const int32_t STATE_DONE = -2;
115677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brownstatic const int32_t STATE_CANCEL = -3;
116677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown
117501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brownstatic pthread_mutex_t g_unwind_signal_mutex = PTHREAD_MUTEX_INITIALIZER;
118501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brownstatic volatile struct {
119677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown    int32_t tid_state;
120f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    const map_info_t* map_info_list;
121501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    backtrace_frame_t* backtrace;
122501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    size_t ignore_depth;
123501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    size_t max_depth;
124501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    size_t returned_frames;
125501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown} g_unwind_signal_state;
126501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
12746beebea823878218fc0f14723829d83886af978Edwin Vanestatic void unwind_backtrace_thread_signal_handler(int n __attribute__((unused)), siginfo_t* siginfo, void* sigcontext) {
128677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown    if (!android_atomic_acquire_cas(gettid(), STATE_DUMPING, &g_unwind_signal_state.tid_state)) {
129501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        g_unwind_signal_state.returned_frames = unwind_backtrace_signal_arch(
130f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown                siginfo, sigcontext,
131f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown                g_unwind_signal_state.map_info_list,
132f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown                g_unwind_signal_state.backtrace,
133501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown                g_unwind_signal_state.ignore_depth,
134501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown                g_unwind_signal_state.max_depth);
135677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown        android_atomic_release_store(STATE_DONE, &g_unwind_signal_state.tid_state);
136f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    } else {
137f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        ALOGV("Received spurious SIGURG on thread %d that was intended for thread %d.",
138677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown                gettid(), android_atomic_acquire_load(&g_unwind_signal_state.tid_state));
139501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    }
140501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown}
141501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#endif
142501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
143501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brownssize_t unwind_backtrace_thread(pid_t tid, backtrace_frame_t* backtrace,
144501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        size_t ignore_depth, size_t max_depth) {
145f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    if (tid == gettid()) {
146f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        return unwind_backtrace(backtrace, ignore_depth + 1, max_depth);
147f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    }
148f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown
149f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    ALOGV("Unwinding thread %d from thread %d.", tid, gettid());
150f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown
151501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#ifdef CORKSCREW_HAVE_ARCH
152501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    struct sigaction act;
153501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    struct sigaction oact;
154501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    memset(&act, 0, sizeof(act));
155501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    act.sa_sigaction = unwind_backtrace_thread_signal_handler;
156677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown    act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
157501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    sigemptyset(&act.sa_mask);
158501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
159501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    pthread_mutex_lock(&g_unwind_signal_mutex);
160f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    map_info_t* milist = acquire_my_map_info_list();
161501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
162501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    ssize_t frames = -1;
163501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    if (!sigaction(SIGURG, &act, &oact)) {
164f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        g_unwind_signal_state.map_info_list = milist;
165f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        g_unwind_signal_state.backtrace = backtrace;
166f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        g_unwind_signal_state.ignore_depth = ignore_depth;
167f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        g_unwind_signal_state.max_depth = max_depth;
168f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        g_unwind_signal_state.returned_frames = 0;
169677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown        android_atomic_release_store(tid, &g_unwind_signal_state.tid_state);
170f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown
171677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown        // Signal the specific thread that we want to dump.
172677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown        int32_t tid_state = tid;
173677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown        if (tgkill(getpid(), tid, SIGURG)) {
174f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown            ALOGV("Failed to send SIGURG to thread %d.", tid);
175f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        } else {
176677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown            // Wait for the other thread to start dumping the stack, or time out.
177677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown            int wait_millis = 250;
178677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown            for (;;) {
179677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown                tid_state = android_atomic_acquire_load(&g_unwind_signal_state.tid_state);
180677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown                if (tid_state != tid) {
181677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown                    break;
182677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown                }
183677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown                if (wait_millis--) {
184677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown                    ALOGV("Waiting for thread %d to start dumping the stack...", tid);
185677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown                    usleep(1000);
186677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown                } else {
187677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown                    ALOGV("Timed out waiting for thread %d to start dumping the stack.", tid);
188677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown                    break;
189677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown                }
190677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown            }
191677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown        }
192677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown
193677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown        // Try to cancel the dump if it has not started yet.
194677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown        if (tid_state == tid) {
195677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown            if (!android_atomic_acquire_cas(tid, STATE_CANCEL, &g_unwind_signal_state.tid_state)) {
196677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown                ALOGV("Canceled thread %d stack dump.", tid);
197677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown                tid_state = STATE_CANCEL;
198677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown            } else {
199677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown                tid_state = android_atomic_acquire_load(&g_unwind_signal_state.tid_state);
200501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown            }
201677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown        }
202677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown
203677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown        // Wait indefinitely for the dump to finish or be canceled.
204677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown        // We cannot apply a timeout here because the other thread is accessing state that
205677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown        // is owned by this thread, such as milist.  It should not take very
206677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown        // long to take the dump once started.
207677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown        while (tid_state == STATE_DUMPING) {
208677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown            ALOGV("Waiting for thread %d to finish dumping the stack...", tid);
209677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown            usleep(1000);
210677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown            tid_state = android_atomic_acquire_load(&g_unwind_signal_state.tid_state);
211677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown        }
212677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown
213677545639cbc3d3018146534f27c8f5bdcd1bb46Jeff Brown        if (tid_state == STATE_DONE) {
214501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown            frames = g_unwind_signal_state.returned_frames;
215501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        }
216f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown
217501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        sigaction(SIGURG, &oact, NULL);
218501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    }
219501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
220f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    release_my_map_info_list(milist);
221501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    pthread_mutex_unlock(&g_unwind_signal_mutex);
222501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    return frames;
223501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#else
224501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    return -1;
225501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#endif
226501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown}
227501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
228501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brownssize_t unwind_backtrace_ptrace(pid_t tid, const ptrace_context_t* context,
229501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) {
230501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#ifdef CORKSCREW_HAVE_ARCH
231501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    return unwind_backtrace_ptrace_arch(tid, context, backtrace, ignore_depth, max_depth);
232501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#else
233501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    return -1;
234501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#endif
235501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown}
236501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
237501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brownstatic void init_backtrace_symbol(backtrace_symbol_t* symbol, uintptr_t pc) {
238501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    symbol->relative_pc = pc;
23919b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown    symbol->relative_symbol_addr = 0;
240f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    symbol->map_name = NULL;
24119b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown    symbol->symbol_name = NULL;
242501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    symbol->demangled_name = NULL;
243501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown}
244501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
245501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brownvoid get_backtrace_symbols(const backtrace_frame_t* backtrace, size_t frames,
246501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        backtrace_symbol_t* backtrace_symbols) {
247f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    map_info_t* milist = acquire_my_map_info_list();
248501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    for (size_t i = 0; i < frames; i++) {
249501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        const backtrace_frame_t* frame = &backtrace[i];
250501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        backtrace_symbol_t* symbol = &backtrace_symbols[i];
251501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        init_backtrace_symbol(symbol, frame->absolute_pc);
252501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
253501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        const map_info_t* mi = find_map_info(milist, frame->absolute_pc);
254501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        if (mi) {
255501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown            symbol->relative_pc = frame->absolute_pc - mi->start;
256f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown            if (mi->name[0]) {
257f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown                symbol->map_name = strdup(mi->name);
258f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown            }
259501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#if HAVE_DLADDR
260501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown            Dl_info info;
261501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown            if (dladdr((const void*)frame->absolute_pc, &info) && info.dli_sname) {
26219b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown                symbol->relative_symbol_addr = (uintptr_t)info.dli_saddr
26319b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown                        - (uintptr_t)info.dli_fbase;
26419b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown                symbol->symbol_name = strdup(info.dli_sname);
26519b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown                symbol->demangled_name = demangle_symbol_name(symbol->symbol_name);
266501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown            }
267501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#endif
268501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        }
269501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    }
270f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    release_my_map_info_list(milist);
271501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown}
272501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
273501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brownvoid get_backtrace_symbols_ptrace(const ptrace_context_t* context,
274501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        const backtrace_frame_t* backtrace, size_t frames,
275501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        backtrace_symbol_t* backtrace_symbols) {
276501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    for (size_t i = 0; i < frames; i++) {
277501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        const backtrace_frame_t* frame = &backtrace[i];
278501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        backtrace_symbol_t* symbol = &backtrace_symbols[i];
279501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        init_backtrace_symbol(symbol, frame->absolute_pc);
280501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
281501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        const map_info_t* mi;
282501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        const symbol_t* s;
283501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        find_symbol_ptrace(context, frame->absolute_pc, &mi, &s);
284501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        if (mi) {
285501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown            symbol->relative_pc = frame->absolute_pc - mi->start;
286f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown            if (mi->name[0]) {
287f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown                symbol->map_name = strdup(mi->name);
288f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown            }
289501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        }
290501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        if (s) {
29119b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown            symbol->relative_symbol_addr = s->start;
29219b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown            symbol->symbol_name = strdup(s->name);
29319b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown            symbol->demangled_name = demangle_symbol_name(symbol->symbol_name);
294501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        }
295501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    }
296501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown}
297501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
298501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brownvoid free_backtrace_symbols(backtrace_symbol_t* backtrace_symbols, size_t frames) {
299501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    for (size_t i = 0; i < frames; i++) {
300501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        backtrace_symbol_t* symbol = &backtrace_symbols[i];
301f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        free(symbol->map_name);
30219b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown        free(symbol->symbol_name);
303501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        free(symbol->demangled_name);
304501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        init_backtrace_symbol(symbol, 0);
305501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    }
306501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown}
30719b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown
30846beebea823878218fc0f14723829d83886af978Edwin Vanevoid format_backtrace_line(unsigned frameNumber, const backtrace_frame_t* frame __attribute__((unused)),
30919b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown        const backtrace_symbol_t* symbol, char* buffer, size_t bufferSize) {
31019b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown    const char* mapName = symbol->map_name ? symbol->map_name : "<unknown>";
31119b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown    const char* symbolName = symbol->demangled_name ? symbol->demangled_name : symbol->symbol_name;
31219b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown    size_t fieldWidth = (bufferSize - 80) / 2;
31319b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown    if (symbolName) {
31419b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown        uint32_t pc_offset = symbol->relative_pc - symbol->relative_symbol_addr;
31519b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown        if (pc_offset) {
31619b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown            snprintf(buffer, bufferSize, "#%02d  pc %08x  %.*s (%.*s+%u)",
31719b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown                    frameNumber, symbol->relative_pc, fieldWidth, mapName,
31819b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown                    fieldWidth, symbolName, pc_offset);
31919b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown        } else {
32019b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown            snprintf(buffer, bufferSize, "#%02d  pc %08x  %.*s (%.*s)",
32119b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown                    frameNumber, symbol->relative_pc, fieldWidth, mapName,
32219b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown                    fieldWidth, symbolName);
32319b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown        }
32419b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown    } else {
32519b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown        snprintf(buffer, bufferSize, "#%02d  pc %08x  %.*s",
32619b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown                frameNumber, symbol->relative_pc, fieldWidth, mapName);
32719b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown    }
32819b39f371be5250e7b9e88016be1e5e665367b3fJeff Brown}
329