2016-06-16 00:29:00 +00:00
|
|
|
/*
|
2016-10-19 01:17:52 +00:00
|
|
|
* Copyright 2016, The Android Open Source Project
|
2016-06-16 00:29:00 +00:00
|
|
|
*
|
2016-10-19 01:17:52 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2016-06-16 00:29:00 +00:00
|
|
|
*
|
2016-10-19 01:17:52 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2016-06-16 00:29:00 +00:00
|
|
|
*/
|
|
|
|
|
2016-10-19 01:17:52 +00:00
|
|
|
#include <debuggerd/client.h>
|
2016-06-16 00:29:00 +00:00
|
|
|
|
2016-10-19 01:17:52 +00:00
|
|
|
#include <fcntl.h>
|
2016-06-16 00:29:00 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdlib.h>
|
2017-03-24 23:26:03 +00:00
|
|
|
#include <sys/poll.h>
|
2016-10-19 01:17:52 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2019-07-14 19:15:35 +00:00
|
|
|
#include <time.h>
|
2016-06-16 00:29:00 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2016-10-19 01:17:52 +00:00
|
|
|
#include <chrono>
|
2019-07-14 19:15:35 +00:00
|
|
|
#include <iomanip>
|
2016-06-16 00:29:00 +00:00
|
|
|
|
2019-01-10 01:01:49 +00:00
|
|
|
#include <android-base/cmsg.h>
|
2017-03-24 23:26:03 +00:00
|
|
|
#include <android-base/file.h>
|
2016-10-19 01:17:52 +00:00
|
|
|
#include <android-base/logging.h>
|
2017-06-01 19:19:53 +00:00
|
|
|
#include <android-base/parseint.h>
|
2016-10-19 01:17:52 +00:00
|
|
|
#include <android-base/stringprintf.h>
|
2017-06-01 19:19:53 +00:00
|
|
|
#include <android-base/strings.h>
|
2016-10-19 01:17:52 +00:00
|
|
|
#include <android-base/unique_fd.h>
|
2019-12-13 22:11:04 +00:00
|
|
|
#include <bionic/reserved_signals.h>
|
2016-10-19 01:17:52 +00:00
|
|
|
#include <cutils/sockets.h>
|
2019-01-04 21:57:09 +00:00
|
|
|
#include <procinfo/process.h>
|
2017-05-10 09:58:59 +00:00
|
|
|
|
|
|
|
#include "debuggerd/handler.h"
|
|
|
|
#include "protocol.h"
|
|
|
|
#include "util.h"
|
2016-10-07 23:42:05 +00:00
|
|
|
|
2017-03-24 23:26:03 +00:00
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2019-07-14 19:15:35 +00:00
|
|
|
using android::base::ReadFileToString;
|
2019-01-10 01:01:49 +00:00
|
|
|
using android::base::SendFileDescriptors;
|
2016-10-19 01:17:52 +00:00
|
|
|
using android::base::unique_fd;
|
2019-07-14 19:15:35 +00:00
|
|
|
using android::base::WriteStringToFd;
|
2016-06-16 00:29:00 +00:00
|
|
|
|
2017-05-24 14:07:25 +00:00
|
|
|
static bool send_signal(pid_t pid, const DebuggerdDumpType dump_type) {
|
2019-12-13 22:11:04 +00:00
|
|
|
const int signal = (dump_type == kDebuggerdJavaBacktrace) ? SIGQUIT : BIONIC_SIGNAL_DEBUGGER;
|
2016-10-19 01:17:52 +00:00
|
|
|
sigval val;
|
2017-05-24 14:07:25 +00:00
|
|
|
val.sival_int = (dump_type == kDebuggerdNativeBacktrace) ? 1 : 0;
|
|
|
|
|
|
|
|
if (sigqueue(pid, signal, val) != 0) {
|
2016-10-19 01:17:52 +00:00
|
|
|
PLOG(ERROR) << "libdebuggerd_client: failed to send signal to pid " << pid;
|
|
|
|
return false;
|
2016-06-16 00:29:00 +00:00
|
|
|
}
|
2016-10-19 01:17:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-06-16 00:29:00 +00:00
|
|
|
|
2017-03-24 23:26:03 +00:00
|
|
|
template <typename Duration>
|
|
|
|
static void populate_timeval(struct timeval* tv, const Duration& duration) {
|
|
|
|
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(duration);
|
|
|
|
auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(duration - seconds);
|
|
|
|
tv->tv_sec = static_cast<long>(seconds.count());
|
|
|
|
tv->tv_usec = static_cast<long>(microseconds.count());
|
|
|
|
}
|
|
|
|
|
2019-07-14 19:15:35 +00:00
|
|
|
/**
|
|
|
|
* Returns the wchan data for each thread in the process,
|
|
|
|
* or empty string if unable to obtain any data.
|
|
|
|
*/
|
|
|
|
static std::string get_wchan_data(pid_t pid) {
|
|
|
|
std::stringstream buffer;
|
|
|
|
std::vector<pid_t> tids;
|
|
|
|
|
|
|
|
if (!android::procinfo::GetProcessTids(pid, &tids)) {
|
|
|
|
LOG(WARNING) << "libdebuggerd_client: Failed to get process tids";
|
|
|
|
return buffer.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::stringstream data;
|
|
|
|
for (int tid : tids) {
|
|
|
|
std::string path = "/proc/" + std::to_string(pid) + "/task/" + std::to_string(tid) + "/wchan";
|
|
|
|
std::string wchan_str;
|
|
|
|
if (!ReadFileToString(path, &wchan_str, true)) {
|
|
|
|
PLOG(WARNING) << "libdebuggerd_client: Failed to read \"" << path << "\"";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
data << "sysTid=" << std::left << std::setw(10) << tid << wchan_str << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (std::string str = data.str(); !str.empty()) {
|
2020-07-23 22:26:10 +00:00
|
|
|
buffer << "\n----- Waiting Channels: pid " << pid << " at " << get_timestamp() << " -----\n"
|
|
|
|
<< "Cmd line: " << get_process_name(pid) << "\n";
|
2019-07-14 19:15:35 +00:00
|
|
|
buffer << "\n" << str << "\n";
|
2020-07-23 22:26:10 +00:00
|
|
|
buffer << "----- end " << std::to_string(pid) << " -----\n";
|
2019-07-14 19:15:35 +00:00
|
|
|
buffer << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dump_wchan_data(const std::string& data, int fd, pid_t pid) {
|
|
|
|
if (!WriteStringToFd(data, fd)) {
|
|
|
|
LOG(WARNING) << "libdebuggerd_client: Failed to dump wchan data for pid: " << pid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-04 21:57:09 +00:00
|
|
|
bool debuggerd_trigger_dump(pid_t tid, DebuggerdDumpType dump_type, unsigned int timeout_ms,
|
2017-05-24 14:07:25 +00:00
|
|
|
unique_fd output_fd) {
|
2019-01-04 21:57:09 +00:00
|
|
|
pid_t pid = tid;
|
|
|
|
if (dump_type == kDebuggerdJavaBacktrace) {
|
|
|
|
// Java dumps always get sent to the tgid, so we need to resolve our tid to a tgid.
|
|
|
|
android::procinfo::ProcessInfo procinfo;
|
|
|
|
std::string error;
|
|
|
|
if (!android::procinfo::GetProcessInfo(tid, &procinfo, &error)) {
|
|
|
|
LOG(ERROR) << "libdebugged_client: failed to get process info: " << error;
|
2019-01-08 14:29:22 +00:00
|
|
|
return false;
|
2019-01-04 21:57:09 +00:00
|
|
|
}
|
|
|
|
pid = procinfo.pid;
|
|
|
|
}
|
|
|
|
|
2016-10-19 01:17:52 +00:00
|
|
|
LOG(INFO) << "libdebuggerd_client: started dumping process " << pid;
|
|
|
|
unique_fd sockfd;
|
|
|
|
const auto end = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_ms);
|
2017-04-04 20:43:21 +00:00
|
|
|
auto time_left = [&end]() { return end - std::chrono::steady_clock::now(); };
|
2017-03-24 23:26:03 +00:00
|
|
|
auto set_timeout = [timeout_ms, &time_left](int sockfd) {
|
2016-10-19 01:17:52 +00:00
|
|
|
if (timeout_ms <= 0) {
|
2017-04-04 20:43:21 +00:00
|
|
|
return sockfd;
|
2016-06-16 00:29:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 23:26:03 +00:00
|
|
|
auto remaining = time_left();
|
|
|
|
if (remaining < decltype(remaining)::zero()) {
|
2017-04-04 20:43:21 +00:00
|
|
|
LOG(ERROR) << "libdebuggerd_client: timeout expired";
|
2017-03-30 23:40:47 +00:00
|
|
|
return -1;
|
2016-06-16 00:29:00 +00:00
|
|
|
}
|
2017-04-04 20:43:21 +00:00
|
|
|
|
2017-03-24 23:26:03 +00:00
|
|
|
struct timeval timeout;
|
|
|
|
populate_timeval(&timeout, remaining);
|
2016-06-16 00:29:00 +00:00
|
|
|
|
2016-10-19 01:17:52 +00:00
|
|
|
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) != 0) {
|
2017-04-04 20:43:21 +00:00
|
|
|
PLOG(ERROR) << "libdebuggerd_client: failed to set receive timeout";
|
2017-03-30 23:40:47 +00:00
|
|
|
return -1;
|
2016-10-19 01:17:52 +00:00
|
|
|
}
|
|
|
|
if (setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) != 0) {
|
2017-04-04 20:43:21 +00:00
|
|
|
PLOG(ERROR) << "libdebuggerd_client: failed to set send timeout";
|
2017-03-30 23:40:47 +00:00
|
|
|
return -1;
|
2016-10-19 01:17:52 +00:00
|
|
|
}
|
2016-10-07 23:42:05 +00:00
|
|
|
|
2017-03-30 23:40:47 +00:00
|
|
|
return sockfd;
|
2016-10-19 01:17:52 +00:00
|
|
|
};
|
2016-10-07 23:42:05 +00:00
|
|
|
|
2016-10-19 01:17:52 +00:00
|
|
|
sockfd.reset(socket(AF_LOCAL, SOCK_SEQPACKET, 0));
|
|
|
|
if (sockfd == -1) {
|
|
|
|
PLOG(ERROR) << "libdebugger_client: failed to create socket";
|
|
|
|
return false;
|
2016-06-16 00:29:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 23:40:47 +00:00
|
|
|
if (socket_local_client_connect(set_timeout(sockfd.get()), kTombstonedInterceptSocketName,
|
2016-10-19 01:17:52 +00:00
|
|
|
ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET) == -1) {
|
|
|
|
PLOG(ERROR) << "libdebuggerd_client: failed to connect to tombstoned";
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-16 00:29:00 +00:00
|
|
|
|
2019-10-08 06:28:15 +00:00
|
|
|
InterceptRequest req = {
|
|
|
|
.dump_type = dump_type,
|
|
|
|
.pid = pid,
|
|
|
|
};
|
2017-03-24 23:26:03 +00:00
|
|
|
if (!set_timeout(sockfd)) {
|
2016-10-19 01:17:52 +00:00
|
|
|
PLOG(ERROR) << "libdebugger_client: failed to set timeout";
|
2017-03-24 23:26:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an intermediate pipe to pass to the other end.
|
|
|
|
unique_fd pipe_read, pipe_write;
|
|
|
|
if (!Pipe(&pipe_read, &pipe_write)) {
|
|
|
|
PLOG(ERROR) << "libdebuggerd_client: failed to create pipe";
|
|
|
|
return false;
|
2016-06-16 00:29:00 +00:00
|
|
|
}
|
|
|
|
|
2017-06-01 19:19:53 +00:00
|
|
|
std::string pipe_size_str;
|
|
|
|
int pipe_buffer_size = 1024 * 1024;
|
|
|
|
if (android::base::ReadFileToString("/proc/sys/fs/pipe-max-size", &pipe_size_str)) {
|
|
|
|
pipe_size_str = android::base::Trim(pipe_size_str);
|
|
|
|
|
|
|
|
if (!android::base::ParseInt(pipe_size_str.c_str(), &pipe_buffer_size, 0)) {
|
|
|
|
LOG(FATAL) << "failed to parse pipe max size '" << pipe_size_str << "'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fcntl(pipe_read.get(), F_SETPIPE_SZ, pipe_buffer_size) != pipe_buffer_size) {
|
|
|
|
PLOG(ERROR) << "failed to set pipe buffer size";
|
|
|
|
}
|
|
|
|
|
2019-01-10 01:01:49 +00:00
|
|
|
ssize_t rc = SendFileDescriptors(set_timeout(sockfd), &req, sizeof(req), pipe_write.get());
|
|
|
|
pipe_write.reset();
|
|
|
|
if (rc != sizeof(req)) {
|
2016-10-19 01:17:52 +00:00
|
|
|
PLOG(ERROR) << "libdebuggerd_client: failed to send output fd to tombstoned";
|
|
|
|
return false;
|
2016-06-16 00:29:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 23:40:47 +00:00
|
|
|
// Check to make sure we've successfully registered.
|
|
|
|
InterceptResponse response;
|
2019-01-10 01:01:49 +00:00
|
|
|
rc = TEMP_FAILURE_RETRY(recv(set_timeout(sockfd.get()), &response, sizeof(response), MSG_TRUNC));
|
2017-03-30 23:40:47 +00:00
|
|
|
if (rc == 0) {
|
2018-09-06 20:00:57 +00:00
|
|
|
LOG(ERROR) << "libdebuggerd_client: failed to read initial response from tombstoned: "
|
|
|
|
<< "timeout reached?";
|
|
|
|
return false;
|
|
|
|
} else if (rc == -1) {
|
|
|
|
PLOG(ERROR) << "libdebuggerd_client: failed to read initial response from tombstoned";
|
2017-03-30 23:40:47 +00:00
|
|
|
return false;
|
|
|
|
} else if (rc != sizeof(response)) {
|
2018-09-06 20:00:57 +00:00
|
|
|
LOG(ERROR) << "libdebuggerd_client: received packet of unexpected length from tombstoned while "
|
|
|
|
"reading initial response: expected "
|
|
|
|
<< sizeof(response) << ", received " << rc;
|
2017-03-30 23:40:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-10-19 01:17:52 +00:00
|
|
|
|
2017-03-30 23:40:47 +00:00
|
|
|
if (response.status != InterceptStatus::kRegistered) {
|
|
|
|
LOG(ERROR) << "libdebuggerd_client: unexpected registration response: "
|
|
|
|
<< static_cast<int>(response.status);
|
2017-03-24 23:26:03 +00:00
|
|
|
return false;
|
2016-06-16 00:29:00 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 21:57:09 +00:00
|
|
|
if (!send_signal(tid, dump_type)) {
|
2017-04-06 03:20:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-03-30 23:40:47 +00:00
|
|
|
|
|
|
|
rc = TEMP_FAILURE_RETRY(recv(set_timeout(sockfd.get()), &response, sizeof(response), MSG_TRUNC));
|
2016-10-19 01:17:52 +00:00
|
|
|
if (rc == 0) {
|
2018-09-06 20:00:57 +00:00
|
|
|
LOG(ERROR) << "libdebuggerd_client: failed to read status response from tombstoned: "
|
|
|
|
"timeout reached?";
|
|
|
|
return false;
|
|
|
|
} else if (rc == -1) {
|
|
|
|
PLOG(ERROR) << "libdebuggerd_client: failed to read status response from tombstoned";
|
2016-10-19 01:17:52 +00:00
|
|
|
return false;
|
|
|
|
} else if (rc != sizeof(response)) {
|
2018-09-06 20:00:57 +00:00
|
|
|
LOG(ERROR) << "libdebuggerd_client: received packet of unexpected length from tombstoned while "
|
|
|
|
"reading confirmation response: expected "
|
|
|
|
<< sizeof(response) << ", received " << rc;
|
2016-10-19 01:17:52 +00:00
|
|
|
return false;
|
2016-10-07 23:42:05 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 23:40:47 +00:00
|
|
|
if (response.status != InterceptStatus::kStarted) {
|
2016-10-19 01:17:52 +00:00
|
|
|
response.error_message[sizeof(response.error_message) - 1] = '\0';
|
|
|
|
LOG(ERROR) << "libdebuggerd_client: tombstoned reported failure: " << response.error_message;
|
2017-03-24 23:26:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forward output from the pipe to the output fd.
|
|
|
|
while (true) {
|
2017-04-04 20:43:21 +00:00
|
|
|
auto remaining_ms = std::chrono::duration_cast<std::chrono::milliseconds>(time_left()).count();
|
|
|
|
if (timeout_ms <= 0) {
|
|
|
|
remaining_ms = -1;
|
|
|
|
} else if (remaining_ms < 0) {
|
2017-03-24 23:26:03 +00:00
|
|
|
LOG(ERROR) << "libdebuggerd_client: timeout expired";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct pollfd pfd = {
|
|
|
|
.fd = pipe_read.get(), .events = POLLIN, .revents = 0,
|
|
|
|
};
|
|
|
|
|
2017-04-04 20:43:21 +00:00
|
|
|
rc = poll(&pfd, 1, remaining_ms);
|
2017-03-24 23:26:03 +00:00
|
|
|
if (rc == -1) {
|
|
|
|
if (errno == EINTR) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
PLOG(ERROR) << "libdebuggerd_client: error while polling";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (rc == 0) {
|
|
|
|
LOG(ERROR) << "libdebuggerd_client: timeout expired";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buf[1024];
|
|
|
|
rc = TEMP_FAILURE_RETRY(read(pipe_read.get(), buf, sizeof(buf)));
|
|
|
|
if (rc == 0) {
|
|
|
|
// Done.
|
|
|
|
break;
|
|
|
|
} else if (rc == -1) {
|
|
|
|
PLOG(ERROR) << "libdebuggerd_client: error while reading";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!android::base::WriteFully(output_fd.get(), buf, rc)) {
|
|
|
|
PLOG(ERROR) << "libdebuggerd_client: error while writing";
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-07 23:42:05 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 01:17:52 +00:00
|
|
|
LOG(INFO) << "libdebuggerd_client: done dumping process " << pid;
|
2016-10-07 23:42:05 +00:00
|
|
|
|
2016-10-19 01:17:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-06-16 00:29:00 +00:00
|
|
|
|
2017-05-24 14:07:25 +00:00
|
|
|
int dump_backtrace_to_file(pid_t tid, DebuggerdDumpType dump_type, int fd) {
|
|
|
|
return dump_backtrace_to_file_timeout(tid, dump_type, 0, fd);
|
2016-10-19 01:17:52 +00:00
|
|
|
}
|
2016-06-16 00:29:00 +00:00
|
|
|
|
2017-05-24 14:07:25 +00:00
|
|
|
int dump_backtrace_to_file_timeout(pid_t tid, DebuggerdDumpType dump_type, int timeout_secs,
|
|
|
|
int fd) {
|
2016-10-19 01:17:52 +00:00
|
|
|
android::base::unique_fd copy(dup(fd));
|
|
|
|
if (copy == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
2019-07-14 19:15:35 +00:00
|
|
|
|
|
|
|
// debuggerd_trigger_dump results in every thread in the process being interrupted
|
|
|
|
// by a signal, so we need to fetch the wchan data before calling that.
|
|
|
|
std::string wchan_data = get_wchan_data(tid);
|
|
|
|
|
2016-10-19 01:17:52 +00:00
|
|
|
int timeout_ms = timeout_secs > 0 ? timeout_secs * 1000 : 0;
|
2019-07-14 19:15:35 +00:00
|
|
|
int ret = debuggerd_trigger_dump(tid, dump_type, timeout_ms, std::move(copy)) ? 0 : -1;
|
|
|
|
|
|
|
|
// Dump wchan data, since only privileged processes (CAP_SYS_ADMIN) can read
|
|
|
|
// kernel stack traces (/proc/*/stack).
|
|
|
|
dump_wchan_data(wchan_data, fd, tid);
|
|
|
|
|
|
|
|
return ret;
|
2016-06-16 00:29:00 +00:00
|
|
|
}
|