debugger.c revision 053b865412d1982ad1dc0e840898d82527deeb99
1/*
2 * Copyright (C) 2012 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 <stdlib.h>
18#include <unistd.h>
19
20#include <cutils/debugger.h>
21#include <cutils/sockets.h>
22
23int dump_tombstone(pid_t tid, char* pathbuf, size_t pathlen) {
24    int s = socket_local_client(DEBUGGER_SOCKET_NAME,
25            ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
26    if (s < 0) {
27        return -1;
28    }
29
30    debugger_msg_t msg;
31    msg.tid = tid;
32    msg.action = DEBUGGER_ACTION_DUMP_TOMBSTONE;
33
34    int result = 0;
35    if (TEMP_FAILURE_RETRY(write(s, &msg, sizeof(msg))) != sizeof(msg)) {
36        result = -1;
37    } else {
38        char ack;
39        if (TEMP_FAILURE_RETRY(read(s, &ack, 1)) != 1) {
40            result = -1;
41        } else {
42            if (pathbuf && pathlen) {
43                ssize_t n = TEMP_FAILURE_RETRY(read(s, pathbuf, pathlen - 1));
44                if (n <= 0) {
45                    result = -1;
46                } else {
47                    pathbuf[n] = '\0';
48                }
49            }
50        }
51    }
52    TEMP_FAILURE_RETRY(close(s));
53    return result;
54}
55
56int dump_backtrace_to_file(pid_t tid, int fd) {
57    int s = socket_local_client(DEBUGGER_SOCKET_NAME,
58            ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
59    if (s < 0) {
60        return -1;
61    }
62
63    debugger_msg_t msg;
64    msg.tid = tid;
65    msg.action = DEBUGGER_ACTION_DUMP_BACKTRACE;
66
67    int result = 0;
68    if (TEMP_FAILURE_RETRY(write(s, &msg, sizeof(msg))) != sizeof(msg)) {
69        result = -1;
70    } else {
71        char ack;
72        if (TEMP_FAILURE_RETRY(read(s, &ack, 1)) != 1) {
73            result = -1;
74        } else {
75            char buffer[4096];
76            ssize_t n;
77            while ((n = TEMP_FAILURE_RETRY(read(s, buffer, sizeof(buffer)))) > 0) {
78                if (TEMP_FAILURE_RETRY(write(fd, buffer, n)) != n) {
79                    result = -1;
80                    break;
81                }
82            }
83        }
84    }
85    TEMP_FAILURE_RETRY(close(s));
86    return result;
87}
88