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