blob: eda7182060ad6a82c40dddb5e19eb2c832790407 [file] [log] [blame]
Jeff Brown053b8652012-06-06 16:25:03 -07001/*
Mark Salyzynfca0bd12013-12-12 12:21:20 -08002 * Copyright (C) 2012-2014 The Android Open Source Project
Jeff Brown053b8652012-06-06 16:25:03 -07003 *
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
Brigid Smith62ba4892014-06-10 11:53:08 -070017#define LOG_TAG "DEBUG"
18
Josh Gaoc3706662017-08-29 13:08:32 -070019#include "libdebuggerd/tombstone.h"
20
Kévin PETIT4bb47722013-12-18 16:44:24 +000021#include <errno.h>
Kévin PETIT4bb47722013-12-18 16:44:24 +000022#include <signal.h>
23#include <stddef.h>
24#include <stdio.h>
25#include <stdlib.h>
Christopher Ferrisb999b822022-02-09 17:57:21 -080026#include <sys/prctl.h>
Christopher Ferrisbdea3bb2021-11-17 01:09:22 +000027#include <sys/types.h>
28#include <unistd.h>
Jeff Brown053b8652012-06-06 16:25:03 -070029
Christopher Ferris6e964032015-05-15 17:30:21 -070030#include <memory>
31#include <string>
32
Josh Gao57f58f82017-03-15 23:23:22 -070033#include <android-base/file.h>
Josh Gao57f58f82017-03-15 23:23:22 -070034#include <android-base/unique_fd.h>
35#include <android/log.h>
Josh Gao618cea32021-01-26 17:45:43 -080036#include <async_safe/log.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070037#include <log/log.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070038#include <private/android_filesystem_config.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070039#include <unwindstack/Memory.h>
40#include <unwindstack/Regs.h>
Christopher Ferris60eb1972019-01-15 15:18:43 -080041#include <unwindstack/Unwinder.h>
Jeff Brown053b8652012-06-06 16:25:03 -070042
Josh Gaoc3706662017-08-29 13:08:32 -070043#include "libdebuggerd/backtrace.h"
Josh Gaoc3706662017-08-29 13:08:32 -070044#include "libdebuggerd/open_files_list.h"
Josh Gao2b2ae0c2017-08-21 14:31:17 -070045#include "libdebuggerd/utility.h"
Elliott Hughesa660cb32020-07-23 15:26:10 -070046#include "util.h"
Jeff Brown053b8652012-06-06 16:25:03 -070047
Josh Gao76e1e302021-01-26 15:53:11 -080048#include "tombstone.pb.h"
49
Josh Gao2b2ae0c2017-08-21 14:31:17 -070050using android::base::unique_fd;
51
Elliott Hughese1415a52018-02-15 09:18:21 -080052using namespace std::literals::string_literals;
53
Josh Gao76e1e302021-01-26 15:53:11 -080054void engrave_tombstone_ucontext(int tombstone_fd, int proto_fd, uint64_t abort_msg_address,
55 siginfo_t* siginfo, ucontext_t* ucontext) {
Misha Wagner39c5b8c2019-04-18 16:07:33 +010056 pid_t uid = getuid();
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080057 pid_t pid = getpid();
58 pid_t tid = gettid();
59
Josh Gaoe73c9322017-02-08 16:06:26 -080060 log_t log;
61 log.current_tid = tid;
62 log.crashed_tid = tid;
63 log.tfd = tombstone_fd;
64 log.amfd_data = nullptr;
65
Elliott Hughesa660cb32020-07-23 15:26:10 -070066 std::string thread_name = get_thread_name(tid);
Josh Gao31348a72021-03-29 21:53:42 -070067 std::vector<std::string> command_line = get_command_line(pid);
Josh Gao57f58f82017-03-15 23:23:22 -070068
Christopher Ferris60eb1972019-01-15 15:18:43 -080069 std::unique_ptr<unwindstack::Regs> regs(
70 unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), ucontext));
Josh Gaoe73c9322017-02-08 16:06:26 -080071
Josh Gao76e1e302021-01-26 15:53:11 -080072 std::string selinux_label;
73 android::base::ReadFileToString("/proc/self/attr/current", &selinux_label);
74
Josh Gao2b2ae0c2017-08-21 14:31:17 -070075 std::map<pid_t, ThreadInfo> threads;
Elliott Hughesa660cb32020-07-23 15:26:10 -070076 threads[tid] = ThreadInfo{
Christopher Ferrisb999b822022-02-09 17:57:21 -080077 .registers = std::move(regs), .uid = uid, .tid = tid, .thread_name = std::move(thread_name),
78 .pid = pid, .command_line = std::move(command_line), .selinux_label = std::move(selinux_label),
79 .siginfo = siginfo,
80#if defined(__aarch64__)
81 // Only supported on aarch64 for now.
82 .tagged_addr_ctrl = prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0),
83 .pac_enabled_keys = prctl(PR_PAC_GET_ENABLED_KEYS, 0, 0, 0, 0),
84#endif
Josh Gao2b2ae0c2017-08-21 14:31:17 -070085 };
Christopher Ferrisb999b822022-02-09 17:57:21 -080086 if (pid == tid) {
87 const ThreadInfo& thread = threads[pid];
88 if (!iterate_tids(pid, [&threads, &thread](pid_t tid) {
89 threads[tid] = ThreadInfo{
90 .uid = thread.uid,
91 .tid = tid,
92 .pid = thread.pid,
93 .command_line = thread.command_line,
94 .thread_name = get_thread_name(tid),
95 .tagged_addr_ctrl = thread.tagged_addr_ctrl,
96 .pac_enabled_keys = thread.pac_enabled_keys,
97 };
98 })) {
99 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to open /proc/%d/task: %s", pid,
100 strerror(errno));
101 }
102 }
Josh Gao77b00ed2017-05-05 18:11:23 -0700103
yidong zhangcbf7c462021-06-24 17:51:56 +0800104 auto process_memory =
105 unwindstack::Memory::CreateProcessMemoryCached(getpid());
Christopher Ferrisc6d42832022-04-19 21:38:59 -0700106 unwindstack::UnwinderFromPid unwinder(kMaxFrames, pid, unwindstack::Regs::CurrentArch(), nullptr,
107 process_memory);
Christopher Ferrisb05c4722020-09-23 15:51:46 -0700108 if (!unwinder.Init()) {
Christopher Ferrisb999b822022-02-09 17:57:21 -0800109 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to init unwinder object");
110 return;
Josh Gaoe73c9322017-02-08 16:06:26 -0800111 }
Josh Gaofdc95c92017-09-13 15:33:39 -0700112
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800113 ProcessInfo process_info;
114 process_info.abort_msg_address = abort_msg_address;
Josh Gao76e1e302021-01-26 15:53:11 -0800115 engrave_tombstone(unique_fd(dup(tombstone_fd)), unique_fd(dup(proto_fd)), &unwinder, threads, tid,
116 process_info, nullptr, nullptr);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700117}
118
Josh Gao76e1e302021-01-26 15:53:11 -0800119void engrave_tombstone(unique_fd output_fd, unique_fd proto_fd, unwindstack::Unwinder* unwinder,
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700120 const std::map<pid_t, ThreadInfo>& threads, pid_t target_thread,
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800121 const ProcessInfo& process_info, OpenFilesList* open_files,
122 std::string* amfd_data) {
Elliott Hughesa660cb32020-07-23 15:26:10 -0700123 // Don't copy log messages to tombstone unless this is a development device.
Josh Gao76e1e302021-01-26 15:53:11 -0800124 Tombstone tombstone;
125 engrave_tombstone_proto(&tombstone, unwinder, threads, target_thread, process_info, open_files);
126
Josh Gao618cea32021-01-26 17:45:43 -0800127 if (proto_fd != -1) {
128 if (!tombstone.SerializeToFileDescriptor(proto_fd.get())) {
129 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to write proto tombstone: %s",
130 strerror(errno));
131 }
Josh Gao76e1e302021-01-26 15:53:11 -0800132 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700133
134 log_t log;
135 log.current_tid = target_thread;
136 log.crashed_tid = target_thread;
137 log.tfd = output_fd.get();
138 log.amfd_data = amfd_data;
139
Christopher Ferrisbdea3bb2021-11-17 01:09:22 +0000140 tombstone_proto_to_text(tombstone, [&log](const std::string& line, bool should_log) {
141 _LOG(&log, should_log ? logtype::HEADER : logtype::LOGS, "%s\n", line.c_str());
142 });
Josh Gaoe73c9322017-02-08 16:06:26 -0800143}