debugger.cpp revision f858bd1c6eec7eb6bbfc8844e0de096be011e99a
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *  * Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 *  * Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in
12 *    the documentation and/or other materials provided with the
13 *    distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include "linker.h"
30
31#include <errno.h>
32#include <signal.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <sys/mman.h>
36#include <sys/prctl.h>
37#include <sys/socket.h>
38#include <sys/un.h>
39#include <unistd.h>
40
41extern "C" int tgkill(int tgid, int tid, int sig);
42
43#if __LP64__
44#define DEBUGGER_SOCKET_NAME "android:debuggerd64"
45#else
46#define DEBUGGER_SOCKET_NAME "android:debuggerd"
47#endif
48
49enum debugger_action_t {
50    // dump a crash
51    DEBUGGER_ACTION_CRASH,
52    // dump a tombstone file
53    DEBUGGER_ACTION_DUMP_TOMBSTONE,
54    // dump a backtrace only back to the socket
55    DEBUGGER_ACTION_DUMP_BACKTRACE,
56};
57
58/* message sent over the socket */
59struct debugger_msg_t {
60  // version 1 included:
61  debugger_action_t action;
62  pid_t tid;
63
64  // version 2 added:
65  uintptr_t abort_msg_address;
66};
67
68// see man(2) prctl, specifically the section about PR_GET_NAME
69#define MAX_TASK_NAME_LEN (16)
70
71static int socket_abstract_client(const char* name, int type) {
72    sockaddr_un addr;
73
74    // Test with length +1 for the *initial* '\0'.
75    size_t namelen = strlen(name);
76    if ((namelen + 1) > sizeof(addr.sun_path)) {
77        errno = EINVAL;
78        return -1;
79    }
80
81    /* This is used for abstract socket namespace, we need
82     * an initial '\0' at the start of the Unix socket path.
83     *
84     * Note: The path in this case is *not* supposed to be
85     * '\0'-terminated. ("man 7 unix" for the gory details.)
86     */
87    memset(&addr, 0, sizeof(addr));
88    addr.sun_family = AF_LOCAL;
89    addr.sun_path[0] = 0;
90    memcpy(addr.sun_path + 1, name, namelen);
91
92    socklen_t alen = namelen + offsetof(sockaddr_un, sun_path) + 1;
93
94    int s = socket(AF_LOCAL, type, 0);
95    if (s == -1) {
96        return -1;
97    }
98
99    int err = TEMP_FAILURE_RETRY(connect(s, reinterpret_cast<sockaddr*>(&addr), alen));
100    if (err == -1) {
101        close(s);
102        s = -1;
103    }
104
105    return s;
106}
107
108/*
109 * Writes a summary of the signal to the log file.  We do this so that, if
110 * for some reason we're not able to contact debuggerd, there is still some
111 * indication of the failure in the log.
112 *
113 * We could be here as a result of native heap corruption, or while a
114 * mutex is being held, so we don't want to use any libc functions that
115 * could allocate memory or hold a lock.
116 */
117static void log_signal_summary(int signum, const siginfo_t* info) {
118    const char* signal_name;
119    switch (signum) {
120        case SIGILL:    signal_name = "SIGILL";     break;
121        case SIGABRT:   signal_name = "SIGABRT";    break;
122        case SIGBUS:    signal_name = "SIGBUS";     break;
123        case SIGFPE:    signal_name = "SIGFPE";     break;
124        case SIGSEGV:   signal_name = "SIGSEGV";    break;
125#if defined(SIGSTKFLT)
126        case SIGSTKFLT: signal_name = "SIGSTKFLT";  break;
127#endif
128        case SIGPIPE:   signal_name = "SIGPIPE";    break;
129        default:        signal_name = "???";        break;
130    }
131
132    char thread_name[MAX_TASK_NAME_LEN + 1]; // one more for termination
133    if (prctl(PR_GET_NAME, (unsigned long)thread_name, 0, 0, 0) != 0) {
134        strcpy(thread_name, "<name unknown>");
135    } else {
136        // short names are null terminated by prctl, but the man page
137        // implies that 16 byte names are not.
138        thread_name[MAX_TASK_NAME_LEN] = 0;
139    }
140
141    // "info" will be NULL if the siginfo_t information was not available.
142    if (info != NULL) {
143        __libc_format_log(ANDROID_LOG_FATAL, "libc",
144                          "Fatal signal %d (%s) at %p (code=%d), thread %d (%s)",
145                          signum, signal_name, info->si_addr, info->si_code,
146                          gettid(), thread_name);
147    } else {
148        __libc_format_log(ANDROID_LOG_FATAL, "libc",
149                          "Fatal signal %d (%s), thread %d (%s)",
150                          signum, signal_name, gettid(), thread_name);
151    }
152}
153
154/*
155 * Returns true if the handler for signal "signum" has SA_SIGINFO set.
156 */
157static bool have_siginfo(int signum) {
158    struct sigaction old_action, new_action;
159
160    memset(&new_action, 0, sizeof(new_action));
161    new_action.sa_handler = SIG_DFL;
162    new_action.sa_flags = SA_RESTART;
163    sigemptyset(&new_action.sa_mask);
164
165    if (sigaction(signum, &new_action, &old_action) < 0) {
166      __libc_format_log(ANDROID_LOG_WARN, "libc", "Failed testing for SA_SIGINFO: %s",
167                        strerror(errno));
168      return false;
169    }
170    bool result = (old_action.sa_flags & SA_SIGINFO) != 0;
171
172    if (sigaction(signum, &old_action, NULL) == -1) {
173      __libc_format_log(ANDROID_LOG_WARN, "libc", "Restore failed in test for SA_SIGINFO: %s",
174                        strerror(errno));
175    }
176    return result;
177}
178
179/*
180 * Catches fatal signals so we can ask debuggerd to ptrace us before
181 * we crash.
182 */
183void debuggerd_signal_handler(int n, siginfo_t* info, void*) {
184    /*
185     * It's possible somebody cleared the SA_SIGINFO flag, which would mean
186     * our "info" arg holds an undefined value.
187     */
188    if (!have_siginfo(n)) {
189        info = NULL;
190    }
191
192    log_signal_summary(n, info);
193
194    pid_t tid = gettid();
195    int s = socket_abstract_client(DEBUGGER_SOCKET_NAME, SOCK_STREAM);
196
197    if (s >= 0) {
198        // debuggerd knows our pid from the credentials on the
199        // local socket but we need to tell it the tid of the crashing thread.
200        // debuggerd will be paranoid and verify that we sent a tid
201        // that's actually in our process.
202        debugger_msg_t msg;
203        msg.action = DEBUGGER_ACTION_CRASH;
204        msg.tid = tid;
205        msg.abort_msg_address = reinterpret_cast<uintptr_t>(gAbortMessage);
206        int ret = TEMP_FAILURE_RETRY(write(s, &msg, sizeof(msg)));
207        if (ret == sizeof(msg)) {
208            // if the write failed, there is no point trying to read a response.
209            ret = TEMP_FAILURE_RETRY(read(s, &tid, 1));
210            int saved_errno = errno;
211            notify_gdb_of_libraries();
212            errno = saved_errno;
213        }
214
215        if (ret < 0) {
216            /* read or write failed -- broken connection? */
217            __libc_format_log(ANDROID_LOG_FATAL, "libc", "Failed while talking to debuggerd: %s",
218                              strerror(errno));
219        }
220
221        close(s);
222    } else {
223        /* socket failed; maybe process ran out of fds */
224        __libc_format_log(ANDROID_LOG_FATAL, "libc", "Unable to open connection to debuggerd: %s",
225                          strerror(errno));
226    }
227
228    /* remove our net so we fault for real when we return */
229    signal(n, SIG_DFL);
230
231    /*
232     * These signals are not re-thrown when we resume.  This means that
233     * crashing due to (say) SIGPIPE doesn't work the way you'd expect it
234     * to.  We work around this by throwing them manually.  We don't want
235     * to do this for *all* signals because it'll screw up the address for
236     * faults like SIGSEGV.
237     */
238    switch (n) {
239        case SIGABRT:
240        case SIGFPE:
241        case SIGPIPE:
242#if defined(SIGSTKFLT)
243        case SIGSTKFLT:
244#endif
245            (void) tgkill(getpid(), gettid(), n);
246            break;
247        default:    // SIGILL, SIGBUS, SIGSEGV
248            break;
249    }
250}
251
252void debuggerd_init() {
253    struct sigaction action;
254    memset(&action, 0, sizeof(action));
255    sigemptyset(&action.sa_mask);
256    action.sa_sigaction = debuggerd_signal_handler;
257    action.sa_flags = SA_RESTART | SA_SIGINFO;
258
259    // Use the alternate signal stack if available so we can catch stack overflows.
260    action.sa_flags |= SA_ONSTACK;
261
262    sigaction(SIGABRT, &action, NULL);
263    sigaction(SIGBUS, &action, NULL);
264    sigaction(SIGFPE, &action, NULL);
265    sigaction(SIGILL, &action, NULL);
266    sigaction(SIGPIPE, &action, NULL);
267    sigaction(SIGSEGV, &action, NULL);
268#if defined(SIGSTKFLT)
269    sigaction(SIGSTKFLT, &action, NULL);
270#endif
271    sigaction(SIGTRAP, &action, NULL);
272}
273