blob: 93f7572365e909d02efe80d11dbd4d45fa8fc552 [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>
37#include <android-base/parseint.h>
38#include <android-base/properties.h>
39#include <android-base/stringprintf.h>
Josh Gao57f58f82017-03-15 23:23:22 -070040#include <android-base/strings.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070041#include <android-base/unique_fd.h>
42#include <cutils/sockets.h>
Vijay Venkatramana95acea2017-01-23 20:11:51 -080043#include <log/log.h>
Josh Gaob0e51e32017-06-01 12:08:10 -070044#include <private/android_filesystem_config.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070045#include <procinfo/process.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070046
Josh Gao8bb03902017-05-30 15:31:02 -070047#define ATRACE_TAG ATRACE_TAG_BIONIC
48#include <utils/Trace.h>
49
Josh Gao2b2ae0c2017-08-21 14:31:17 -070050#include <unwindstack/Regs.h>
51
Josh Gaoc3706662017-08-29 13:08:32 -070052#include "libdebuggerd/backtrace.h"
53#include "libdebuggerd/tombstone.h"
54#include "libdebuggerd/utility.h"
Josh Gaocbe70cb2016-10-18 18:17:52 -070055
56#include "debuggerd/handler.h"
Narayan Kamath2d377cd2017-05-10 10:58:59 +010057#include "tombstoned/tombstoned.h"
Josh Gaoc3706662017-08-29 13:08:32 -070058
59#include "protocol.h"
Narayan Kamath2d377cd2017-05-10 10:58:59 +010060#include "util.h"
Josh Gaocbe70cb2016-10-18 18:17:52 -070061
62using android::base::unique_fd;
63using android::base::StringPrintf;
Josh Gao57f58f82017-03-15 23:23:22 -070064
Josh Gao2b2ae0c2017-08-21 14:31:17 -070065using unwindstack::Regs;
Josh Gaocbe70cb2016-10-18 18:17:52 -070066
Josh Gaofe902762017-02-01 16:31:43 -080067static bool pid_contains_tid(int pid_proc_fd, pid_t tid) {
68 struct stat st;
69 std::string task_path = StringPrintf("task/%d", tid);
70 return fstatat(pid_proc_fd, task_path.c_str(), &st, 0) == 0;
Josh Gaocbe70cb2016-10-18 18:17:52 -070071}
72
Josh Gaofd13bf02017-08-18 15:37:26 -070073static pid_t get_tracer(pid_t tracee) {
74 // Check to see if the thread is being ptraced by another process.
75 android::procinfo::ProcessInfo process_info;
76 if (android::procinfo::GetProcessInfo(tracee, &process_info)) {
77 return process_info.tracer;
78 }
79 return -1;
80}
81
Josh Gaocbe70cb2016-10-18 18:17:52 -070082// Attach to a thread, and verify that it's still a member of the given process
Josh Gao2b2ae0c2017-08-21 14:31:17 -070083static bool ptrace_seize_thread(int pid_proc_fd, pid_t tid, std::string* error, int flags = 0) {
84 if (ptrace(PTRACE_SEIZE, tid, 0, flags) != 0) {
Josh Gaofd13bf02017-08-18 15:37:26 -070085 if (errno == EPERM) {
86 pid_t tracer = get_tracer(tid);
87 if (tracer != -1) {
88 *error = StringPrintf("failed to attach to thread %d, already traced by %d (%s)", tid,
89 tracer, get_process_name(tracer).c_str());
90 return false;
91 }
92 }
93
Josh Gao42fd74b2017-01-20 12:51:11 -080094 *error = StringPrintf("failed to attach to thread %d: %s", tid, strerror(errno));
Josh Gaocbe70cb2016-10-18 18:17:52 -070095 return false;
96 }
97
98 // Make sure that the task we attached to is actually part of the pid we're dumping.
Josh Gaofe902762017-02-01 16:31:43 -080099 if (!pid_contains_tid(pid_proc_fd, tid)) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700100 if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700101 PLOG(WARNING) << "failed to detach from thread " << tid;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700102 }
Josh Gaofe902762017-02-01 16:31:43 -0800103 *error = StringPrintf("thread %d is not in process", tid);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700104 return false;
105 }
Josh Gao122479f2017-01-22 16:42:32 -0800106
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700107 return true;
108}
109
110static bool wait_for_stop(pid_t tid, int* received_signal) {
111 while (true) {
112 int status;
113 pid_t result = waitpid(tid, &status, __WALL);
114 if (result != tid) {
115 PLOG(ERROR) << "waitpid failed on " << tid << " while detaching";
116 return false;
117 }
118
119 if (WIFSTOPPED(status)) {
120 if (status >> 16 == PTRACE_EVENT_STOP) {
121 *received_signal = 0;
122 } else {
123 *received_signal = WSTOPSIG(status);
124 }
125 return true;
126 }
127 }
128}
129
130// Interrupt a process and wait for it to be interrupted.
131static bool ptrace_interrupt(pid_t tid, int* received_signal) {
132 if (ptrace(PTRACE_INTERRUPT, tid, 0, 0) == 0) {
133 return wait_for_stop(tid, received_signal);
Josh Gao3407d7c2017-06-13 17:21:12 +0000134 }
135
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700136 PLOG(ERROR) << "failed to interrupt " << tid << " to detach";
137 return false;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700138}
139
Josh Gaob0e51e32017-06-01 12:08:10 -0700140static bool activity_manager_notify(pid_t pid, int signal, const std::string& amfd_data) {
Josh Gao8bb03902017-05-30 15:31:02 -0700141 ATRACE_CALL();
Josh Gaob0e51e32017-06-01 12:08:10 -0700142 android::base::unique_fd amfd(socket_local_client(
143 "/data/system/ndebugsocket", ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700144 if (amfd.get() == -1) {
145 PLOG(ERROR) << "unable to connect to activity manager";
146 return false;
147 }
148
149 struct timeval tv = {
150 .tv_sec = 1,
151 .tv_usec = 0,
152 };
153 if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
154 PLOG(ERROR) << "failed to set send timeout on activity manager socket";
155 return false;
156 }
157 tv.tv_sec = 3; // 3 seconds on handshake read
158 if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
159 PLOG(ERROR) << "failed to set receive timeout on activity manager socket";
160 return false;
161 }
162
163 // Activity Manager protocol: binary 32-bit network-byte-order ints for the
164 // pid and signal number, followed by the raw text of the dump, culminating
165 // in a zero byte that marks end-of-data.
166 uint32_t datum = htonl(pid);
167 if (!android::base::WriteFully(amfd, &datum, 4)) {
168 PLOG(ERROR) << "AM pid write failed";
169 return false;
170 }
171 datum = htonl(signal);
172 if (!android::base::WriteFully(amfd, &datum, 4)) {
173 PLOG(ERROR) << "AM signal write failed";
174 return false;
175 }
176 if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size() + 1)) {
177 PLOG(ERROR) << "AM data write failed";
178 return false;
179 }
180
181 // 3 sec timeout reading the ack; we're fine if the read fails.
182 char ack;
183 android::base::ReadFully(amfd, &ack, 1);
184 return true;
185}
186
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700187// Globals used by the abort handler.
188static pid_t g_target_thread = -1;
189static bool g_tombstoned_connected = false;
190static unique_fd g_tombstoned_socket;
191static unique_fd g_output_fd;
Josh Gao57594112017-01-22 17:41:15 -0800192
Josh Gao38ac45d2018-04-27 13:31:47 -0700193static void DefuseSignalHandlers() {
194 // Don't try to dump ourselves.
195 struct sigaction action = {};
196 action.sa_handler = SIG_DFL;
197 debuggerd_register_handlers(&action);
198
199 sigset_t mask;
200 sigemptyset(&mask);
201 if (sigprocmask(SIG_SETMASK, &mask, nullptr) != 0) {
202 PLOG(FATAL) << "failed to set signal mask";
203 }
204}
205
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700206static void Initialize(char** argv) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700207 android::base::InitLogging(argv);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700208 android::base::SetAborter([](const char* abort_msg) {
209 // If we abort before we get an output fd, contact tombstoned to let any
210 // potential listeners know that we failed.
211 if (!g_tombstoned_connected) {
212 if (!tombstoned_connect(g_target_thread, &g_tombstoned_socket, &g_output_fd,
213 kDebuggerdAnyIntercept)) {
214 // We failed to connect, not much we can do.
215 LOG(ERROR) << "failed to connected to tombstoned to report failure";
216 _exit(1);
217 }
218 }
219
220 dprintf(g_output_fd.get(), "crash_dump failed to dump process");
221 if (g_target_thread != 1) {
222 dprintf(g_output_fd.get(), " %d: %s\n", g_target_thread, abort_msg);
223 } else {
224 dprintf(g_output_fd.get(), ": %s\n", abort_msg);
225 }
226
227 _exit(1);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700228 });
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700229}
Josh Gaoe7402502017-06-01 11:55:25 -0700230
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700231static void ParseArgs(int argc, char** argv, pid_t* pseudothread_tid, DebuggerdDumpType* dump_type) {
Narayan Kamatha73df602017-05-24 15:07:25 +0100232 if (argc != 4) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700233 LOG(FATAL) << "wrong number of args: " << argc << " (expected 4)";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700234 }
235
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700236 if (!android::base::ParseInt(argv[1], &g_target_thread, 1, std::numeric_limits<pid_t>::max())) {
237 LOG(FATAL) << "invalid target tid: " << argv[1];
Josh Gaocbe70cb2016-10-18 18:17:52 -0700238 }
239
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700240 if (!android::base::ParseInt(argv[2], pseudothread_tid, 1, std::numeric_limits<pid_t>::max())) {
Josh Gaof6ad5852017-02-15 12:21:11 -0800241 LOG(FATAL) << "invalid pseudothread tid: " << argv[2];
Josh Gao2f11a252017-02-13 14:46:19 -0800242 }
243
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700244 int dump_type_int;
245 if (!android::base::ParseInt(argv[3], &dump_type_int, 0, 1)) {
Narayan Kamatha73df602017-05-24 15:07:25 +0100246 LOG(FATAL) << "invalid requested dump type: " << argv[3];
247 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700248 *dump_type = static_cast<DebuggerdDumpType>(dump_type_int);
249}
Narayan Kamatha73df602017-05-24 15:07:25 +0100250
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700251static void ReadCrashInfo(unique_fd& fd, siginfo_t* siginfo,
Josh Gao9da1f512018-08-06 15:38:29 -0700252 std::unique_ptr<unwindstack::Regs>* regs, uintptr_t* abort_msg_address,
253 uintptr_t* fdsan_table_address) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700254 std::aligned_storage<sizeof(CrashInfo) + 1, alignof(CrashInfo)>::type buf;
Josh Gao9da1f512018-08-06 15:38:29 -0700255 CrashInfo* crash_info = reinterpret_cast<CrashInfo*>(&buf);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700256 ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), &buf, sizeof(buf)));
257 if (rc == -1) {
258 PLOG(FATAL) << "failed to read target ucontext";
Josh Gao9da1f512018-08-06 15:38:29 -0700259 } else {
260 ssize_t expected_size = 0;
261 switch (crash_info->header.version) {
262 case 1:
263 expected_size = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataV1);
264 break;
265
266 case 2:
267 expected_size = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataV2);
268 break;
269
270 default:
271 LOG(FATAL) << "unexpected CrashInfo version: " << crash_info->header.version;
272 break;
273 };
274
275 if (rc != expected_size) {
276 LOG(FATAL) << "read " << rc << " bytes when reading target crash information, expected "
277 << expected_size;
278 }
Josh Gaoc7fe0602017-03-13 14:13:29 -0700279 }
280
Josh Gao9da1f512018-08-06 15:38:29 -0700281 *fdsan_table_address = 0;
282 switch (crash_info->header.version) {
283 case 2:
284 *fdsan_table_address = crash_info->data.v2.fdsan_table_address;
285 case 1:
286 *abort_msg_address = crash_info->data.v1.abort_msg_address;
287 *siginfo = crash_info->data.v1.siginfo;
288 regs->reset(Regs::CreateFromUcontext(Regs::CurrentArch(), &crash_info->data.v1.ucontext));
289 break;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700290
Josh Gao9da1f512018-08-06 15:38:29 -0700291 default:
292 __builtin_unreachable();
293 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700294}
295
296// Wait for a process to clone and return the child's pid.
297// Note: this leaves the parent in PTRACE_EVENT_STOP.
298static pid_t wait_for_clone(pid_t pid, bool resume_child) {
299 int status;
300 pid_t result = TEMP_FAILURE_RETRY(waitpid(pid, &status, __WALL));
301 if (result == -1) {
302 PLOG(FATAL) << "failed to waitpid";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700303 }
304
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700305 if (WIFEXITED(status)) {
306 LOG(FATAL) << "traced process exited with status " << WEXITSTATUS(status);
307 } else if (WIFSIGNALED(status)) {
308 LOG(FATAL) << "traced process exited with signal " << WTERMSIG(status);
309 } else if (!WIFSTOPPED(status)) {
310 LOG(FATAL) << "process didn't stop? (status = " << status << ")";
311 }
312
313 if (status >> 8 != (SIGTRAP | (PTRACE_EVENT_CLONE << 8))) {
314 LOG(FATAL) << "process didn't stop due to PTRACE_O_TRACECLONE (status = " << status << ")";
315 }
316
317 pid_t child;
318 if (ptrace(PTRACE_GETEVENTMSG, pid, 0, &child) != 0) {
319 PLOG(FATAL) << "failed to get child pid via PTRACE_GETEVENTMSG";
320 }
321
322 int stop_signal;
323 if (!wait_for_stop(child, &stop_signal)) {
324 PLOG(FATAL) << "failed to waitpid on child";
325 }
326
327 CHECK_EQ(0, stop_signal);
328
329 if (resume_child) {
330 if (ptrace(PTRACE_CONT, child, 0, 0) != 0) {
331 PLOG(FATAL) << "failed to resume child (pid = " << child << ")";
332 }
333 }
334
335 return child;
336}
337
338static pid_t wait_for_vm_process(pid_t pseudothread_tid) {
339 // The pseudothread will double-fork, we want its grandchild.
340 pid_t intermediate = wait_for_clone(pseudothread_tid, true);
341 pid_t vm_pid = wait_for_clone(intermediate, false);
342 if (ptrace(PTRACE_DETACH, intermediate, 0, 0) != 0) {
343 PLOG(FATAL) << "failed to detach from intermediate vm process";
344 }
345
346 return vm_pid;
347}
348
349int main(int argc, char** argv) {
Josh Gao38ac45d2018-04-27 13:31:47 -0700350 DefuseSignalHandlers();
351
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700352 atrace_begin(ATRACE_TAG, "before reparent");
353 pid_t target_process = getppid();
354
355 // Open /proc/`getppid()` before we daemonize.
356 std::string target_proc_path = "/proc/" + std::to_string(target_process);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700357 int target_proc_fd = open(target_proc_path.c_str(), O_DIRECTORY | O_RDONLY);
358 if (target_proc_fd == -1) {
359 PLOG(FATAL) << "failed to open " << target_proc_path;
360 }
361
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700362 // Make sure getppid() hasn't changed.
363 if (getppid() != target_process) {
364 LOG(FATAL) << "parent died";
Josh Gao2a18b822017-02-16 19:17:28 -0800365 }
Josh Gao8bb03902017-05-30 15:31:02 -0700366 atrace_end(ATRACE_TAG);
367
Josh Gaocbe70cb2016-10-18 18:17:52 -0700368 // Reparent ourselves to init, so that the signal handler can waitpid on the
369 // original process to avoid leaving a zombie for non-fatal dumps.
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700370 // Move the input/output pipes off of stdout/stderr, out of paranoia.
371 unique_fd output_pipe(dup(STDOUT_FILENO));
372 unique_fd input_pipe(dup(STDIN_FILENO));
373
374 unique_fd fork_exit_read, fork_exit_write;
375 if (!Pipe(&fork_exit_read, &fork_exit_write)) {
376 PLOG(FATAL) << "failed to create pipe";
377 }
378
Josh Gaocbe70cb2016-10-18 18:17:52 -0700379 pid_t forkpid = fork();
380 if (forkpid == -1) {
381 PLOG(FATAL) << "fork failed";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700382 } else if (forkpid == 0) {
383 fork_exit_read.reset();
384 } else {
385 // We need the pseudothread to live until we get around to verifying the vm pid against it.
386 // The last thing it does is block on a waitpid on us, so wait until our child tells us to die.
387 fork_exit_write.reset();
388 char buf;
389 TEMP_FAILURE_RETRY(read(fork_exit_read.get(), &buf, sizeof(buf)));
390 _exit(0);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700391 }
392
Josh Gao8bb03902017-05-30 15:31:02 -0700393 ATRACE_NAME("after reparent");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700394 pid_t pseudothread_tid;
395 DebuggerdDumpType dump_type;
Josh Gao9da1f512018-08-06 15:38:29 -0700396 uintptr_t abort_msg_address = 0;
397 uintptr_t fdsan_table_address = 0;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700398
399 Initialize(argv);
400 ParseArgs(argc, argv, &pseudothread_tid, &dump_type);
Josh Gao8bb03902017-05-30 15:31:02 -0700401
Josh Gao7c6e3132017-01-22 17:59:02 -0800402 // Die if we take too long.
Andreas Gampeb02851a2017-06-22 19:45:53 -0700403 //
404 // Note: processes with many threads and minidebug-info can take a bit to
405 // unwind, do not make this too small. b/62828735
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700406 alarm(30);
Josh Gao7c6e3132017-01-22 17:59:02 -0800407
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700408 // Get the process name (aka cmdline).
409 std::string process_name = get_process_name(g_target_thread);
Josh Gao2f11a252017-02-13 14:46:19 -0800410
411 // Collect the list of open files.
412 OpenFilesList open_files;
Josh Gao8bb03902017-05-30 15:31:02 -0700413 {
414 ATRACE_NAME("open files");
Josh Gaoce841d92018-08-06 18:26:42 -0700415 populate_open_files_list(&open_files, g_target_thread);
Josh Gao8bb03902017-05-30 15:31:02 -0700416 }
Josh Gao2f11a252017-02-13 14:46:19 -0800417
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700418 // In order to reduce the duration that we pause the process for, we ptrace
419 // the threads, fetch their registers and associated information, and then
420 // fork a separate process as a snapshot of the process's address space.
421 std::set<pid_t> threads;
422 if (!android::procinfo::GetProcessTids(g_target_thread, &threads)) {
423 PLOG(FATAL) << "failed to get process threads";
424 }
Josh Gao3407d7c2017-06-13 17:21:12 +0000425
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700426 std::map<pid_t, ThreadInfo> thread_info;
427 siginfo_t siginfo;
428 std::string error;
429
430 {
431 ATRACE_NAME("ptrace");
432 for (pid_t thread : threads) {
433 // Trace the pseudothread separately, so we can use different options.
434 if (thread == pseudothread_tid) {
435 continue;
436 }
437
438 if (!ptrace_seize_thread(target_proc_fd, thread, &error)) {
439 bool fatal = thread == g_target_thread;
440 LOG(fatal ? FATAL : WARNING) << error;
441 }
442
443 ThreadInfo info;
444 info.pid = target_process;
445 info.tid = thread;
446 info.process_name = process_name;
447 info.thread_name = get_thread_name(thread);
448
449 if (!ptrace_interrupt(thread, &info.signo)) {
450 PLOG(WARNING) << "failed to ptrace interrupt thread " << thread;
451 ptrace(PTRACE_DETACH, thread, 0, 0);
452 continue;
453 }
454
455 if (thread == g_target_thread) {
456 // Read the thread's registers along with the rest of the crash info out of the pipe.
Josh Gao9da1f512018-08-06 15:38:29 -0700457 ReadCrashInfo(input_pipe, &siginfo, &info.registers, &abort_msg_address,
458 &fdsan_table_address);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700459 info.siginfo = &siginfo;
460 info.signo = info.siginfo->si_signo;
461 } else {
462 info.registers.reset(Regs::RemoteGet(thread));
463 if (!info.registers) {
464 PLOG(WARNING) << "failed to fetch registers for thread " << thread;
465 ptrace(PTRACE_DETACH, thread, 0, 0);
466 continue;
467 }
468 }
469
470 thread_info[thread] = std::move(info);
471 }
472 }
473
474 // Trace the pseudothread with PTRACE_O_TRACECLONE and tell it to fork.
475 if (!ptrace_seize_thread(target_proc_fd, pseudothread_tid, &error, PTRACE_O_TRACECLONE)) {
476 LOG(FATAL) << "failed to seize pseudothread: " << error;
477 }
478
479 if (TEMP_FAILURE_RETRY(write(output_pipe.get(), "\1", 1)) != 1) {
480 PLOG(FATAL) << "failed to write to pseudothread";
481 }
482
483 pid_t vm_pid = wait_for_vm_process(pseudothread_tid);
484 if (ptrace(PTRACE_DETACH, pseudothread_tid, 0, 0) != 0) {
485 PLOG(FATAL) << "failed to detach from pseudothread";
486 }
487
488 // The pseudothread can die now.
489 fork_exit_write.reset();
490
491 // Defer the message until later, for readability.
492 bool wait_for_gdb = android::base::GetBoolProperty("debug.debuggerd.wait_for_gdb", false);
493 if (siginfo.si_signo == DEBUGGER_SIGNAL) {
494 wait_for_gdb = false;
495 }
496
497 // Detach from all of our attached threads before resuming.
498 for (const auto& [tid, thread] : thread_info) {
499 int resume_signal = thread.signo == DEBUGGER_SIGNAL ? 0 : thread.signo;
500 if (wait_for_gdb) {
501 resume_signal = 0;
502 if (tgkill(target_process, tid, SIGSTOP) != 0) {
503 PLOG(WARNING) << "failed to send SIGSTOP to " << tid;
504 }
505 }
506
507 LOG(DEBUG) << "detaching from thread " << tid;
508 if (ptrace(PTRACE_DETACH, tid, 0, resume_signal) != 0) {
509 PLOG(ERROR) << "failed to detach from thread " << tid;
510 }
511 }
512
513 // Drop our capabilities now that we've fetched all of the information we need.
Josh Gao2f11a252017-02-13 14:46:19 -0800514 drop_capabilities();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700515
Josh Gao8bb03902017-05-30 15:31:02 -0700516 {
517 ATRACE_NAME("tombstoned_connect");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700518 LOG(INFO) << "obtaining output fd from tombstoned, type: " << dump_type;
519 g_tombstoned_connected =
520 tombstoned_connect(g_target_thread, &g_tombstoned_socket, &g_output_fd, dump_type);
Josh Gao8bb03902017-05-30 15:31:02 -0700521 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700522
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700523 if (g_tombstoned_connected) {
524 if (TEMP_FAILURE_RETRY(dup2(g_output_fd.get(), STDOUT_FILENO)) == -1) {
525 PLOG(ERROR) << "failed to dup2 output fd (" << g_output_fd.get() << ") to STDOUT_FILENO";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700526 }
527 } else {
528 unique_fd devnull(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR)));
529 TEMP_FAILURE_RETRY(dup2(devnull.get(), STDOUT_FILENO));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700530 g_output_fd = std::move(devnull);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700531 }
532
Josh Gao9da1f512018-08-06 15:38:29 -0700533 LOG(INFO) << "performing dump of process " << target_process
534 << " (target tid = " << g_target_thread << ")";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700535
536 int signo = siginfo.si_signo;
Josh Gaofe902762017-02-01 16:31:43 -0800537 bool fatal_signal = signo != DEBUGGER_SIGNAL;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700538 bool backtrace = false;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700539
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700540 // si_value is special when used with DEBUGGER_SIGNAL.
Josh Gaocbe70cb2016-10-18 18:17:52 -0700541 // 0: dump tombstone
542 // 1: dump backtrace
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700543 if (!fatal_signal) {
544 int si_val = siginfo.si_value.sival_int;
545 if (si_val == 0) {
546 backtrace = false;
547 } else if (si_val == 1) {
548 backtrace = true;
549 } else {
550 LOG(WARNING) << "unknown si_value value " << si_val;
551 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700552 }
553
Josh Gaocbe70cb2016-10-18 18:17:52 -0700554 // TODO: Use seccomp to lock ourselves down.
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700555 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(vm_pid, false));
556 if (!map) {
557 LOG(FATAL) << "failed to create backtrace map";
558 }
559
560 std::shared_ptr<unwindstack::Memory> process_memory = map->GetProcessMemory();
561 if (!process_memory) {
562 LOG(FATAL) << "failed to get unwindstack::Memory handle";
563 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700564
Josh Gaocbe70cb2016-10-18 18:17:52 -0700565 std::string amfd_data;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700566 if (backtrace) {
Josh Gao8bb03902017-05-30 15:31:02 -0700567 ATRACE_NAME("dump_backtrace");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700568 dump_backtrace(std::move(g_output_fd), map.get(), thread_info, g_target_thread);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700569 } else {
Josh Gaoce841d92018-08-06 18:26:42 -0700570 {
571 ATRACE_NAME("fdsan table dump");
572 populate_fdsan_table(&open_files, process_memory, fdsan_table_address);
573 }
574
575 {
576 ATRACE_NAME("engrave_tombstone");
577 engrave_tombstone(std::move(g_output_fd), map.get(), process_memory.get(), thread_info,
578 g_target_thread, abort_msg_address, &open_files, &amfd_data);
579 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700580 }
581
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700582 if (fatal_signal) {
583 // Don't try to notify ActivityManager if it just crashed, or we might hang until timeout.
584 if (thread_info[target_process].thread_name != "system_server") {
585 activity_manager_notify(target_process, signo, amfd_data);
Josh Gao7c6e3132017-01-22 17:59:02 -0800586 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700587 }
588
589 if (wait_for_gdb) {
Josh Gao7c6e3132017-01-22 17:59:02 -0800590 // Use ALOGI to line up with output from engrave_tombstone.
591 ALOGI(
dimitry6429e202017-09-12 10:46:20 +0200592 "***********************************************************\n"
593 "* Process %d has been suspended while crashing.\n"
594 "* To attach gdbserver and start gdb, run this on the host:\n"
595 "*\n"
596 "* gdbclient.py -p %d\n"
597 "*\n"
598 "***********************************************************",
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700599 target_process, target_process);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700600 }
601
602 // Close stdout before we notify tombstoned of completion.
603 close(STDOUT_FILENO);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700604 if (g_tombstoned_connected && !tombstoned_notify_completion(g_tombstoned_socket.get())) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700605 LOG(ERROR) << "failed to notify tombstoned of completion";
606 }
607
608 return 0;
609}