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