| Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 1 | /* |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 2 | * Copyright 2016, The Android Open Source Project |
| Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 3 | * |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 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 |
| Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 7 | * |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 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. |
| Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 17 | #include <debuggerd/client.h> |
| Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 18 | |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
| Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 20 | #include <signal.h> |
| Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 21 | #include <stdlib.h> |
| Josh Gao | ae9d767 | 2017-03-24 16:26:03 -0700 | [diff] [blame] | 22 | #include <sys/poll.h> |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 25 | #include <unistd.h> |
| 26 | |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 27 | #include <chrono> |
| Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 28 | |
| Josh Gao | ae9d767 | 2017-03-24 16:26:03 -0700 | [diff] [blame] | 29 | #include <android-base/file.h> |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 30 | #include <android-base/logging.h> |
| 31 | #include <android-base/stringprintf.h> |
| 32 | #include <android-base/unique_fd.h> |
| 33 | #include <cutils/sockets.h> |
| 34 | #include <debuggerd/handler.h> |
| 35 | #include <debuggerd/protocol.h> |
| 36 | #include <debuggerd/util.h> |
| Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 37 | |
| Josh Gao | ae9d767 | 2017-03-24 16:26:03 -0700 | [diff] [blame] | 38 | using namespace std::chrono_literals; |
| 39 | |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 40 | using android::base::unique_fd; |
| Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 41 | |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 42 | static bool send_signal(pid_t pid, bool backtrace) { |
| 43 | sigval val; |
| 44 | val.sival_int = backtrace; |
| 45 | if (sigqueue(pid, DEBUGGER_SIGNAL, val) != 0) { |
| 46 | PLOG(ERROR) << "libdebuggerd_client: failed to send signal to pid " << pid; |
| Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 47 | return false; |
| 48 | } |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 49 | return true; |
| Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| Josh Gao | ae9d767 | 2017-03-24 16:26:03 -0700 | [diff] [blame] | 52 | template <typename Duration> |
| 53 | static void populate_timeval(struct timeval* tv, const Duration& duration) { |
| 54 | auto seconds = std::chrono::duration_cast<std::chrono::seconds>(duration); |
| 55 | auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(duration - seconds); |
| 56 | tv->tv_sec = static_cast<long>(seconds.count()); |
| 57 | tv->tv_usec = static_cast<long>(microseconds.count()); |
| 58 | } |
| 59 | |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 60 | bool debuggerd_trigger_dump(pid_t pid, unique_fd output_fd, DebuggerdDumpType dump_type, |
| 61 | int timeout_ms) { |
| 62 | LOG(INFO) << "libdebuggerd_client: started dumping process " << pid; |
| 63 | unique_fd sockfd; |
| 64 | const auto end = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_ms); |
| Josh Gao | ae9d767 | 2017-03-24 16:26:03 -0700 | [diff] [blame] | 65 | auto time_left = [timeout_ms, &end]() { return end - std::chrono::steady_clock::now(); }; |
| 66 | auto set_timeout = [timeout_ms, &time_left](int sockfd) { |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 67 | if (timeout_ms <= 0) { |
| Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame^] | 68 | return -1; |
| Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 69 | } |
| Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 70 | |
| Josh Gao | ae9d767 | 2017-03-24 16:26:03 -0700 | [diff] [blame] | 71 | auto remaining = time_left(); |
| 72 | if (remaining < decltype(remaining)::zero()) { |
| Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame^] | 73 | LOG(ERROR) << "timeout expired"; |
| 74 | return -1; |
| Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 75 | } |
| Josh Gao | ae9d767 | 2017-03-24 16:26:03 -0700 | [diff] [blame] | 76 | struct timeval timeout; |
| 77 | populate_timeval(&timeout, remaining); |
| Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 78 | |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 79 | if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) != 0) { |
| Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame^] | 80 | return -1; |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 81 | } |
| 82 | if (setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) != 0) { |
| Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame^] | 83 | return -1; |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 84 | } |
| Josh Gao | 218f7fb | 2016-10-07 16:42:05 -0700 | [diff] [blame] | 85 | |
| Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame^] | 86 | return sockfd; |
| Josh Gao | 218f7fb | 2016-10-07 16:42:05 -0700 | [diff] [blame] | 87 | }; |
| Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 88 | |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 89 | sockfd.reset(socket(AF_LOCAL, SOCK_SEQPACKET, 0)); |
| 90 | if (sockfd == -1) { |
| 91 | PLOG(ERROR) << "libdebugger_client: failed to create socket"; |
| 92 | return false; |
| Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame^] | 95 | if (socket_local_client_connect(set_timeout(sockfd.get()), kTombstonedInterceptSocketName, |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 96 | ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET) == -1) { |
| 97 | PLOG(ERROR) << "libdebuggerd_client: failed to connect to tombstoned"; |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | InterceptRequest req = {.pid = pid }; |
| Josh Gao | ae9d767 | 2017-03-24 16:26:03 -0700 | [diff] [blame] | 102 | if (!set_timeout(sockfd)) { |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 103 | PLOG(ERROR) << "libdebugger_client: failed to set timeout"; |
| Josh Gao | ae9d767 | 2017-03-24 16:26:03 -0700 | [diff] [blame] | 104 | return false; |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| Josh Gao | ae9d767 | 2017-03-24 16:26:03 -0700 | [diff] [blame] | 107 | // Create an intermediate pipe to pass to the other end. |
| 108 | unique_fd pipe_read, pipe_write; |
| 109 | if (!Pipe(&pipe_read, &pipe_write)) { |
| 110 | PLOG(ERROR) << "libdebuggerd_client: failed to create pipe"; |
| 111 | return false; |
| 112 | } |
| 113 | |
| Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame^] | 114 | if (send_fd(set_timeout(sockfd), &req, sizeof(req), std::move(pipe_write)) != sizeof(req)) { |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 115 | PLOG(ERROR) << "libdebuggerd_client: failed to send output fd to tombstoned"; |
| 116 | return false; |
| 117 | } |
| 118 | |
| Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame^] | 119 | // Check to make sure we've successfully registered. |
| 120 | InterceptResponse response; |
| 121 | ssize_t rc = |
| 122 | TEMP_FAILURE_RETRY(recv(set_timeout(sockfd.get()), &response, sizeof(response), MSG_TRUNC)); |
| 123 | if (rc == 0) { |
| 124 | LOG(ERROR) << "libdebuggerd_client: failed to read response from tombstoned: timeout reached?"; |
| 125 | return false; |
| 126 | } else if (rc != sizeof(response)) { |
| 127 | LOG(ERROR) |
| 128 | << "libdebuggerd_client: received packet of unexpected length from tombstoned: expected " |
| 129 | << sizeof(response) << ", received " << rc; |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | if (response.status != InterceptStatus::kRegistered) { |
| 134 | LOG(ERROR) << "libdebuggerd_client: unexpected registration response: " |
| 135 | << static_cast<int>(response.status); |
| 136 | return false; |
| 137 | } |
| 138 | |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 139 | bool backtrace = dump_type == kDebuggerdBacktrace; |
| 140 | send_signal(pid, backtrace); |
| 141 | |
| Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame^] | 142 | rc = TEMP_FAILURE_RETRY(recv(set_timeout(sockfd.get()), &response, sizeof(response), MSG_TRUNC)); |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 143 | if (rc == 0) { |
| 144 | LOG(ERROR) << "libdebuggerd_client: failed to read response from tombstoned: timeout reached?"; |
| 145 | return false; |
| 146 | } else if (rc != sizeof(response)) { |
| 147 | LOG(ERROR) |
| 148 | << "libdebuggerd_client: received packet of unexpected length from tombstoned: expected " |
| 149 | << sizeof(response) << ", received " << rc; |
| 150 | return false; |
| 151 | } |
| 152 | |
| Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame^] | 153 | if (response.status != InterceptStatus::kStarted) { |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 154 | response.error_message[sizeof(response.error_message) - 1] = '\0'; |
| 155 | LOG(ERROR) << "libdebuggerd_client: tombstoned reported failure: " << response.error_message; |
| Josh Gao | ae9d767 | 2017-03-24 16:26:03 -0700 | [diff] [blame] | 156 | return false; |
| 157 | } |
| 158 | |
| 159 | // Forward output from the pipe to the output fd. |
| 160 | while (true) { |
| 161 | auto remaining_ms = std::chrono::duration_cast<std::chrono::milliseconds>(time_left()); |
| 162 | if (remaining_ms <= 1ms) { |
| 163 | LOG(ERROR) << "libdebuggerd_client: timeout expired"; |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | struct pollfd pfd = { |
| 168 | .fd = pipe_read.get(), .events = POLLIN, .revents = 0, |
| 169 | }; |
| 170 | |
| 171 | rc = poll(&pfd, 1, remaining_ms.count()); |
| 172 | if (rc == -1) { |
| 173 | if (errno == EINTR) { |
| 174 | continue; |
| 175 | } else { |
| 176 | PLOG(ERROR) << "libdebuggerd_client: error while polling"; |
| 177 | return false; |
| 178 | } |
| 179 | } else if (rc == 0) { |
| 180 | LOG(ERROR) << "libdebuggerd_client: timeout expired"; |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | char buf[1024]; |
| 185 | rc = TEMP_FAILURE_RETRY(read(pipe_read.get(), buf, sizeof(buf))); |
| 186 | if (rc == 0) { |
| 187 | // Done. |
| 188 | break; |
| 189 | } else if (rc == -1) { |
| 190 | PLOG(ERROR) << "libdebuggerd_client: error while reading"; |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | if (!android::base::WriteFully(output_fd.get(), buf, rc)) { |
| 195 | PLOG(ERROR) << "libdebuggerd_client: error while writing"; |
| 196 | return false; |
| 197 | } |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | LOG(INFO) << "libdebuggerd_client: done dumping process " << pid; |
| 201 | |
| 202 | return true; |
| Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 205 | int dump_backtrace_to_file(pid_t tid, int fd) { |
| 206 | return dump_backtrace_to_file_timeout(tid, fd, 0); |
| 207 | } |
| 208 | |
| 209 | int dump_backtrace_to_file_timeout(pid_t tid, int fd, int timeout_secs) { |
| 210 | android::base::unique_fd copy(dup(fd)); |
| 211 | if (copy == -1) { |
| 212 | return -1; |
| Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 213 | } |
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 214 | int timeout_ms = timeout_secs > 0 ? timeout_secs * 1000 : 0; |
| 215 | return debuggerd_trigger_dump(tid, std::move(copy), kDebuggerdBacktrace, timeout_ms) ? 0 : -1; |
| Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 216 | } |