blob: 224444f1f15555286f43431360d1a08c166bc06a [file] [log] [blame]
Josh Gao9c02dc52016-06-15 17:29:00 -07001/*
Josh Gaocbe70cb2016-10-18 18:17:52 -07002 * Copyright 2016, The Android Open Source Project
Josh Gao9c02dc52016-06-15 17:29:00 -07003 *
Josh Gaocbe70cb2016-10-18 18:17:52 -07004 * 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 Gao9c02dc52016-06-15 17:29:00 -07007 *
Josh Gaocbe70cb2016-10-18 18:17:52 -07008 * 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 Gao9c02dc52016-06-15 17:29:00 -070015 */
16
Josh Gaocbe70cb2016-10-18 18:17:52 -070017#include <debuggerd/client.h>
Josh Gao9c02dc52016-06-15 17:29:00 -070018
Josh Gaocbe70cb2016-10-18 18:17:52 -070019#include <fcntl.h>
Josh Gao9c02dc52016-06-15 17:29:00 -070020#include <signal.h>
Josh Gao9c02dc52016-06-15 17:29:00 -070021#include <stdlib.h>
Josh Gaoae9d7672017-03-24 16:26:03 -070022#include <sys/poll.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070023#include <sys/stat.h>
24#include <sys/types.h>
Josh Gao9c02dc52016-06-15 17:29:00 -070025#include <unistd.h>
26
Josh Gaocbe70cb2016-10-18 18:17:52 -070027#include <chrono>
Josh Gao9c02dc52016-06-15 17:29:00 -070028
Josh Gaoae9d7672017-03-24 16:26:03 -070029#include <android-base/file.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070030#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 Gao9c02dc52016-06-15 17:29:00 -070037
Josh Gaoae9d7672017-03-24 16:26:03 -070038using namespace std::chrono_literals;
39
Josh Gaocbe70cb2016-10-18 18:17:52 -070040using android::base::unique_fd;
Josh Gao9c02dc52016-06-15 17:29:00 -070041
Josh Gaocbe70cb2016-10-18 18:17:52 -070042static 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 Gao9c02dc52016-06-15 17:29:00 -070047 return false;
48 }
Josh Gaocbe70cb2016-10-18 18:17:52 -070049 return true;
Josh Gao9c02dc52016-06-15 17:29:00 -070050}
51
Josh Gaoae9d7672017-03-24 16:26:03 -070052template <typename Duration>
53static 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 Gaocbe70cb2016-10-18 18:17:52 -070060bool 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 Gaoae9d7672017-03-24 16:26:03 -070065 auto time_left = [timeout_ms, &end]() { return end - std::chrono::steady_clock::now(); };
66 auto set_timeout = [timeout_ms, &time_left](int sockfd) {
Josh Gaocbe70cb2016-10-18 18:17:52 -070067 if (timeout_ms <= 0) {
Josh Gao460b3362017-03-30 16:40:47 -070068 return -1;
Josh Gao9c02dc52016-06-15 17:29:00 -070069 }
Josh Gao9c02dc52016-06-15 17:29:00 -070070
Josh Gaoae9d7672017-03-24 16:26:03 -070071 auto remaining = time_left();
72 if (remaining < decltype(remaining)::zero()) {
Josh Gao460b3362017-03-30 16:40:47 -070073 LOG(ERROR) << "timeout expired";
74 return -1;
Josh Gao9c02dc52016-06-15 17:29:00 -070075 }
Josh Gaoae9d7672017-03-24 16:26:03 -070076 struct timeval timeout;
77 populate_timeval(&timeout, remaining);
Josh Gao9c02dc52016-06-15 17:29:00 -070078
Josh Gaocbe70cb2016-10-18 18:17:52 -070079 if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) != 0) {
Josh Gao460b3362017-03-30 16:40:47 -070080 return -1;
Josh Gaocbe70cb2016-10-18 18:17:52 -070081 }
82 if (setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) != 0) {
Josh Gao460b3362017-03-30 16:40:47 -070083 return -1;
Josh Gaocbe70cb2016-10-18 18:17:52 -070084 }
Josh Gao218f7fb2016-10-07 16:42:05 -070085
Josh Gao460b3362017-03-30 16:40:47 -070086 return sockfd;
Josh Gao218f7fb2016-10-07 16:42:05 -070087 };
Josh Gao9c02dc52016-06-15 17:29:00 -070088
Josh Gaocbe70cb2016-10-18 18:17:52 -070089 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 Gao9c02dc52016-06-15 17:29:00 -070093 }
94
Josh Gao460b3362017-03-30 16:40:47 -070095 if (socket_local_client_connect(set_timeout(sockfd.get()), kTombstonedInterceptSocketName,
Josh Gaocbe70cb2016-10-18 18:17:52 -070096 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 Gaoae9d7672017-03-24 16:26:03 -0700102 if (!set_timeout(sockfd)) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700103 PLOG(ERROR) << "libdebugger_client: failed to set timeout";
Josh Gaoae9d7672017-03-24 16:26:03 -0700104 return false;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700105 }
106
Josh Gaoae9d7672017-03-24 16:26:03 -0700107 // 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 Gao460b3362017-03-30 16:40:47 -0700114 if (send_fd(set_timeout(sockfd), &req, sizeof(req), std::move(pipe_write)) != sizeof(req)) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700115 PLOG(ERROR) << "libdebuggerd_client: failed to send output fd to tombstoned";
116 return false;
117 }
118
Josh Gao460b3362017-03-30 16:40:47 -0700119 // 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 Gaocbe70cb2016-10-18 18:17:52 -0700139 bool backtrace = dump_type == kDebuggerdBacktrace;
140 send_signal(pid, backtrace);
141
Josh Gao460b3362017-03-30 16:40:47 -0700142 rc = TEMP_FAILURE_RETRY(recv(set_timeout(sockfd.get()), &response, sizeof(response), MSG_TRUNC));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700143 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 Gao460b3362017-03-30 16:40:47 -0700153 if (response.status != InterceptStatus::kStarted) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700154 response.error_message[sizeof(response.error_message) - 1] = '\0';
155 LOG(ERROR) << "libdebuggerd_client: tombstoned reported failure: " << response.error_message;
Josh Gaoae9d7672017-03-24 16:26:03 -0700156 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 Gaocbe70cb2016-10-18 18:17:52 -0700198 }
199
200 LOG(INFO) << "libdebuggerd_client: done dumping process " << pid;
201
202 return true;
Josh Gao9c02dc52016-06-15 17:29:00 -0700203}
204
Josh Gaocbe70cb2016-10-18 18:17:52 -0700205int dump_backtrace_to_file(pid_t tid, int fd) {
206 return dump_backtrace_to_file_timeout(tid, fd, 0);
207}
208
209int 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 Gao9c02dc52016-06-15 17:29:00 -0700213 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700214 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 Gao9c02dc52016-06-15 17:29:00 -0700216}