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