blob: cb7cbbe0fe5a2867b652d921de8e0c2fec0d5776 [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>
Josh Gao5675f3c2017-06-01 12:19:53 -070031#include <android-base/parseint.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070032#include <android-base/stringprintf.h>
Josh Gao5675f3c2017-06-01 12:19:53 -070033#include <android-base/strings.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070034#include <android-base/unique_fd.h>
35#include <cutils/sockets.h>
Narayan Kamath2d377cd2017-05-10 10:58:59 +010036
37#include "debuggerd/handler.h"
38#include "protocol.h"
39#include "util.h"
Josh Gao9c02dc52016-06-15 17:29:00 -070040
Josh Gaoae9d7672017-03-24 16:26:03 -070041using namespace std::chrono_literals;
42
Josh Gaocbe70cb2016-10-18 18:17:52 -070043using android::base::unique_fd;
Josh Gao9c02dc52016-06-15 17:29:00 -070044
Narayan Kamatha73df602017-05-24 15:07:25 +010045static bool send_signal(pid_t pid, const DebuggerdDumpType dump_type) {
46 const int signal = (dump_type == kDebuggerdJavaBacktrace) ? SIGQUIT : DEBUGGER_SIGNAL;
Josh Gaocbe70cb2016-10-18 18:17:52 -070047 sigval val;
Narayan Kamatha73df602017-05-24 15:07:25 +010048 val.sival_int = (dump_type == kDebuggerdNativeBacktrace) ? 1 : 0;
49
50 if (sigqueue(pid, signal, val) != 0) {
Josh Gaocbe70cb2016-10-18 18:17:52 -070051 PLOG(ERROR) << "libdebuggerd_client: failed to send signal to pid " << pid;
Josh Gao9c02dc52016-06-15 17:29:00 -070052 return false;
53 }
Josh Gaocbe70cb2016-10-18 18:17:52 -070054 return true;
Josh Gao9c02dc52016-06-15 17:29:00 -070055}
56
Josh Gaoae9d7672017-03-24 16:26:03 -070057template <typename Duration>
58static 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 Kamatha73df602017-05-24 15:07:25 +010065bool debuggerd_trigger_dump(pid_t pid, DebuggerdDumpType dump_type, unsigned int timeout_ms,
66 unique_fd output_fd) {
Josh Gaocbe70cb2016-10-18 18:17:52 -070067 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 Gao287d50d2017-04-04 13:43:21 -070070 auto time_left = [&end]() { return end - std::chrono::steady_clock::now(); };
Josh Gaoae9d7672017-03-24 16:26:03 -070071 auto set_timeout = [timeout_ms, &time_left](int sockfd) {
Josh Gaocbe70cb2016-10-18 18:17:52 -070072 if (timeout_ms <= 0) {
Josh Gao287d50d2017-04-04 13:43:21 -070073 return sockfd;
Josh Gao9c02dc52016-06-15 17:29:00 -070074 }
Josh Gao9c02dc52016-06-15 17:29:00 -070075
Josh Gaoae9d7672017-03-24 16:26:03 -070076 auto remaining = time_left();
77 if (remaining < decltype(remaining)::zero()) {
Josh Gao287d50d2017-04-04 13:43:21 -070078 LOG(ERROR) << "libdebuggerd_client: timeout expired";
Josh Gao460b3362017-03-30 16:40:47 -070079 return -1;
Josh Gao9c02dc52016-06-15 17:29:00 -070080 }
Josh Gao287d50d2017-04-04 13:43:21 -070081
Josh Gaoae9d7672017-03-24 16:26:03 -070082 struct timeval timeout;
83 populate_timeval(&timeout, remaining);
Josh Gao9c02dc52016-06-15 17:29:00 -070084
Josh Gaocbe70cb2016-10-18 18:17:52 -070085 if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) != 0) {
Josh Gao287d50d2017-04-04 13:43:21 -070086 PLOG(ERROR) << "libdebuggerd_client: failed to set receive timeout";
Josh Gao460b3362017-03-30 16:40:47 -070087 return -1;
Josh Gaocbe70cb2016-10-18 18:17:52 -070088 }
89 if (setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) != 0) {
Josh Gao287d50d2017-04-04 13:43:21 -070090 PLOG(ERROR) << "libdebuggerd_client: failed to set send timeout";
Josh Gao460b3362017-03-30 16:40:47 -070091 return -1;
Josh Gaocbe70cb2016-10-18 18:17:52 -070092 }
Josh Gao218f7fb2016-10-07 16:42:05 -070093
Josh Gao460b3362017-03-30 16:40:47 -070094 return sockfd;
Josh Gao218f7fb2016-10-07 16:42:05 -070095 };
Josh Gao9c02dc52016-06-15 17:29:00 -070096
Josh Gaocbe70cb2016-10-18 18:17:52 -070097 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 Gao9c02dc52016-06-15 17:29:00 -0700101 }
102
Josh Gao460b3362017-03-30 16:40:47 -0700103 if (socket_local_client_connect(set_timeout(sockfd.get()), kTombstonedInterceptSocketName,
Josh Gaocbe70cb2016-10-18 18:17:52 -0700104 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET) == -1) {
105 PLOG(ERROR) << "libdebuggerd_client: failed to connect to tombstoned";
106 return false;
107 }
108
Narayan Kamatha73df602017-05-24 15:07:25 +0100109 InterceptRequest req = {.pid = pid, .dump_type = dump_type};
Josh Gaoae9d7672017-03-24 16:26:03 -0700110 if (!set_timeout(sockfd)) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700111 PLOG(ERROR) << "libdebugger_client: failed to set timeout";
Josh Gaoae9d7672017-03-24 16:26:03 -0700112 return false;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700113 }
114
Josh Gaoae9d7672017-03-24 16:26:03 -0700115 // 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 Gao5675f3c2017-06-01 12:19:53 -0700122 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 Gao460b3362017-03-30 16:40:47 -0700136 if (send_fd(set_timeout(sockfd), &req, sizeof(req), std::move(pipe_write)) != sizeof(req)) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700137 PLOG(ERROR) << "libdebuggerd_client: failed to send output fd to tombstoned";
138 return false;
139 }
140
Josh Gao460b3362017-03-30 16:40:47 -0700141 // 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 Kamatha73df602017-05-24 15:07:25 +0100161 if (!send_signal(pid, dump_type)) {
Liu Changcheng34922212017-04-06 11:20:14 +0800162 return false;
163 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700164
Josh Gao460b3362017-03-30 16:40:47 -0700165 rc = TEMP_FAILURE_RETRY(recv(set_timeout(sockfd.get()), &response, sizeof(response), MSG_TRUNC));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700166 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 Gao460b3362017-03-30 16:40:47 -0700176 if (response.status != InterceptStatus::kStarted) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700177 response.error_message[sizeof(response.error_message) - 1] = '\0';
178 LOG(ERROR) << "libdebuggerd_client: tombstoned reported failure: " << response.error_message;
Josh Gaoae9d7672017-03-24 16:26:03 -0700179 return false;
180 }
181
182 // Forward output from the pipe to the output fd.
183 while (true) {
Josh Gao287d50d2017-04-04 13:43:21 -0700184 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 Gaoae9d7672017-03-24 16:26:03 -0700188 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 Gao287d50d2017-04-04 13:43:21 -0700196 rc = poll(&pfd, 1, remaining_ms);
Josh Gaoae9d7672017-03-24 16:26:03 -0700197 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 Gaocbe70cb2016-10-18 18:17:52 -0700223 }
224
225 LOG(INFO) << "libdebuggerd_client: done dumping process " << pid;
226
227 return true;
Josh Gao9c02dc52016-06-15 17:29:00 -0700228}
229
Narayan Kamatha73df602017-05-24 15:07:25 +0100230int 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 Gaocbe70cb2016-10-18 18:17:52 -0700232}
233
Narayan Kamatha73df602017-05-24 15:07:25 +0100234int dump_backtrace_to_file_timeout(pid_t tid, DebuggerdDumpType dump_type, int timeout_secs,
235 int fd) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700236 android::base::unique_fd copy(dup(fd));
237 if (copy == -1) {
238 return -1;
Josh Gao9c02dc52016-06-15 17:29:00 -0700239 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700240 int timeout_ms = timeout_secs > 0 ? timeout_secs * 1000 : 0;
Narayan Kamatha73df602017-05-24 15:07:25 +0100241 return debuggerd_trigger_dump(tid, dump_type, timeout_ms, std::move(copy)) ? 0 : -1;
Josh Gao9c02dc52016-06-15 17:29:00 -0700242}