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