• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2006, 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 #include <stdio.h>
18 #include <errno.h>
19 #include <signal.h>
20 #include <pthread.h>
21 #include <stdarg.h>
22 #include <fcntl.h>
23 #include <sys/types.h>
24 #include <dirent.h>
25 #include <time.h>
26 
27 #include <sys/ptrace.h>
28 #include <sys/wait.h>
29 #include <elf.h>
30 #include <sys/stat.h>
31 #include <sys/poll.h>
32 
33 #include <log/logger.h>
34 
35 #include <cutils/sockets.h>
36 #include <cutils/properties.h>
37 #include <cutils/debugger.h>
38 
39 #include <linux/input.h>
40 
41 #include <private/android_filesystem_config.h>
42 
43 #include "backtrace.h"
44 #include "getevent.h"
45 #include "tombstone.h"
46 #include "utility.h"
47 
48 struct debugger_request_t {
49   debugger_action_t action;
50   pid_t pid, tid;
51   uid_t uid, gid;
52   uintptr_t abort_msg_address;
53   int32_t original_si_code;
54 };
55 
wait_for_user_action(const debugger_request_t & request)56 static void wait_for_user_action(const debugger_request_t &request) {
57   // Find out the name of the process that crashed.
58   char path[64];
59   snprintf(path, sizeof(path), "/proc/%d/exe", request.pid);
60 
61   char exe[PATH_MAX];
62   int count;
63   if ((count = readlink(path, exe, sizeof(exe) - 1)) == -1) {
64     ALOGE("readlink('%s') failed: %s", path, strerror(errno));
65     strlcpy(exe, "unknown", sizeof(exe));
66   } else {
67     exe[count] = '\0';
68   }
69 
70   // Explain how to attach the debugger.
71   ALOGI("********************************************************\n"
72         "* Process %d has been suspended while crashing.\n"
73         "* To attach gdbserver for a gdb connection on port 5039\n"
74         "* and start gdbclient:\n"
75         "*\n"
76         "*     gdbclient %s :5039 %d\n"
77         "*\n"
78         "* Wait for gdb to start, then press the VOLUME DOWN key\n"
79         "* to let the process continue crashing.\n"
80         "********************************************************",
81         request.pid, exe, request.tid);
82 
83   // Wait for VOLUME DOWN.
84   if (init_getevent() == 0) {
85     while (true) {
86       input_event e;
87       if (get_event(&e, -1) == 0) {
88         if (e.type == EV_KEY && e.code == KEY_VOLUMEDOWN && e.value == 0) {
89           break;
90         }
91       }
92     }
93     uninit_getevent();
94   }
95 
96   ALOGI("debuggerd resuming process %d", request.pid);
97 }
98 
get_process_info(pid_t tid,pid_t * out_pid,uid_t * out_uid,uid_t * out_gid)99 static int get_process_info(pid_t tid, pid_t* out_pid, uid_t* out_uid, uid_t* out_gid) {
100   char path[64];
101   snprintf(path, sizeof(path), "/proc/%d/status", tid);
102 
103   FILE* fp = fopen(path, "r");
104   if (!fp) {
105     return -1;
106   }
107 
108   int fields = 0;
109   char line[1024];
110   while (fgets(line, sizeof(line), fp)) {
111     size_t len = strlen(line);
112     if (len > 6 && !memcmp(line, "Tgid:\t", 6)) {
113       *out_pid = atoi(line + 6);
114       fields |= 1;
115     } else if (len > 5 && !memcmp(line, "Uid:\t", 5)) {
116       *out_uid = atoi(line + 5);
117       fields |= 2;
118     } else if (len > 5 && !memcmp(line, "Gid:\t", 5)) {
119       *out_gid = atoi(line + 5);
120       fields |= 4;
121     }
122   }
123   fclose(fp);
124   return fields == 7 ? 0 : -1;
125 }
126 
read_request(int fd,debugger_request_t * out_request)127 static int read_request(int fd, debugger_request_t* out_request) {
128   ucred cr;
129   socklen_t len = sizeof(cr);
130   int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
131   if (status != 0) {
132     ALOGE("cannot get credentials");
133     return -1;
134   }
135 
136   ALOGV("reading tid");
137   fcntl(fd, F_SETFL, O_NONBLOCK);
138 
139   pollfd pollfds[1];
140   pollfds[0].fd = fd;
141   pollfds[0].events = POLLIN;
142   pollfds[0].revents = 0;
143   status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000));
144   if (status != 1) {
145     ALOGE("timed out reading tid (from pid=%d uid=%d)\n", cr.pid, cr.uid);
146     return -1;
147   }
148 
149   debugger_msg_t msg;
150   memset(&msg, 0, sizeof(msg));
151   status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg)));
152   if (status < 0) {
153     ALOGE("read failure? %s (pid=%d uid=%d)\n", strerror(errno), cr.pid, cr.uid);
154     return -1;
155   }
156   if (status != sizeof(debugger_msg_t)) {
157     ALOGE("invalid crash request of size %d (from pid=%d uid=%d)\n", status, cr.pid, cr.uid);
158     return -1;
159   }
160 
161   out_request->action = msg.action;
162   out_request->tid = msg.tid;
163   out_request->pid = cr.pid;
164   out_request->uid = cr.uid;
165   out_request->gid = cr.gid;
166   out_request->abort_msg_address = msg.abort_msg_address;
167   out_request->original_si_code = msg.original_si_code;
168 
169   if (msg.action == DEBUGGER_ACTION_CRASH) {
170     // Ensure that the tid reported by the crashing process is valid.
171     char buf[64];
172     struct stat s;
173     snprintf(buf, sizeof buf, "/proc/%d/task/%d", out_request->pid, out_request->tid);
174     if (stat(buf, &s)) {
175       ALOGE("tid %d does not exist in pid %d. ignoring debug request\n",
176           out_request->tid, out_request->pid);
177       return -1;
178     }
179   } else if (cr.uid == 0
180             || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) {
181     // Only root or system can ask us to attach to any process and dump it explicitly.
182     // However, system is only allowed to collect backtraces but cannot dump tombstones.
183     status = get_process_info(out_request->tid, &out_request->pid,
184                               &out_request->uid, &out_request->gid);
185     if (status < 0) {
186       ALOGE("tid %d does not exist. ignoring explicit dump request\n", out_request->tid);
187       return -1;
188     }
189   } else {
190     // No one else is allowed to dump arbitrary processes.
191     return -1;
192   }
193   return 0;
194 }
195 
should_attach_gdb(debugger_request_t * request)196 static bool should_attach_gdb(debugger_request_t* request) {
197   if (request->action == DEBUGGER_ACTION_CRASH) {
198     char value[PROPERTY_VALUE_MAX];
199     property_get("debug.db.uid", value, "-1");
200     int debug_uid = atoi(value);
201     return debug_uid >= 0 && request->uid <= (uid_t)debug_uid;
202   }
203   return false;
204 }
205 
handle_request(int fd)206 static void handle_request(int fd) {
207   ALOGV("handle_request(%d)\n", fd);
208 
209   debugger_request_t request;
210   memset(&request, 0, sizeof(request));
211   int status = read_request(fd, &request);
212   if (!status) {
213     ALOGV("BOOM: pid=%d uid=%d gid=%d tid=%d\n",
214          request.pid, request.uid, request.gid, request.tid);
215 
216     // At this point, the thread that made the request is blocked in
217     // a read() call.  If the thread has crashed, then this gives us
218     // time to PTRACE_ATTACH to it before it has a chance to really fault.
219     //
220     // The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
221     // won't necessarily have stopped by the time ptrace() returns.  (We
222     // currently assume it does.)  We write to the file descriptor to
223     // ensure that it can run as soon as we call PTRACE_CONT below.
224     // See details in bionic/libc/linker/debugger.c, in function
225     // debugger_signal_handler().
226     if (ptrace(PTRACE_ATTACH, request.tid, 0, 0)) {
227       ALOGE("ptrace attach failed: %s\n", strerror(errno));
228     } else {
229       bool detach_failed = false;
230       bool tid_unresponsive = false;
231       bool attach_gdb = should_attach_gdb(&request);
232       if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
233         ALOGE("failed responding to client: %s\n", strerror(errno));
234       } else {
235         char* tombstone_path = NULL;
236 
237         if (request.action == DEBUGGER_ACTION_CRASH) {
238           close(fd);
239           fd = -1;
240         }
241 
242         int total_sleep_time_usec = 0;
243         for (;;) {
244           int signal = wait_for_sigstop(request.tid, &total_sleep_time_usec, &detach_failed);
245           if (signal == -1) {
246             tid_unresponsive = true;
247             break;
248           }
249 
250           switch (signal) {
251             case SIGSTOP:
252               if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
253                 ALOGV("stopped -- dumping to tombstone\n");
254                 tombstone_path = engrave_tombstone(request.pid, request.tid,
255                                                    signal, request.original_si_code,
256                                                    request.abort_msg_address, true,
257                                                    &detach_failed, &total_sleep_time_usec);
258               } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
259                 ALOGV("stopped -- dumping to fd\n");
260                 dump_backtrace(fd, -1, request.pid, request.tid, &detach_failed,
261                                &total_sleep_time_usec);
262               } else {
263                 ALOGV("stopped -- continuing\n");
264                 status = ptrace(PTRACE_CONT, request.tid, 0, 0);
265                 if (status) {
266                   ALOGE("ptrace continue failed: %s\n", strerror(errno));
267                 }
268                 continue; // loop again
269               }
270               break;
271 
272             case SIGABRT:
273             case SIGBUS:
274             case SIGFPE:
275             case SIGILL:
276             case SIGPIPE:
277             case SIGSEGV:
278 #ifdef SIGSTKFLT
279             case SIGSTKFLT:
280 #endif
281             case SIGTRAP:
282               ALOGV("stopped -- fatal signal\n");
283               // Send a SIGSTOP to the process to make all of
284               // the non-signaled threads stop moving.  Without
285               // this we get a lot of "ptrace detach failed:
286               // No such process".
287               kill(request.pid, SIGSTOP);
288               // don't dump sibling threads when attaching to GDB because it
289               // makes the process less reliable, apparently...
290               tombstone_path = engrave_tombstone(request.pid, request.tid,
291                                                  signal, request.original_si_code,
292                                                  request.abort_msg_address, !attach_gdb,
293                                                  &detach_failed, &total_sleep_time_usec);
294               break;
295 
296             default:
297               ALOGE("process stopped due to unexpected signal %d\n", signal);
298               break;
299           }
300           break;
301         }
302 
303         if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
304           if (tombstone_path) {
305             write(fd, tombstone_path, strlen(tombstone_path));
306           }
307           close(fd);
308           fd = -1;
309         }
310         free(tombstone_path);
311       }
312 
313       if (!tid_unresponsive) {
314         ALOGV("detaching");
315         if (attach_gdb) {
316           // stop the process so we can debug
317           kill(request.pid, SIGSTOP);
318         }
319         if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
320           ALOGE("ptrace detach from %d failed: %s", request.tid, strerror(errno));
321           detach_failed = true;
322         } else if (attach_gdb) {
323           // if debug.db.uid is set, its value indicates if we should wait
324           // for user action for the crashing process.
325           // in this case, we log a message and turn the debug LED on
326           // waiting for a gdb connection (for instance)
327           wait_for_user_action(request);
328         }
329       }
330 
331       // resume stopped process (so it can crash in peace).
332       kill(request.pid, SIGCONT);
333 
334       // If we didn't successfully detach, we're still the parent, and the
335       // actual parent won't receive a death notification via wait(2).  At this point
336       // there's not much we can do about that.
337       if (detach_failed) {
338         ALOGE("debuggerd committing suicide to free the zombie!\n");
339         kill(getpid(), SIGKILL);
340       }
341     }
342 
343   }
344   if (fd >= 0) {
345     close(fd);
346   }
347 }
348 
do_server()349 static int do_server() {
350   // debuggerd crashes can't be reported to debuggerd.
351   // Reset all of the crash handlers.
352   signal(SIGABRT, SIG_DFL);
353   signal(SIGBUS, SIG_DFL);
354   signal(SIGFPE, SIG_DFL);
355   signal(SIGILL, SIG_DFL);
356   signal(SIGSEGV, SIG_DFL);
357 #ifdef SIGSTKFLT
358   signal(SIGSTKFLT, SIG_DFL);
359 #endif
360   signal(SIGTRAP, SIG_DFL);
361 
362   // Ignore failed writes to closed sockets
363   signal(SIGPIPE, SIG_IGN);
364 
365   int logsocket = socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
366   if (logsocket < 0) {
367     logsocket = -1;
368   } else {
369     fcntl(logsocket, F_SETFD, FD_CLOEXEC);
370   }
371 
372   struct sigaction act;
373   act.sa_handler = SIG_DFL;
374   sigemptyset(&act.sa_mask);
375   sigaddset(&act.sa_mask,SIGCHLD);
376   act.sa_flags = SA_NOCLDWAIT;
377   sigaction(SIGCHLD, &act, 0);
378 
379   int s = socket_local_server(DEBUGGER_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
380   if (s < 0)
381     return 1;
382   fcntl(s, F_SETFD, FD_CLOEXEC);
383 
384   ALOGI("debuggerd: " __DATE__ " " __TIME__ "\n");
385 
386   for (;;) {
387     sockaddr addr;
388     socklen_t alen = sizeof(addr);
389 
390     ALOGV("waiting for connection\n");
391     int fd = accept(s, &addr, &alen);
392     if (fd < 0) {
393       ALOGV("accept failed: %s\n", strerror(errno));
394       continue;
395     }
396 
397     fcntl(fd, F_SETFD, FD_CLOEXEC);
398 
399     handle_request(fd);
400   }
401   return 0;
402 }
403 
do_explicit_dump(pid_t tid,bool dump_backtrace)404 static int do_explicit_dump(pid_t tid, bool dump_backtrace) {
405   fprintf(stdout, "Sending request to dump task %d.\n", tid);
406 
407   if (dump_backtrace) {
408     fflush(stdout);
409     if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) {
410       fputs("Error dumping backtrace.\n", stderr);
411       return 1;
412     }
413   } else {
414     char tombstone_path[PATH_MAX];
415     if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) {
416       fputs("Error dumping tombstone.\n", stderr);
417       return 1;
418     }
419     fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
420   }
421   return 0;
422 }
423 
usage()424 static void usage() {
425   fputs("Usage: -b [<tid>]\n"
426         "  -b dump backtrace to console, otherwise dump full tombstone file\n"
427         "\n"
428         "If tid specified, sends a request to debuggerd to dump that task.\n"
429         "Otherwise, starts the debuggerd server.\n", stderr);
430 }
431 
main(int argc,char ** argv)432 int main(int argc, char** argv) {
433   if (argc == 1) {
434     return do_server();
435   }
436 
437   bool dump_backtrace = false;
438   bool have_tid = false;
439   pid_t tid = 0;
440   for (int i = 1; i < argc; i++) {
441     if (!strcmp(argv[i], "-b")) {
442       dump_backtrace = true;
443     } else if (!have_tid) {
444       tid = atoi(argv[i]);
445       have_tid = true;
446     } else {
447       usage();
448       return 1;
449     }
450   }
451   if (!have_tid) {
452     usage();
453     return 1;
454   }
455   return do_explicit_dump(tid, dump_backtrace);
456 }
457