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 <sys/types.h> 23 #include <sys/socket.h> 24 #include <unistd.h> 25 26 #include <cutils/debugger.h> 27 #include <cutils/sockets.h> 28 29 #define LOG_TAG "DEBUG" 30 #include <log/log.h> 31 32 #if defined(__LP64__) 33 #include <elf.h> 34 35 static bool is32bit(pid_t tid) { 36 char* exeline; 37 if (asprintf(&exeline, "/proc/%d/exe", tid) == -1) { 38 return false; 39 } 40 int fd = open(exeline, O_RDONLY | O_CLOEXEC); 41 free(exeline); 42 if (fd == -1) { 43 return false; 44 } 45 46 char ehdr[EI_NIDENT]; 47 ssize_t bytes = read(fd, &ehdr, sizeof(ehdr)); 48 close(fd); 49 if (bytes != (ssize_t) sizeof(ehdr) || memcmp(ELFMAG, ehdr, SELFMAG) != 0) { 50 return false; 51 } 52 if (ehdr[EI_CLASS] == ELFCLASS32) { 53 return true; 54 } 55 return false; 56 } 57 #endif 58 59 static int send_request(int sock_fd, void* msg_ptr, size_t msg_len) { 60 int result = 0; 61 if (TEMP_FAILURE_RETRY(write(sock_fd, msg_ptr, msg_len)) != (ssize_t) msg_len) { 62 result = -1; 63 } else { 64 char ack; 65 if (TEMP_FAILURE_RETRY(read(sock_fd, &ack, 1)) != 1) { 66 result = -1; 67 } 68 } 69 return result; 70 } 71 72 static int make_dump_request(debugger_action_t action, pid_t tid, int timeout_secs) { 73 const char* socket_name; 74 debugger_msg_t msg; 75 size_t msg_len; 76 void* msg_ptr; 77 78 #if defined(__LP64__) 79 debugger32_msg_t msg32; 80 if (is32bit(tid)) { 81 msg_len = sizeof(debugger32_msg_t); 82 memset(&msg32, 0, msg_len); 83 msg32.tid = tid; 84 msg32.action = action; 85 msg_ptr = &msg32; 86 87 socket_name = DEBUGGER32_SOCKET_NAME; 88 } else 89 #endif 90 { 91 msg_len = sizeof(debugger_msg_t); 92 memset(&msg, 0, msg_len); 93 msg.tid = tid; 94 msg.action = action; 95 msg_ptr = &msg; 96 97 socket_name = DEBUGGER_SOCKET_NAME; 98 } 99 100 int sock_fd = socket_local_client(socket_name, ANDROID_SOCKET_NAMESPACE_ABSTRACT, 101 SOCK_STREAM | SOCK_CLOEXEC); 102 if (sock_fd < 0) { 103 return -1; 104 } 105 106 if (timeout_secs > 0) { 107 struct timeval tm; 108 tm.tv_sec = timeout_secs; 109 tm.tv_usec = 0; 110 if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tm, sizeof(tm)) == -1) { 111 ALOGE("WARNING: Cannot set receive timeout value on socket: %s", strerror(errno)); 112 } 113 114 if (setsockopt(sock_fd, SOL_SOCKET, SO_SNDTIMEO, &tm, sizeof(tm)) == -1) { 115 ALOGE("WARNING: Cannot set send timeout value on socket: %s", strerror(errno)); 116 } 117 } 118 119 if (send_request(sock_fd, msg_ptr, msg_len) < 0) { 120 TEMP_FAILURE_RETRY(close(sock_fd)); 121 return -1; 122 } 123 124 return sock_fd; 125 } 126 127 int dump_backtrace_to_file(pid_t tid, int fd) { 128 return dump_backtrace_to_file_timeout(tid, fd, 0); 129 } 130 131 int dump_backtrace_to_file_timeout(pid_t tid, int fd, int timeout_secs) { 132 int sock_fd = make_dump_request(DEBUGGER_ACTION_DUMP_BACKTRACE, tid, timeout_secs); 133 if (sock_fd < 0) { 134 return -1; 135 } 136 137 /* Write the data read from the socket to the fd. */ 138 int result = 0; 139 char buffer[1024]; 140 ssize_t n; 141 while ((n = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer)))) > 0) { 142 if (TEMP_FAILURE_RETRY(write(fd, buffer, n)) != n) { 143 result = -1; 144 break; 145 } 146 } 147 TEMP_FAILURE_RETRY(close(sock_fd)); 148 return result; 149 } 150 151 int dump_tombstone(pid_t tid, char* pathbuf, size_t pathlen) { 152 return dump_tombstone_timeout(tid, pathbuf, pathlen, 0); 153 } 154 155 int dump_tombstone_timeout(pid_t tid, char* pathbuf, size_t pathlen, int timeout_secs) { 156 int sock_fd = make_dump_request(DEBUGGER_ACTION_DUMP_TOMBSTONE, tid, timeout_secs); 157 if (sock_fd < 0) { 158 return -1; 159 } 160 161 /* Read the tombstone file name. */ 162 char buffer[100]; /* This is larger than the largest tombstone path. */ 163 int result = 0; 164 ssize_t n = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer) - 1)); 165 if (n <= 0) { 166 result = -1; 167 } else { 168 if (pathbuf && pathlen) { 169 if (n >= (ssize_t) pathlen) { 170 n = pathlen - 1; 171 } 172 buffer[n] = '\0'; 173 memcpy(pathbuf, buffer, n + 1); 174 } 175 } 176 TEMP_FAILURE_RETRY(close(sock_fd)); 177 return result; 178 } 179