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 | |
| 158 | *tombstoned_socket = std::move(sockfd); |
| 159 | *output_fd = std::move(tmp_output_fd); |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | static bool tombstoned_notify_completion(int tombstoned_socket) { |
| 164 | TombstonedCrashPacket packet = {}; |
| 165 | packet.packet_type = CrashPacketType::kCompletedDump; |
| 166 | if (TEMP_FAILURE_RETRY(write(tombstoned_socket, &packet, sizeof(packet))) != sizeof(packet)) { |
| 167 | return false; |
| 168 | } |
| 169 | return true; |
| 170 | } |
| 171 | |
Josh Gao | 5759411 | 2017-01-22 17:41:15 -0800 | [diff] [blame] | 172 | static void signal_handler(int) { |
| 173 | // We can't log easily, because the heap might be corrupt. |
| 174 | // Just die and let the surrounding log context explain things. |
| 175 | _exit(1); |
| 176 | } |
| 177 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 178 | static void abort_handler(pid_t target, const bool& tombstoned_connected, |
| 179 | unique_fd& tombstoned_socket, unique_fd& output_fd, |
| 180 | const char* abort_msg) { |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 181 | // If we abort before we get an output fd, contact tombstoned to let any |
| 182 | // potential listeners know that we failed. |
| 183 | if (!tombstoned_connected) { |
| 184 | if (!tombstoned_connect(target, &tombstoned_socket, &output_fd)) { |
| 185 | // We failed to connect, not much we can do. |
| 186 | LOG(ERROR) << "failed to connected to tombstoned to report failure"; |
| 187 | _exit(1); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | dprintf(output_fd.get(), "crash_dump failed to dump process %d: %s\n", target, abort_msg); |
| 192 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 193 | _exit(1); |
| 194 | } |
| 195 | |
Josh Gao | 85bcaf6 | 2017-02-01 16:35:31 -0800 | [diff] [blame^] | 196 | static void drop_capabilities() { |
| 197 | __user_cap_header_struct capheader; |
| 198 | memset(&capheader, 0, sizeof(capheader)); |
| 199 | capheader.version = _LINUX_CAPABILITY_VERSION_3; |
| 200 | capheader.pid = 0; |
| 201 | |
| 202 | __user_cap_data_struct capdata[2]; |
| 203 | memset(&capdata, 0, sizeof(capdata)); |
| 204 | |
| 205 | if (capset(&capheader, &capdata[0]) == -1) { |
| 206 | PLOG(FATAL) << "failed to drop capabilities"; |
| 207 | } |
| 208 | |
| 209 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) { |
| 210 | PLOG(FATAL) << "failed to set PR_SET_NO_NEW_PRIVS"; |
| 211 | } |
| 212 | } |
| 213 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 214 | static void check_process(int proc_fd, pid_t expected_pid) { |
| 215 | android::procinfo::ProcessInfo proc_info; |
| 216 | if (!android::procinfo::GetProcessInfoFromProcPidFd(proc_fd, &proc_info)) { |
| 217 | LOG(FATAL) << "failed to fetch process info"; |
| 218 | } |
| 219 | |
| 220 | if (proc_info.pid != expected_pid) { |
| 221 | LOG(FATAL) << "pid mismatch: expected " << expected_pid << ", actual " << proc_info.ppid; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | int main(int argc, char** argv) { |
| 226 | pid_t target = getppid(); |
| 227 | bool tombstoned_connected = false; |
| 228 | unique_fd tombstoned_socket; |
| 229 | unique_fd output_fd; |
| 230 | |
| 231 | android::base::InitLogging(argv); |
| 232 | android::base::SetAborter([&](const char* abort_msg) { |
| 233 | abort_handler(target, tombstoned_connected, tombstoned_socket, output_fd, abort_msg); |
| 234 | }); |
| 235 | |
Josh Gao | 5759411 | 2017-01-22 17:41:15 -0800 | [diff] [blame] | 236 | // Don't try to dump ourselves. |
| 237 | struct sigaction action = {}; |
| 238 | action.sa_handler = signal_handler; |
| 239 | debuggerd_register_handlers(&action); |
| 240 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 241 | if (argc != 2) { |
| 242 | return 1; |
| 243 | } |
| 244 | |
| 245 | pid_t main_tid; |
| 246 | |
| 247 | if (target == 1) { |
| 248 | LOG(FATAL) << "target died before we could attach"; |
| 249 | } |
| 250 | |
| 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 | |
| 255 | android::procinfo::ProcessInfo target_info; |
| 256 | if (!android::procinfo::GetProcessInfo(main_tid, &target_info)) { |
| 257 | LOG(FATAL) << "failed to fetch process info for target " << main_tid; |
| 258 | } |
| 259 | |
| 260 | if (main_tid != target_info.tid || target != target_info.pid) { |
| 261 | LOG(FATAL) << "target info mismatch, expected pid " << target << ", tid " << main_tid |
| 262 | << ", received pid " << target_info.pid << ", tid " << target_info.tid; |
| 263 | } |
| 264 | |
| 265 | // Open /proc/`getppid()` in the original process, and pass it down to the forked child. |
| 266 | std::string target_proc_path = "/proc/" + std::to_string(target); |
| 267 | int target_proc_fd = open(target_proc_path.c_str(), O_DIRECTORY | O_RDONLY); |
| 268 | if (target_proc_fd == -1) { |
| 269 | PLOG(FATAL) << "failed to open " << target_proc_path; |
| 270 | } |
| 271 | |
| 272 | // Reparent ourselves to init, so that the signal handler can waitpid on the |
| 273 | // original process to avoid leaving a zombie for non-fatal dumps. |
| 274 | pid_t forkpid = fork(); |
| 275 | if (forkpid == -1) { |
| 276 | PLOG(FATAL) << "fork failed"; |
| 277 | } else if (forkpid != 0) { |
| 278 | exit(0); |
| 279 | } |
| 280 | |
Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 281 | // Die if we take too long. |
| 282 | alarm(20); |
| 283 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 284 | check_process(target_proc_fd, target); |
| 285 | |
Josh Gao | 42fd74b | 2017-01-20 12:51:11 -0800 | [diff] [blame] | 286 | std::string attach_error; |
Josh Gao | fe90276 | 2017-02-01 16:31:43 -0800 | [diff] [blame] | 287 | if (!ptrace_seize_thread(target_proc_fd, main_tid, &attach_error)) { |
Josh Gao | 42fd74b | 2017-01-20 12:51:11 -0800 | [diff] [blame] | 288 | LOG(FATAL) << attach_error; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | check_process(target_proc_fd, target); |
| 292 | |
| 293 | LOG(INFO) << "obtaining output fd from tombstoned"; |
| 294 | tombstoned_connected = tombstoned_connect(target, &tombstoned_socket, &output_fd); |
| 295 | |
| 296 | // Write a '\1' to stdout to tell the crashing process to resume. |
| 297 | if (TEMP_FAILURE_RETRY(write(STDOUT_FILENO, "\1", 1)) == -1) { |
| 298 | PLOG(ERROR) << "failed to communicate to target process"; |
| 299 | } |
| 300 | |
| 301 | if (tombstoned_connected) { |
| 302 | if (TEMP_FAILURE_RETRY(dup2(output_fd.get(), STDOUT_FILENO)) == -1) { |
| 303 | PLOG(ERROR) << "failed to dup2 output fd (" << output_fd.get() << ") to STDOUT_FILENO"; |
| 304 | } |
| 305 | } else { |
| 306 | unique_fd devnull(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR))); |
| 307 | TEMP_FAILURE_RETRY(dup2(devnull.get(), STDOUT_FILENO)); |
Josh Gao | 0a37901 | 2017-01-24 15:20:42 -0800 | [diff] [blame] | 308 | output_fd = std::move(devnull); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 311 | LOG(INFO) << "performing dump of process " << target << " (target tid = " << main_tid << ")"; |
| 312 | |
Josh Gao | 122479f | 2017-01-22 16:42:32 -0800 | [diff] [blame] | 313 | // At this point, the thread that made the request has been attached and is |
| 314 | // in ptrace-stopped state. After resumption, the triggering signal that has |
| 315 | // been queued will be delivered. |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 316 | if (ptrace(PTRACE_CONT, main_tid, 0, 0) != 0) { |
| 317 | PLOG(ERROR) << "PTRACE_CONT(" << main_tid << ") failed"; |
| 318 | exit(1); |
| 319 | } |
| 320 | |
| 321 | siginfo_t siginfo = {}; |
| 322 | if (!wait_for_signal(main_tid, &siginfo)) { |
| 323 | printf("failed to wait for signal in tid %d: %s\n", main_tid, strerror(errno)); |
| 324 | exit(1); |
| 325 | } |
| 326 | |
| 327 | int signo = siginfo.si_signo; |
Josh Gao | fe90276 | 2017-02-01 16:31:43 -0800 | [diff] [blame] | 328 | bool fatal_signal = signo != DEBUGGER_SIGNAL; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 329 | bool backtrace = false; |
| 330 | uintptr_t abort_address = 0; |
| 331 | |
| 332 | // si_value can represent three things: |
| 333 | // 0: dump tombstone |
| 334 | // 1: dump backtrace |
| 335 | // everything else: abort message address (implies dump tombstone) |
| 336 | if (siginfo.si_value.sival_int == 1) { |
| 337 | backtrace = true; |
| 338 | } else if (siginfo.si_value.sival_ptr != nullptr) { |
| 339 | abort_address = reinterpret_cast<uintptr_t>(siginfo.si_value.sival_ptr); |
| 340 | } |
| 341 | |
| 342 | // Now that we have the signal that kicked things off, attach all of the |
| 343 | // sibling threads, and then proceed. |
Josh Gao | 42fd74b | 2017-01-20 12:51:11 -0800 | [diff] [blame] | 344 | std::set<pid_t> attached_siblings; |
Josh Gao | fe90276 | 2017-02-01 16:31:43 -0800 | [diff] [blame] | 345 | { |
| 346 | std::set<pid_t> siblings; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 347 | if (!android::procinfo::GetProcessTids(target, &siblings)) { |
| 348 | PLOG(FATAL) << "failed to get process siblings"; |
| 349 | } |
| 350 | siblings.erase(main_tid); |
| 351 | |
| 352 | for (pid_t sibling_tid : siblings) { |
Josh Gao | fe90276 | 2017-02-01 16:31:43 -0800 | [diff] [blame] | 353 | if (!ptrace_seize_thread(target_proc_fd, sibling_tid, &attach_error)) { |
Josh Gao | 42fd74b | 2017-01-20 12:51:11 -0800 | [diff] [blame] | 354 | LOG(WARNING) << attach_error; |
| 355 | } else { |
| 356 | attached_siblings.insert(sibling_tid); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
Josh Gao | 85bcaf6 | 2017-02-01 16:35:31 -0800 | [diff] [blame^] | 361 | // Drop our capabilities now that we've attached to the threads we care about. |
| 362 | drop_capabilities(); |
| 363 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 364 | check_process(target_proc_fd, target); |
| 365 | |
| 366 | // TODO: Use seccomp to lock ourselves down. |
| 367 | |
| 368 | std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(main_tid)); |
| 369 | std::string amfd_data; |
| 370 | |
| 371 | if (backtrace) { |
Josh Gao | 42fd74b | 2017-01-20 12:51:11 -0800 | [diff] [blame] | 372 | 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] | 373 | } else { |
| 374 | // Collect the list of open files. |
| 375 | OpenFilesList open_files; |
| 376 | populate_open_files_list(target, &open_files); |
| 377 | |
Josh Gao | 42fd74b | 2017-01-20 12:51:11 -0800 | [diff] [blame] | 378 | engrave_tombstone(output_fd.get(), backtrace_map.get(), open_files, target, main_tid, |
| 379 | attached_siblings, abort_address, fatal_signal ? &amfd_data : nullptr); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 382 | // We don't actually need to PTRACE_DETACH, as long as our tracees aren't in |
| 383 | // group-stop state, which is true as long as no stopping signals are sent. |
| 384 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 385 | 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] | 386 | if (!fatal_signal || siginfo.si_code == SI_USER) { |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 387 | // Don't wait_for_gdb when the process didn't actually crash. |
Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 388 | wait_for_gdb = false; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 391 | // If the process crashed or we need to send it SIGSTOP for wait_for_gdb, |
| 392 | // get it in a state where it can receive signals, and then send the relevant |
| 393 | // signal. |
| 394 | if (wait_for_gdb || fatal_signal) { |
| 395 | if (ptrace(PTRACE_INTERRUPT, main_tid, 0, 0) != 0) { |
| 396 | PLOG(ERROR) << "failed to use PTRACE_INTERRUPT on " << main_tid; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 397 | } |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 398 | |
Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 399 | if (tgkill(target, main_tid, wait_for_gdb ? SIGSTOP : signo) != 0) { |
| 400 | PLOG(ERROR) << "failed to resend signal " << signo << " to " << main_tid; |
| 401 | } |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | if (wait_for_gdb) { |
Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 405 | // Use ALOGI to line up with output from engrave_tombstone. |
| 406 | ALOGI( |
| 407 | "***********************************************************\n" |
| 408 | "* Process %d has been suspended while crashing.\n" |
| 409 | "* To attach gdbserver and start gdb, run this on the host:\n" |
| 410 | "*\n" |
| 411 | "* gdbclient.py -p %d\n" |
| 412 | "*\n" |
| 413 | "***********************************************************", |
| 414 | target, main_tid); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | if (fatal_signal) { |
| 418 | activity_manager_notify(target, signo, amfd_data); |
| 419 | } |
| 420 | |
| 421 | // Close stdout before we notify tombstoned of completion. |
| 422 | close(STDOUT_FILENO); |
Josh Gao | 0a37901 | 2017-01-24 15:20:42 -0800 | [diff] [blame] | 423 | if (tombstoned_connected && !tombstoned_notify_completion(tombstoned_socket.get())) { |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 424 | LOG(ERROR) << "failed to notify tombstoned of completion"; |
| 425 | } |
| 426 | |
| 427 | return 0; |
| 428 | } |