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