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