The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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 <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <unistd.h> |
| 32 | #include <ctype.h> |
| 33 | #include <signal.h> |
| 34 | #include <sys/mman.h> |
David 'Digit' Turner | 7934a79 | 2009-10-16 12:13:34 -0700 | [diff] [blame^] | 35 | #include <errno.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 36 | |
| 37 | #include "linker.h" |
| 38 | |
| 39 | #include <sys/socket.h> |
| 40 | #include <cutils/sockets.h> |
| 41 | |
| 42 | void notify_gdb_of_libraries(); |
| 43 | |
David 'Digit' Turner | 7934a79 | 2009-10-16 12:13:34 -0700 | [diff] [blame^] | 44 | #define RETRY_ON_EINTR(ret,cond) \ |
| 45 | do { \ |
| 46 | ret = (cond); \ |
| 47 | } while (ret < 0 && errno == EINTR) |
| 48 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 49 | void debugger_signal_handler(int n) |
| 50 | { |
| 51 | unsigned tid; |
| 52 | int s; |
| 53 | |
| 54 | /* avoid picking up GC interrupts */ |
| 55 | signal(SIGUSR1, SIG_IGN); |
| 56 | |
| 57 | tid = gettid(); |
| 58 | s = socket_local_client("android:debuggerd", |
| 59 | ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM); |
| 60 | |
| 61 | if(s >= 0) { |
| 62 | /* debugger knows our pid from the credentials on the |
| 63 | * local socket but we need to tell it our tid. It |
| 64 | * is paranoid and will verify that we are giving a tid |
| 65 | * that's actually in our process |
| 66 | */ |
David 'Digit' Turner | 7934a79 | 2009-10-16 12:13:34 -0700 | [diff] [blame^] | 67 | int ret; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 68 | |
David 'Digit' Turner | 7934a79 | 2009-10-16 12:13:34 -0700 | [diff] [blame^] | 69 | RETRY_ON_EINTR(ret, write(s, &tid, sizeof(unsigned))); |
| 70 | if (ret == sizeof(unsigned)) { |
| 71 | /* if the write failed, there is no point to read on |
| 72 | * the file descriptor. */ |
| 73 | RETRY_ON_EINTR(ret, read(s, &tid, 1)); |
| 74 | notify_gdb_of_libraries(); |
| 75 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 76 | close(s); |
| 77 | } |
| 78 | |
| 79 | /* remove our net so we fault for real when we return */ |
| 80 | signal(n, SIG_IGN); |
| 81 | } |
| 82 | |
| 83 | void debugger_init() |
| 84 | { |
| 85 | signal(SIGILL, debugger_signal_handler); |
| 86 | signal(SIGABRT, debugger_signal_handler); |
| 87 | signal(SIGBUS, debugger_signal_handler); |
| 88 | signal(SIGFPE, debugger_signal_handler); |
| 89 | signal(SIGSEGV, debugger_signal_handler); |
| 90 | signal(SIGSTKFLT, debugger_signal_handler); |
| 91 | signal(SIGPIPE, debugger_signal_handler); |
| 92 | } |