blob: 437450c05b252a848971b4466cb6ca4063178401 [file] [log] [blame]
Josh Gaocbe70cb2016-10-18 18:17:52 -07001/*
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 Gao85bcaf62017-02-01 16:35:31 -080021#include <sys/prctl.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070022#include <sys/ptrace.h>
23#include <sys/types.h>
24#include <sys/un.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070025#include <sys/wait.h>
Josh Gao85bcaf62017-02-01 16:35:31 -080026#include <syscall.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070027#include <unistd.h>
28
29#include <limits>
Josh Gao57f58f82017-03-15 23:23:22 -070030#include <map>
Josh Gaocbe70cb2016-10-18 18:17:52 -070031#include <memory>
32#include <set>
33#include <vector>
34
35#include <android-base/file.h>
36#include <android-base/logging.h>
Josh Gao8d44b142018-09-18 13:22:22 -070037#include <android-base/macros.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070038#include <android-base/parseint.h>
39#include <android-base/properties.h>
40#include <android-base/stringprintf.h>
Josh Gao57f58f82017-03-15 23:23:22 -070041#include <android-base/strings.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070042#include <android-base/unique_fd.h>
43#include <cutils/sockets.h>
Vijay Venkatramana95acea2017-01-23 20:11:51 -080044#include <log/log.h>
Josh Gaob0e51e32017-06-01 12:08:10 -070045#include <private/android_filesystem_config.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070046#include <procinfo/process.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070047
Josh Gao8bb03902017-05-30 15:31:02 -070048#define ATRACE_TAG ATRACE_TAG_BIONIC
49#include <utils/Trace.h>
50
Christopher Ferris60eb1972019-01-15 15:18:43 -080051#include <unwindstack/JitDebug.h>
52#include <unwindstack/Maps.h>
53#include <unwindstack/Memory.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070054#include <unwindstack/Regs.h>
Christopher Ferris60eb1972019-01-15 15:18:43 -080055#include <unwindstack/Unwinder.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070056
Josh Gaoc3706662017-08-29 13:08:32 -070057#include "libdebuggerd/backtrace.h"
58#include "libdebuggerd/tombstone.h"
59#include "libdebuggerd/utility.h"
Josh Gaocbe70cb2016-10-18 18:17:52 -070060
61#include "debuggerd/handler.h"
Narayan Kamath2d377cd2017-05-10 10:58:59 +010062#include "tombstoned/tombstoned.h"
Josh Gaoc3706662017-08-29 13:08:32 -070063
64#include "protocol.h"
Narayan Kamath2d377cd2017-05-10 10:58:59 +010065#include "util.h"
Josh Gaocbe70cb2016-10-18 18:17:52 -070066
67using android::base::unique_fd;
68using android::base::StringPrintf;
Josh Gao57f58f82017-03-15 23:23:22 -070069
Josh Gaofe902762017-02-01 16:31:43 -080070static bool pid_contains_tid(int pid_proc_fd, pid_t tid) {
71 struct stat st;
72 std::string task_path = StringPrintf("task/%d", tid);
73 return fstatat(pid_proc_fd, task_path.c_str(), &st, 0) == 0;
Josh Gaocbe70cb2016-10-18 18:17:52 -070074}
75
Josh Gaofd13bf02017-08-18 15:37:26 -070076static pid_t get_tracer(pid_t tracee) {
77 // Check to see if the thread is being ptraced by another process.
78 android::procinfo::ProcessInfo process_info;
79 if (android::procinfo::GetProcessInfo(tracee, &process_info)) {
80 return process_info.tracer;
81 }
82 return -1;
83}
84
Josh Gaocbe70cb2016-10-18 18:17:52 -070085// Attach to a thread, and verify that it's still a member of the given process
Josh Gao2b2ae0c2017-08-21 14:31:17 -070086static bool ptrace_seize_thread(int pid_proc_fd, pid_t tid, std::string* error, int flags = 0) {
87 if (ptrace(PTRACE_SEIZE, tid, 0, flags) != 0) {
Josh Gaofd13bf02017-08-18 15:37:26 -070088 if (errno == EPERM) {
89 pid_t tracer = get_tracer(tid);
90 if (tracer != -1) {
91 *error = StringPrintf("failed to attach to thread %d, already traced by %d (%s)", tid,
92 tracer, get_process_name(tracer).c_str());
93 return false;
94 }
95 }
96
Josh Gao42fd74b2017-01-20 12:51:11 -080097 *error = StringPrintf("failed to attach to thread %d: %s", tid, strerror(errno));
Josh Gaocbe70cb2016-10-18 18:17:52 -070098 return false;
99 }
100
101 // Make sure that the task we attached to is actually part of the pid we're dumping.
Josh Gaofe902762017-02-01 16:31:43 -0800102 if (!pid_contains_tid(pid_proc_fd, tid)) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700103 if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700104 PLOG(WARNING) << "failed to detach from thread " << tid;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700105 }
Josh Gaofe902762017-02-01 16:31:43 -0800106 *error = StringPrintf("thread %d is not in process", tid);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700107 return false;
108 }
Josh Gao122479f2017-01-22 16:42:32 -0800109
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700110 return true;
111}
112
113static bool wait_for_stop(pid_t tid, int* received_signal) {
114 while (true) {
115 int status;
116 pid_t result = waitpid(tid, &status, __WALL);
117 if (result != tid) {
118 PLOG(ERROR) << "waitpid failed on " << tid << " while detaching";
119 return false;
120 }
121
122 if (WIFSTOPPED(status)) {
123 if (status >> 16 == PTRACE_EVENT_STOP) {
124 *received_signal = 0;
125 } else {
126 *received_signal = WSTOPSIG(status);
127 }
128 return true;
129 }
130 }
131}
132
133// Interrupt a process and wait for it to be interrupted.
134static bool ptrace_interrupt(pid_t tid, int* received_signal) {
135 if (ptrace(PTRACE_INTERRUPT, tid, 0, 0) == 0) {
136 return wait_for_stop(tid, received_signal);
Josh Gao3407d7c2017-06-13 17:21:12 +0000137 }
138
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700139 PLOG(ERROR) << "failed to interrupt " << tid << " to detach";
140 return false;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700141}
142
Josh Gaob0e51e32017-06-01 12:08:10 -0700143static bool activity_manager_notify(pid_t pid, int signal, const std::string& amfd_data) {
Josh Gao8bb03902017-05-30 15:31:02 -0700144 ATRACE_CALL();
Josh Gaob0e51e32017-06-01 12:08:10 -0700145 android::base::unique_fd amfd(socket_local_client(
146 "/data/system/ndebugsocket", ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700147 if (amfd.get() == -1) {
148 PLOG(ERROR) << "unable to connect to activity manager";
149 return false;
150 }
151
152 struct timeval tv = {
153 .tv_sec = 1,
154 .tv_usec = 0,
155 };
156 if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
157 PLOG(ERROR) << "failed to set send timeout on activity manager socket";
158 return false;
159 }
160 tv.tv_sec = 3; // 3 seconds on handshake read
161 if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
162 PLOG(ERROR) << "failed to set receive timeout on activity manager socket";
163 return false;
164 }
165
166 // Activity Manager protocol: binary 32-bit network-byte-order ints for the
167 // pid and signal number, followed by the raw text of the dump, culminating
168 // in a zero byte that marks end-of-data.
169 uint32_t datum = htonl(pid);
170 if (!android::base::WriteFully(amfd, &datum, 4)) {
171 PLOG(ERROR) << "AM pid write failed";
172 return false;
173 }
174 datum = htonl(signal);
175 if (!android::base::WriteFully(amfd, &datum, 4)) {
176 PLOG(ERROR) << "AM signal write failed";
177 return false;
178 }
179 if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size() + 1)) {
180 PLOG(ERROR) << "AM data write failed";
181 return false;
182 }
183
184 // 3 sec timeout reading the ack; we're fine if the read fails.
185 char ack;
186 android::base::ReadFully(amfd, &ack, 1);
187 return true;
188}
189
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700190// Globals used by the abort handler.
191static pid_t g_target_thread = -1;
192static bool g_tombstoned_connected = false;
193static unique_fd g_tombstoned_socket;
194static unique_fd g_output_fd;
Josh Gao57594112017-01-22 17:41:15 -0800195
Josh Gao38ac45d2018-04-27 13:31:47 -0700196static void DefuseSignalHandlers() {
197 // Don't try to dump ourselves.
198 struct sigaction action = {};
199 action.sa_handler = SIG_DFL;
200 debuggerd_register_handlers(&action);
201
202 sigset_t mask;
203 sigemptyset(&mask);
204 if (sigprocmask(SIG_SETMASK, &mask, nullptr) != 0) {
205 PLOG(FATAL) << "failed to set signal mask";
206 }
207}
208
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700209static void Initialize(char** argv) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700210 android::base::InitLogging(argv);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700211 android::base::SetAborter([](const char* abort_msg) {
212 // If we abort before we get an output fd, contact tombstoned to let any
213 // potential listeners know that we failed.
214 if (!g_tombstoned_connected) {
215 if (!tombstoned_connect(g_target_thread, &g_tombstoned_socket, &g_output_fd,
216 kDebuggerdAnyIntercept)) {
217 // We failed to connect, not much we can do.
218 LOG(ERROR) << "failed to connected to tombstoned to report failure";
219 _exit(1);
220 }
221 }
222
223 dprintf(g_output_fd.get(), "crash_dump failed to dump process");
224 if (g_target_thread != 1) {
225 dprintf(g_output_fd.get(), " %d: %s\n", g_target_thread, abort_msg);
226 } else {
227 dprintf(g_output_fd.get(), ": %s\n", abort_msg);
228 }
229
230 _exit(1);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700231 });
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700232}
Josh Gaoe7402502017-06-01 11:55:25 -0700233
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700234static void ParseArgs(int argc, char** argv, pid_t* pseudothread_tid, DebuggerdDumpType* dump_type) {
Narayan Kamatha73df602017-05-24 15:07:25 +0100235 if (argc != 4) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700236 LOG(FATAL) << "wrong number of args: " << argc << " (expected 4)";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700237 }
238
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700239 if (!android::base::ParseInt(argv[1], &g_target_thread, 1, std::numeric_limits<pid_t>::max())) {
240 LOG(FATAL) << "invalid target tid: " << argv[1];
Josh Gaocbe70cb2016-10-18 18:17:52 -0700241 }
242
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700243 if (!android::base::ParseInt(argv[2], pseudothread_tid, 1, std::numeric_limits<pid_t>::max())) {
Josh Gaof6ad5852017-02-15 12:21:11 -0800244 LOG(FATAL) << "invalid pseudothread tid: " << argv[2];
Josh Gao2f11a252017-02-13 14:46:19 -0800245 }
246
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700247 int dump_type_int;
248 if (!android::base::ParseInt(argv[3], &dump_type_int, 0, 1)) {
Narayan Kamatha73df602017-05-24 15:07:25 +0100249 LOG(FATAL) << "invalid requested dump type: " << argv[3];
250 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700251 *dump_type = static_cast<DebuggerdDumpType>(dump_type_int);
252}
Narayan Kamatha73df602017-05-24 15:07:25 +0100253
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700254static void ReadCrashInfo(unique_fd& fd, siginfo_t* siginfo,
Josh Gao9da1f512018-08-06 15:38:29 -0700255 std::unique_ptr<unwindstack::Regs>* regs, uintptr_t* abort_msg_address,
256 uintptr_t* fdsan_table_address) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700257 std::aligned_storage<sizeof(CrashInfo) + 1, alignof(CrashInfo)>::type buf;
Josh Gao9da1f512018-08-06 15:38:29 -0700258 CrashInfo* crash_info = reinterpret_cast<CrashInfo*>(&buf);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700259 ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), &buf, sizeof(buf)));
260 if (rc == -1) {
261 PLOG(FATAL) << "failed to read target ucontext";
Josh Gao9da1f512018-08-06 15:38:29 -0700262 } else {
263 ssize_t expected_size = 0;
264 switch (crash_info->header.version) {
265 case 1:
266 expected_size = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataV1);
267 break;
268
269 case 2:
270 expected_size = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataV2);
271 break;
272
273 default:
274 LOG(FATAL) << "unexpected CrashInfo version: " << crash_info->header.version;
275 break;
276 };
277
278 if (rc != expected_size) {
279 LOG(FATAL) << "read " << rc << " bytes when reading target crash information, expected "
280 << expected_size;
281 }
Josh Gaoc7fe0602017-03-13 14:13:29 -0700282 }
283
Josh Gao9da1f512018-08-06 15:38:29 -0700284 *fdsan_table_address = 0;
285 switch (crash_info->header.version) {
286 case 2:
287 *fdsan_table_address = crash_info->data.v2.fdsan_table_address;
Josh Gao8d44b142018-09-18 13:22:22 -0700288 FALLTHROUGH_INTENDED;
Josh Gao9da1f512018-08-06 15:38:29 -0700289 case 1:
290 *abort_msg_address = crash_info->data.v1.abort_msg_address;
291 *siginfo = crash_info->data.v1.siginfo;
Christopher Ferris60eb1972019-01-15 15:18:43 -0800292 regs->reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(),
293 &crash_info->data.v1.ucontext));
Josh Gao9da1f512018-08-06 15:38:29 -0700294 break;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700295
Josh Gao9da1f512018-08-06 15:38:29 -0700296 default:
297 __builtin_unreachable();
298 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700299}
300
301// Wait for a process to clone and return the child's pid.
302// Note: this leaves the parent in PTRACE_EVENT_STOP.
303static pid_t wait_for_clone(pid_t pid, bool resume_child) {
304 int status;
305 pid_t result = TEMP_FAILURE_RETRY(waitpid(pid, &status, __WALL));
306 if (result == -1) {
307 PLOG(FATAL) << "failed to waitpid";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700308 }
309
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700310 if (WIFEXITED(status)) {
311 LOG(FATAL) << "traced process exited with status " << WEXITSTATUS(status);
312 } else if (WIFSIGNALED(status)) {
313 LOG(FATAL) << "traced process exited with signal " << WTERMSIG(status);
314 } else if (!WIFSTOPPED(status)) {
315 LOG(FATAL) << "process didn't stop? (status = " << status << ")";
316 }
317
318 if (status >> 8 != (SIGTRAP | (PTRACE_EVENT_CLONE << 8))) {
319 LOG(FATAL) << "process didn't stop due to PTRACE_O_TRACECLONE (status = " << status << ")";
320 }
321
322 pid_t child;
323 if (ptrace(PTRACE_GETEVENTMSG, pid, 0, &child) != 0) {
324 PLOG(FATAL) << "failed to get child pid via PTRACE_GETEVENTMSG";
325 }
326
327 int stop_signal;
328 if (!wait_for_stop(child, &stop_signal)) {
329 PLOG(FATAL) << "failed to waitpid on child";
330 }
331
332 CHECK_EQ(0, stop_signal);
333
334 if (resume_child) {
335 if (ptrace(PTRACE_CONT, child, 0, 0) != 0) {
336 PLOG(FATAL) << "failed to resume child (pid = " << child << ")";
337 }
338 }
339
340 return child;
341}
342
343static pid_t wait_for_vm_process(pid_t pseudothread_tid) {
344 // The pseudothread will double-fork, we want its grandchild.
345 pid_t intermediate = wait_for_clone(pseudothread_tid, true);
346 pid_t vm_pid = wait_for_clone(intermediate, false);
347 if (ptrace(PTRACE_DETACH, intermediate, 0, 0) != 0) {
348 PLOG(FATAL) << "failed to detach from intermediate vm process";
349 }
350
351 return vm_pid;
352}
353
Jinguang Dong8ac2f272018-11-24 17:12:33 +0800354static void InstallSigPipeHandler() {
355 struct sigaction action = {};
356 action.sa_handler = SIG_IGN;
357 action.sa_flags = SA_RESTART;
358 sigaction(SIGPIPE, &action, nullptr);
359}
360
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700361int main(int argc, char** argv) {
Josh Gao38ac45d2018-04-27 13:31:47 -0700362 DefuseSignalHandlers();
Jinguang Dong8ac2f272018-11-24 17:12:33 +0800363 InstallSigPipeHandler();
Josh Gao38ac45d2018-04-27 13:31:47 -0700364
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700365 atrace_begin(ATRACE_TAG, "before reparent");
366 pid_t target_process = getppid();
367
368 // Open /proc/`getppid()` before we daemonize.
369 std::string target_proc_path = "/proc/" + std::to_string(target_process);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700370 int target_proc_fd = open(target_proc_path.c_str(), O_DIRECTORY | O_RDONLY);
371 if (target_proc_fd == -1) {
372 PLOG(FATAL) << "failed to open " << target_proc_path;
373 }
374
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700375 // Make sure getppid() hasn't changed.
376 if (getppid() != target_process) {
377 LOG(FATAL) << "parent died";
Josh Gao2a18b822017-02-16 19:17:28 -0800378 }
Josh Gao8bb03902017-05-30 15:31:02 -0700379 atrace_end(ATRACE_TAG);
380
Josh Gaocbe70cb2016-10-18 18:17:52 -0700381 // Reparent ourselves to init, so that the signal handler can waitpid on the
382 // original process to avoid leaving a zombie for non-fatal dumps.
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700383 // Move the input/output pipes off of stdout/stderr, out of paranoia.
384 unique_fd output_pipe(dup(STDOUT_FILENO));
385 unique_fd input_pipe(dup(STDIN_FILENO));
386
387 unique_fd fork_exit_read, fork_exit_write;
388 if (!Pipe(&fork_exit_read, &fork_exit_write)) {
389 PLOG(FATAL) << "failed to create pipe";
390 }
391
Josh Gaocbe70cb2016-10-18 18:17:52 -0700392 pid_t forkpid = fork();
393 if (forkpid == -1) {
394 PLOG(FATAL) << "fork failed";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700395 } else if (forkpid == 0) {
396 fork_exit_read.reset();
397 } else {
398 // We need the pseudothread to live until we get around to verifying the vm pid against it.
399 // The last thing it does is block on a waitpid on us, so wait until our child tells us to die.
400 fork_exit_write.reset();
401 char buf;
402 TEMP_FAILURE_RETRY(read(fork_exit_read.get(), &buf, sizeof(buf)));
403 _exit(0);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700404 }
405
Josh Gao8bb03902017-05-30 15:31:02 -0700406 ATRACE_NAME("after reparent");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700407 pid_t pseudothread_tid;
408 DebuggerdDumpType dump_type;
Josh Gao9da1f512018-08-06 15:38:29 -0700409 uintptr_t abort_msg_address = 0;
410 uintptr_t fdsan_table_address = 0;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700411
412 Initialize(argv);
413 ParseArgs(argc, argv, &pseudothread_tid, &dump_type);
Josh Gao8bb03902017-05-30 15:31:02 -0700414
Josh Gao7c6e3132017-01-22 17:59:02 -0800415 // Die if we take too long.
Andreas Gampeb02851a2017-06-22 19:45:53 -0700416 //
417 // Note: processes with many threads and minidebug-info can take a bit to
418 // unwind, do not make this too small. b/62828735
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700419 alarm(30);
Josh Gao7c6e3132017-01-22 17:59:02 -0800420
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700421 // Get the process name (aka cmdline).
422 std::string process_name = get_process_name(g_target_thread);
Josh Gao2f11a252017-02-13 14:46:19 -0800423
424 // Collect the list of open files.
425 OpenFilesList open_files;
Josh Gao8bb03902017-05-30 15:31:02 -0700426 {
427 ATRACE_NAME("open files");
Josh Gaoce841d92018-08-06 18:26:42 -0700428 populate_open_files_list(&open_files, g_target_thread);
Josh Gao8bb03902017-05-30 15:31:02 -0700429 }
Josh Gao2f11a252017-02-13 14:46:19 -0800430
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700431 // In order to reduce the duration that we pause the process for, we ptrace
432 // the threads, fetch their registers and associated information, and then
433 // fork a separate process as a snapshot of the process's address space.
434 std::set<pid_t> threads;
435 if (!android::procinfo::GetProcessTids(g_target_thread, &threads)) {
436 PLOG(FATAL) << "failed to get process threads";
437 }
Josh Gao3407d7c2017-06-13 17:21:12 +0000438
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700439 std::map<pid_t, ThreadInfo> thread_info;
440 siginfo_t siginfo;
441 std::string error;
442
443 {
444 ATRACE_NAME("ptrace");
445 for (pid_t thread : threads) {
446 // Trace the pseudothread separately, so we can use different options.
447 if (thread == pseudothread_tid) {
448 continue;
449 }
450
451 if (!ptrace_seize_thread(target_proc_fd, thread, &error)) {
452 bool fatal = thread == g_target_thread;
453 LOG(fatal ? FATAL : WARNING) << error;
454 }
455
456 ThreadInfo info;
457 info.pid = target_process;
458 info.tid = thread;
459 info.process_name = process_name;
460 info.thread_name = get_thread_name(thread);
461
462 if (!ptrace_interrupt(thread, &info.signo)) {
463 PLOG(WARNING) << "failed to ptrace interrupt thread " << thread;
464 ptrace(PTRACE_DETACH, thread, 0, 0);
465 continue;
466 }
467
468 if (thread == g_target_thread) {
469 // Read the thread's registers along with the rest of the crash info out of the pipe.
Josh Gao9da1f512018-08-06 15:38:29 -0700470 ReadCrashInfo(input_pipe, &siginfo, &info.registers, &abort_msg_address,
471 &fdsan_table_address);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700472 info.siginfo = &siginfo;
473 info.signo = info.siginfo->si_signo;
474 } else {
Christopher Ferris60eb1972019-01-15 15:18:43 -0800475 info.registers.reset(unwindstack::Regs::RemoteGet(thread));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700476 if (!info.registers) {
477 PLOG(WARNING) << "failed to fetch registers for thread " << thread;
478 ptrace(PTRACE_DETACH, thread, 0, 0);
479 continue;
480 }
481 }
482
483 thread_info[thread] = std::move(info);
484 }
485 }
486
487 // Trace the pseudothread with PTRACE_O_TRACECLONE and tell it to fork.
488 if (!ptrace_seize_thread(target_proc_fd, pseudothread_tid, &error, PTRACE_O_TRACECLONE)) {
489 LOG(FATAL) << "failed to seize pseudothread: " << error;
490 }
491
492 if (TEMP_FAILURE_RETRY(write(output_pipe.get(), "\1", 1)) != 1) {
493 PLOG(FATAL) << "failed to write to pseudothread";
494 }
495
496 pid_t vm_pid = wait_for_vm_process(pseudothread_tid);
497 if (ptrace(PTRACE_DETACH, pseudothread_tid, 0, 0) != 0) {
498 PLOG(FATAL) << "failed to detach from pseudothread";
499 }
500
501 // The pseudothread can die now.
502 fork_exit_write.reset();
503
504 // Defer the message until later, for readability.
505 bool wait_for_gdb = android::base::GetBoolProperty("debug.debuggerd.wait_for_gdb", false);
506 if (siginfo.si_signo == DEBUGGER_SIGNAL) {
507 wait_for_gdb = false;
508 }
509
510 // Detach from all of our attached threads before resuming.
511 for (const auto& [tid, thread] : thread_info) {
512 int resume_signal = thread.signo == DEBUGGER_SIGNAL ? 0 : thread.signo;
513 if (wait_for_gdb) {
514 resume_signal = 0;
515 if (tgkill(target_process, tid, SIGSTOP) != 0) {
516 PLOG(WARNING) << "failed to send SIGSTOP to " << tid;
517 }
518 }
519
520 LOG(DEBUG) << "detaching from thread " << tid;
521 if (ptrace(PTRACE_DETACH, tid, 0, resume_signal) != 0) {
522 PLOG(ERROR) << "failed to detach from thread " << tid;
523 }
524 }
525
526 // Drop our capabilities now that we've fetched all of the information we need.
Josh Gao2f11a252017-02-13 14:46:19 -0800527 drop_capabilities();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700528
Josh Gao8bb03902017-05-30 15:31:02 -0700529 {
530 ATRACE_NAME("tombstoned_connect");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700531 LOG(INFO) << "obtaining output fd from tombstoned, type: " << dump_type;
532 g_tombstoned_connected =
533 tombstoned_connect(g_target_thread, &g_tombstoned_socket, &g_output_fd, dump_type);
Josh Gao8bb03902017-05-30 15:31:02 -0700534 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700535
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700536 if (g_tombstoned_connected) {
537 if (TEMP_FAILURE_RETRY(dup2(g_output_fd.get(), STDOUT_FILENO)) == -1) {
538 PLOG(ERROR) << "failed to dup2 output fd (" << g_output_fd.get() << ") to STDOUT_FILENO";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700539 }
540 } else {
541 unique_fd devnull(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR)));
542 TEMP_FAILURE_RETRY(dup2(devnull.get(), STDOUT_FILENO));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700543 g_output_fd = std::move(devnull);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700544 }
545
Josh Gao9da1f512018-08-06 15:38:29 -0700546 LOG(INFO) << "performing dump of process " << target_process
547 << " (target tid = " << g_target_thread << ")";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700548
549 int signo = siginfo.si_signo;
Josh Gaofe902762017-02-01 16:31:43 -0800550 bool fatal_signal = signo != DEBUGGER_SIGNAL;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700551 bool backtrace = false;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700552
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700553 // si_value is special when used with DEBUGGER_SIGNAL.
Josh Gaocbe70cb2016-10-18 18:17:52 -0700554 // 0: dump tombstone
555 // 1: dump backtrace
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700556 if (!fatal_signal) {
557 int si_val = siginfo.si_value.sival_int;
558 if (si_val == 0) {
559 backtrace = false;
560 } else if (si_val == 1) {
561 backtrace = true;
562 } else {
563 LOG(WARNING) << "unknown si_value value " << si_val;
564 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700565 }
566
Josh Gaocbe70cb2016-10-18 18:17:52 -0700567 // TODO: Use seccomp to lock ourselves down.
Christopher Ferris60eb1972019-01-15 15:18:43 -0800568 unwindstack::UnwinderFromPid unwinder(256, vm_pid);
David Srbecky85b5fec2018-02-23 18:06:13 +0000569 if (!unwinder.Init()) {
Christopher Ferris60eb1972019-01-15 15:18:43 -0800570 LOG(FATAL) << "Failed to init unwinder object.";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700571 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700572
Josh Gaocbe70cb2016-10-18 18:17:52 -0700573 std::string amfd_data;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700574 if (backtrace) {
Josh Gao8bb03902017-05-30 15:31:02 -0700575 ATRACE_NAME("dump_backtrace");
Christopher Ferris60eb1972019-01-15 15:18:43 -0800576 dump_backtrace(std::move(g_output_fd), &unwinder, thread_info, g_target_thread);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700577 } else {
Josh Gaoce841d92018-08-06 18:26:42 -0700578 {
579 ATRACE_NAME("fdsan table dump");
Christopher Ferris60eb1972019-01-15 15:18:43 -0800580 populate_fdsan_table(&open_files, unwinder.GetProcessMemory(), fdsan_table_address);
Josh Gaoce841d92018-08-06 18:26:42 -0700581 }
582
583 {
584 ATRACE_NAME("engrave_tombstone");
Christopher Ferris60eb1972019-01-15 15:18:43 -0800585 engrave_tombstone(std::move(g_output_fd), &unwinder, thread_info, g_target_thread,
586 abort_msg_address, &open_files, &amfd_data);
Josh Gaoce841d92018-08-06 18:26:42 -0700587 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700588 }
589
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700590 if (fatal_signal) {
591 // Don't try to notify ActivityManager if it just crashed, or we might hang until timeout.
592 if (thread_info[target_process].thread_name != "system_server") {
593 activity_manager_notify(target_process, signo, amfd_data);
Josh Gao7c6e3132017-01-22 17:59:02 -0800594 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700595 }
596
597 if (wait_for_gdb) {
Josh Gao7c6e3132017-01-22 17:59:02 -0800598 // Use ALOGI to line up with output from engrave_tombstone.
599 ALOGI(
dimitry6429e202017-09-12 10:46:20 +0200600 "***********************************************************\n"
601 "* Process %d has been suspended while crashing.\n"
602 "* To attach gdbserver and start gdb, run this on the host:\n"
603 "*\n"
604 "* gdbclient.py -p %d\n"
605 "*\n"
606 "***********************************************************",
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700607 target_process, target_process);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700608 }
609
610 // Close stdout before we notify tombstoned of completion.
611 close(STDOUT_FILENO);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700612 if (g_tombstoned_connected && !tombstoned_notify_completion(g_tombstoned_socket.get())) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700613 LOG(ERROR) << "failed to notify tombstoned of completion";
614 }
615
616 return 0;
617}