]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/platform-bionic.git/blob - linker/debugger.cpp
Merge "Sync with upstream for gethnamaddr.c."
[android-sdk/platform-bionic.git] / linker / debugger.cpp
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  */
29 #include "linker.h"
31 #include <errno.h>
32 #include <inttypes.h>
33 #include <signal.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <sys/mman.h>
37 #include <sys/prctl.h>
38 #include <sys/socket.h>
39 #include <sys/un.h>
40 #include <unistd.h>
42 extern "C" int tgkill(int tgid, int tid, int sig);
44 #if __LP64__
45 #define DEBUGGER_SOCKET_NAME "android:debuggerd64"
46 #else
47 #define DEBUGGER_SOCKET_NAME "android:debuggerd"
48 #endif
50 enum debugger_action_t {
51   // dump a crash
52   DEBUGGER_ACTION_CRASH,
53   // dump a tombstone file
54   DEBUGGER_ACTION_DUMP_TOMBSTONE,
55   // dump a backtrace only back to the socket
56   DEBUGGER_ACTION_DUMP_BACKTRACE,
57 };
59 /* message sent over the socket */
60 struct debugger_msg_t {
61   // version 1 included:
62   debugger_action_t action;
63   pid_t tid;
65   // version 2 added:
66   uintptr_t abort_msg_address;
68   // version 3 added:
69   int32_t original_si_code;
70 };
72 // see man(2) prctl, specifically the section about PR_GET_NAME
73 #define MAX_TASK_NAME_LEN (16)
75 static int socket_abstract_client(const char* name, int type) {
76   sockaddr_un addr;
78   // Test with length +1 for the *initial* '\0'.
79   size_t namelen = strlen(name);
80   if ((namelen + 1) > sizeof(addr.sun_path)) {
81     errno = EINVAL;
82     return -1;
83   }
85   // This is used for abstract socket namespace, we need
86   // an initial '\0' at the start of the Unix socket path.
87   //
88   // Note: The path in this case is *not* supposed to be
89   // '\0'-terminated. ("man 7 unix" for the gory details.)
90   memset(&addr, 0, sizeof(addr));
91   addr.sun_family = AF_LOCAL;
92   addr.sun_path[0] = 0;
93   memcpy(addr.sun_path + 1, name, namelen);
95   socklen_t alen = namelen + offsetof(sockaddr_un, sun_path) + 1;
97   int s = socket(AF_LOCAL, type, 0);
98   if (s == -1) {
99     return -1;
100   }
102   int rc = TEMP_FAILURE_RETRY(connect(s, reinterpret_cast<sockaddr*>(&addr), alen));
103   if (rc == -1) {
104     close(s);
105     return -1;
106   }
108   return s;
111 /*
112  * Writes a summary of the signal to the log file.  We do this so that, if
113  * for some reason we're not able to contact debuggerd, there is still some
114  * indication of the failure in the log.
115  *
116  * We could be here as a result of native heap corruption, or while a
117  * mutex is being held, so we don't want to use any libc functions that
118  * could allocate memory or hold a lock.
119  */
120 static void log_signal_summary(int signum, const siginfo_t* info) {
121   const char* signal_name = "???";
122   bool has_address = false;
123   switch (signum) {
124     case SIGABRT:
125       signal_name = "SIGABRT";
126       break;
127     case SIGBUS:
128       signal_name = "SIGBUS";
129       has_address = true;
130       break;
131     case SIGFPE:
132       signal_name = "SIGFPE";
133       has_address = true;
134       break;
135     case SIGILL:
136       signal_name = "SIGILL";
137       has_address = true;
138       break;
139     case SIGPIPE:
140       signal_name = "SIGPIPE";
141       break;
142     case SIGSEGV:
143       signal_name = "SIGSEGV";
144       has_address = true;
145       break;
146 #if defined(SIGSTKFLT)
147     case SIGSTKFLT:
148       signal_name = "SIGSTKFLT";
149       break;
150 #endif
151     case SIGTRAP:
152       signal_name = "SIGTRAP";
153       break;
154   }
156   char thread_name[MAX_TASK_NAME_LEN + 1]; // one more for termination
157   if (prctl(PR_GET_NAME, (unsigned long)thread_name, 0, 0, 0) != 0) {
158     strcpy(thread_name, "<name unknown>");
159   } else {
160     // short names are null terminated by prctl, but the man page
161     // implies that 16 byte names are not.
162     thread_name[MAX_TASK_NAME_LEN] = 0;
163   }
165   // "info" will be null if the siginfo_t information was not available.
166   // Many signals don't have an address or a code.
167   char code_desc[32]; // ", code -6"
168   char addr_desc[32]; // ", fault addr 0x1234"
169   addr_desc[0] = code_desc[0] = 0;
170   if (info != nullptr) {
171     // For a rethrown signal, this si_code will be right and the one debuggerd shows will
172     // always be SI_TKILL.
173     __libc_format_buffer(code_desc, sizeof(code_desc), ", code %d", info->si_code);
174     if (has_address) {
175       __libc_format_buffer(addr_desc, sizeof(addr_desc), ", fault addr %p", info->si_addr);
176     }
177   }
178   __libc_format_log(ANDROID_LOG_FATAL, "libc",
179                     "Fatal signal %d (%s)%s%s in tid %d (%s)",
180                     signum, signal_name, code_desc, addr_desc, gettid(), thread_name);
183 /*
184  * Returns true if the handler for signal "signum" has SA_SIGINFO set.
185  */
186 static bool have_siginfo(int signum) {
187   struct sigaction old_action, new_action;
189   memset(&new_action, 0, sizeof(new_action));
190   new_action.sa_handler = SIG_DFL;
191   new_action.sa_flags = SA_RESTART;
192   sigemptyset(&new_action.sa_mask);
194   if (sigaction(signum, &new_action, &old_action) < 0) {
195     __libc_format_log(ANDROID_LOG_WARN, "libc", "Failed testing for SA_SIGINFO: %s",
196                       strerror(errno));
197     return false;
198   }
199   bool result = (old_action.sa_flags & SA_SIGINFO) != 0;
201   if (sigaction(signum, &old_action, nullptr) == -1) {
202     __libc_format_log(ANDROID_LOG_WARN, "libc", "Restore failed in test for SA_SIGINFO: %s",
203                       strerror(errno));
204   }
205   return result;
208 static void send_debuggerd_packet(siginfo_t* info) {
209   if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0) {
210     // process has disabled core dumps and PTRACE_ATTACH, and does not want to be dumped.
211     // Honor that intention by not connecting to debuggerd and asking it
212     // to dump our internal state.
213     __libc_format_log(ANDROID_LOG_INFO, "libc",
214                       "Suppressing debuggerd output because prctl(PR_GET_DUMPABLE)==0");
215     return;
216   }
218   int s = socket_abstract_client(DEBUGGER_SOCKET_NAME, SOCK_STREAM | SOCK_CLOEXEC);
219   if (s == -1) {
220     __libc_format_log(ANDROID_LOG_FATAL, "libc", "Unable to open connection to debuggerd: %s",
221                       strerror(errno));
222     return;
223   }
225   // debuggerd knows our pid from the credentials on the
226   // local socket but we need to tell it the tid of the crashing thread.
227   // debuggerd will be paranoid and verify that we sent a tid
228   // that's actually in our process.
229   debugger_msg_t msg;
230   msg.action = DEBUGGER_ACTION_CRASH;
231   msg.tid = gettid();
232   msg.abort_msg_address = reinterpret_cast<uintptr_t>(g_abort_message);
233   msg.original_si_code = (info != nullptr) ? info->si_code : 0;
234   int ret = TEMP_FAILURE_RETRY(write(s, &msg, sizeof(msg)));
235   if (ret == sizeof(msg)) {
236     char debuggerd_ack;
237     ret = TEMP_FAILURE_RETRY(read(s, &debuggerd_ack, 1));
238     int saved_errno = errno;
239     notify_gdb_of_libraries();
240     errno = saved_errno;
241   } else {
242     // read or write failed -- broken connection?
243     __libc_format_log(ANDROID_LOG_FATAL, "libc", "Failed while talking to debuggerd: %s",
244                       strerror(errno));
245   }
247   close(s);
250 /*
251  * Catches fatal signals so we can ask debuggerd to ptrace us before
252  * we crash.
253  */
254 static void debuggerd_signal_handler(int signal_number, siginfo_t* info, void*) {
255   // It's possible somebody cleared the SA_SIGINFO flag, which would mean
256   // our "info" arg holds an undefined value.
257   if (!have_siginfo(signal_number)) {
258     info = nullptr;
259   }
261   log_signal_summary(signal_number, info);
263   send_debuggerd_packet(info);
265   // Remove our net so we fault for real when we return.
266   signal(signal_number, SIG_DFL);
268   // These signals are not re-thrown when we resume.  This means that
269   // crashing due to (say) SIGPIPE doesn't work the way you'd expect it
270   // to.  We work around this by throwing them manually.  We don't want
271   // to do this for *all* signals because it'll screw up the si_addr for
272   // faults like SIGSEGV. It does screw up the si_code, which is why we
273   // passed that to debuggerd above.
274   switch (signal_number) {
275     case SIGABRT:
276     case SIGFPE:
277     case SIGPIPE:
278 #if defined(SIGSTKFLT)
279     case SIGSTKFLT:
280 #endif
281     case SIGTRAP:
282       tgkill(getpid(), gettid(), signal_number);
283       break;
284     default:    // SIGILL, SIGBUS, SIGSEGV
285       break;
286   }
289 __LIBC_HIDDEN__ void debuggerd_init() {
290   struct sigaction action;
291   memset(&action, 0, sizeof(action));
292   sigemptyset(&action.sa_mask);
293   action.sa_sigaction = debuggerd_signal_handler;
294   action.sa_flags = SA_RESTART | SA_SIGINFO;
296   // Use the alternate signal stack if available so we can catch stack overflows.
297   action.sa_flags |= SA_ONSTACK;
299   sigaction(SIGABRT, &action, nullptr);
300   sigaction(SIGBUS, &action, nullptr);
301   sigaction(SIGFPE, &action, nullptr);
302   sigaction(SIGILL, &action, nullptr);
303   sigaction(SIGPIPE, &action, nullptr);
304   sigaction(SIGSEGV, &action, nullptr);
305 #if defined(SIGSTKFLT)
306   sigaction(SIGSTKFLT, &action, nullptr);
307 #endif
308   sigaction(SIGTRAP, &action, nullptr);