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