blob: 1bd3cc8cd3341274930e7f1c34165f935cb07038 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
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' Turner7934a792009-10-16 12:13:34 -070035#include <errno.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080036
37#include "linker.h"
38
39#include <sys/socket.h>
40#include <cutils/sockets.h>
41
42void notify_gdb_of_libraries();
43
David 'Digit' Turner7934a792009-10-16 12:13:34 -070044#define RETRY_ON_EINTR(ret,cond) \
45 do { \
46 ret = (cond); \
47 } while (ret < 0 && errno == EINTR)
48
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080049void 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' Turner7934a792009-10-16 12:13:34 -070067 int ret;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080068
David 'Digit' Turner7934a792009-10-16 12:13:34 -070069 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 Project1dc9e472009-03-03 19:28:35 -080076 close(s);
77 }
78
79 /* remove our net so we fault for real when we return */
80 signal(n, SIG_IGN);
81}
82
83void 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}