1 /*
2 * Copyright (C) 2012-2014 The Android Open Source Project
3 *
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
17 #define LOG_TAG "DEBUG"
18
19 #include "libdebuggerd/tombstone.h"
20 #include "libdebuggerd/tombstone_proto_to_text.h"
21
22 #include <errno.h>
23 #include <signal.h>
24 #include <stddef.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/prctl.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30
31 #include <memory>
32 #include <string>
33
34 #include <android-base/file.h>
35 #include <android-base/unique_fd.h>
36 #include <android/log.h>
37 #include <async_safe/log.h>
38 #include <log/log.h>
39 #include <private/android_filesystem_config.h>
40 #include <unwindstack/AndroidUnwinder.h>
41 #include <unwindstack/Error.h>
42 #include <unwindstack/Regs.h>
43
44 #include "libdebuggerd/backtrace.h"
45 #include "libdebuggerd/open_files_list.h"
46 #include "libdebuggerd/utility.h"
47 #include "util.h"
48
49 #include "tombstone.pb.h"
50
51 using android::base::unique_fd;
52
53 using namespace std::literals::string_literals;
54
engrave_tombstone_ucontext(int tombstone_fd,int proto_fd,uint64_t abort_msg_address,siginfo_t * siginfo,ucontext_t * ucontext)55 void engrave_tombstone_ucontext(int tombstone_fd, int proto_fd, uint64_t abort_msg_address,
56 siginfo_t* siginfo, ucontext_t* ucontext) {
57 pid_t uid = getuid();
58 pid_t pid = getpid();
59 pid_t target_tid = gettid();
60
61 log_t log;
62 log.current_tid = target_tid;
63 log.crashed_tid = target_tid;
64 log.tfd = tombstone_fd;
65 log.amfd_data = nullptr;
66
67 std::string thread_name = get_thread_name(target_tid);
68 std::vector<std::string> command_line = get_command_line(pid);
69
70 std::unique_ptr<unwindstack::Regs> regs(
71 unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), ucontext));
72
73 std::string selinux_label;
74 android::base::ReadFileToString("/proc/self/attr/current", &selinux_label);
75
76 std::map<pid_t, ThreadInfo> threads;
77 threads[target_tid] = ThreadInfo {
78 .registers = std::move(regs), .uid = uid, .tid = target_tid,
79 .thread_name = std::move(thread_name), .pid = pid, .command_line = std::move(command_line),
80 .selinux_label = std::move(selinux_label), .siginfo = siginfo, .signo = siginfo->si_signo,
81 // Only supported on aarch64 for now.
82 #if defined(__aarch64__)
83 .tagged_addr_ctrl = prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0),
84 .pac_enabled_keys = prctl(PR_PAC_GET_ENABLED_KEYS, 0, 0, 0, 0),
85 #endif
86 };
87 const ThreadInfo& thread = threads[pid];
88 if (!iterate_tids(pid, [&threads, &thread, &target_tid](pid_t tid) {
89 if (target_tid == tid) {
90 return;
91 }
92 threads[tid] = ThreadInfo{
93 .uid = thread.uid,
94 .tid = tid,
95 .pid = thread.pid,
96 .command_line = thread.command_line,
97 .thread_name = get_thread_name(tid),
98 .tagged_addr_ctrl = thread.tagged_addr_ctrl,
99 .pac_enabled_keys = thread.pac_enabled_keys,
100 };
101 })) {
102 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to open /proc/%d/task: %s", pid,
103 strerror(errno));
104 }
105
106 // Do not use the thread cache here because it will call pthread_key_create
107 // which doesn't work in linker code. See b/189803009.
108 // Use a normal cached object because the thread is stopped, and there
109 // is no chance of data changing between reads.
110 auto process_memory = unwindstack::Memory::CreateProcessMemoryCached(getpid());
111 unwindstack::AndroidLocalUnwinder unwinder(process_memory);
112 unwindstack::ErrorData error;
113 if (!unwinder.Initialize(error)) {
114 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to init unwinder object: %s",
115 unwindstack::GetErrorCodeString(error.code));
116 return;
117 }
118
119 ProcessInfo process_info;
120 process_info.abort_msg_address = abort_msg_address;
121 engrave_tombstone(unique_fd(dup(tombstone_fd)), unique_fd(dup(proto_fd)), &unwinder, threads,
122 target_tid, process_info, nullptr, nullptr);
123 }
124
engrave_tombstone(unique_fd output_fd,unique_fd proto_fd,unwindstack::AndroidUnwinder * unwinder,const std::map<pid_t,ThreadInfo> & threads,pid_t target_thread,const ProcessInfo & process_info,OpenFilesList * open_files,std::string * amfd_data,const Architecture * guest_arch,unwindstack::AndroidUnwinder * guest_unwinder)125 void engrave_tombstone(unique_fd output_fd, unique_fd proto_fd,
126 unwindstack::AndroidUnwinder* unwinder,
127 const std::map<pid_t, ThreadInfo>& threads, pid_t target_thread,
128 const ProcessInfo& process_info, OpenFilesList* open_files,
129 std::string* amfd_data, const Architecture* guest_arch,
130 unwindstack::AndroidUnwinder* guest_unwinder) {
131 // Don't copy log messages to tombstone unless this is a development device.
132 Tombstone tombstone;
133 engrave_tombstone_proto(&tombstone, unwinder, threads, target_thread, process_info, open_files,
134 guest_arch, guest_unwinder);
135
136 if (proto_fd != -1) {
137 if (!tombstone.SerializeToFileDescriptor(proto_fd.get())) {
138 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to write proto tombstone: %s",
139 strerror(errno));
140 }
141 }
142
143 log_t log;
144 log.current_tid = target_thread;
145 log.crashed_tid = target_thread;
146 log.tfd = output_fd.get();
147 log.amfd_data = amfd_data;
148
149 tombstone_proto_to_text(
150 tombstone,
151 [&log](const std::string& line, bool should_log) {
152 _LOG(&log, should_log ? logtype::HEADER : logtype::LOGS, "%s\n", line.c_str());
153 },
154 [](const BacktraceFrame&) {});
155 }
156