blob: 0ce55738a2a43c5bc3de538cc1fe87bf4abcdd34 [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>
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -070039#include <unwindstack/AndroidUnwinder.h>
40#include <unwindstack/Error.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070041#include <unwindstack/Regs.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();
Christopher Ferris7c2e7e32022-05-27 12:57:58 -070058 pid_t target_tid = gettid();
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080059
Josh Gaoe73c9322017-02-08 16:06:26 -080060 log_t log;
Christopher Ferris7c2e7e32022-05-27 12:57:58 -070061 log.current_tid = target_tid;
62 log.crashed_tid = target_tid;
Josh Gaoe73c9322017-02-08 16:06:26 -080063 log.tfd = tombstone_fd;
64 log.amfd_data = nullptr;
65
Christopher Ferris7c2e7e32022-05-27 12:57:58 -070066 std::string thread_name = get_thread_name(target_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;
Christopher Ferris7c2e7e32022-05-27 12:57:58 -070076 threads[target_tid] = ThreadInfo {
77 .registers = std::move(regs), .uid = uid, .tid = target_tid,
78 .thread_name = std::move(thread_name), .pid = pid, .command_line = std::move(command_line),
Xiaohui Niu7bfbe412024-04-26 11:08:15 +080079 .selinux_label = std::move(selinux_label), .siginfo = siginfo, .signo = siginfo->si_signo,
Christopher Ferrisb999b822022-02-09 17:57:21 -080080 // Only supported on aarch64 for now.
Christopher Ferrisfac411d2022-10-27 15:23:38 -070081#if defined(__aarch64__)
82 .tagged_addr_ctrl = prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0),
Christopher Ferrisb999b822022-02-09 17:57:21 -080083 .pac_enabled_keys = prctl(PR_PAC_GET_ENABLED_KEYS, 0, 0, 0, 0),
84#endif
Josh Gao2b2ae0c2017-08-21 14:31:17 -070085 };
Christopher Ferris7c2e7e32022-05-27 12:57:58 -070086 const ThreadInfo& thread = threads[pid];
87 if (!iterate_tids(pid, [&threads, &thread, &target_tid](pid_t tid) {
88 if (target_tid == tid) {
89 return;
90 }
Christopher Ferris7c2e7e32022-05-27 12:57:58 -070091 threads[tid] = ThreadInfo{
92 .uid = thread.uid,
93 .tid = tid,
94 .pid = thread.pid,
95 .command_line = thread.command_line,
96 .thread_name = get_thread_name(tid),
97 .tagged_addr_ctrl = thread.tagged_addr_ctrl,
98 .pac_enabled_keys = thread.pac_enabled_keys,
99 };
100 })) {
101 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to open /proc/%d/task: %s", pid,
102 strerror(errno));
Christopher Ferrisb999b822022-02-09 17:57:21 -0800103 }
Josh Gao77b00ed2017-05-05 18:11:23 -0700104
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700105 // Do not use the thread cache here because it will call pthread_key_create
106 // which doesn't work in linker code. See b/189803009.
107 // Use a normal cached object because the thread is stopped, and there
108 // is no chance of data changing between reads.
109 auto process_memory = unwindstack::Memory::CreateProcessMemoryCached(getpid());
110 unwindstack::AndroidLocalUnwinder unwinder(process_memory);
111 unwindstack::ErrorData error;
112 if (!unwinder.Initialize(error)) {
113 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to init unwinder object: %s",
114 unwindstack::GetErrorCodeString(error.code));
Christopher Ferrisb999b822022-02-09 17:57:21 -0800115 return;
Josh Gaoe73c9322017-02-08 16:06:26 -0800116 }
Josh Gaofdc95c92017-09-13 15:33:39 -0700117
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800118 ProcessInfo process_info;
119 process_info.abort_msg_address = abort_msg_address;
Christopher Ferris7c2e7e32022-05-27 12:57:58 -0700120 engrave_tombstone(unique_fd(dup(tombstone_fd)), unique_fd(dup(proto_fd)), &unwinder, threads,
121 target_tid, process_info, nullptr, nullptr);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700122}
123
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700124void engrave_tombstone(unique_fd output_fd, unique_fd proto_fd,
125 unwindstack::AndroidUnwinder* unwinder,
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700126 const std::map<pid_t, ThreadInfo>& threads, pid_t target_thread,
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800127 const ProcessInfo& process_info, OpenFilesList* open_files,
Sijie Chenc8027932024-05-10 18:40:48 +0000128 std::string* amfd_data, const Architecture* guest_arch,
129 unwindstack::AndroidUnwinder* guest_unwinder) {
Elliott Hughesa660cb32020-07-23 15:26:10 -0700130 // Don't copy log messages to tombstone unless this is a development device.
Josh Gao76e1e302021-01-26 15:53:11 -0800131 Tombstone tombstone;
Sijie Chenc8027932024-05-10 18:40:48 +0000132 engrave_tombstone_proto(&tombstone, unwinder, threads, target_thread, process_info, open_files,
133 guest_arch, guest_unwinder);
Josh Gao76e1e302021-01-26 15:53:11 -0800134
Josh Gao618cea32021-01-26 17:45:43 -0800135 if (proto_fd != -1) {
136 if (!tombstone.SerializeToFileDescriptor(proto_fd.get())) {
137 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to write proto tombstone: %s",
138 strerror(errno));
139 }
Josh Gao76e1e302021-01-26 15:53:11 -0800140 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700141
142 log_t log;
143 log.current_tid = target_thread;
144 log.crashed_tid = target_thread;
145 log.tfd = output_fd.get();
146 log.amfd_data = amfd_data;
147
Christopher Ferrisbdea3bb2021-11-17 01:09:22 +0000148 tombstone_proto_to_text(tombstone, [&log](const std::string& line, bool should_log) {
149 _LOG(&log, should_log ? logtype::HEADER : logtype::LOGS, "%s\n", line.c_str());
150 });
Josh Gaoe73c9322017-02-08 16:06:26 -0800151}