Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 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 | |
Christopher Ferris | 6bcc4ac | 2014-09-12 14:44:20 -0700 | [diff] [blame] | 17 | #include <stdbool.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <stdio.h> |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 20 | #include <stdlib.h> |
Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 21 | #include <string.h> |
Christopher Ferris | fa41e0f | 2015-01-13 19:07:12 -0800 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | #include <sys/socket.h> |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | |
| 26 | #include <cutils/debugger.h> |
| 27 | #include <cutils/sockets.h> |
| 28 | |
Christopher Ferris | fa41e0f | 2015-01-13 19:07:12 -0800 | [diff] [blame] | 29 | #define LOG_TAG "DEBUG" |
| 30 | #include <log/log.h> |
| 31 | |
Christopher Ferris | 6bcc4ac | 2014-09-12 14:44:20 -0700 | [diff] [blame] | 32 | static 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 Ferris | fa41e0f | 2015-01-13 19:07:12 -0800 | [diff] [blame] | 45 | static int make_dump_request(debugger_action_t action, pid_t tid, int timeout_secs) { |
Christopher Ferris | 6bcc4ac | 2014-09-12 14:44:20 -0700 | [diff] [blame] | 46 | debugger_msg_t msg; |
Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 47 | memset(&msg, 0, sizeof(msg)); |
| 48 | msg.tid = tid; |
| 49 | msg.action = action; |
Christopher Ferris | 6bcc4ac | 2014-09-12 14:44:20 -0700 | [diff] [blame] | 50 | |
Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 51 | int sock_fd = socket_local_client(DEBUGGER_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, |
Christopher Ferris | 6bcc4ac | 2014-09-12 14:44:20 -0700 | [diff] [blame] | 52 | SOCK_STREAM | SOCK_CLOEXEC); |
| 53 | if (sock_fd < 0) { |
| 54 | return -1; |
| 55 | } |
| 56 | |
Christopher Ferris | fa41e0f | 2015-01-13 19:07:12 -0800 | [diff] [blame] | 57 | if (timeout_secs > 0) { |
| 58 | struct timeval tm; |
| 59 | tm.tv_sec = timeout_secs; |
| 60 | tm.tv_usec = 0; |
| 61 | if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tm, sizeof(tm)) == -1) { |
| 62 | ALOGE("WARNING: Cannot set receive timeout value on socket: %s", strerror(errno)); |
| 63 | } |
| 64 | |
| 65 | if (setsockopt(sock_fd, SOL_SOCKET, SO_SNDTIMEO, &tm, sizeof(tm)) == -1) { |
| 66 | ALOGE("WARNING: Cannot set send timeout value on socket: %s", strerror(errno)); |
| 67 | } |
| 68 | } |
| 69 | |
Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 70 | if (send_request(sock_fd, &msg, sizeof(msg)) < 0) { |
Elliott Hughes | 9fc8343 | 2015-05-15 19:16:40 -0700 | [diff] [blame] | 71 | close(sock_fd); |
Christopher Ferris | 6bcc4ac | 2014-09-12 14:44:20 -0700 | [diff] [blame] | 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | return sock_fd; |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | int dump_backtrace_to_file(pid_t tid, int fd) { |
Christopher Ferris | fa41e0f | 2015-01-13 19:07:12 -0800 | [diff] [blame] | 79 | return dump_backtrace_to_file_timeout(tid, fd, 0); |
| 80 | } |
| 81 | |
| 82 | int dump_backtrace_to_file_timeout(pid_t tid, int fd, int timeout_secs) { |
| 83 | int sock_fd = make_dump_request(DEBUGGER_ACTION_DUMP_BACKTRACE, tid, timeout_secs); |
Christopher Ferris | 6bcc4ac | 2014-09-12 14:44:20 -0700 | [diff] [blame] | 84 | if (sock_fd < 0) { |
| 85 | return -1; |
| 86 | } |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 87 | |
Christopher Ferris | 6bcc4ac | 2014-09-12 14:44:20 -0700 | [diff] [blame] | 88 | /* Write the data read from the socket to the fd. */ |
| 89 | int result = 0; |
| 90 | char buffer[1024]; |
| 91 | ssize_t n; |
| 92 | while ((n = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer)))) > 0) { |
| 93 | if (TEMP_FAILURE_RETRY(write(fd, buffer, n)) != n) { |
| 94 | result = -1; |
| 95 | break; |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 96 | } |
Christopher Ferris | 6bcc4ac | 2014-09-12 14:44:20 -0700 | [diff] [blame] | 97 | } |
Elliott Hughes | 9fc8343 | 2015-05-15 19:16:40 -0700 | [diff] [blame] | 98 | close(sock_fd); |
Christopher Ferris | 6bcc4ac | 2014-09-12 14:44:20 -0700 | [diff] [blame] | 99 | return result; |
| 100 | } |
| 101 | |
| 102 | int dump_tombstone(pid_t tid, char* pathbuf, size_t pathlen) { |
Christopher Ferris | fa41e0f | 2015-01-13 19:07:12 -0800 | [diff] [blame] | 103 | return dump_tombstone_timeout(tid, pathbuf, pathlen, 0); |
| 104 | } |
| 105 | |
| 106 | int dump_tombstone_timeout(pid_t tid, char* pathbuf, size_t pathlen, int timeout_secs) { |
| 107 | int sock_fd = make_dump_request(DEBUGGER_ACTION_DUMP_TOMBSTONE, tid, timeout_secs); |
Christopher Ferris | 6bcc4ac | 2014-09-12 14:44:20 -0700 | [diff] [blame] | 108 | if (sock_fd < 0) { |
| 109 | return -1; |
| 110 | } |
| 111 | |
| 112 | /* Read the tombstone file name. */ |
| 113 | char buffer[100]; /* This is larger than the largest tombstone path. */ |
| 114 | int result = 0; |
| 115 | ssize_t n = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer) - 1)); |
| 116 | if (n <= 0) { |
| 117 | result = -1; |
| 118 | } else { |
| 119 | if (pathbuf && pathlen) { |
| 120 | if (n >= (ssize_t) pathlen) { |
| 121 | n = pathlen - 1; |
| 122 | } |
| 123 | buffer[n] = '\0'; |
| 124 | memcpy(pathbuf, buffer, n + 1); |
| 125 | } |
| 126 | } |
Elliott Hughes | 9fc8343 | 2015-05-15 19:16:40 -0700 | [diff] [blame] | 127 | close(sock_fd); |
Christopher Ferris | 6bcc4ac | 2014-09-12 14:44:20 -0700 | [diff] [blame] | 128 | return result; |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 129 | } |