| 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/capability.h> | 
|  | 22 | #include <sys/prctl.h> | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 23 | #include <sys/ptrace.h> | 
|  | 24 | #include <sys/types.h> | 
|  | 25 | #include <sys/un.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 | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 50 | #include "backtrace.h" | 
|  | 51 | #include "tombstone.h" | 
|  | 52 | #include "utility.h" | 
|  | 53 |  | 
|  | 54 | #include "debuggerd/handler.h" | 
| Narayan Kamath | 2d377cd | 2017-05-10 10:58:59 +0100 | [diff] [blame] | 55 | #include "protocol.h" | 
|  | 56 | #include "tombstoned/tombstoned.h" | 
|  | 57 | #include "util.h" | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 58 |  | 
|  | 59 | using android::base::unique_fd; | 
| Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 60 | using android::base::ReadFileToString; | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 61 | using android::base::StringPrintf; | 
| Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 62 | using android::base::Trim; | 
|  | 63 |  | 
|  | 64 | static std::string get_process_name(pid_t pid) { | 
|  | 65 | std::string result = "<unknown>"; | 
|  | 66 | ReadFileToString(StringPrintf("/proc/%d/cmdline", pid), &result); | 
|  | 67 | return result; | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | static std::string get_thread_name(pid_t tid) { | 
|  | 71 | std::string result = "<unknown>"; | 
|  | 72 | ReadFileToString(StringPrintf("/proc/%d/comm", tid), &result); | 
|  | 73 | return Trim(result); | 
|  | 74 | } | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 75 |  | 
| Josh Gao | fe90276 | 2017-02-01 16:31:43 -0800 | [diff] [blame] | 76 | static bool pid_contains_tid(int pid_proc_fd, pid_t tid) { | 
|  | 77 | struct stat st; | 
|  | 78 | std::string task_path = StringPrintf("task/%d", tid); | 
|  | 79 | return fstatat(pid_proc_fd, task_path.c_str(), &st, 0) == 0; | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 80 | } | 
|  | 81 |  | 
| Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 82 | static pid_t get_tracer(pid_t tracee) { | 
|  | 83 | // Check to see if the thread is being ptraced by another process. | 
|  | 84 | android::procinfo::ProcessInfo process_info; | 
|  | 85 | if (android::procinfo::GetProcessInfo(tracee, &process_info)) { | 
|  | 86 | return process_info.tracer; | 
|  | 87 | } | 
|  | 88 | return -1; | 
|  | 89 | } | 
|  | 90 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 91 | // Attach to a thread, and verify that it's still a member of the given process | 
| Josh Gao | fe90276 | 2017-02-01 16:31:43 -0800 | [diff] [blame] | 92 | static bool ptrace_seize_thread(int pid_proc_fd, pid_t tid, std::string* error) { | 
| Josh Gao | 122479f | 2017-01-22 16:42:32 -0800 | [diff] [blame] | 93 | if (ptrace(PTRACE_SEIZE, tid, 0, 0) != 0) { | 
| Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 94 | if (errno == EPERM) { | 
|  | 95 | pid_t tracer = get_tracer(tid); | 
|  | 96 | if (tracer != -1) { | 
|  | 97 | *error = StringPrintf("failed to attach to thread %d, already traced by %d (%s)", tid, | 
|  | 98 | tracer, get_process_name(tracer).c_str()); | 
|  | 99 | return false; | 
|  | 100 | } | 
|  | 101 | } | 
|  | 102 |  | 
| Josh Gao | 42fd74b | 2017-01-20 12:51:11 -0800 | [diff] [blame] | 103 | *error = StringPrintf("failed to attach to thread %d: %s", tid, strerror(errno)); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 104 | return false; | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | // Make sure that the task we attached to is actually part of the pid we're dumping. | 
| Josh Gao | fe90276 | 2017-02-01 16:31:43 -0800 | [diff] [blame] | 108 | if (!pid_contains_tid(pid_proc_fd, tid)) { | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 109 | if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) { | 
|  | 110 | PLOG(FATAL) << "failed to detach from thread " << tid; | 
|  | 111 | } | 
| Josh Gao | fe90276 | 2017-02-01 16:31:43 -0800 | [diff] [blame] | 112 | *error = StringPrintf("thread %d is not in process", tid); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 113 | return false; | 
|  | 114 | } | 
| Josh Gao | 122479f | 2017-01-22 16:42:32 -0800 | [diff] [blame] | 115 |  | 
| Josh Gao | 3407d7c | 2017-06-13 17:21:12 +0000 | [diff] [blame] | 116 | // Put the task into ptrace-stop state. | 
|  | 117 | if (ptrace(PTRACE_INTERRUPT, tid, 0, 0) != 0) { | 
|  | 118 | PLOG(FATAL) << "failed to interrupt thread " << tid; | 
|  | 119 | } | 
|  | 120 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 121 | return true; | 
|  | 122 | } | 
|  | 123 |  | 
| Josh Gao | b0e51e3 | 2017-06-01 12:08:10 -0700 | [diff] [blame] | 124 | 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] | 125 | ATRACE_CALL(); | 
| Josh Gao | b0e51e3 | 2017-06-01 12:08:10 -0700 | [diff] [blame] | 126 | android::base::unique_fd amfd(socket_local_client( | 
|  | 127 | "/data/system/ndebugsocket", ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM)); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 128 | if (amfd.get() == -1) { | 
|  | 129 | PLOG(ERROR) << "unable to connect to activity manager"; | 
|  | 130 | return false; | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | struct timeval tv = { | 
|  | 134 | .tv_sec = 1, | 
|  | 135 | .tv_usec = 0, | 
|  | 136 | }; | 
|  | 137 | if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) { | 
|  | 138 | PLOG(ERROR) << "failed to set send timeout on activity manager socket"; | 
|  | 139 | return false; | 
|  | 140 | } | 
|  | 141 | tv.tv_sec = 3;  // 3 seconds on handshake read | 
|  | 142 | if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) { | 
|  | 143 | PLOG(ERROR) << "failed to set receive timeout on activity manager socket"; | 
|  | 144 | return false; | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | // Activity Manager protocol: binary 32-bit network-byte-order ints for the | 
|  | 148 | // pid and signal number, followed by the raw text of the dump, culminating | 
|  | 149 | // in a zero byte that marks end-of-data. | 
|  | 150 | uint32_t datum = htonl(pid); | 
|  | 151 | if (!android::base::WriteFully(amfd, &datum, 4)) { | 
|  | 152 | PLOG(ERROR) << "AM pid write failed"; | 
|  | 153 | return false; | 
|  | 154 | } | 
|  | 155 | datum = htonl(signal); | 
|  | 156 | if (!android::base::WriteFully(amfd, &datum, 4)) { | 
|  | 157 | PLOG(ERROR) << "AM signal write failed"; | 
|  | 158 | return false; | 
|  | 159 | } | 
|  | 160 | if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size() + 1)) { | 
|  | 161 | PLOG(ERROR) << "AM data write failed"; | 
|  | 162 | return false; | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | // 3 sec timeout reading the ack; we're fine if the read fails. | 
|  | 166 | char ack; | 
|  | 167 | android::base::ReadFully(amfd, &ack, 1); | 
|  | 168 | return true; | 
|  | 169 | } | 
|  | 170 |  | 
| Josh Gao | 5759411 | 2017-01-22 17:41:15 -0800 | [diff] [blame] | 171 | static void signal_handler(int) { | 
|  | 172 | // We can't log easily, because the heap might be corrupt. | 
|  | 173 | // Just die and let the surrounding log context explain things. | 
|  | 174 | _exit(1); | 
|  | 175 | } | 
|  | 176 |  | 
| Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 177 | static void abort_handler(pid_t target, const bool tombstoned_connected, | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 178 | unique_fd& tombstoned_socket, unique_fd& output_fd, | 
|  | 179 | const char* abort_msg) { | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 180 | // If we abort before we get an output fd, contact tombstoned to let any | 
|  | 181 | // potential listeners know that we failed. | 
|  | 182 | if (!tombstoned_connected) { | 
| Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 183 | if (!tombstoned_connect(target, &tombstoned_socket, &output_fd, kDebuggerdAnyIntercept)) { | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 184 | // We failed to connect, not much we can do. | 
|  | 185 | LOG(ERROR) << "failed to connected to tombstoned to report failure"; | 
|  | 186 | _exit(1); | 
|  | 187 | } | 
|  | 188 | } | 
|  | 189 |  | 
| Josh Gao | 428daaf | 2017-03-10 14:49:19 -0800 | [diff] [blame] | 190 | dprintf(output_fd.get(), "crash_dump failed to dump process"); | 
|  | 191 | if (target != 1) { | 
|  | 192 | dprintf(output_fd.get(), " %d: %s\n", target, abort_msg); | 
|  | 193 | } else { | 
|  | 194 | dprintf(output_fd.get(), ": %s\n", abort_msg); | 
|  | 195 | } | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 196 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 197 | _exit(1); | 
|  | 198 | } | 
|  | 199 |  | 
| Josh Gao | 85bcaf6 | 2017-02-01 16:35:31 -0800 | [diff] [blame] | 200 | static void drop_capabilities() { | 
| Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 201 | ATRACE_CALL(); | 
| Josh Gao | 85bcaf6 | 2017-02-01 16:35:31 -0800 | [diff] [blame] | 202 | __user_cap_header_struct capheader; | 
|  | 203 | memset(&capheader, 0, sizeof(capheader)); | 
|  | 204 | capheader.version = _LINUX_CAPABILITY_VERSION_3; | 
|  | 205 | capheader.pid = 0; | 
|  | 206 |  | 
|  | 207 | __user_cap_data_struct capdata[2]; | 
|  | 208 | memset(&capdata, 0, sizeof(capdata)); | 
|  | 209 |  | 
|  | 210 | if (capset(&capheader, &capdata[0]) == -1) { | 
|  | 211 | PLOG(FATAL) << "failed to drop capabilities"; | 
|  | 212 | } | 
|  | 213 |  | 
|  | 214 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) { | 
|  | 215 | PLOG(FATAL) << "failed to set PR_SET_NO_NEW_PRIVS"; | 
|  | 216 | } | 
|  | 217 | } | 
|  | 218 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 219 | int main(int argc, char** argv) { | 
| Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 220 | atrace_begin(ATRACE_TAG, "before reparent"); | 
|  | 221 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 222 | pid_t target = getppid(); | 
|  | 223 | bool tombstoned_connected = false; | 
|  | 224 | unique_fd tombstoned_socket; | 
|  | 225 | unique_fd output_fd; | 
|  | 226 |  | 
|  | 227 | android::base::InitLogging(argv); | 
|  | 228 | android::base::SetAborter([&](const char* abort_msg) { | 
|  | 229 | abort_handler(target, tombstoned_connected, tombstoned_socket, output_fd, abort_msg); | 
|  | 230 | }); | 
|  | 231 |  | 
| Josh Gao | 5759411 | 2017-01-22 17:41:15 -0800 | [diff] [blame] | 232 | // Don't try to dump ourselves. | 
|  | 233 | struct sigaction action = {}; | 
|  | 234 | action.sa_handler = signal_handler; | 
|  | 235 | debuggerd_register_handlers(&action); | 
|  | 236 |  | 
| Josh Gao | e740250 | 2017-06-01 11:55:25 -0700 | [diff] [blame] | 237 | sigset_t mask; | 
|  | 238 | sigemptyset(&mask); | 
|  | 239 | if (sigprocmask(SIG_SETMASK, &mask, nullptr) != 0) { | 
|  | 240 | PLOG(FATAL) << "failed to set signal mask"; | 
|  | 241 | } | 
|  | 242 |  | 
| Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 243 | if (argc != 4) { | 
|  | 244 | LOG(FATAL) << "Wrong number of args: " << argc << " (expected 4)"; | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 245 | } | 
|  | 246 |  | 
|  | 247 | pid_t main_tid; | 
| Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 248 | pid_t pseudothread_tid; | 
| Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 249 | int dump_type; | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 250 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 251 | if (!android::base::ParseInt(argv[1], &main_tid, 1, std::numeric_limits<pid_t>::max())) { | 
|  | 252 | LOG(FATAL) << "invalid main tid: " << argv[1]; | 
|  | 253 | } | 
|  | 254 |  | 
| Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 255 | 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] | 256 | LOG(FATAL) << "invalid pseudothread tid: " << argv[2]; | 
| Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 257 | } | 
|  | 258 |  | 
| Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 259 | if (!android::base::ParseInt(argv[3], &dump_type, 0, 1)) { | 
|  | 260 | LOG(FATAL) << "invalid requested dump type: " << argv[3]; | 
|  | 261 | } | 
|  | 262 |  | 
| Josh Gao | c7fe060 | 2017-03-13 14:13:29 -0700 | [diff] [blame] | 263 | if (target == 1) { | 
|  | 264 | LOG(FATAL) << "target died before we could attach (received main tid = " << main_tid << ")"; | 
|  | 265 | } | 
|  | 266 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 267 | android::procinfo::ProcessInfo target_info; | 
|  | 268 | if (!android::procinfo::GetProcessInfo(main_tid, &target_info)) { | 
|  | 269 | LOG(FATAL) << "failed to fetch process info for target " << main_tid; | 
|  | 270 | } | 
|  | 271 |  | 
|  | 272 | if (main_tid != target_info.tid || target != target_info.pid) { | 
|  | 273 | LOG(FATAL) << "target info mismatch, expected pid " << target << ", tid " << main_tid | 
|  | 274 | << ", received pid " << target_info.pid << ", tid " << target_info.tid; | 
|  | 275 | } | 
|  | 276 |  | 
|  | 277 | // Open /proc/`getppid()` in the original process, and pass it down to the forked child. | 
|  | 278 | std::string target_proc_path = "/proc/" + std::to_string(target); | 
|  | 279 | int target_proc_fd = open(target_proc_path.c_str(), O_DIRECTORY | O_RDONLY); | 
|  | 280 | if (target_proc_fd == -1) { | 
|  | 281 | PLOG(FATAL) << "failed to open " << target_proc_path; | 
|  | 282 | } | 
|  | 283 |  | 
| Josh Gao | 2a18b82 | 2017-02-16 19:17:28 -0800 | [diff] [blame] | 284 | // Make sure our parent didn't die. | 
|  | 285 | if (getppid() != target) { | 
|  | 286 | PLOG(FATAL) << "parent died"; | 
|  | 287 | } | 
|  | 288 |  | 
| Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 289 | atrace_end(ATRACE_TAG); | 
|  | 290 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 291 | // Reparent ourselves to init, so that the signal handler can waitpid on the | 
|  | 292 | // original process to avoid leaving a zombie for non-fatal dumps. | 
|  | 293 | pid_t forkpid = fork(); | 
|  | 294 | if (forkpid == -1) { | 
|  | 295 | PLOG(FATAL) << "fork failed"; | 
|  | 296 | } else if (forkpid != 0) { | 
|  | 297 | exit(0); | 
|  | 298 | } | 
|  | 299 |  | 
| Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 300 | ATRACE_NAME("after reparent"); | 
|  | 301 |  | 
| Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 302 | // Die if we take too long. | 
| Andreas Gampe | b02851a | 2017-06-22 19:45:53 -0700 | [diff] [blame] | 303 | // | 
|  | 304 | // Note: processes with many threads and minidebug-info can take a bit to | 
|  | 305 | //       unwind, do not make this too small. b/62828735 | 
|  | 306 | alarm(5); | 
| Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 307 |  | 
| Josh Gao | 42fd74b | 2017-01-20 12:51:11 -0800 | [diff] [blame] | 308 | std::string attach_error; | 
| Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 309 |  | 
| Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 310 | std::map<pid_t, std::string> threads; | 
| Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 311 |  | 
| Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 312 | { | 
| Josh Gao | 3407d7c | 2017-06-13 17:21:12 +0000 | [diff] [blame] | 313 | ATRACE_NAME("ptrace"); | 
| Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 314 | // Seize the main thread. | 
|  | 315 | if (!ptrace_seize_thread(target_proc_fd, main_tid, &attach_error)) { | 
|  | 316 | LOG(FATAL) << attach_error; | 
| Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 317 | } | 
|  | 318 |  | 
| Josh Gao | 3407d7c | 2017-06-13 17:21:12 +0000 | [diff] [blame] | 319 | // Seize the siblings. | 
|  | 320 | { | 
|  | 321 | std::set<pid_t> siblings; | 
|  | 322 | if (!android::procinfo::GetProcessTids(target, &siblings)) { | 
|  | 323 | PLOG(FATAL) << "failed to get process siblings"; | 
| Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 324 | } | 
| Josh Gao | 3407d7c | 2017-06-13 17:21:12 +0000 | [diff] [blame] | 325 |  | 
|  | 326 | // but not the already attached main thread. | 
|  | 327 | siblings.erase(main_tid); | 
|  | 328 | // or the handler pseudothread. | 
|  | 329 | siblings.erase(pseudothread_tid); | 
|  | 330 |  | 
|  | 331 | for (pid_t sibling_tid : siblings) { | 
|  | 332 | if (!ptrace_seize_thread(target_proc_fd, sibling_tid, &attach_error)) { | 
|  | 333 | LOG(WARNING) << attach_error; | 
|  | 334 | } else { | 
|  | 335 | threads.emplace(sibling_tid, get_thread_name(sibling_tid)); | 
|  | 336 | } | 
|  | 337 | } | 
| Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 338 | } | 
|  | 339 | } | 
|  | 340 |  | 
| Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 341 | // Collect the backtrace map, open files, and process/thread names, while we still have caps. | 
| Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 342 | std::unique_ptr<BacktraceMap> backtrace_map; | 
|  | 343 | { | 
|  | 344 | ATRACE_NAME("backtrace map"); | 
|  | 345 | backtrace_map.reset(BacktraceMap::Create(main_tid)); | 
|  | 346 | if (!backtrace_map) { | 
|  | 347 | LOG(FATAL) << "failed to create backtrace map"; | 
|  | 348 | } | 
| Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 349 | } | 
| Christopher Ferris | 9a8c855 | 2017-08-11 15:29:19 -0700 | [diff] [blame] | 350 | std::unique_ptr<BacktraceMap> backtrace_map_new; | 
|  | 351 | backtrace_map_new.reset(BacktraceMap::CreateNew(main_tid)); | 
|  | 352 | if (!backtrace_map_new) { | 
|  | 353 | LOG(FATAL) << "failed to create backtrace map new"; | 
|  | 354 | } | 
| Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 355 |  | 
|  | 356 | // Collect the list of open files. | 
|  | 357 | OpenFilesList open_files; | 
| Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 358 | { | 
|  | 359 | ATRACE_NAME("open files"); | 
|  | 360 | populate_open_files_list(target, &open_files); | 
|  | 361 | } | 
| Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 362 |  | 
| Josh Gao | 3407d7c | 2017-06-13 17:21:12 +0000 | [diff] [blame] | 363 | std::string process_name = get_process_name(main_tid); | 
|  | 364 | threads.emplace(main_tid, get_thread_name(main_tid)); | 
|  | 365 |  | 
| Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 366 | // Drop our capabilities now that we've attached to the threads we care about. | 
|  | 367 | drop_capabilities(); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 368 |  | 
| Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 369 | { | 
|  | 370 | ATRACE_NAME("tombstoned_connect"); | 
|  | 371 | const DebuggerdDumpType dump_type_enum = static_cast<DebuggerdDumpType>(dump_type); | 
|  | 372 | LOG(INFO) << "obtaining output fd from tombstoned, type: " << dump_type_enum; | 
|  | 373 | tombstoned_connected = tombstoned_connect(target, &tombstoned_socket, &output_fd, dump_type_enum); | 
|  | 374 | } | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 375 |  | 
|  | 376 | // Write a '\1' to stdout to tell the crashing process to resume. | 
| Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 377 | // It also restores the value of PR_SET_DUMPABLE at this point. | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 378 | if (TEMP_FAILURE_RETRY(write(STDOUT_FILENO, "\1", 1)) == -1) { | 
|  | 379 | PLOG(ERROR) << "failed to communicate to target process"; | 
|  | 380 | } | 
|  | 381 |  | 
|  | 382 | if (tombstoned_connected) { | 
|  | 383 | if (TEMP_FAILURE_RETRY(dup2(output_fd.get(), STDOUT_FILENO)) == -1) { | 
|  | 384 | PLOG(ERROR) << "failed to dup2 output fd (" << output_fd.get() << ") to STDOUT_FILENO"; | 
|  | 385 | } | 
|  | 386 | } else { | 
|  | 387 | unique_fd devnull(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR))); | 
|  | 388 | TEMP_FAILURE_RETRY(dup2(devnull.get(), STDOUT_FILENO)); | 
| Josh Gao | 0a37901 | 2017-01-24 15:20:42 -0800 | [diff] [blame] | 389 | output_fd = std::move(devnull); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 390 | } | 
|  | 391 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 392 | LOG(INFO) << "performing dump of process " << target << " (target tid = " << main_tid << ")"; | 
|  | 393 |  | 
| Josh Gao | 122479f | 2017-01-22 16:42:32 -0800 | [diff] [blame] | 394 | // At this point, the thread that made the request has been attached and is | 
|  | 395 | // in ptrace-stopped state. After resumption, the triggering signal that has | 
|  | 396 | // been queued will be delivered. | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 397 | if (ptrace(PTRACE_CONT, main_tid, 0, 0) != 0) { | 
|  | 398 | PLOG(ERROR) << "PTRACE_CONT(" << main_tid << ") failed"; | 
|  | 399 | exit(1); | 
|  | 400 | } | 
|  | 401 |  | 
|  | 402 | siginfo_t siginfo = {}; | 
| Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 403 | { | 
|  | 404 | ATRACE_NAME("wait_for_signal"); | 
|  | 405 | if (!wait_for_signal(main_tid, &siginfo)) { | 
|  | 406 | printf("failed to wait for signal in tid %d: %s\n", main_tid, strerror(errno)); | 
|  | 407 | exit(1); | 
|  | 408 | } | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 409 | } | 
|  | 410 |  | 
|  | 411 | int signo = siginfo.si_signo; | 
| Josh Gao | fe90276 | 2017-02-01 16:31:43 -0800 | [diff] [blame] | 412 | bool fatal_signal = signo != DEBUGGER_SIGNAL; | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 413 | bool backtrace = false; | 
|  | 414 | uintptr_t abort_address = 0; | 
|  | 415 |  | 
|  | 416 | // si_value can represent three things: | 
|  | 417 | //   0: dump tombstone | 
|  | 418 | //   1: dump backtrace | 
|  | 419 | //   everything else: abort message address (implies dump tombstone) | 
|  | 420 | if (siginfo.si_value.sival_int == 1) { | 
|  | 421 | backtrace = true; | 
|  | 422 | } else if (siginfo.si_value.sival_ptr != nullptr) { | 
|  | 423 | abort_address = reinterpret_cast<uintptr_t>(siginfo.si_value.sival_ptr); | 
|  | 424 | } | 
|  | 425 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 426 | // TODO: Use seccomp to lock ourselves down. | 
|  | 427 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 428 | std::string amfd_data; | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 429 | if (backtrace) { | 
| Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 430 | ATRACE_NAME("dump_backtrace"); | 
| Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 431 | dump_backtrace(output_fd.get(), backtrace_map.get(), target, main_tid, process_name, threads, 0); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 432 | } else { | 
| Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 433 | ATRACE_NAME("engrave_tombstone"); | 
| Christopher Ferris | 9a8c855 | 2017-08-11 15:29:19 -0700 | [diff] [blame] | 434 | engrave_tombstone(output_fd.get(), backtrace_map.get(), backtrace_map_new.get(), &open_files, | 
|  | 435 | target, main_tid, process_name, threads, abort_address, | 
|  | 436 | fatal_signal ? &amfd_data : nullptr); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 437 | } | 
|  | 438 |  | 
| Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 439 | // We don't actually need to PTRACE_DETACH, as long as our tracees aren't in | 
|  | 440 | // group-stop state, which is true as long as no stopping signals are sent. | 
|  | 441 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 442 | bool wait_for_gdb = android::base::GetBoolProperty("debug.debuggerd.wait_for_gdb", false); | 
| Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 443 | if (!fatal_signal || siginfo.si_code == SI_USER) { | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 444 | // Don't wait_for_gdb when the process didn't actually crash. | 
| Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 445 | wait_for_gdb = false; | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 446 | } | 
|  | 447 |  | 
| Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 448 | // If the process crashed or we need to send it SIGSTOP for wait_for_gdb, | 
|  | 449 | // get it in a state where it can receive signals, and then send the relevant | 
|  | 450 | // signal. | 
|  | 451 | if (wait_for_gdb || fatal_signal) { | 
|  | 452 | if (ptrace(PTRACE_INTERRUPT, main_tid, 0, 0) != 0) { | 
|  | 453 | PLOG(ERROR) << "failed to use PTRACE_INTERRUPT on " << main_tid; | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 454 | } | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 455 |  | 
| Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 456 | if (tgkill(target, main_tid, wait_for_gdb ? SIGSTOP : signo) != 0) { | 
|  | 457 | PLOG(ERROR) << "failed to resend signal " << signo << " to " << main_tid; | 
|  | 458 | } | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 459 | } | 
|  | 460 |  | 
|  | 461 | if (wait_for_gdb) { | 
| Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 462 | // Use ALOGI to line up with output from engrave_tombstone. | 
|  | 463 | ALOGI( | 
|  | 464 | "***********************************************************\n" | 
|  | 465 | "* Process %d has been suspended while crashing.\n" | 
|  | 466 | "* To attach gdbserver and start gdb, run this on the host:\n" | 
|  | 467 | "*\n" | 
|  | 468 | "*     gdbclient.py -p %d\n" | 
|  | 469 | "*\n" | 
|  | 470 | "***********************************************************", | 
|  | 471 | target, main_tid); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 472 | } | 
|  | 473 |  | 
|  | 474 | if (fatal_signal) { | 
| Josh Gao | b0e51e3 | 2017-06-01 12:08:10 -0700 | [diff] [blame] | 475 | // Don't try to notify ActivityManager if it just crashed, or we might hang until timeout. | 
|  | 476 | if (target_info.name != "system_server" || target_info.uid != AID_SYSTEM) { | 
|  | 477 | activity_manager_notify(target, signo, amfd_data); | 
|  | 478 | } | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 479 | } | 
|  | 480 |  | 
|  | 481 | // Close stdout before we notify tombstoned of completion. | 
|  | 482 | close(STDOUT_FILENO); | 
| Josh Gao | 0a37901 | 2017-01-24 15:20:42 -0800 | [diff] [blame] | 483 | if (tombstoned_connected && !tombstoned_notify_completion(tombstoned_socket.get())) { | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 484 | LOG(ERROR) << "failed to notify tombstoned of completion"; | 
|  | 485 | } | 
|  | 486 |  | 
|  | 487 | return 0; | 
|  | 488 | } |