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