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
21 #include <errno.h>
22 #include <signal.h>
23 #include <stddef.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/prctl.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29
30 #include <memory>
31 #include <string>
32
33 #include <android-base/file.h>
34 #include <android-base/unique_fd.h>
35 #include <android/log.h>
36 #include <async_safe/log.h>
37 #include <log/log.h>
38 #include <private/android_filesystem_config.h>
39 #include <unwindstack/AndroidUnwinder.h>
40 #include <unwindstack/Error.h>
41 #include <unwindstack/Regs.h>
42
43 #include "libdebuggerd/backtrace.h"
44 #include "libdebuggerd/open_files_list.h"
45 #include "libdebuggerd/utility.h"
46 #include "util.h"
47
48 #include "tombstone.pb.h"
49
50 using android::base::unique_fd;
51
52 using namespace std::literals::string_literals;
53
engrave_tombstone_ucontext(int tombstone_fd,int proto_fd,uint64_t abort_msg_address,siginfo_t * siginfo,ucontext_t * ucontext)54 void engrave_tombstone_ucontext(int tombstone_fd, int proto_fd, uint64_t abort_msg_address,
55 siginfo_t* siginfo, ucontext_t* ucontext) {
56 pid_t uid = getuid();
57 pid_t pid = getpid();
58 pid_t target_tid = gettid();
59
60 log_t log;
61 log.current_tid = target_tid;
62 log.crashed_tid = target_tid;
63 log.tfd = tombstone_fd;
64 log.amfd_data = nullptr;
65
66 std::string thread_name = get_thread_name(target_tid);
67 std::vector<std::string> command_line = get_command_line(pid);
68
69 std::unique_ptr<unwindstack::Regs> regs(
70 unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), ucontext));
71
72 std::string selinux_label;
73 android::base::ReadFileToString("/proc/self/attr/current", &selinux_label);
74
75 std::map<pid_t, ThreadInfo> threads;
76 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),
79 .selinux_label = std::move(selinux_label), .siginfo = siginfo,
80 // Only supported on aarch64 for now.
81 #if defined(__aarch64__)
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
85 };
86 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 }
91 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));
103 }
104
105 // 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));
115 return;
116 }
117
118 ProcessInfo process_info;
119 process_info.abort_msg_address = abort_msg_address;
120 engrave_tombstone(unique_fd(dup(tombstone_fd)), unique_fd(dup(proto_fd)), &unwinder, threads,
121 target_tid, process_info, nullptr, nullptr);
122 }
123
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)124 void engrave_tombstone(unique_fd output_fd, unique_fd proto_fd,
125 unwindstack::AndroidUnwinder* unwinder,
126 const std::map<pid_t, ThreadInfo>& threads, pid_t target_thread,
127 const ProcessInfo& process_info, OpenFilesList* open_files,
128 std::string* amfd_data) {
129 // Don't copy log messages to tombstone unless this is a development device.
130 Tombstone tombstone;
131 engrave_tombstone_proto(&tombstone, unwinder, threads, target_thread, process_info, open_files);
132
133 if (proto_fd != -1) {
134 if (!tombstone.SerializeToFileDescriptor(proto_fd.get())) {
135 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to write proto tombstone: %s",
136 strerror(errno));
137 }
138 }
139
140 log_t log;
141 log.current_tid = target_thread;
142 log.crashed_tid = target_thread;
143 log.tfd = output_fd.get();
144 log.amfd_data = amfd_data;
145
146 tombstone_proto_to_text(tombstone, [&log](const std::string& line, bool should_log) {
147 _LOG(&log, should_log ? logtype::HEADER : logtype::LOGS, "%s\n", line.c_str());
148 });
149 }
150