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