• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/Memory.h>
40 #include <unwindstack/Regs.h>
41 #include <unwindstack/Unwinder.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 tid = gettid();
59 
60   log_t log;
61   log.current_tid = tid;
62   log.crashed_tid = tid;
63   log.tfd = tombstone_fd;
64   log.amfd_data = nullptr;
65 
66   std::string thread_name = get_thread_name(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[tid] = ThreadInfo{
77     .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
85   };
86   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   }
103 
104   auto process_memory =
105       unwindstack::Memory::CreateProcessMemoryCached(getpid());
106   unwindstack::UnwinderFromPid unwinder(kMaxFrames, pid, unwindstack::Regs::CurrentArch(), nullptr,
107                                         process_memory);
108   if (!unwinder.Init()) {
109     async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to init unwinder object");
110     return;
111   }
112 
113   ProcessInfo process_info;
114   process_info.abort_msg_address = abort_msg_address;
115   engrave_tombstone(unique_fd(dup(tombstone_fd)), unique_fd(dup(proto_fd)), &unwinder, threads, tid,
116                     process_info, nullptr, nullptr);
117 }
118 
engrave_tombstone(unique_fd output_fd,unique_fd proto_fd,unwindstack::Unwinder * unwinder,const std::map<pid_t,ThreadInfo> & threads,pid_t target_thread,const ProcessInfo & process_info,OpenFilesList * open_files,std::string * amfd_data)119 void engrave_tombstone(unique_fd output_fd, unique_fd proto_fd, unwindstack::Unwinder* unwinder,
120                        const std::map<pid_t, ThreadInfo>& threads, pid_t target_thread,
121                        const ProcessInfo& process_info, OpenFilesList* open_files,
122                        std::string* amfd_data) {
123   // Don't copy log messages to tombstone unless this is a development device.
124   Tombstone tombstone;
125   engrave_tombstone_proto(&tombstone, unwinder, threads, target_thread, process_info, open_files);
126 
127   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     }
132   }
133 
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 
140   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   });
143 }
144