1053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown/*
2053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown * Copyright (C) 2012 The Android Open Source Project
3053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown *
4053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown * Licensed under the Apache License, Version 2.0 (the "License");
5053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown * you may not use this file except in compliance with the License.
6053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown * You may obtain a copy of the License at
7053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown *
8053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown *      http://www.apache.org/licenses/LICENSE-2.0
9053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown *
10053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown * Unless required by applicable law or agreed to in writing, software
11053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown * distributed under the License is distributed on an "AS IS" BASIS,
12053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown * See the License for the specific language governing permissions and
14053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown * limitations under the License.
15053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown */
16053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown
17053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown#include <stdlib.h>
18053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown#include <unistd.h>
19053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown
20053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown#include <cutils/debugger.h>
21053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown#include <cutils/sockets.h>
22053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown
23053b865412d1982ad1dc0e840898d82527deeb99Jeff Brownint dump_tombstone(pid_t tid, char* pathbuf, size_t pathlen) {
24053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown    int s = socket_local_client(DEBUGGER_SOCKET_NAME,
25053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown            ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
26053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown    if (s < 0) {
27053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown        return -1;
28053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown    }
29053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown
30053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown    debugger_msg_t msg;
31053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown    msg.tid = tid;
32053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown    msg.action = DEBUGGER_ACTION_DUMP_TOMBSTONE;
33053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown
34053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown    int result = 0;
35053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown    if (TEMP_FAILURE_RETRY(write(s, &msg, sizeof(msg))) != sizeof(msg)) {
36053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown        result = -1;
37053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown    } else {
38053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown        char ack;
39053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown        if (TEMP_FAILURE_RETRY(read(s, &ack, 1)) != 1) {
40053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown            result = -1;
41053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown        } else {
42053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown            if (pathbuf && pathlen) {
43053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown                ssize_t n = TEMP_FAILURE_RETRY(read(s, pathbuf, pathlen - 1));
44053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown                if (n <= 0) {
45053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown                    result = -1;
46053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown                } else {
47053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown                    pathbuf[n] = '\0';
48053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown                }
49053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown            }
50053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown        }
51053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown    }
52053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown    TEMP_FAILURE_RETRY(close(s));
53053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown    return result;
54053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown}
55053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown
56053b865412d1982ad1dc0e840898d82527deeb99Jeff Brownint dump_backtrace_to_file(pid_t tid, int fd) {
57053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown    int s = socket_local_client(DEBUGGER_SOCKET_NAME,
58053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown            ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
59053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown    if (s < 0) {
60053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown        return -1;
61053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown    }
62053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown
63053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown    debugger_msg_t msg;
64053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown    msg.tid = tid;
65053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown    msg.action = DEBUGGER_ACTION_DUMP_BACKTRACE;
66053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown
67053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown    int result = 0;
68053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown    if (TEMP_FAILURE_RETRY(write(s, &msg, sizeof(msg))) != sizeof(msg)) {
69053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown        result = -1;
70053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown    } else {
71053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown        char ack;
72053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown        if (TEMP_FAILURE_RETRY(read(s, &ack, 1)) != 1) {
73053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown            result = -1;
74053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown        } else {
75053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown            char buffer[4096];
76053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown            ssize_t n;
77053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown            while ((n = TEMP_FAILURE_RETRY(read(s, buffer, sizeof(buffer)))) > 0) {
78053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown                if (TEMP_FAILURE_RETRY(write(fd, buffer, n)) != n) {
79053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown                    result = -1;
80053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown                    break;
81053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown                }
82053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown            }
83053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown        }
84053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown    }
85053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown    TEMP_FAILURE_RETRY(close(s));
86053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown    return result;
87053b865412d1982ad1dc0e840898d82527deeb99Jeff Brown}
88