Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016, 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 | #include <arpa/inet.h> |
| 18 | #include <dirent.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <stdlib.h> |
Josh Gao | 85bcaf6 | 2017-02-01 16:35:31 -0800 | [diff] [blame] | 21 | #include <sys/prctl.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 22 | #include <sys/ptrace.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/un.h> |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 25 | #include <sys/wait.h> |
Josh Gao | 85bcaf6 | 2017-02-01 16:35:31 -0800 | [diff] [blame] | 26 | #include <syscall.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 27 | #include <unistd.h> |
| 28 | |
| 29 | #include <limits> |
Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 30 | #include <map> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 31 | #include <memory> |
| 32 | #include <set> |
| 33 | #include <vector> |
| 34 | |
Elliott Hughes | a029d98 | 2021-06-21 12:36:47 -0700 | [diff] [blame] | 35 | #include <android-base/errno_restorer.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 36 | #include <android-base/file.h> |
| 37 | #include <android-base/logging.h> |
Josh Gao | 8d44b14 | 2018-09-18 13:22:22 -0700 | [diff] [blame] | 38 | #include <android-base/macros.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 39 | #include <android-base/parseint.h> |
| 40 | #include <android-base/properties.h> |
| 41 | #include <android-base/stringprintf.h> |
Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 42 | #include <android-base/strings.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 43 | #include <android-base/unique_fd.h> |
Mitch Phillips | e4adff0 | 2021-01-21 20:41:50 -0800 | [diff] [blame] | 44 | #include <bionic/macros.h> |
Josh Gao | a48b41b | 2019-12-13 14:11:04 -0800 | [diff] [blame] | 45 | #include <bionic/reserved_signals.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 46 | #include <cutils/sockets.h> |
Vijay Venkatraman | a95acea | 2017-01-23 20:11:51 -0800 | [diff] [blame] | 47 | #include <log/log.h> |
Josh Gao | b0e51e3 | 2017-06-01 12:08:10 -0700 | [diff] [blame] | 48 | #include <private/android_filesystem_config.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 49 | #include <procinfo/process.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 50 | |
Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 51 | #define ATRACE_TAG ATRACE_TAG_BIONIC |
| 52 | #include <utils/Trace.h> |
| 53 | |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 54 | #include <unwindstack/DexFiles.h> |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 55 | #include <unwindstack/JitDebug.h> |
| 56 | #include <unwindstack/Maps.h> |
| 57 | #include <unwindstack/Memory.h> |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 58 | #include <unwindstack/Regs.h> |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 59 | #include <unwindstack/Unwinder.h> |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 60 | |
Josh Gao | c370666 | 2017-08-29 13:08:32 -0700 | [diff] [blame] | 61 | #include "libdebuggerd/backtrace.h" |
| 62 | #include "libdebuggerd/tombstone.h" |
| 63 | #include "libdebuggerd/utility.h" |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 64 | |
| 65 | #include "debuggerd/handler.h" |
Narayan Kamath | 2d377cd | 2017-05-10 10:58:59 +0100 | [diff] [blame] | 66 | #include "tombstoned/tombstoned.h" |
Josh Gao | c370666 | 2017-08-29 13:08:32 -0700 | [diff] [blame] | 67 | |
| 68 | #include "protocol.h" |
Narayan Kamath | 2d377cd | 2017-05-10 10:58:59 +0100 | [diff] [blame] | 69 | #include "util.h" |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 70 | |
Elliott Hughes | a029d98 | 2021-06-21 12:36:47 -0700 | [diff] [blame] | 71 | using android::base::ErrnoRestorer; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 72 | using android::base::StringPrintf; |
Elliott Hughes | a029d98 | 2021-06-21 12:36:47 -0700 | [diff] [blame] | 73 | using android::base::unique_fd; |
Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 74 | |
Josh Gao | fe90276 | 2017-02-01 16:31:43 -0800 | [diff] [blame] | 75 | static bool pid_contains_tid(int pid_proc_fd, pid_t tid) { |
| 76 | struct stat st; |
| 77 | std::string task_path = StringPrintf("task/%d", tid); |
| 78 | return fstatat(pid_proc_fd, task_path.c_str(), &st, 0) == 0; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 81 | static pid_t get_tracer(pid_t tracee) { |
| 82 | // Check to see if the thread is being ptraced by another process. |
| 83 | android::procinfo::ProcessInfo process_info; |
| 84 | if (android::procinfo::GetProcessInfo(tracee, &process_info)) { |
| 85 | return process_info.tracer; |
| 86 | } |
| 87 | return -1; |
| 88 | } |
| 89 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 90 | // Attach to a thread, and verify that it's still a member of the given process |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 91 | static bool ptrace_seize_thread(int pid_proc_fd, pid_t tid, std::string* error, int flags = 0) { |
| 92 | if (ptrace(PTRACE_SEIZE, tid, 0, flags) != 0) { |
Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 93 | if (errno == EPERM) { |
Elliott Hughes | a029d98 | 2021-06-21 12:36:47 -0700 | [diff] [blame] | 94 | ErrnoRestorer errno_restorer; // In case get_tracer() fails and we fall through. |
| 95 | pid_t tracer_pid = get_tracer(tid); |
| 96 | if (tracer_pid > 0) { |
Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 97 | *error = StringPrintf("failed to attach to thread %d, already traced by %d (%s)", tid, |
Elliott Hughes | a029d98 | 2021-06-21 12:36:47 -0700 | [diff] [blame] | 98 | tracer_pid, get_process_name(tracer_pid).c_str()); |
Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 99 | return false; |
| 100 | } |
| 101 | } |
| 102 | |
Josh Gao | 42fd74b | 2017-01-20 12:51:11 -0800 | [diff] [blame] | 103 | *error = StringPrintf("failed to attach to thread %d: %s", tid, strerror(errno)); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 104 | return false; |
| 105 | } |
| 106 | |
| 107 | // Make sure that the task we attached to is actually part of the pid we're dumping. |
Josh Gao | fe90276 | 2017-02-01 16:31:43 -0800 | [diff] [blame] | 108 | if (!pid_contains_tid(pid_proc_fd, tid)) { |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 109 | if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 110 | PLOG(WARNING) << "failed to detach from thread " << tid; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 111 | } |
Josh Gao | fe90276 | 2017-02-01 16:31:43 -0800 | [diff] [blame] | 112 | *error = StringPrintf("thread %d is not in process", tid); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 113 | return false; |
| 114 | } |
Josh Gao | 122479f | 2017-01-22 16:42:32 -0800 | [diff] [blame] | 115 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 116 | return true; |
| 117 | } |
| 118 | |
| 119 | static bool wait_for_stop(pid_t tid, int* received_signal) { |
| 120 | while (true) { |
| 121 | int status; |
| 122 | pid_t result = waitpid(tid, &status, __WALL); |
| 123 | if (result != tid) { |
| 124 | PLOG(ERROR) << "waitpid failed on " << tid << " while detaching"; |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | if (WIFSTOPPED(status)) { |
| 129 | if (status >> 16 == PTRACE_EVENT_STOP) { |
| 130 | *received_signal = 0; |
| 131 | } else { |
| 132 | *received_signal = WSTOPSIG(status); |
| 133 | } |
| 134 | return true; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // Interrupt a process and wait for it to be interrupted. |
| 140 | static bool ptrace_interrupt(pid_t tid, int* received_signal) { |
| 141 | if (ptrace(PTRACE_INTERRUPT, tid, 0, 0) == 0) { |
| 142 | return wait_for_stop(tid, received_signal); |
Josh Gao | 3407d7c | 2017-06-13 17:21:12 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 145 | PLOG(ERROR) << "failed to interrupt " << tid << " to detach"; |
| 146 | return false; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Josh Gao | b0e51e3 | 2017-06-01 12:08:10 -0700 | [diff] [blame] | 149 | static bool activity_manager_notify(pid_t pid, int signal, const std::string& amfd_data) { |
Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 150 | ATRACE_CALL(); |
Josh Gao | b0e51e3 | 2017-06-01 12:08:10 -0700 | [diff] [blame] | 151 | android::base::unique_fd amfd(socket_local_client( |
| 152 | "/data/system/ndebugsocket", ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM)); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 153 | if (amfd.get() == -1) { |
| 154 | PLOG(ERROR) << "unable to connect to activity manager"; |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | struct timeval tv = { |
Peter Collingbourne | fb5eac9 | 2021-03-11 12:51:25 -0800 | [diff] [blame] | 159 | .tv_sec = 1 * android::base::HwTimeoutMultiplier(), |
Evgenii Stepanov | 2a55e1a | 2021-01-29 11:50:30 -0800 | [diff] [blame] | 160 | .tv_usec = 0, |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 161 | }; |
| 162 | if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) { |
| 163 | PLOG(ERROR) << "failed to set send timeout on activity manager socket"; |
| 164 | return false; |
| 165 | } |
Peter Collingbourne | fb5eac9 | 2021-03-11 12:51:25 -0800 | [diff] [blame] | 166 | tv.tv_sec = 3 * android::base::HwTimeoutMultiplier(); // 3 seconds on handshake read |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 167 | if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) { |
| 168 | PLOG(ERROR) << "failed to set receive timeout on activity manager socket"; |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | // Activity Manager protocol: binary 32-bit network-byte-order ints for the |
| 173 | // pid and signal number, followed by the raw text of the dump, culminating |
| 174 | // in a zero byte that marks end-of-data. |
| 175 | uint32_t datum = htonl(pid); |
| 176 | if (!android::base::WriteFully(amfd, &datum, 4)) { |
| 177 | PLOG(ERROR) << "AM pid write failed"; |
| 178 | return false; |
| 179 | } |
| 180 | datum = htonl(signal); |
| 181 | if (!android::base::WriteFully(amfd, &datum, 4)) { |
| 182 | PLOG(ERROR) << "AM signal write failed"; |
| 183 | return false; |
| 184 | } |
| 185 | if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size() + 1)) { |
| 186 | PLOG(ERROR) << "AM data write failed"; |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | // 3 sec timeout reading the ack; we're fine if the read fails. |
| 191 | char ack; |
| 192 | android::base::ReadFully(amfd, &ack, 1); |
| 193 | return true; |
| 194 | } |
| 195 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 196 | // Globals used by the abort handler. |
| 197 | static pid_t g_target_thread = -1; |
| 198 | static bool g_tombstoned_connected = false; |
| 199 | static unique_fd g_tombstoned_socket; |
| 200 | static unique_fd g_output_fd; |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 201 | static unique_fd g_proto_fd; |
Josh Gao | 5759411 | 2017-01-22 17:41:15 -0800 | [diff] [blame] | 202 | |
Josh Gao | 38ac45d | 2018-04-27 13:31:47 -0700 | [diff] [blame] | 203 | static void DefuseSignalHandlers() { |
| 204 | // Don't try to dump ourselves. |
| 205 | struct sigaction action = {}; |
| 206 | action.sa_handler = SIG_DFL; |
| 207 | debuggerd_register_handlers(&action); |
| 208 | |
| 209 | sigset_t mask; |
| 210 | sigemptyset(&mask); |
| 211 | if (sigprocmask(SIG_SETMASK, &mask, nullptr) != 0) { |
| 212 | PLOG(FATAL) << "failed to set signal mask"; |
| 213 | } |
| 214 | } |
| 215 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 216 | static void Initialize(char** argv) { |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 217 | android::base::InitLogging(argv); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 218 | android::base::SetAborter([](const char* abort_msg) { |
| 219 | // If we abort before we get an output fd, contact tombstoned to let any |
| 220 | // potential listeners know that we failed. |
| 221 | if (!g_tombstoned_connected) { |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 222 | if (!tombstoned_connect(g_target_thread, &g_tombstoned_socket, &g_output_fd, &g_proto_fd, |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 223 | kDebuggerdAnyIntercept)) { |
| 224 | // We failed to connect, not much we can do. |
| 225 | LOG(ERROR) << "failed to connected to tombstoned to report failure"; |
| 226 | _exit(1); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | dprintf(g_output_fd.get(), "crash_dump failed to dump process"); |
| 231 | if (g_target_thread != 1) { |
| 232 | dprintf(g_output_fd.get(), " %d: %s\n", g_target_thread, abort_msg); |
| 233 | } else { |
| 234 | dprintf(g_output_fd.get(), ": %s\n", abort_msg); |
| 235 | } |
| 236 | |
| 237 | _exit(1); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 238 | }); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 239 | } |
Josh Gao | e740250 | 2017-06-01 11:55:25 -0700 | [diff] [blame] | 240 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 241 | static void ParseArgs(int argc, char** argv, pid_t* pseudothread_tid, DebuggerdDumpType* dump_type) { |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 242 | if (argc != 4) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 243 | LOG(FATAL) << "wrong number of args: " << argc << " (expected 4)"; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 246 | if (!android::base::ParseInt(argv[1], &g_target_thread, 1, std::numeric_limits<pid_t>::max())) { |
| 247 | LOG(FATAL) << "invalid target tid: " << argv[1]; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 250 | if (!android::base::ParseInt(argv[2], pseudothread_tid, 1, std::numeric_limits<pid_t>::max())) { |
Josh Gao | f6ad585 | 2017-02-15 12:21:11 -0800 | [diff] [blame] | 251 | LOG(FATAL) << "invalid pseudothread tid: " << argv[2]; |
Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 254 | int dump_type_int; |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 255 | if (!android::base::ParseInt(argv[3], &dump_type_int, 0)) { |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 256 | LOG(FATAL) << "invalid requested dump type: " << argv[3]; |
| 257 | } |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 258 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 259 | *dump_type = static_cast<DebuggerdDumpType>(dump_type_int); |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 260 | switch (*dump_type) { |
| 261 | case kDebuggerdNativeBacktrace: |
| 262 | case kDebuggerdTombstone: |
| 263 | case kDebuggerdTombstoneProto: |
| 264 | break; |
| 265 | |
| 266 | default: |
| 267 | LOG(FATAL) << "invalid requested dump type: " << dump_type_int; |
| 268 | } |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 269 | } |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 270 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 271 | static void ReadCrashInfo(unique_fd& fd, siginfo_t* siginfo, |
Peter Collingbourne | 843f7e6 | 2020-02-28 19:07:33 -0800 | [diff] [blame] | 272 | std::unique_ptr<unwindstack::Regs>* regs, ProcessInfo* process_info) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 273 | std::aligned_storage<sizeof(CrashInfo) + 1, alignof(CrashInfo)>::type buf; |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 274 | CrashInfo* crash_info = reinterpret_cast<CrashInfo*>(&buf); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 275 | ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), &buf, sizeof(buf))); |
| 276 | if (rc == -1) { |
| 277 | PLOG(FATAL) << "failed to read target ucontext"; |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 278 | } else { |
| 279 | ssize_t expected_size = 0; |
| 280 | switch (crash_info->header.version) { |
| 281 | case 1: |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 282 | case 2: |
Peter Collingbourne | f3d542f | 2020-03-05 16:46:15 -0800 | [diff] [blame] | 283 | case 3: |
| 284 | expected_size = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataStatic); |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 285 | break; |
| 286 | |
Peter Collingbourne | f3d542f | 2020-03-05 16:46:15 -0800 | [diff] [blame] | 287 | case 4: |
| 288 | expected_size = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataDynamic); |
Mitch Phillips | e0b4bb1 | 2020-02-14 14:54:31 -0800 | [diff] [blame] | 289 | break; |
| 290 | |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 291 | default: |
| 292 | LOG(FATAL) << "unexpected CrashInfo version: " << crash_info->header.version; |
| 293 | break; |
| 294 | }; |
| 295 | |
Peter Collingbourne | f3d542f | 2020-03-05 16:46:15 -0800 | [diff] [blame] | 296 | if (rc < expected_size) { |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 297 | LOG(FATAL) << "read " << rc << " bytes when reading target crash information, expected " |
| 298 | << expected_size; |
| 299 | } |
Josh Gao | c7fe060 | 2017-03-13 14:13:29 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 302 | switch (crash_info->header.version) { |
Peter Collingbourne | f3d542f | 2020-03-05 16:46:15 -0800 | [diff] [blame] | 303 | case 4: |
| 304 | process_info->fdsan_table_address = crash_info->data.d.fdsan_table_address; |
| 305 | process_info->gwp_asan_state = crash_info->data.d.gwp_asan_state; |
| 306 | process_info->gwp_asan_metadata = crash_info->data.d.gwp_asan_metadata; |
Peter Collingbourne | f862252 | 2020-04-07 14:07:32 -0700 | [diff] [blame] | 307 | process_info->scudo_stack_depot = crash_info->data.d.scudo_stack_depot; |
| 308 | process_info->scudo_region_info = crash_info->data.d.scudo_region_info; |
Peter Collingbourne | bb4b49c | 2021-01-06 21:02:19 -0800 | [diff] [blame] | 309 | process_info->scudo_ring_buffer = crash_info->data.d.scudo_ring_buffer; |
Josh Gao | 8d44b14 | 2018-09-18 13:22:22 -0700 | [diff] [blame] | 310 | FALLTHROUGH_INTENDED; |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 311 | case 1: |
Peter Collingbourne | f3d542f | 2020-03-05 16:46:15 -0800 | [diff] [blame] | 312 | case 2: |
| 313 | case 3: |
| 314 | process_info->abort_msg_address = crash_info->data.s.abort_msg_address; |
| 315 | *siginfo = crash_info->data.s.siginfo; |
Peter Collingbourne | f03af88 | 2020-03-20 18:09:00 -0700 | [diff] [blame] | 316 | if (signal_has_si_addr(siginfo)) { |
Peter Collingbourne | f03af88 | 2020-03-20 18:09:00 -0700 | [diff] [blame] | 317 | process_info->has_fault_address = true; |
Mitch Phillips | e4adff0 | 2021-01-21 20:41:50 -0800 | [diff] [blame] | 318 | process_info->maybe_tagged_fault_address = reinterpret_cast<uintptr_t>(siginfo->si_addr); |
| 319 | process_info->untagged_fault_address = |
| 320 | untag_address(reinterpret_cast<uintptr_t>(siginfo->si_addr)); |
Peter Collingbourne | f03af88 | 2020-03-20 18:09:00 -0700 | [diff] [blame] | 321 | } |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 322 | regs->reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), |
Peter Collingbourne | f3d542f | 2020-03-05 16:46:15 -0800 | [diff] [blame] | 323 | &crash_info->data.s.ucontext)); |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 324 | break; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 325 | |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 326 | default: |
| 327 | __builtin_unreachable(); |
| 328 | } |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | // Wait for a process to clone and return the child's pid. |
| 332 | // Note: this leaves the parent in PTRACE_EVENT_STOP. |
| 333 | static pid_t wait_for_clone(pid_t pid, bool resume_child) { |
| 334 | int status; |
| 335 | pid_t result = TEMP_FAILURE_RETRY(waitpid(pid, &status, __WALL)); |
| 336 | if (result == -1) { |
| 337 | PLOG(FATAL) << "failed to waitpid"; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 340 | if (WIFEXITED(status)) { |
| 341 | LOG(FATAL) << "traced process exited with status " << WEXITSTATUS(status); |
| 342 | } else if (WIFSIGNALED(status)) { |
| 343 | LOG(FATAL) << "traced process exited with signal " << WTERMSIG(status); |
| 344 | } else if (!WIFSTOPPED(status)) { |
| 345 | LOG(FATAL) << "process didn't stop? (status = " << status << ")"; |
| 346 | } |
| 347 | |
| 348 | if (status >> 8 != (SIGTRAP | (PTRACE_EVENT_CLONE << 8))) { |
| 349 | LOG(FATAL) << "process didn't stop due to PTRACE_O_TRACECLONE (status = " << status << ")"; |
| 350 | } |
| 351 | |
| 352 | pid_t child; |
| 353 | if (ptrace(PTRACE_GETEVENTMSG, pid, 0, &child) != 0) { |
| 354 | PLOG(FATAL) << "failed to get child pid via PTRACE_GETEVENTMSG"; |
| 355 | } |
| 356 | |
| 357 | int stop_signal; |
| 358 | if (!wait_for_stop(child, &stop_signal)) { |
| 359 | PLOG(FATAL) << "failed to waitpid on child"; |
| 360 | } |
| 361 | |
| 362 | CHECK_EQ(0, stop_signal); |
| 363 | |
| 364 | if (resume_child) { |
| 365 | if (ptrace(PTRACE_CONT, child, 0, 0) != 0) { |
| 366 | PLOG(FATAL) << "failed to resume child (pid = " << child << ")"; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | return child; |
| 371 | } |
| 372 | |
| 373 | static pid_t wait_for_vm_process(pid_t pseudothread_tid) { |
| 374 | // The pseudothread will double-fork, we want its grandchild. |
| 375 | pid_t intermediate = wait_for_clone(pseudothread_tid, true); |
| 376 | pid_t vm_pid = wait_for_clone(intermediate, false); |
| 377 | if (ptrace(PTRACE_DETACH, intermediate, 0, 0) != 0) { |
| 378 | PLOG(FATAL) << "failed to detach from intermediate vm process"; |
| 379 | } |
| 380 | |
| 381 | return vm_pid; |
| 382 | } |
| 383 | |
Jinguang Dong | 8ac2f27 | 2018-11-24 17:12:33 +0800 | [diff] [blame] | 384 | static void InstallSigPipeHandler() { |
| 385 | struct sigaction action = {}; |
| 386 | action.sa_handler = SIG_IGN; |
| 387 | action.sa_flags = SA_RESTART; |
| 388 | sigaction(SIGPIPE, &action, nullptr); |
| 389 | } |
| 390 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 391 | int main(int argc, char** argv) { |
Josh Gao | 38ac45d | 2018-04-27 13:31:47 -0700 | [diff] [blame] | 392 | DefuseSignalHandlers(); |
Jinguang Dong | 8ac2f27 | 2018-11-24 17:12:33 +0800 | [diff] [blame] | 393 | InstallSigPipeHandler(); |
Josh Gao | 38ac45d | 2018-04-27 13:31:47 -0700 | [diff] [blame] | 394 | |
Josh Gao | 18cb681 | 2019-04-16 13:17:08 -0700 | [diff] [blame] | 395 | // There appears to be a bug in the kernel where our death causes SIGHUP to |
| 396 | // be sent to our process group if we exit while it has stopped jobs (e.g. |
Elliott Hughes | e4781d5 | 2021-03-17 09:15:15 -0700 | [diff] [blame] | 397 | // because of wait_for_debugger). Use setsid to create a new process group to |
Josh Gao | 18cb681 | 2019-04-16 13:17:08 -0700 | [diff] [blame] | 398 | // avoid hitting this. |
| 399 | setsid(); |
| 400 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 401 | atrace_begin(ATRACE_TAG, "before reparent"); |
| 402 | pid_t target_process = getppid(); |
| 403 | |
| 404 | // Open /proc/`getppid()` before we daemonize. |
| 405 | std::string target_proc_path = "/proc/" + std::to_string(target_process); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 406 | int target_proc_fd = open(target_proc_path.c_str(), O_DIRECTORY | O_RDONLY); |
| 407 | if (target_proc_fd == -1) { |
| 408 | PLOG(FATAL) << "failed to open " << target_proc_path; |
| 409 | } |
| 410 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 411 | // Make sure getppid() hasn't changed. |
| 412 | if (getppid() != target_process) { |
| 413 | LOG(FATAL) << "parent died"; |
Josh Gao | 2a18b82 | 2017-02-16 19:17:28 -0800 | [diff] [blame] | 414 | } |
Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 415 | atrace_end(ATRACE_TAG); |
| 416 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 417 | // Reparent ourselves to init, so that the signal handler can waitpid on the |
| 418 | // original process to avoid leaving a zombie for non-fatal dumps. |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 419 | // Move the input/output pipes off of stdout/stderr, out of paranoia. |
| 420 | unique_fd output_pipe(dup(STDOUT_FILENO)); |
| 421 | unique_fd input_pipe(dup(STDIN_FILENO)); |
| 422 | |
| 423 | unique_fd fork_exit_read, fork_exit_write; |
| 424 | if (!Pipe(&fork_exit_read, &fork_exit_write)) { |
| 425 | PLOG(FATAL) << "failed to create pipe"; |
| 426 | } |
| 427 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 428 | pid_t forkpid = fork(); |
| 429 | if (forkpid == -1) { |
| 430 | PLOG(FATAL) << "fork failed"; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 431 | } else if (forkpid == 0) { |
| 432 | fork_exit_read.reset(); |
| 433 | } else { |
| 434 | // We need the pseudothread to live until we get around to verifying the vm pid against it. |
| 435 | // The last thing it does is block on a waitpid on us, so wait until our child tells us to die. |
| 436 | fork_exit_write.reset(); |
| 437 | char buf; |
| 438 | TEMP_FAILURE_RETRY(read(fork_exit_read.get(), &buf, sizeof(buf))); |
| 439 | _exit(0); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 442 | ATRACE_NAME("after reparent"); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 443 | pid_t pseudothread_tid; |
| 444 | DebuggerdDumpType dump_type; |
Peter Collingbourne | 843f7e6 | 2020-02-28 19:07:33 -0800 | [diff] [blame] | 445 | ProcessInfo process_info; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 446 | |
| 447 | Initialize(argv); |
| 448 | ParseArgs(argc, argv, &pseudothread_tid, &dump_type); |
Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 449 | |
Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 450 | // Die if we take too long. |
Andreas Gampe | b02851a | 2017-06-22 19:45:53 -0700 | [diff] [blame] | 451 | // |
| 452 | // Note: processes with many threads and minidebug-info can take a bit to |
| 453 | // unwind, do not make this too small. b/62828735 |
Peter Collingbourne | fb5eac9 | 2021-03-11 12:51:25 -0800 | [diff] [blame] | 454 | alarm(30 * android::base::HwTimeoutMultiplier()); |
Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 455 | |
Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 456 | // Collect the list of open files. |
| 457 | OpenFilesList open_files; |
Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 458 | { |
| 459 | ATRACE_NAME("open files"); |
Josh Gao | ce841d9 | 2018-08-06 18:26:42 -0700 | [diff] [blame] | 460 | populate_open_files_list(&open_files, g_target_thread); |
Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 461 | } |
Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 462 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 463 | // In order to reduce the duration that we pause the process for, we ptrace |
| 464 | // the threads, fetch their registers and associated information, and then |
| 465 | // fork a separate process as a snapshot of the process's address space. |
| 466 | std::set<pid_t> threads; |
| 467 | if (!android::procinfo::GetProcessTids(g_target_thread, &threads)) { |
| 468 | PLOG(FATAL) << "failed to get process threads"; |
| 469 | } |
Josh Gao | 3407d7c | 2017-06-13 17:21:12 +0000 | [diff] [blame] | 470 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 471 | std::map<pid_t, ThreadInfo> thread_info; |
| 472 | siginfo_t siginfo; |
| 473 | std::string error; |
| 474 | |
| 475 | { |
| 476 | ATRACE_NAME("ptrace"); |
| 477 | for (pid_t thread : threads) { |
| 478 | // Trace the pseudothread separately, so we can use different options. |
| 479 | if (thread == pseudothread_tid) { |
| 480 | continue; |
| 481 | } |
| 482 | |
| 483 | if (!ptrace_seize_thread(target_proc_fd, thread, &error)) { |
| 484 | bool fatal = thread == g_target_thread; |
| 485 | LOG(fatal ? FATAL : WARNING) << error; |
| 486 | } |
| 487 | |
| 488 | ThreadInfo info; |
| 489 | info.pid = target_process; |
| 490 | info.tid = thread; |
Josh Gao | 5df504c | 2019-05-09 12:48:17 -0700 | [diff] [blame] | 491 | info.uid = getuid(); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 492 | info.thread_name = get_thread_name(thread); |
| 493 | |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 494 | unique_fd attr_fd(openat(target_proc_fd, "attr/current", O_RDONLY | O_CLOEXEC)); |
| 495 | if (!android::base::ReadFdToString(attr_fd, &info.selinux_label)) { |
| 496 | PLOG(WARNING) << "failed to read selinux label"; |
| 497 | } |
| 498 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 499 | if (!ptrace_interrupt(thread, &info.signo)) { |
| 500 | PLOG(WARNING) << "failed to ptrace interrupt thread " << thread; |
| 501 | ptrace(PTRACE_DETACH, thread, 0, 0); |
| 502 | continue; |
| 503 | } |
| 504 | |
Elliott Hughes | d13ea52 | 2022-01-13 09:20:26 -0800 | [diff] [blame^] | 505 | struct iovec tagged_addr_iov = { |
Peter Collingbourne | 864f15d | 2020-09-14 20:27:36 -0700 | [diff] [blame] | 506 | &info.tagged_addr_ctrl, |
| 507 | sizeof(info.tagged_addr_ctrl), |
| 508 | }; |
| 509 | if (ptrace(PTRACE_GETREGSET, thread, NT_ARM_TAGGED_ADDR_CTRL, |
Elliott Hughes | d13ea52 | 2022-01-13 09:20:26 -0800 | [diff] [blame^] | 510 | reinterpret_cast<void*>(&tagged_addr_iov)) == -1) { |
Peter Collingbourne | 864f15d | 2020-09-14 20:27:36 -0700 | [diff] [blame] | 511 | info.tagged_addr_ctrl = -1; |
| 512 | } |
Peter Collingbourne | 864f15d | 2020-09-14 20:27:36 -0700 | [diff] [blame] | 513 | |
Elliott Hughes | d13ea52 | 2022-01-13 09:20:26 -0800 | [diff] [blame^] | 514 | struct iovec pac_enabled_keys_iov = { |
| 515 | &info.pac_enabled_keys, |
| 516 | sizeof(info.pac_enabled_keys), |
| 517 | }; |
| 518 | if (ptrace(PTRACE_GETREGSET, thread, NT_ARM_PAC_ENABLED_KEYS, |
| 519 | reinterpret_cast<void*>(&pac_enabled_keys_iov)) == -1) { |
| 520 | info.pac_enabled_keys = -1; |
| 521 | } |
| 522 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 523 | if (thread == g_target_thread) { |
| 524 | // Read the thread's registers along with the rest of the crash info out of the pipe. |
Peter Collingbourne | 843f7e6 | 2020-02-28 19:07:33 -0800 | [diff] [blame] | 525 | ReadCrashInfo(input_pipe, &siginfo, &info.registers, &process_info); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 526 | info.siginfo = &siginfo; |
| 527 | info.signo = info.siginfo->si_signo; |
Josh Gao | 31348a7 | 2021-03-29 21:53:42 -0700 | [diff] [blame] | 528 | |
| 529 | info.command_line = get_command_line(g_target_thread); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 530 | } else { |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 531 | info.registers.reset(unwindstack::Regs::RemoteGet(thread)); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 532 | if (!info.registers) { |
| 533 | PLOG(WARNING) << "failed to fetch registers for thread " << thread; |
| 534 | ptrace(PTRACE_DETACH, thread, 0, 0); |
| 535 | continue; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | thread_info[thread] = std::move(info); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | // Trace the pseudothread with PTRACE_O_TRACECLONE and tell it to fork. |
| 544 | if (!ptrace_seize_thread(target_proc_fd, pseudothread_tid, &error, PTRACE_O_TRACECLONE)) { |
| 545 | LOG(FATAL) << "failed to seize pseudothread: " << error; |
| 546 | } |
| 547 | |
| 548 | if (TEMP_FAILURE_RETRY(write(output_pipe.get(), "\1", 1)) != 1) { |
| 549 | PLOG(FATAL) << "failed to write to pseudothread"; |
| 550 | } |
| 551 | |
| 552 | pid_t vm_pid = wait_for_vm_process(pseudothread_tid); |
| 553 | if (ptrace(PTRACE_DETACH, pseudothread_tid, 0, 0) != 0) { |
| 554 | PLOG(FATAL) << "failed to detach from pseudothread"; |
| 555 | } |
| 556 | |
| 557 | // The pseudothread can die now. |
| 558 | fork_exit_write.reset(); |
| 559 | |
| 560 | // Defer the message until later, for readability. |
Elliott Hughes | e4781d5 | 2021-03-17 09:15:15 -0700 | [diff] [blame] | 561 | bool wait_for_debugger = android::base::GetBoolProperty( |
| 562 | "debug.debuggerd.wait_for_debugger", |
| 563 | android::base::GetBoolProperty("debug.debuggerd.wait_for_gdb", false)); |
Josh Gao | a48b41b | 2019-12-13 14:11:04 -0800 | [diff] [blame] | 564 | if (siginfo.si_signo == BIONIC_SIGNAL_DEBUGGER) { |
Elliott Hughes | e4781d5 | 2021-03-17 09:15:15 -0700 | [diff] [blame] | 565 | wait_for_debugger = false; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | // Detach from all of our attached threads before resuming. |
| 569 | for (const auto& [tid, thread] : thread_info) { |
Josh Gao | a48b41b | 2019-12-13 14:11:04 -0800 | [diff] [blame] | 570 | int resume_signal = thread.signo == BIONIC_SIGNAL_DEBUGGER ? 0 : thread.signo; |
Elliott Hughes | e4781d5 | 2021-03-17 09:15:15 -0700 | [diff] [blame] | 571 | if (wait_for_debugger) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 572 | resume_signal = 0; |
| 573 | if (tgkill(target_process, tid, SIGSTOP) != 0) { |
| 574 | PLOG(WARNING) << "failed to send SIGSTOP to " << tid; |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | LOG(DEBUG) << "detaching from thread " << tid; |
| 579 | if (ptrace(PTRACE_DETACH, tid, 0, resume_signal) != 0) { |
| 580 | PLOG(ERROR) << "failed to detach from thread " << tid; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | // Drop our capabilities now that we've fetched all of the information we need. |
Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 585 | drop_capabilities(); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 586 | |
Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 587 | { |
| 588 | ATRACE_NAME("tombstoned_connect"); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 589 | LOG(INFO) << "obtaining output fd from tombstoned, type: " << dump_type; |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 590 | g_tombstoned_connected = tombstoned_connect(g_target_thread, &g_tombstoned_socket, &g_output_fd, |
| 591 | &g_proto_fd, dump_type); |
Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 592 | } |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 593 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 594 | if (g_tombstoned_connected) { |
| 595 | if (TEMP_FAILURE_RETRY(dup2(g_output_fd.get(), STDOUT_FILENO)) == -1) { |
| 596 | PLOG(ERROR) << "failed to dup2 output fd (" << g_output_fd.get() << ") to STDOUT_FILENO"; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 597 | } |
| 598 | } else { |
| 599 | unique_fd devnull(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR))); |
| 600 | TEMP_FAILURE_RETRY(dup2(devnull.get(), STDOUT_FILENO)); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 601 | g_output_fd = std::move(devnull); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 604 | LOG(INFO) << "performing dump of process " << target_process |
| 605 | << " (target tid = " << g_target_thread << ")"; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 606 | |
| 607 | int signo = siginfo.si_signo; |
Josh Gao | a48b41b | 2019-12-13 14:11:04 -0800 | [diff] [blame] | 608 | bool fatal_signal = signo != BIONIC_SIGNAL_DEBUGGER; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 609 | bool backtrace = false; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 610 | |
Josh Gao | a48b41b | 2019-12-13 14:11:04 -0800 | [diff] [blame] | 611 | // si_value is special when used with BIONIC_SIGNAL_DEBUGGER. |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 612 | // 0: dump tombstone |
| 613 | // 1: dump backtrace |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 614 | if (!fatal_signal) { |
| 615 | int si_val = siginfo.si_value.sival_int; |
| 616 | if (si_val == 0) { |
| 617 | backtrace = false; |
| 618 | } else if (si_val == 1) { |
| 619 | backtrace = true; |
| 620 | } else { |
| 621 | LOG(WARNING) << "unknown si_value value " << si_val; |
| 622 | } |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 623 | } |
| 624 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 625 | // TODO: Use seccomp to lock ourselves down. |
Christopher Ferris | b05c472 | 2020-09-23 15:51:46 -0700 | [diff] [blame] | 626 | unwindstack::UnwinderFromPid unwinder(256, vm_pid, unwindstack::Regs::CurrentArch()); |
| 627 | if (!unwinder.Init()) { |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 628 | LOG(FATAL) << "Failed to init unwinder object."; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 629 | } |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 630 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 631 | std::string amfd_data; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 632 | if (backtrace) { |
Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 633 | ATRACE_NAME("dump_backtrace"); |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 634 | dump_backtrace(std::move(g_output_fd), &unwinder, thread_info, g_target_thread); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 635 | } else { |
Josh Gao | ce841d9 | 2018-08-06 18:26:42 -0700 | [diff] [blame] | 636 | { |
| 637 | ATRACE_NAME("fdsan table dump"); |
Peter Collingbourne | 843f7e6 | 2020-02-28 19:07:33 -0800 | [diff] [blame] | 638 | populate_fdsan_table(&open_files, unwinder.GetProcessMemory(), |
| 639 | process_info.fdsan_table_address); |
Josh Gao | ce841d9 | 2018-08-06 18:26:42 -0700 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | { |
| 643 | ATRACE_NAME("engrave_tombstone"); |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 644 | engrave_tombstone(std::move(g_output_fd), std::move(g_proto_fd), &unwinder, thread_info, |
| 645 | g_target_thread, process_info, &open_files, &amfd_data); |
Josh Gao | ce841d9 | 2018-08-06 18:26:42 -0700 | [diff] [blame] | 646 | } |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 647 | } |
| 648 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 649 | if (fatal_signal) { |
| 650 | // Don't try to notify ActivityManager if it just crashed, or we might hang until timeout. |
| 651 | if (thread_info[target_process].thread_name != "system_server") { |
| 652 | activity_manager_notify(target_process, signo, amfd_data); |
Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 653 | } |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Elliott Hughes | e4781d5 | 2021-03-17 09:15:15 -0700 | [diff] [blame] | 656 | if (wait_for_debugger) { |
Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 657 | // Use ALOGI to line up with output from engrave_tombstone. |
| 658 | ALOGI( |
dimitry | 6429e20 | 2017-09-12 10:46:20 +0200 | [diff] [blame] | 659 | "***********************************************************\n" |
| 660 | "* Process %d has been suspended while crashing.\n" |
Elliott Hughes | e4781d5 | 2021-03-17 09:15:15 -0700 | [diff] [blame] | 661 | "* To attach the debugger, run this on the host:\n" |
dimitry | 6429e20 | 2017-09-12 10:46:20 +0200 | [diff] [blame] | 662 | "*\n" |
| 663 | "* gdbclient.py -p %d\n" |
| 664 | "*\n" |
| 665 | "***********************************************************", |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 666 | target_process, target_process); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | // Close stdout before we notify tombstoned of completion. |
| 670 | close(STDOUT_FILENO); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 671 | if (g_tombstoned_connected && !tombstoned_notify_completion(g_tombstoned_socket.get())) { |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 672 | LOG(ERROR) << "failed to notify tombstoned of completion"; |
| 673 | } |
| 674 | |
| 675 | return 0; |
| 676 | } |