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 <stdbool.h>
18 #include <fcntl.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #include <cutils/debugger.h>
25 #include <cutils/sockets.h>
26
27 #if defined(__LP64__)
28 #include <elf.h>
29
is32bit(pid_t tid)30 static bool is32bit(pid_t tid) {
31 char* exeline;
32 if (asprintf(&exeline, "/proc/%d/exe", tid) == -1) {
33 return false;
34 }
35 int fd = open(exeline, O_RDONLY | O_CLOEXEC);
36 free(exeline);
37 if (fd == -1) {
38 return false;
39 }
40
41 char ehdr[EI_NIDENT];
42 ssize_t bytes = read(fd, &ehdr, sizeof(ehdr));
43 close(fd);
44 if (bytes != (ssize_t) sizeof(ehdr) || memcmp(ELFMAG, ehdr, SELFMAG) != 0) {
45 return false;
46 }
47 if (ehdr[EI_CLASS] == ELFCLASS32) {
48 return true;
49 }
50 return false;
51 }
52 #endif
53
send_request(int sock_fd,void * msg_ptr,size_t msg_len)54 static int send_request(int sock_fd, void* msg_ptr, size_t msg_len) {
55 int result = 0;
56 if (TEMP_FAILURE_RETRY(write(sock_fd, msg_ptr, msg_len)) != (ssize_t) msg_len) {
57 result = -1;
58 } else {
59 char ack;
60 if (TEMP_FAILURE_RETRY(read(sock_fd, &ack, 1)) != 1) {
61 result = -1;
62 }
63 }
64 return result;
65 }
66
make_dump_request(debugger_action_t action,pid_t tid)67 static int make_dump_request(debugger_action_t action, pid_t tid) {
68 const char* socket_name;
69 debugger_msg_t msg;
70 size_t msg_len;
71 void* msg_ptr;
72
73 #if defined(__LP64__)
74 debugger32_msg_t msg32;
75 if (is32bit(tid)) {
76 msg_len = sizeof(debugger32_msg_t);
77 memset(&msg32, 0, msg_len);
78 msg32.tid = tid;
79 msg32.action = action;
80 msg_ptr = &msg32;
81
82 socket_name = DEBUGGER32_SOCKET_NAME;
83 } else
84 #endif
85 {
86 msg_len = sizeof(debugger_msg_t);
87 memset(&msg, 0, msg_len);
88 msg.tid = tid;
89 msg.action = action;
90 msg_ptr = &msg;
91
92 socket_name = DEBUGGER_SOCKET_NAME;
93 }
94
95 int sock_fd = socket_local_client(socket_name, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
96 SOCK_STREAM | SOCK_CLOEXEC);
97 if (sock_fd < 0) {
98 return -1;
99 }
100
101 if (send_request(sock_fd, msg_ptr, msg_len) < 0) {
102 TEMP_FAILURE_RETRY(close(sock_fd));
103 return -1;
104 }
105
106 return sock_fd;
107 }
108
dump_backtrace_to_file(pid_t tid,int fd)109 int dump_backtrace_to_file(pid_t tid, int fd) {
110 int sock_fd = make_dump_request(DEBUGGER_ACTION_DUMP_BACKTRACE, tid);
111 if (sock_fd < 0) {
112 return -1;
113 }
114
115 /* Write the data read from the socket to the fd. */
116 int result = 0;
117 char buffer[1024];
118 ssize_t n;
119 while ((n = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer)))) > 0) {
120 if (TEMP_FAILURE_RETRY(write(fd, buffer, n)) != n) {
121 result = -1;
122 break;
123 }
124 }
125 TEMP_FAILURE_RETRY(close(sock_fd));
126 return result;
127 }
128
dump_tombstone(pid_t tid,char * pathbuf,size_t pathlen)129 int dump_tombstone(pid_t tid, char* pathbuf, size_t pathlen) {
130 int sock_fd = make_dump_request(DEBUGGER_ACTION_DUMP_TOMBSTONE, tid);
131 if (sock_fd < 0) {
132 return -1;
133 }
134
135 /* Read the tombstone file name. */
136 char buffer[100]; /* This is larger than the largest tombstone path. */
137 int result = 0;
138 ssize_t n = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer) - 1));
139 if (n <= 0) {
140 result = -1;
141 } else {
142 if (pathbuf && pathlen) {
143 if (n >= (ssize_t) pathlen) {
144 n = pathlen - 1;
145 }
146 buffer[n] = '\0';
147 memcpy(pathbuf, buffer, n + 1);
148 }
149 }
150 TEMP_FAILURE_RETRY(close(sock_fd));
151 return result;
152 }
153