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> |
Kalesh Singh | 1489e27 | 2019-07-14 12:15:35 -0700 | [diff] [blame] | 25 | #include <time.h> |
Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 28 | #include <chrono> |
Kalesh Singh | 1489e27 | 2019-07-14 12:15:35 -0700 | [diff] [blame] | 29 | #include <iomanip> |
Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 30 | |
Josh Gao | 5f87bbd | 2019-01-09 17:01:49 -0800 | [diff] [blame] | 31 | #include <android-base/cmsg.h> |
Josh Gao | ae9d767 | 2017-03-24 16:26:03 -0700 | [diff] [blame] | 32 | #include <android-base/file.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 33 | #include <android-base/logging.h> |
Josh Gao | 5675f3c | 2017-06-01 12:19:53 -0700 | [diff] [blame] | 34 | #include <android-base/parseint.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 35 | #include <android-base/stringprintf.h> |
Josh Gao | 5675f3c | 2017-06-01 12:19:53 -0700 | [diff] [blame] | 36 | #include <android-base/strings.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 37 | #include <android-base/unique_fd.h> |
Josh Gao | a48b41b | 2019-12-13 14:11:04 -0800 | [diff] [blame^] | 38 | #include <bionic/reserved_signals.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 39 | #include <cutils/sockets.h> |
Josh Gao | 4175cee | 2019-01-04 13:57:09 -0800 | [diff] [blame] | 40 | #include <procinfo/process.h> |
Narayan Kamath | 2d377cd | 2017-05-10 10:58:59 +0100 | [diff] [blame] | 41 | |
| 42 | #include "debuggerd/handler.h" |
| 43 | #include "protocol.h" |
| 44 | #include "util.h" |
Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 45 | |
Josh Gao | ae9d767 | 2017-03-24 16:26:03 -0700 | [diff] [blame] | 46 | using namespace std::chrono_literals; |
| 47 | |
Kalesh Singh | 1489e27 | 2019-07-14 12:15:35 -0700 | [diff] [blame] | 48 | using android::base::ReadFileToString; |
Josh Gao | 5f87bbd | 2019-01-09 17:01:49 -0800 | [diff] [blame] | 49 | using android::base::SendFileDescriptors; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 50 | using android::base::unique_fd; |
Kalesh Singh | 1489e27 | 2019-07-14 12:15:35 -0700 | [diff] [blame] | 51 | using android::base::WriteStringToFd; |
Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 52 | |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 53 | static bool send_signal(pid_t pid, const DebuggerdDumpType dump_type) { |
Josh Gao | a48b41b | 2019-12-13 14:11:04 -0800 | [diff] [blame^] | 54 | const int signal = (dump_type == kDebuggerdJavaBacktrace) ? SIGQUIT : BIONIC_SIGNAL_DEBUGGER; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 55 | sigval val; |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 56 | val.sival_int = (dump_type == kDebuggerdNativeBacktrace) ? 1 : 0; |
| 57 | |
| 58 | if (sigqueue(pid, signal, val) != 0) { |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 59 | PLOG(ERROR) << "libdebuggerd_client: failed to send signal to pid " << pid; |
Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 60 | return false; |
| 61 | } |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 62 | return true; |
Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Josh Gao | ae9d767 | 2017-03-24 16:26:03 -0700 | [diff] [blame] | 65 | template <typename Duration> |
| 66 | static void populate_timeval(struct timeval* tv, const Duration& duration) { |
| 67 | auto seconds = std::chrono::duration_cast<std::chrono::seconds>(duration); |
| 68 | auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(duration - seconds); |
| 69 | tv->tv_sec = static_cast<long>(seconds.count()); |
| 70 | tv->tv_usec = static_cast<long>(microseconds.count()); |
| 71 | } |
| 72 | |
Kalesh Singh | 1489e27 | 2019-07-14 12:15:35 -0700 | [diff] [blame] | 73 | static void get_wchan_header(pid_t pid, std::stringstream& buffer) { |
| 74 | struct tm now; |
| 75 | time_t t = time(nullptr); |
| 76 | localtime_r(&t, &now); |
| 77 | char timestamp[32]; |
| 78 | strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", &now); |
| 79 | std::string time_now(timestamp); |
| 80 | |
| 81 | std::string path = "/proc/" + std::to_string(pid) + "/cmdline"; |
| 82 | |
| 83 | char proc_name_buf[1024]; |
| 84 | const char* proc_name = nullptr; |
| 85 | std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(path.c_str(), "r"), &fclose); |
| 86 | |
| 87 | if (fp) { |
| 88 | proc_name = fgets(proc_name_buf, sizeof(proc_name_buf), fp.get()); |
| 89 | } |
| 90 | |
| 91 | if (!proc_name) { |
| 92 | proc_name = "<unknown>"; |
| 93 | } |
| 94 | |
| 95 | buffer << "\n----- Waiting Channels: pid " << pid << " at " << time_now << " -----\n" |
| 96 | << "Cmd line: " << proc_name << "\n"; |
| 97 | } |
| 98 | |
| 99 | static void get_wchan_footer(pid_t pid, std::stringstream& buffer) { |
| 100 | buffer << "----- end " << std::to_string(pid) << " -----\n"; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Returns the wchan data for each thread in the process, |
| 105 | * or empty string if unable to obtain any data. |
| 106 | */ |
| 107 | static std::string get_wchan_data(pid_t pid) { |
| 108 | std::stringstream buffer; |
| 109 | std::vector<pid_t> tids; |
| 110 | |
| 111 | if (!android::procinfo::GetProcessTids(pid, &tids)) { |
| 112 | LOG(WARNING) << "libdebuggerd_client: Failed to get process tids"; |
| 113 | return buffer.str(); |
| 114 | } |
| 115 | |
| 116 | std::stringstream data; |
| 117 | for (int tid : tids) { |
| 118 | std::string path = "/proc/" + std::to_string(pid) + "/task/" + std::to_string(tid) + "/wchan"; |
| 119 | std::string wchan_str; |
| 120 | if (!ReadFileToString(path, &wchan_str, true)) { |
| 121 | PLOG(WARNING) << "libdebuggerd_client: Failed to read \"" << path << "\""; |
| 122 | continue; |
| 123 | } |
| 124 | data << "sysTid=" << std::left << std::setw(10) << tid << wchan_str << "\n"; |
| 125 | } |
| 126 | |
| 127 | if (std::string str = data.str(); !str.empty()) { |
| 128 | get_wchan_header(pid, buffer); |
| 129 | buffer << "\n" << str << "\n"; |
| 130 | get_wchan_footer(pid, buffer); |
| 131 | buffer << "\n"; |
| 132 | } |
| 133 | |
| 134 | return buffer.str(); |
| 135 | } |
| 136 | |
| 137 | static void dump_wchan_data(const std::string& data, int fd, pid_t pid) { |
| 138 | if (!WriteStringToFd(data, fd)) { |
| 139 | LOG(WARNING) << "libdebuggerd_client: Failed to dump wchan data for pid: " << pid; |
| 140 | } |
| 141 | } |
| 142 | |
Josh Gao | 4175cee | 2019-01-04 13:57:09 -0800 | [diff] [blame] | 143 | bool debuggerd_trigger_dump(pid_t tid, DebuggerdDumpType dump_type, unsigned int timeout_ms, |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 144 | unique_fd output_fd) { |
Josh Gao | 4175cee | 2019-01-04 13:57:09 -0800 | [diff] [blame] | 145 | pid_t pid = tid; |
| 146 | if (dump_type == kDebuggerdJavaBacktrace) { |
| 147 | // Java dumps always get sent to the tgid, so we need to resolve our tid to a tgid. |
| 148 | android::procinfo::ProcessInfo procinfo; |
| 149 | std::string error; |
| 150 | if (!android::procinfo::GetProcessInfo(tid, &procinfo, &error)) { |
| 151 | LOG(ERROR) << "libdebugged_client: failed to get process info: " << error; |
Greg Kaiser | 3fa9a59 | 2019-01-08 06:29:22 -0800 | [diff] [blame] | 152 | return false; |
Josh Gao | 4175cee | 2019-01-04 13:57:09 -0800 | [diff] [blame] | 153 | } |
| 154 | pid = procinfo.pid; |
| 155 | } |
| 156 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 157 | LOG(INFO) << "libdebuggerd_client: started dumping process " << pid; |
| 158 | unique_fd sockfd; |
| 159 | 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] | 160 | auto time_left = [&end]() { return end - std::chrono::steady_clock::now(); }; |
Josh Gao | ae9d767 | 2017-03-24 16:26:03 -0700 | [diff] [blame] | 161 | auto set_timeout = [timeout_ms, &time_left](int sockfd) { |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 162 | if (timeout_ms <= 0) { |
Josh Gao | 287d50d | 2017-04-04 13:43:21 -0700 | [diff] [blame] | 163 | return sockfd; |
Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 164 | } |
Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 165 | |
Josh Gao | ae9d767 | 2017-03-24 16:26:03 -0700 | [diff] [blame] | 166 | auto remaining = time_left(); |
| 167 | if (remaining < decltype(remaining)::zero()) { |
Josh Gao | 287d50d | 2017-04-04 13:43:21 -0700 | [diff] [blame] | 168 | LOG(ERROR) << "libdebuggerd_client: timeout expired"; |
Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame] | 169 | return -1; |
Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 170 | } |
Josh Gao | 287d50d | 2017-04-04 13:43:21 -0700 | [diff] [blame] | 171 | |
Josh Gao | ae9d767 | 2017-03-24 16:26:03 -0700 | [diff] [blame] | 172 | struct timeval timeout; |
| 173 | populate_timeval(&timeout, remaining); |
Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 174 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 175 | if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) != 0) { |
Josh Gao | 287d50d | 2017-04-04 13:43:21 -0700 | [diff] [blame] | 176 | PLOG(ERROR) << "libdebuggerd_client: failed to set receive timeout"; |
Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame] | 177 | return -1; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 178 | } |
| 179 | if (setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) != 0) { |
Josh Gao | 287d50d | 2017-04-04 13:43:21 -0700 | [diff] [blame] | 180 | PLOG(ERROR) << "libdebuggerd_client: failed to set send timeout"; |
Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame] | 181 | return -1; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 182 | } |
Josh Gao | 218f7fb | 2016-10-07 16:42:05 -0700 | [diff] [blame] | 183 | |
Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame] | 184 | return sockfd; |
Josh Gao | 218f7fb | 2016-10-07 16:42:05 -0700 | [diff] [blame] | 185 | }; |
Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 186 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 187 | sockfd.reset(socket(AF_LOCAL, SOCK_SEQPACKET, 0)); |
| 188 | if (sockfd == -1) { |
| 189 | PLOG(ERROR) << "libdebugger_client: failed to create socket"; |
| 190 | return false; |
Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame] | 193 | if (socket_local_client_connect(set_timeout(sockfd.get()), kTombstonedInterceptSocketName, |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 194 | ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET) == -1) { |
| 195 | PLOG(ERROR) << "libdebuggerd_client: failed to connect to tombstoned"; |
| 196 | return false; |
| 197 | } |
| 198 | |
Nick Desaulniers | 67d52aa | 2019-10-07 23:28:15 -0700 | [diff] [blame] | 199 | InterceptRequest req = { |
| 200 | .dump_type = dump_type, |
| 201 | .pid = pid, |
| 202 | }; |
Josh Gao | ae9d767 | 2017-03-24 16:26:03 -0700 | [diff] [blame] | 203 | if (!set_timeout(sockfd)) { |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 204 | PLOG(ERROR) << "libdebugger_client: failed to set timeout"; |
Josh Gao | ae9d767 | 2017-03-24 16:26:03 -0700 | [diff] [blame] | 205 | return false; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Josh Gao | ae9d767 | 2017-03-24 16:26:03 -0700 | [diff] [blame] | 208 | // Create an intermediate pipe to pass to the other end. |
| 209 | unique_fd pipe_read, pipe_write; |
| 210 | if (!Pipe(&pipe_read, &pipe_write)) { |
| 211 | PLOG(ERROR) << "libdebuggerd_client: failed to create pipe"; |
| 212 | return false; |
| 213 | } |
| 214 | |
Josh Gao | 5675f3c | 2017-06-01 12:19:53 -0700 | [diff] [blame] | 215 | std::string pipe_size_str; |
| 216 | int pipe_buffer_size = 1024 * 1024; |
| 217 | if (android::base::ReadFileToString("/proc/sys/fs/pipe-max-size", &pipe_size_str)) { |
| 218 | pipe_size_str = android::base::Trim(pipe_size_str); |
| 219 | |
| 220 | if (!android::base::ParseInt(pipe_size_str.c_str(), &pipe_buffer_size, 0)) { |
| 221 | LOG(FATAL) << "failed to parse pipe max size '" << pipe_size_str << "'"; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | if (fcntl(pipe_read.get(), F_SETPIPE_SZ, pipe_buffer_size) != pipe_buffer_size) { |
| 226 | PLOG(ERROR) << "failed to set pipe buffer size"; |
| 227 | } |
| 228 | |
Josh Gao | 5f87bbd | 2019-01-09 17:01:49 -0800 | [diff] [blame] | 229 | ssize_t rc = SendFileDescriptors(set_timeout(sockfd), &req, sizeof(req), pipe_write.get()); |
| 230 | pipe_write.reset(); |
| 231 | if (rc != sizeof(req)) { |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 232 | PLOG(ERROR) << "libdebuggerd_client: failed to send output fd to tombstoned"; |
| 233 | return false; |
| 234 | } |
| 235 | |
Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame] | 236 | // Check to make sure we've successfully registered. |
| 237 | InterceptResponse response; |
Josh Gao | 5f87bbd | 2019-01-09 17:01:49 -0800 | [diff] [blame] | 238 | rc = TEMP_FAILURE_RETRY(recv(set_timeout(sockfd.get()), &response, sizeof(response), MSG_TRUNC)); |
Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame] | 239 | if (rc == 0) { |
Josh Gao | be0c1af | 2018-09-06 13:00:57 -0700 | [diff] [blame] | 240 | LOG(ERROR) << "libdebuggerd_client: failed to read initial response from tombstoned: " |
| 241 | << "timeout reached?"; |
| 242 | return false; |
| 243 | } else if (rc == -1) { |
| 244 | PLOG(ERROR) << "libdebuggerd_client: failed to read initial response from tombstoned"; |
Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame] | 245 | return false; |
| 246 | } else if (rc != sizeof(response)) { |
Josh Gao | be0c1af | 2018-09-06 13:00:57 -0700 | [diff] [blame] | 247 | LOG(ERROR) << "libdebuggerd_client: received packet of unexpected length from tombstoned while " |
| 248 | "reading initial response: expected " |
| 249 | << sizeof(response) << ", received " << rc; |
Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame] | 250 | return false; |
| 251 | } |
| 252 | |
| 253 | if (response.status != InterceptStatus::kRegistered) { |
| 254 | LOG(ERROR) << "libdebuggerd_client: unexpected registration response: " |
| 255 | << static_cast<int>(response.status); |
| 256 | return false; |
| 257 | } |
| 258 | |
Josh Gao | 4175cee | 2019-01-04 13:57:09 -0800 | [diff] [blame] | 259 | if (!send_signal(tid, dump_type)) { |
Liu Changcheng | 3492221 | 2017-04-06 11:20:14 +0800 | [diff] [blame] | 260 | return false; |
| 261 | } |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 262 | |
Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame] | 263 | 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] | 264 | if (rc == 0) { |
Josh Gao | be0c1af | 2018-09-06 13:00:57 -0700 | [diff] [blame] | 265 | LOG(ERROR) << "libdebuggerd_client: failed to read status response from tombstoned: " |
| 266 | "timeout reached?"; |
| 267 | return false; |
| 268 | } else if (rc == -1) { |
| 269 | PLOG(ERROR) << "libdebuggerd_client: failed to read status response from tombstoned"; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 270 | return false; |
| 271 | } else if (rc != sizeof(response)) { |
Josh Gao | be0c1af | 2018-09-06 13:00:57 -0700 | [diff] [blame] | 272 | LOG(ERROR) << "libdebuggerd_client: received packet of unexpected length from tombstoned while " |
| 273 | "reading confirmation response: expected " |
| 274 | << sizeof(response) << ", received " << rc; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 275 | return false; |
| 276 | } |
| 277 | |
Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame] | 278 | if (response.status != InterceptStatus::kStarted) { |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 279 | response.error_message[sizeof(response.error_message) - 1] = '\0'; |
| 280 | LOG(ERROR) << "libdebuggerd_client: tombstoned reported failure: " << response.error_message; |
Josh Gao | ae9d767 | 2017-03-24 16:26:03 -0700 | [diff] [blame] | 281 | return false; |
| 282 | } |
| 283 | |
| 284 | // Forward output from the pipe to the output fd. |
| 285 | while (true) { |
Josh Gao | 287d50d | 2017-04-04 13:43:21 -0700 | [diff] [blame] | 286 | auto remaining_ms = std::chrono::duration_cast<std::chrono::milliseconds>(time_left()).count(); |
| 287 | if (timeout_ms <= 0) { |
| 288 | remaining_ms = -1; |
| 289 | } else if (remaining_ms < 0) { |
Josh Gao | ae9d767 | 2017-03-24 16:26:03 -0700 | [diff] [blame] | 290 | LOG(ERROR) << "libdebuggerd_client: timeout expired"; |
| 291 | return false; |
| 292 | } |
| 293 | |
| 294 | struct pollfd pfd = { |
| 295 | .fd = pipe_read.get(), .events = POLLIN, .revents = 0, |
| 296 | }; |
| 297 | |
Josh Gao | 287d50d | 2017-04-04 13:43:21 -0700 | [diff] [blame] | 298 | rc = poll(&pfd, 1, remaining_ms); |
Josh Gao | ae9d767 | 2017-03-24 16:26:03 -0700 | [diff] [blame] | 299 | if (rc == -1) { |
| 300 | if (errno == EINTR) { |
| 301 | continue; |
| 302 | } else { |
| 303 | PLOG(ERROR) << "libdebuggerd_client: error while polling"; |
| 304 | return false; |
| 305 | } |
| 306 | } else if (rc == 0) { |
| 307 | LOG(ERROR) << "libdebuggerd_client: timeout expired"; |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | char buf[1024]; |
| 312 | rc = TEMP_FAILURE_RETRY(read(pipe_read.get(), buf, sizeof(buf))); |
| 313 | if (rc == 0) { |
| 314 | // Done. |
| 315 | break; |
| 316 | } else if (rc == -1) { |
| 317 | PLOG(ERROR) << "libdebuggerd_client: error while reading"; |
| 318 | return false; |
| 319 | } |
| 320 | |
| 321 | if (!android::base::WriteFully(output_fd.get(), buf, rc)) { |
| 322 | PLOG(ERROR) << "libdebuggerd_client: error while writing"; |
| 323 | return false; |
| 324 | } |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | LOG(INFO) << "libdebuggerd_client: done dumping process " << pid; |
| 328 | |
| 329 | return true; |
Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 332 | int dump_backtrace_to_file(pid_t tid, DebuggerdDumpType dump_type, int fd) { |
| 333 | return dump_backtrace_to_file_timeout(tid, dump_type, 0, fd); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 336 | int dump_backtrace_to_file_timeout(pid_t tid, DebuggerdDumpType dump_type, int timeout_secs, |
| 337 | int fd) { |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 338 | android::base::unique_fd copy(dup(fd)); |
| 339 | if (copy == -1) { |
| 340 | return -1; |
Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 341 | } |
Kalesh Singh | 1489e27 | 2019-07-14 12:15:35 -0700 | [diff] [blame] | 342 | |
| 343 | // debuggerd_trigger_dump results in every thread in the process being interrupted |
| 344 | // by a signal, so we need to fetch the wchan data before calling that. |
| 345 | std::string wchan_data = get_wchan_data(tid); |
| 346 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 347 | int timeout_ms = timeout_secs > 0 ? timeout_secs * 1000 : 0; |
Kalesh Singh | 1489e27 | 2019-07-14 12:15:35 -0700 | [diff] [blame] | 348 | int ret = debuggerd_trigger_dump(tid, dump_type, timeout_ms, std::move(copy)) ? 0 : -1; |
| 349 | |
| 350 | // Dump wchan data, since only privileged processes (CAP_SYS_ADMIN) can read |
| 351 | // kernel stack traces (/proc/*/stack). |
| 352 | dump_wchan_data(wchan_data, fd, tid); |
| 353 | |
| 354 | return ret; |
Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 355 | } |