2012-06-07 01:25:03 +02:00
|
|
|
/*
|
2013-12-12 21:21:20 +01:00
|
|
|
* Copyright (C) 2012-2014 The Android Open Source Project
|
2012-06-07 01:25:03 +02: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
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-06-10 20:53:08 +02:00
|
|
|
#define LOG_TAG "DEBUG"
|
|
|
|
|
2017-08-29 22:08:32 +02:00
|
|
|
#include "libdebuggerd/tombstone.h"
|
|
|
|
|
2013-12-18 17:44:24 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
2012-06-07 01:25:03 +02:00
|
|
|
#include <stddef.h>
|
2013-12-18 17:44:24 +01:00
|
|
|
#include <stdio.h>
|
2012-06-07 01:25:03 +02:00
|
|
|
#include <stdlib.h>
|
2022-02-10 02:57:21 +01:00
|
|
|
#include <sys/prctl.h>
|
2021-11-17 02:09:22 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2012-06-07 01:25:03 +02:00
|
|
|
|
2015-05-16 02:30:21 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
|
2017-03-16 07:23:22 +01:00
|
|
|
#include <android-base/file.h>
|
|
|
|
#include <android-base/unique_fd.h>
|
|
|
|
#include <android/log.h>
|
2021-01-27 02:45:43 +01:00
|
|
|
#include <async_safe/log.h>
|
2016-10-17 23:28:00 +02:00
|
|
|
#include <log/log.h>
|
2016-09-29 00:54:45 +02:00
|
|
|
#include <private/android_filesystem_config.h>
|
2022-03-16 00:56:09 +01:00
|
|
|
#include <unwindstack/AndroidUnwinder.h>
|
|
|
|
#include <unwindstack/Error.h>
|
2017-08-21 23:31:17 +02:00
|
|
|
#include <unwindstack/Regs.h>
|
2012-06-07 01:25:03 +02:00
|
|
|
|
2017-08-29 22:08:32 +02:00
|
|
|
#include "libdebuggerd/backtrace.h"
|
|
|
|
#include "libdebuggerd/open_files_list.h"
|
2017-08-21 23:31:17 +02:00
|
|
|
#include "libdebuggerd/utility.h"
|
2020-07-24 00:26:10 +02:00
|
|
|
#include "util.h"
|
2012-06-07 01:25:03 +02:00
|
|
|
|
2021-01-27 00:53:11 +01:00
|
|
|
#include "tombstone.pb.h"
|
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
using android::base::unique_fd;
|
2017-02-02 01:59:15 +01:00
|
|
|
|
2018-02-15 18:18:21 +01:00
|
|
|
using namespace std::literals::string_literals;
|
|
|
|
|
2021-01-27 00:53:11 +01:00
|
|
|
void engrave_tombstone_ucontext(int tombstone_fd, int proto_fd, uint64_t abort_msg_address,
|
|
|
|
siginfo_t* siginfo, ucontext_t* ucontext) {
|
2019-04-18 17:07:33 +02:00
|
|
|
pid_t uid = getuid();
|
2017-03-02 02:23:22 +01:00
|
|
|
pid_t pid = getpid();
|
2022-05-27 21:57:58 +02:00
|
|
|
pid_t target_tid = gettid();
|
2017-03-02 02:23:22 +01:00
|
|
|
|
2017-02-09 01:06:26 +01:00
|
|
|
log_t log;
|
2022-05-27 21:57:58 +02:00
|
|
|
log.current_tid = target_tid;
|
|
|
|
log.crashed_tid = target_tid;
|
2017-02-09 01:06:26 +01:00
|
|
|
log.tfd = tombstone_fd;
|
|
|
|
log.amfd_data = nullptr;
|
|
|
|
|
2022-05-27 21:57:58 +02:00
|
|
|
std::string thread_name = get_thread_name(target_tid);
|
2021-03-30 06:53:42 +02:00
|
|
|
std::vector<std::string> command_line = get_command_line(pid);
|
2017-03-16 07:23:22 +01:00
|
|
|
|
2019-01-16 00:18:43 +01:00
|
|
|
std::unique_ptr<unwindstack::Regs> regs(
|
|
|
|
unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), ucontext));
|
2017-08-21 23:31:17 +02:00
|
|
|
|
2021-01-27 00:53:11 +01:00
|
|
|
std::string selinux_label;
|
|
|
|
android::base::ReadFileToString("/proc/self/attr/current", &selinux_label);
|
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
std::map<pid_t, ThreadInfo> threads;
|
2022-05-27 21:57:58 +02:00
|
|
|
threads[target_tid] = ThreadInfo {
|
|
|
|
.registers = std::move(regs), .uid = uid, .tid = target_tid,
|
|
|
|
.thread_name = std::move(thread_name), .pid = pid, .command_line = std::move(command_line),
|
|
|
|
.selinux_label = std::move(selinux_label), .siginfo = siginfo,
|
2022-02-10 02:57:21 +01:00
|
|
|
// Only supported on aarch64 for now.
|
2022-10-28 00:23:38 +02:00
|
|
|
#if defined(__aarch64__)
|
|
|
|
.tagged_addr_ctrl = prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0),
|
2022-02-10 02:57:21 +01:00
|
|
|
.pac_enabled_keys = prctl(PR_PAC_GET_ENABLED_KEYS, 0, 0, 0, 0),
|
|
|
|
#endif
|
2017-08-21 23:31:17 +02:00
|
|
|
};
|
2022-05-27 21:57:58 +02:00
|
|
|
const ThreadInfo& thread = threads[pid];
|
|
|
|
if (!iterate_tids(pid, [&threads, &thread, &target_tid](pid_t tid) {
|
|
|
|
if (target_tid == tid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
threads[tid] = ThreadInfo{
|
|
|
|
.uid = thread.uid,
|
|
|
|
.tid = tid,
|
|
|
|
.pid = thread.pid,
|
|
|
|
.command_line = thread.command_line,
|
|
|
|
.thread_name = get_thread_name(tid),
|
|
|
|
.tagged_addr_ctrl = thread.tagged_addr_ctrl,
|
|
|
|
.pac_enabled_keys = thread.pac_enabled_keys,
|
|
|
|
};
|
|
|
|
})) {
|
|
|
|
async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to open /proc/%d/task: %s", pid,
|
|
|
|
strerror(errno));
|
2022-02-10 02:57:21 +01:00
|
|
|
}
|
2017-08-21 23:31:17 +02:00
|
|
|
|
2022-03-16 00:56:09 +01:00
|
|
|
// Do not use the thread cache here because it will call pthread_key_create
|
|
|
|
// which doesn't work in linker code. See b/189803009.
|
|
|
|
// Use a normal cached object because the thread is stopped, and there
|
|
|
|
// is no chance of data changing between reads.
|
|
|
|
auto process_memory = unwindstack::Memory::CreateProcessMemoryCached(getpid());
|
|
|
|
unwindstack::AndroidLocalUnwinder unwinder(process_memory);
|
|
|
|
unwindstack::ErrorData error;
|
|
|
|
if (!unwinder.Initialize(error)) {
|
|
|
|
async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to init unwinder object: %s",
|
|
|
|
unwindstack::GetErrorCodeString(error.code));
|
2022-02-10 02:57:21 +01:00
|
|
|
return;
|
2017-08-21 23:31:17 +02:00
|
|
|
}
|
|
|
|
|
2020-02-29 04:07:33 +01:00
|
|
|
ProcessInfo process_info;
|
|
|
|
process_info.abort_msg_address = abort_msg_address;
|
2022-05-27 21:57:58 +02:00
|
|
|
engrave_tombstone(unique_fd(dup(tombstone_fd)), unique_fd(dup(proto_fd)), &unwinder, threads,
|
|
|
|
target_tid, process_info, nullptr, nullptr);
|
2017-08-21 23:31:17 +02:00
|
|
|
}
|
|
|
|
|
2022-03-16 00:56:09 +01:00
|
|
|
void engrave_tombstone(unique_fd output_fd, unique_fd proto_fd,
|
|
|
|
unwindstack::AndroidUnwinder* unwinder,
|
2017-08-21 23:31:17 +02:00
|
|
|
const std::map<pid_t, ThreadInfo>& threads, pid_t target_thread,
|
2020-02-29 04:07:33 +01:00
|
|
|
const ProcessInfo& process_info, OpenFilesList* open_files,
|
|
|
|
std::string* amfd_data) {
|
2020-07-24 00:26:10 +02:00
|
|
|
// Don't copy log messages to tombstone unless this is a development device.
|
2021-01-27 00:53:11 +01:00
|
|
|
Tombstone tombstone;
|
|
|
|
engrave_tombstone_proto(&tombstone, unwinder, threads, target_thread, process_info, open_files);
|
|
|
|
|
2021-01-27 02:45:43 +01:00
|
|
|
if (proto_fd != -1) {
|
|
|
|
if (!tombstone.SerializeToFileDescriptor(proto_fd.get())) {
|
|
|
|
async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to write proto tombstone: %s",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
2021-01-27 00:53:11 +01:00
|
|
|
}
|
2017-08-21 23:31:17 +02:00
|
|
|
|
|
|
|
log_t log;
|
|
|
|
log.current_tid = target_thread;
|
|
|
|
log.crashed_tid = target_thread;
|
|
|
|
log.tfd = output_fd.get();
|
|
|
|
log.amfd_data = amfd_data;
|
|
|
|
|
2021-11-17 02:09:22 +01:00
|
|
|
tombstone_proto_to_text(tombstone, [&log](const std::string& line, bool should_log) {
|
|
|
|
_LOG(&log, should_log ? logtype::HEADER : logtype::LOGS, "%s\n", line.c_str());
|
|
|
|
});
|
2017-02-09 01:06:26 +01:00
|
|
|
}
|