2016-10-19 03:17:52 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2016, The Android Open Source Project
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
2017-02-02 01:35:31 +01:00
|
|
|
#include <sys/prctl.h>
|
2016-10-19 03:17:52 +02:00
|
|
|
#include <sys/ptrace.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/un.h>
|
2017-08-21 23:31:17 +02:00
|
|
|
#include <sys/wait.h>
|
2017-02-02 01:35:31 +01:00
|
|
|
#include <syscall.h>
|
2016-10-19 03:17:52 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <limits>
|
2017-03-16 07:23:22 +01:00
|
|
|
#include <map>
|
2016-10-19 03:17:52 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <set>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <android-base/file.h>
|
|
|
|
#include <android-base/logging.h>
|
2018-09-18 22:22:22 +02:00
|
|
|
#include <android-base/macros.h>
|
2016-10-19 03:17:52 +02:00
|
|
|
#include <android-base/parseint.h>
|
|
|
|
#include <android-base/properties.h>
|
|
|
|
#include <android-base/stringprintf.h>
|
2017-03-16 07:23:22 +01:00
|
|
|
#include <android-base/strings.h>
|
2016-10-19 03:17:52 +02:00
|
|
|
#include <android-base/unique_fd.h>
|
|
|
|
#include <cutils/sockets.h>
|
2017-01-24 05:11:51 +01:00
|
|
|
#include <log/log.h>
|
2017-06-01 21:08:10 +02:00
|
|
|
#include <private/android_filesystem_config.h>
|
2016-10-19 03:17:52 +02:00
|
|
|
#include <procinfo/process.h>
|
|
|
|
|
2017-05-31 00:31:02 +02:00
|
|
|
#define ATRACE_TAG ATRACE_TAG_BIONIC
|
|
|
|
#include <utils/Trace.h>
|
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
#include <unwindstack/Regs.h>
|
|
|
|
|
2017-08-29 22:08:32 +02:00
|
|
|
#include "libdebuggerd/backtrace.h"
|
|
|
|
#include "libdebuggerd/tombstone.h"
|
|
|
|
#include "libdebuggerd/utility.h"
|
2016-10-19 03:17:52 +02:00
|
|
|
|
|
|
|
#include "debuggerd/handler.h"
|
2017-05-10 11:58:59 +02:00
|
|
|
#include "tombstoned/tombstoned.h"
|
2017-08-29 22:08:32 +02:00
|
|
|
|
|
|
|
#include "protocol.h"
|
2017-05-10 11:58:59 +02:00
|
|
|
#include "util.h"
|
2016-10-19 03:17:52 +02:00
|
|
|
|
|
|
|
using android::base::unique_fd;
|
|
|
|
using android::base::StringPrintf;
|
2017-03-16 07:23:22 +01:00
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
using unwindstack::Regs;
|
2016-10-19 03:17:52 +02:00
|
|
|
|
2017-02-02 01:31:43 +01:00
|
|
|
static bool pid_contains_tid(int pid_proc_fd, pid_t tid) {
|
|
|
|
struct stat st;
|
|
|
|
std::string task_path = StringPrintf("task/%d", tid);
|
|
|
|
return fstatat(pid_proc_fd, task_path.c_str(), &st, 0) == 0;
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
|
|
|
|
2017-08-19 00:37:26 +02:00
|
|
|
static pid_t get_tracer(pid_t tracee) {
|
|
|
|
// Check to see if the thread is being ptraced by another process.
|
|
|
|
android::procinfo::ProcessInfo process_info;
|
|
|
|
if (android::procinfo::GetProcessInfo(tracee, &process_info)) {
|
|
|
|
return process_info.tracer;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-10-19 03:17:52 +02:00
|
|
|
// Attach to a thread, and verify that it's still a member of the given process
|
2017-08-21 23:31:17 +02:00
|
|
|
static bool ptrace_seize_thread(int pid_proc_fd, pid_t tid, std::string* error, int flags = 0) {
|
|
|
|
if (ptrace(PTRACE_SEIZE, tid, 0, flags) != 0) {
|
2017-08-19 00:37:26 +02:00
|
|
|
if (errno == EPERM) {
|
|
|
|
pid_t tracer = get_tracer(tid);
|
|
|
|
if (tracer != -1) {
|
|
|
|
*error = StringPrintf("failed to attach to thread %d, already traced by %d (%s)", tid,
|
|
|
|
tracer, get_process_name(tracer).c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-20 21:51:11 +01:00
|
|
|
*error = StringPrintf("failed to attach to thread %d: %s", tid, strerror(errno));
|
2016-10-19 03:17:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that the task we attached to is actually part of the pid we're dumping.
|
2017-02-02 01:31:43 +01:00
|
|
|
if (!pid_contains_tid(pid_proc_fd, tid)) {
|
2016-10-19 03:17:52 +02:00
|
|
|
if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
|
2017-08-21 23:31:17 +02:00
|
|
|
PLOG(WARNING) << "failed to detach from thread " << tid;
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
2017-02-02 01:31:43 +01:00
|
|
|
*error = StringPrintf("thread %d is not in process", tid);
|
2016-10-19 03:17:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-01-23 01:42:32 +01:00
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool wait_for_stop(pid_t tid, int* received_signal) {
|
|
|
|
while (true) {
|
|
|
|
int status;
|
|
|
|
pid_t result = waitpid(tid, &status, __WALL);
|
|
|
|
if (result != tid) {
|
|
|
|
PLOG(ERROR) << "waitpid failed on " << tid << " while detaching";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WIFSTOPPED(status)) {
|
|
|
|
if (status >> 16 == PTRACE_EVENT_STOP) {
|
|
|
|
*received_signal = 0;
|
|
|
|
} else {
|
|
|
|
*received_signal = WSTOPSIG(status);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2017-06-13 19:21:12 +02:00
|
|
|
}
|
2017-08-21 23:31:17 +02:00
|
|
|
}
|
2017-06-13 19:21:12 +02:00
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
// Interrupt a process and wait for it to be interrupted.
|
|
|
|
static bool ptrace_interrupt(pid_t tid, int* received_signal) {
|
|
|
|
if (ptrace(PTRACE_INTERRUPT, tid, 0, 0) == 0) {
|
|
|
|
return wait_for_stop(tid, received_signal);
|
|
|
|
}
|
|
|
|
|
|
|
|
PLOG(ERROR) << "failed to interrupt " << tid << " to detach";
|
|
|
|
return false;
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
|
|
|
|
2017-06-01 21:08:10 +02:00
|
|
|
static bool activity_manager_notify(pid_t pid, int signal, const std::string& amfd_data) {
|
2017-05-31 00:31:02 +02:00
|
|
|
ATRACE_CALL();
|
2017-06-01 21:08:10 +02:00
|
|
|
android::base::unique_fd amfd(socket_local_client(
|
|
|
|
"/data/system/ndebugsocket", ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM));
|
2016-10-19 03:17:52 +02:00
|
|
|
if (amfd.get() == -1) {
|
|
|
|
PLOG(ERROR) << "unable to connect to activity manager";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct timeval tv = {
|
|
|
|
.tv_sec = 1,
|
|
|
|
.tv_usec = 0,
|
|
|
|
};
|
|
|
|
if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
|
|
|
|
PLOG(ERROR) << "failed to set send timeout on activity manager socket";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
tv.tv_sec = 3; // 3 seconds on handshake read
|
|
|
|
if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
|
|
|
|
PLOG(ERROR) << "failed to set receive timeout on activity manager socket";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Activity Manager protocol: binary 32-bit network-byte-order ints for the
|
|
|
|
// pid and signal number, followed by the raw text of the dump, culminating
|
|
|
|
// in a zero byte that marks end-of-data.
|
|
|
|
uint32_t datum = htonl(pid);
|
|
|
|
if (!android::base::WriteFully(amfd, &datum, 4)) {
|
|
|
|
PLOG(ERROR) << "AM pid write failed";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
datum = htonl(signal);
|
|
|
|
if (!android::base::WriteFully(amfd, &datum, 4)) {
|
|
|
|
PLOG(ERROR) << "AM signal write failed";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size() + 1)) {
|
|
|
|
PLOG(ERROR) << "AM data write failed";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3 sec timeout reading the ack; we're fine if the read fails.
|
|
|
|
char ack;
|
|
|
|
android::base::ReadFully(amfd, &ack, 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
// Globals used by the abort handler.
|
|
|
|
static pid_t g_target_thread = -1;
|
|
|
|
static bool g_tombstoned_connected = false;
|
|
|
|
static unique_fd g_tombstoned_socket;
|
|
|
|
static unique_fd g_output_fd;
|
2017-01-23 02:41:15 +01:00
|
|
|
|
2018-04-27 22:31:47 +02:00
|
|
|
static void DefuseSignalHandlers() {
|
|
|
|
// Don't try to dump ourselves.
|
|
|
|
struct sigaction action = {};
|
|
|
|
action.sa_handler = SIG_DFL;
|
|
|
|
debuggerd_register_handlers(&action);
|
|
|
|
|
|
|
|
sigset_t mask;
|
|
|
|
sigemptyset(&mask);
|
|
|
|
if (sigprocmask(SIG_SETMASK, &mask, nullptr) != 0) {
|
|
|
|
PLOG(FATAL) << "failed to set signal mask";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
static void Initialize(char** argv) {
|
|
|
|
android::base::InitLogging(argv);
|
|
|
|
android::base::SetAborter([](const char* abort_msg) {
|
|
|
|
// If we abort before we get an output fd, contact tombstoned to let any
|
|
|
|
// potential listeners know that we failed.
|
|
|
|
if (!g_tombstoned_connected) {
|
|
|
|
if (!tombstoned_connect(g_target_thread, &g_tombstoned_socket, &g_output_fd,
|
|
|
|
kDebuggerdAnyIntercept)) {
|
|
|
|
// We failed to connect, not much we can do.
|
|
|
|
LOG(ERROR) << "failed to connected to tombstoned to report failure";
|
|
|
|
_exit(1);
|
|
|
|
}
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
2017-02-02 01:35:31 +01:00
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
dprintf(g_output_fd.get(), "crash_dump failed to dump process");
|
|
|
|
if (g_target_thread != 1) {
|
|
|
|
dprintf(g_output_fd.get(), " %d: %s\n", g_target_thread, abort_msg);
|
|
|
|
} else {
|
|
|
|
dprintf(g_output_fd.get(), ": %s\n", abort_msg);
|
|
|
|
}
|
2016-10-19 03:17:52 +02:00
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
_exit(1);
|
2016-10-19 03:17:52 +02:00
|
|
|
});
|
2017-08-21 23:31:17 +02:00
|
|
|
}
|
2017-06-01 20:55:25 +02:00
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
static void ParseArgs(int argc, char** argv, pid_t* pseudothread_tid, DebuggerdDumpType* dump_type) {
|
2017-05-24 16:07:25 +02:00
|
|
|
if (argc != 4) {
|
2017-08-21 23:31:17 +02:00
|
|
|
LOG(FATAL) << "wrong number of args: " << argc << " (expected 4)";
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
if (!android::base::ParseInt(argv[1], &g_target_thread, 1, std::numeric_limits<pid_t>::max())) {
|
|
|
|
LOG(FATAL) << "invalid target tid: " << argv[1];
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
if (!android::base::ParseInt(argv[2], pseudothread_tid, 1, std::numeric_limits<pid_t>::max())) {
|
2017-02-15 21:21:11 +01:00
|
|
|
LOG(FATAL) << "invalid pseudothread tid: " << argv[2];
|
2017-02-13 23:46:19 +01:00
|
|
|
}
|
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
int dump_type_int;
|
|
|
|
if (!android::base::ParseInt(argv[3], &dump_type_int, 0, 1)) {
|
2017-05-24 16:07:25 +02:00
|
|
|
LOG(FATAL) << "invalid requested dump type: " << argv[3];
|
|
|
|
}
|
2017-08-21 23:31:17 +02:00
|
|
|
*dump_type = static_cast<DebuggerdDumpType>(dump_type_int);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ReadCrashInfo(unique_fd& fd, siginfo_t* siginfo,
|
2018-08-07 00:38:29 +02:00
|
|
|
std::unique_ptr<unwindstack::Regs>* regs, uintptr_t* abort_msg_address,
|
|
|
|
uintptr_t* fdsan_table_address) {
|
2017-08-21 23:31:17 +02:00
|
|
|
std::aligned_storage<sizeof(CrashInfo) + 1, alignof(CrashInfo)>::type buf;
|
2018-08-07 00:38:29 +02:00
|
|
|
CrashInfo* crash_info = reinterpret_cast<CrashInfo*>(&buf);
|
2017-08-21 23:31:17 +02:00
|
|
|
ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), &buf, sizeof(buf)));
|
|
|
|
if (rc == -1) {
|
|
|
|
PLOG(FATAL) << "failed to read target ucontext";
|
2018-08-07 00:38:29 +02:00
|
|
|
} else {
|
|
|
|
ssize_t expected_size = 0;
|
|
|
|
switch (crash_info->header.version) {
|
|
|
|
case 1:
|
|
|
|
expected_size = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataV1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
expected_size = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataV2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
LOG(FATAL) << "unexpected CrashInfo version: " << crash_info->header.version;
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (rc != expected_size) {
|
|
|
|
LOG(FATAL) << "read " << rc << " bytes when reading target crash information, expected "
|
|
|
|
<< expected_size;
|
|
|
|
}
|
2017-08-21 23:31:17 +02:00
|
|
|
}
|
|
|
|
|
2018-08-07 00:38:29 +02:00
|
|
|
*fdsan_table_address = 0;
|
|
|
|
switch (crash_info->header.version) {
|
|
|
|
case 2:
|
|
|
|
*fdsan_table_address = crash_info->data.v2.fdsan_table_address;
|
2018-09-18 22:22:22 +02:00
|
|
|
FALLTHROUGH_INTENDED;
|
2018-08-07 00:38:29 +02:00
|
|
|
case 1:
|
|
|
|
*abort_msg_address = crash_info->data.v1.abort_msg_address;
|
|
|
|
*siginfo = crash_info->data.v1.siginfo;
|
|
|
|
regs->reset(Regs::CreateFromUcontext(Regs::CurrentArch(), &crash_info->data.v1.ucontext));
|
|
|
|
break;
|
2017-08-21 23:31:17 +02:00
|
|
|
|
2018-08-07 00:38:29 +02:00
|
|
|
default:
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
2017-08-21 23:31:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for a process to clone and return the child's pid.
|
|
|
|
// Note: this leaves the parent in PTRACE_EVENT_STOP.
|
|
|
|
static pid_t wait_for_clone(pid_t pid, bool resume_child) {
|
|
|
|
int status;
|
|
|
|
pid_t result = TEMP_FAILURE_RETRY(waitpid(pid, &status, __WALL));
|
|
|
|
if (result == -1) {
|
|
|
|
PLOG(FATAL) << "failed to waitpid";
|
|
|
|
}
|
2017-05-24 16:07:25 +02:00
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
LOG(FATAL) << "traced process exited with status " << WEXITSTATUS(status);
|
|
|
|
} else if (WIFSIGNALED(status)) {
|
|
|
|
LOG(FATAL) << "traced process exited with signal " << WTERMSIG(status);
|
|
|
|
} else if (!WIFSTOPPED(status)) {
|
|
|
|
LOG(FATAL) << "process didn't stop? (status = " << status << ")";
|
2017-03-13 22:13:29 +01:00
|
|
|
}
|
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
if (status >> 8 != (SIGTRAP | (PTRACE_EVENT_CLONE << 8))) {
|
|
|
|
LOG(FATAL) << "process didn't stop due to PTRACE_O_TRACECLONE (status = " << status << ")";
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
pid_t child;
|
|
|
|
if (ptrace(PTRACE_GETEVENTMSG, pid, 0, &child) != 0) {
|
|
|
|
PLOG(FATAL) << "failed to get child pid via PTRACE_GETEVENTMSG";
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
int stop_signal;
|
|
|
|
if (!wait_for_stop(child, &stop_signal)) {
|
|
|
|
PLOG(FATAL) << "failed to waitpid on child";
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_EQ(0, stop_signal);
|
|
|
|
|
|
|
|
if (resume_child) {
|
|
|
|
if (ptrace(PTRACE_CONT, child, 0, 0) != 0) {
|
|
|
|
PLOG(FATAL) << "failed to resume child (pid = " << child << ")";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
static pid_t wait_for_vm_process(pid_t pseudothread_tid) {
|
|
|
|
// The pseudothread will double-fork, we want its grandchild.
|
|
|
|
pid_t intermediate = wait_for_clone(pseudothread_tid, true);
|
|
|
|
pid_t vm_pid = wait_for_clone(intermediate, false);
|
|
|
|
if (ptrace(PTRACE_DETACH, intermediate, 0, 0) != 0) {
|
|
|
|
PLOG(FATAL) << "failed to detach from intermediate vm process";
|
|
|
|
}
|
|
|
|
|
|
|
|
return vm_pid;
|
|
|
|
}
|
|
|
|
|
tombstoned: fixed tombstones failed issue
There is a problem about tombstone, which it will fail to
generate tombstone file in some scenarios due to socket
communication exception.
Reproduce step:
step 1: reboot device
step 2: ps -ef |grep zygote , get the pid of zygote64
(Attention: zygote64 should never been killed or reboot,
otherwise we can get the tombstone file)
step 3: kill -5 pid of zygote64
step 4: cd data/tombstones/, and could not find the tombstone
file of zygote64.
[Cause Analysis]
1. There are following logs by logcat:
11-19 15:38:43.789 569 569 F libc : Fatal signal 5 (SIGTRAP),
code 0 (SI_USER) in tid 569 (main), pid 569 (main)
11-19 15:38:43.829 6115 6115 I crash_dump64: obtaining output
fd from tombstoned, type: kDebuggerdTombstone
11-19 15:38:43.830 569 5836 I Zygote : Process 6114 exited
cleanly (0)
11-19 15:38:43.830 777 777 I /system/bin/tombstoned: received
crash request for pid 569
11-19 15:38:43.831 6115 6115 I crash_dump64: performing dump of
process 569 (target tid = 569)
...
11-19 15:38:43.937 777 777 W /system/bin/tombstoned: crash
socket received short read of length 0 (expected 12)
2. The last log was print by function of crash_request_cb in
file of tombstoned.cpp, following related code:
rc = TEMP_FAILURE_RETRY(read(sockfd, &request, sizeof(request)));
if (rc == -1) {
PLOG(WARNING) << "failed to read from crash socket";
goto fail;
} else if (rc != sizeof(request)) {
LOG(WARNING) << "crash socket received short read of length " << rc << " (expected "
<< sizeof(request) << ")";
goto fail;
}
Tombstoned read message by socket, and now the message length is
zero. Some socket communication exception occurs at that time.
We try to let crash_dump resend the socket message when the
communication is abnormal. Just as this CL.
Test: 1 reboot device
2 ps -ef |grep zygote , get the pid of zygote64
(Attention: zygote64 should never been killed or reboot,
otherwise we can get the tombstone file)
3 kill -5 pid of zygote64
4 cd data/tombstones/, and could find the tombstone file of
zygote64.
Change-Id: Ic152b081024d6c12f757927079fd221b63445b18
2018-11-24 10:12:33 +01:00
|
|
|
static void InstallSigPipeHandler() {
|
|
|
|
struct sigaction action = {};
|
|
|
|
action.sa_handler = SIG_IGN;
|
|
|
|
action.sa_flags = SA_RESTART;
|
|
|
|
sigaction(SIGPIPE, &action, nullptr);
|
|
|
|
}
|
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
int main(int argc, char** argv) {
|
2018-04-27 22:31:47 +02:00
|
|
|
DefuseSignalHandlers();
|
tombstoned: fixed tombstones failed issue
There is a problem about tombstone, which it will fail to
generate tombstone file in some scenarios due to socket
communication exception.
Reproduce step:
step 1: reboot device
step 2: ps -ef |grep zygote , get the pid of zygote64
(Attention: zygote64 should never been killed or reboot,
otherwise we can get the tombstone file)
step 3: kill -5 pid of zygote64
step 4: cd data/tombstones/, and could not find the tombstone
file of zygote64.
[Cause Analysis]
1. There are following logs by logcat:
11-19 15:38:43.789 569 569 F libc : Fatal signal 5 (SIGTRAP),
code 0 (SI_USER) in tid 569 (main), pid 569 (main)
11-19 15:38:43.829 6115 6115 I crash_dump64: obtaining output
fd from tombstoned, type: kDebuggerdTombstone
11-19 15:38:43.830 569 5836 I Zygote : Process 6114 exited
cleanly (0)
11-19 15:38:43.830 777 777 I /system/bin/tombstoned: received
crash request for pid 569
11-19 15:38:43.831 6115 6115 I crash_dump64: performing dump of
process 569 (target tid = 569)
...
11-19 15:38:43.937 777 777 W /system/bin/tombstoned: crash
socket received short read of length 0 (expected 12)
2. The last log was print by function of crash_request_cb in
file of tombstoned.cpp, following related code:
rc = TEMP_FAILURE_RETRY(read(sockfd, &request, sizeof(request)));
if (rc == -1) {
PLOG(WARNING) << "failed to read from crash socket";
goto fail;
} else if (rc != sizeof(request)) {
LOG(WARNING) << "crash socket received short read of length " << rc << " (expected "
<< sizeof(request) << ")";
goto fail;
}
Tombstoned read message by socket, and now the message length is
zero. Some socket communication exception occurs at that time.
We try to let crash_dump resend the socket message when the
communication is abnormal. Just as this CL.
Test: 1 reboot device
2 ps -ef |grep zygote , get the pid of zygote64
(Attention: zygote64 should never been killed or reboot,
otherwise we can get the tombstone file)
3 kill -5 pid of zygote64
4 cd data/tombstones/, and could find the tombstone file of
zygote64.
Change-Id: Ic152b081024d6c12f757927079fd221b63445b18
2018-11-24 10:12:33 +01:00
|
|
|
InstallSigPipeHandler();
|
2018-04-27 22:31:47 +02:00
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
atrace_begin(ATRACE_TAG, "before reparent");
|
|
|
|
pid_t target_process = getppid();
|
|
|
|
|
|
|
|
// Open /proc/`getppid()` before we daemonize.
|
|
|
|
std::string target_proc_path = "/proc/" + std::to_string(target_process);
|
2016-10-19 03:17:52 +02:00
|
|
|
int target_proc_fd = open(target_proc_path.c_str(), O_DIRECTORY | O_RDONLY);
|
|
|
|
if (target_proc_fd == -1) {
|
|
|
|
PLOG(FATAL) << "failed to open " << target_proc_path;
|
|
|
|
}
|
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
// Make sure getppid() hasn't changed.
|
|
|
|
if (getppid() != target_process) {
|
|
|
|
LOG(FATAL) << "parent died";
|
2017-02-17 04:17:28 +01:00
|
|
|
}
|
2017-05-31 00:31:02 +02:00
|
|
|
atrace_end(ATRACE_TAG);
|
|
|
|
|
2016-10-19 03:17:52 +02:00
|
|
|
// Reparent ourselves to init, so that the signal handler can waitpid on the
|
|
|
|
// original process to avoid leaving a zombie for non-fatal dumps.
|
2017-08-21 23:31:17 +02:00
|
|
|
// Move the input/output pipes off of stdout/stderr, out of paranoia.
|
|
|
|
unique_fd output_pipe(dup(STDOUT_FILENO));
|
|
|
|
unique_fd input_pipe(dup(STDIN_FILENO));
|
|
|
|
|
|
|
|
unique_fd fork_exit_read, fork_exit_write;
|
|
|
|
if (!Pipe(&fork_exit_read, &fork_exit_write)) {
|
|
|
|
PLOG(FATAL) << "failed to create pipe";
|
|
|
|
}
|
|
|
|
|
2016-10-19 03:17:52 +02:00
|
|
|
pid_t forkpid = fork();
|
|
|
|
if (forkpid == -1) {
|
|
|
|
PLOG(FATAL) << "fork failed";
|
2017-08-21 23:31:17 +02:00
|
|
|
} else if (forkpid == 0) {
|
|
|
|
fork_exit_read.reset();
|
|
|
|
} else {
|
|
|
|
// We need the pseudothread to live until we get around to verifying the vm pid against it.
|
|
|
|
// The last thing it does is block on a waitpid on us, so wait until our child tells us to die.
|
|
|
|
fork_exit_write.reset();
|
|
|
|
char buf;
|
|
|
|
TEMP_FAILURE_RETRY(read(fork_exit_read.get(), &buf, sizeof(buf)));
|
|
|
|
_exit(0);
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
|
|
|
|
2017-05-31 00:31:02 +02:00
|
|
|
ATRACE_NAME("after reparent");
|
2017-08-21 23:31:17 +02:00
|
|
|
pid_t pseudothread_tid;
|
|
|
|
DebuggerdDumpType dump_type;
|
2018-08-07 00:38:29 +02:00
|
|
|
uintptr_t abort_msg_address = 0;
|
|
|
|
uintptr_t fdsan_table_address = 0;
|
2017-08-21 23:31:17 +02:00
|
|
|
|
|
|
|
Initialize(argv);
|
|
|
|
ParseArgs(argc, argv, &pseudothread_tid, &dump_type);
|
2017-05-31 00:31:02 +02:00
|
|
|
|
2017-01-23 02:59:02 +01:00
|
|
|
// Die if we take too long.
|
2017-06-23 04:45:53 +02:00
|
|
|
//
|
|
|
|
// Note: processes with many threads and minidebug-info can take a bit to
|
|
|
|
// unwind, do not make this too small. b/62828735
|
2017-08-21 23:31:17 +02:00
|
|
|
alarm(30);
|
2017-01-23 02:59:02 +01:00
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
// Get the process name (aka cmdline).
|
|
|
|
std::string process_name = get_process_name(g_target_thread);
|
2017-02-13 23:46:19 +01:00
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
// Collect the list of open files.
|
|
|
|
OpenFilesList open_files;
|
|
|
|
{
|
|
|
|
ATRACE_NAME("open files");
|
2018-08-07 03:26:42 +02:00
|
|
|
populate_open_files_list(&open_files, g_target_thread);
|
2017-08-21 23:31:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// In order to reduce the duration that we pause the process for, we ptrace
|
|
|
|
// the threads, fetch their registers and associated information, and then
|
|
|
|
// fork a separate process as a snapshot of the process's address space.
|
|
|
|
std::set<pid_t> threads;
|
|
|
|
if (!android::procinfo::GetProcessTids(g_target_thread, &threads)) {
|
|
|
|
PLOG(FATAL) << "failed to get process threads";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<pid_t, ThreadInfo> thread_info;
|
|
|
|
siginfo_t siginfo;
|
|
|
|
std::string error;
|
2017-05-31 00:31:02 +02:00
|
|
|
|
2017-02-13 23:46:19 +01:00
|
|
|
{
|
2017-06-13 19:21:12 +02:00
|
|
|
ATRACE_NAME("ptrace");
|
2017-08-21 23:31:17 +02:00
|
|
|
for (pid_t thread : threads) {
|
|
|
|
// Trace the pseudothread separately, so we can use different options.
|
|
|
|
if (thread == pseudothread_tid) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-02-13 23:46:19 +01:00
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
if (!ptrace_seize_thread(target_proc_fd, thread, &error)) {
|
|
|
|
bool fatal = thread == g_target_thread;
|
|
|
|
LOG(fatal ? FATAL : WARNING) << error;
|
2017-06-13 19:21:12 +02:00
|
|
|
}
|
2017-02-13 23:46:19 +01:00
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
ThreadInfo info;
|
|
|
|
info.pid = target_process;
|
|
|
|
info.tid = thread;
|
|
|
|
info.process_name = process_name;
|
|
|
|
info.thread_name = get_thread_name(thread);
|
2017-06-13 19:21:12 +02:00
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
if (!ptrace_interrupt(thread, &info.signo)) {
|
|
|
|
PLOG(WARNING) << "failed to ptrace interrupt thread " << thread;
|
|
|
|
ptrace(PTRACE_DETACH, thread, 0, 0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (thread == g_target_thread) {
|
|
|
|
// Read the thread's registers along with the rest of the crash info out of the pipe.
|
2018-08-07 00:38:29 +02:00
|
|
|
ReadCrashInfo(input_pipe, &siginfo, &info.registers, &abort_msg_address,
|
|
|
|
&fdsan_table_address);
|
2017-08-21 23:31:17 +02:00
|
|
|
info.siginfo = &siginfo;
|
|
|
|
info.signo = info.siginfo->si_signo;
|
|
|
|
} else {
|
|
|
|
info.registers.reset(Regs::RemoteGet(thread));
|
|
|
|
if (!info.registers) {
|
|
|
|
PLOG(WARNING) << "failed to fetch registers for thread " << thread;
|
|
|
|
ptrace(PTRACE_DETACH, thread, 0, 0);
|
|
|
|
continue;
|
2017-06-13 19:21:12 +02:00
|
|
|
}
|
2017-02-13 23:46:19 +01:00
|
|
|
}
|
2017-08-21 23:31:17 +02:00
|
|
|
|
|
|
|
thread_info[thread] = std::move(info);
|
2017-02-13 23:46:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
// Trace the pseudothread with PTRACE_O_TRACECLONE and tell it to fork.
|
|
|
|
if (!ptrace_seize_thread(target_proc_fd, pseudothread_tid, &error, PTRACE_O_TRACECLONE)) {
|
|
|
|
LOG(FATAL) << "failed to seize pseudothread: " << error;
|
2017-02-13 23:46:19 +01:00
|
|
|
}
|
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
if (TEMP_FAILURE_RETRY(write(output_pipe.get(), "\1", 1)) != 1) {
|
|
|
|
PLOG(FATAL) << "failed to write to pseudothread";
|
2017-05-31 00:31:02 +02:00
|
|
|
}
|
2017-02-13 23:46:19 +01:00
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
pid_t vm_pid = wait_for_vm_process(pseudothread_tid);
|
|
|
|
if (ptrace(PTRACE_DETACH, pseudothread_tid, 0, 0) != 0) {
|
|
|
|
PLOG(FATAL) << "failed to detach from pseudothread";
|
|
|
|
}
|
2017-06-13 19:21:12 +02:00
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
// The pseudothread can die now.
|
|
|
|
fork_exit_write.reset();
|
2016-10-19 03:17:52 +02:00
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
// Defer the message until later, for readability.
|
|
|
|
bool wait_for_gdb = android::base::GetBoolProperty("debug.debuggerd.wait_for_gdb", false);
|
|
|
|
if (siginfo.si_signo == DEBUGGER_SIGNAL) {
|
|
|
|
wait_for_gdb = false;
|
2017-05-31 00:31:02 +02:00
|
|
|
}
|
2016-10-19 03:17:52 +02:00
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
// Detach from all of our attached threads before resuming.
|
|
|
|
for (const auto& [tid, thread] : thread_info) {
|
|
|
|
int resume_signal = thread.signo == DEBUGGER_SIGNAL ? 0 : thread.signo;
|
|
|
|
if (wait_for_gdb) {
|
|
|
|
resume_signal = 0;
|
|
|
|
if (tgkill(target_process, tid, SIGSTOP) != 0) {
|
|
|
|
PLOG(WARNING) << "failed to send SIGSTOP to " << tid;
|
|
|
|
}
|
|
|
|
}
|
2016-10-19 03:17:52 +02:00
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
LOG(DEBUG) << "detaching from thread " << tid;
|
|
|
|
if (ptrace(PTRACE_DETACH, tid, 0, resume_signal) != 0) {
|
|
|
|
PLOG(ERROR) << "failed to detach from thread " << tid;
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
// Drop our capabilities now that we've fetched all of the information we need.
|
|
|
|
drop_capabilities();
|
2016-10-19 03:17:52 +02:00
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
{
|
|
|
|
ATRACE_NAME("tombstoned_connect");
|
|
|
|
LOG(INFO) << "obtaining output fd from tombstoned, type: " << dump_type;
|
|
|
|
g_tombstoned_connected =
|
|
|
|
tombstoned_connect(g_target_thread, &g_tombstoned_socket, &g_output_fd, dump_type);
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
if (g_tombstoned_connected) {
|
|
|
|
if (TEMP_FAILURE_RETRY(dup2(g_output_fd.get(), STDOUT_FILENO)) == -1) {
|
|
|
|
PLOG(ERROR) << "failed to dup2 output fd (" << g_output_fd.get() << ") to STDOUT_FILENO";
|
2017-05-31 00:31:02 +02:00
|
|
|
}
|
2017-08-21 23:31:17 +02:00
|
|
|
} else {
|
|
|
|
unique_fd devnull(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR)));
|
|
|
|
TEMP_FAILURE_RETRY(dup2(devnull.get(), STDOUT_FILENO));
|
|
|
|
g_output_fd = std::move(devnull);
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
|
|
|
|
2018-08-07 00:38:29 +02:00
|
|
|
LOG(INFO) << "performing dump of process " << target_process
|
|
|
|
<< " (target tid = " << g_target_thread << ")";
|
2017-08-21 23:31:17 +02:00
|
|
|
|
2016-10-19 03:17:52 +02:00
|
|
|
int signo = siginfo.si_signo;
|
2017-02-02 01:31:43 +01:00
|
|
|
bool fatal_signal = signo != DEBUGGER_SIGNAL;
|
2016-10-19 03:17:52 +02:00
|
|
|
bool backtrace = false;
|
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
// si_value is special when used with DEBUGGER_SIGNAL.
|
2016-10-19 03:17:52 +02:00
|
|
|
// 0: dump tombstone
|
|
|
|
// 1: dump backtrace
|
2017-08-21 23:31:17 +02:00
|
|
|
if (!fatal_signal) {
|
|
|
|
int si_val = siginfo.si_value.sival_int;
|
|
|
|
if (si_val == 0) {
|
|
|
|
backtrace = false;
|
|
|
|
} else if (si_val == 1) {
|
|
|
|
backtrace = true;
|
|
|
|
} else {
|
|
|
|
LOG(WARNING) << "unknown si_value value " << si_val;
|
|
|
|
}
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Use seccomp to lock ourselves down.
|
2017-08-21 23:31:17 +02:00
|
|
|
std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(vm_pid, false));
|
|
|
|
if (!map) {
|
|
|
|
LOG(FATAL) << "failed to create backtrace map";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<unwindstack::Memory> process_memory = map->GetProcessMemory();
|
|
|
|
if (!process_memory) {
|
|
|
|
LOG(FATAL) << "failed to get unwindstack::Memory handle";
|
|
|
|
}
|
2016-10-19 03:17:52 +02:00
|
|
|
|
|
|
|
std::string amfd_data;
|
|
|
|
if (backtrace) {
|
2017-05-31 00:31:02 +02:00
|
|
|
ATRACE_NAME("dump_backtrace");
|
2017-08-21 23:31:17 +02:00
|
|
|
dump_backtrace(std::move(g_output_fd), map.get(), thread_info, g_target_thread);
|
2016-10-19 03:17:52 +02:00
|
|
|
} else {
|
2018-08-07 03:26:42 +02:00
|
|
|
{
|
|
|
|
ATRACE_NAME("fdsan table dump");
|
|
|
|
populate_fdsan_table(&open_files, process_memory, fdsan_table_address);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
ATRACE_NAME("engrave_tombstone");
|
|
|
|
engrave_tombstone(std::move(g_output_fd), map.get(), process_memory.get(), thread_info,
|
|
|
|
g_target_thread, abort_msg_address, &open_files, &amfd_data);
|
|
|
|
}
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
if (fatal_signal) {
|
|
|
|
// Don't try to notify ActivityManager if it just crashed, or we might hang until timeout.
|
|
|
|
if (thread_info[target_process].thread_name != "system_server") {
|
|
|
|
activity_manager_notify(target_process, signo, amfd_data);
|
2017-01-23 02:59:02 +01:00
|
|
|
}
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (wait_for_gdb) {
|
2017-01-23 02:59:02 +01:00
|
|
|
// Use ALOGI to line up with output from engrave_tombstone.
|
|
|
|
ALOGI(
|
2017-09-12 10:46:20 +02:00
|
|
|
"***********************************************************\n"
|
|
|
|
"* Process %d has been suspended while crashing.\n"
|
|
|
|
"* To attach gdbserver and start gdb, run this on the host:\n"
|
|
|
|
"*\n"
|
|
|
|
"* gdbclient.py -p %d\n"
|
|
|
|
"*\n"
|
|
|
|
"***********************************************************",
|
2017-08-21 23:31:17 +02:00
|
|
|
target_process, target_process);
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close stdout before we notify tombstoned of completion.
|
|
|
|
close(STDOUT_FILENO);
|
2017-08-21 23:31:17 +02:00
|
|
|
if (g_tombstoned_connected && !tombstoned_notify_completion(g_tombstoned_socket.get())) {
|
2016-10-19 03:17:52 +02:00
|
|
|
LOG(ERROR) << "failed to notify tombstoned of completion";
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|