blob: 2cd8ec3f59c1811703772f38fab1bac61de85e5a [file] [log] [blame]
Jeff Brown053b8652012-06-06 16:25:03 -07001/*
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
Christopher Ferris6bcc4ac2014-09-12 14:44:20 -070017#include <stdbool.h>
18#include <fcntl.h>
19#include <stdio.h>
Jeff Brown053b8652012-06-06 16:25:03 -070020#include <stdlib.h>
Elliott Hughes855fcc32014-04-25 16:05:34 -070021#include <string.h>
Christopher Ferrisfa41e0f2015-01-13 19:07:12 -080022#include <sys/types.h>
23#include <sys/socket.h>
Jeff Brown053b8652012-06-06 16:25:03 -070024#include <unistd.h>
25
26#include <cutils/debugger.h>
27#include <cutils/sockets.h>
28
Christopher Ferrisfa41e0f2015-01-13 19:07:12 -080029#define LOG_TAG "DEBUG"
30#include <log/log.h>
31
Christopher Ferris6bcc4ac2014-09-12 14:44:20 -070032static int send_request(int sock_fd, void* msg_ptr, size_t msg_len) {
33 int result = 0;
34 if (TEMP_FAILURE_RETRY(write(sock_fd, msg_ptr, msg_len)) != (ssize_t) msg_len) {
35 result = -1;
36 } else {
37 char ack;
38 if (TEMP_FAILURE_RETRY(read(sock_fd, &ack, 1)) != 1) {
39 result = -1;
40 }
41 }
42 return result;
43}
44
Christopher Ferrisfa41e0f2015-01-13 19:07:12 -080045static int make_dump_request(debugger_action_t action, pid_t tid, int timeout_secs) {
Christopher Ferris6bcc4ac2014-09-12 14:44:20 -070046 const char* socket_name;
47 debugger_msg_t msg;
Christopher Ferris9774df62015-01-15 14:47:36 -080048 memset(&msg, 0, sizeof(msg));
49 msg.tid = tid;
50 msg.action = action;
Christopher Ferris6bcc4ac2014-09-12 14:44:20 -070051
Christopher Ferris9774df62015-01-15 14:47:36 -080052 int sock_fd = socket_local_client(DEBUGGER_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
Christopher Ferris6bcc4ac2014-09-12 14:44:20 -070053 SOCK_STREAM | SOCK_CLOEXEC);
54 if (sock_fd < 0) {
55 return -1;
56 }
57
Christopher Ferrisfa41e0f2015-01-13 19:07:12 -080058 if (timeout_secs > 0) {
59 struct timeval tm;
60 tm.tv_sec = timeout_secs;
61 tm.tv_usec = 0;
62 if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tm, sizeof(tm)) == -1) {
63 ALOGE("WARNING: Cannot set receive timeout value on socket: %s", strerror(errno));
64 }
65
66 if (setsockopt(sock_fd, SOL_SOCKET, SO_SNDTIMEO, &tm, sizeof(tm)) == -1) {
67 ALOGE("WARNING: Cannot set send timeout value on socket: %s", strerror(errno));
68 }
69 }
70
Christopher Ferris9774df62015-01-15 14:47:36 -080071 if (send_request(sock_fd, &msg, sizeof(msg)) < 0) {
Christopher Ferris6bcc4ac2014-09-12 14:44:20 -070072 TEMP_FAILURE_RETRY(close(sock_fd));
73 return -1;
74 }
75
76 return sock_fd;
Jeff Brown053b8652012-06-06 16:25:03 -070077}
78
79int dump_backtrace_to_file(pid_t tid, int fd) {
Christopher Ferrisfa41e0f2015-01-13 19:07:12 -080080 return dump_backtrace_to_file_timeout(tid, fd, 0);
81}
82
83int dump_backtrace_to_file_timeout(pid_t tid, int fd, int timeout_secs) {
84 int sock_fd = make_dump_request(DEBUGGER_ACTION_DUMP_BACKTRACE, tid, timeout_secs);
Christopher Ferris6bcc4ac2014-09-12 14:44:20 -070085 if (sock_fd < 0) {
86 return -1;
87 }
Jeff Brown053b8652012-06-06 16:25:03 -070088
Christopher Ferris6bcc4ac2014-09-12 14:44:20 -070089 /* Write the data read from the socket to the fd. */
90 int result = 0;
91 char buffer[1024];
92 ssize_t n;
93 while ((n = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer)))) > 0) {
94 if (TEMP_FAILURE_RETRY(write(fd, buffer, n)) != n) {
95 result = -1;
96 break;
Jeff Brown053b8652012-06-06 16:25:03 -070097 }
Christopher Ferris6bcc4ac2014-09-12 14:44:20 -070098 }
99 TEMP_FAILURE_RETRY(close(sock_fd));
100 return result;
101}
102
103int dump_tombstone(pid_t tid, char* pathbuf, size_t pathlen) {
Christopher Ferrisfa41e0f2015-01-13 19:07:12 -0800104 return dump_tombstone_timeout(tid, pathbuf, pathlen, 0);
105}
106
107int dump_tombstone_timeout(pid_t tid, char* pathbuf, size_t pathlen, int timeout_secs) {
108 int sock_fd = make_dump_request(DEBUGGER_ACTION_DUMP_TOMBSTONE, tid, timeout_secs);
Christopher Ferris6bcc4ac2014-09-12 14:44:20 -0700109 if (sock_fd < 0) {
110 return -1;
111 }
112
113 /* Read the tombstone file name. */
114 char buffer[100]; /* This is larger than the largest tombstone path. */
115 int result = 0;
116 ssize_t n = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer) - 1));
117 if (n <= 0) {
118 result = -1;
119 } else {
120 if (pathbuf && pathlen) {
121 if (n >= (ssize_t) pathlen) {
122 n = pathlen - 1;
123 }
124 buffer[n] = '\0';
125 memcpy(pathbuf, buffer, n + 1);
126 }
127 }
128 TEMP_FAILURE_RETRY(close(sock_fd));
129 return result;
Jeff Brown053b8652012-06-06 16:25:03 -0700130}