blob: c608a8c333315ad162519b8222d3dfce271e26c7 [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
David Srbeckyb9cc4fb2019-04-05 18:23:32 +000051#include <unwindstack/DexFiles.h>
Christopher Ferris60eb1972019-01-15 15:18:43 -080052#include <unwindstack/JitDebug.h>
53#include <unwindstack/Maps.h>
54#include <unwindstack/Memory.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070055#include <unwindstack/Regs.h>
Christopher Ferris60eb1972019-01-15 15:18:43 -080056#include <unwindstack/Unwinder.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070057
Josh Gaoc3706662017-08-29 13:08:32 -070058#include "libdebuggerd/backtrace.h"
59#include "libdebuggerd/tombstone.h"
60#include "libdebuggerd/utility.h"
Josh Gaocbe70cb2016-10-18 18:17:52 -070061
62#include "debuggerd/handler.h"
Narayan Kamath2d377cd2017-05-10 10:58:59 +010063#include "tombstoned/tombstoned.h"
Josh Gaoc3706662017-08-29 13:08:32 -070064
65#include "protocol.h"
Narayan Kamath2d377cd2017-05-10 10:58:59 +010066#include "util.h"
Josh Gaocbe70cb2016-10-18 18:17:52 -070067
68using android::base::unique_fd;
69using android::base::StringPrintf;
Josh Gao57f58f82017-03-15 23:23:22 -070070
Josh Gaofe902762017-02-01 16:31:43 -080071static bool pid_contains_tid(int pid_proc_fd, pid_t tid) {
72 struct stat st;
73 std::string task_path = StringPrintf("task/%d", tid);
74 return fstatat(pid_proc_fd, task_path.c_str(), &st, 0) == 0;
Josh Gaocbe70cb2016-10-18 18:17:52 -070075}
76
Josh Gaofd13bf02017-08-18 15:37:26 -070077static pid_t get_tracer(pid_t tracee) {
78 // Check to see if the thread is being ptraced by another process.
79 android::procinfo::ProcessInfo process_info;
80 if (android::procinfo::GetProcessInfo(tracee, &process_info)) {
81 return process_info.tracer;
82 }
83 return -1;
84}
85
Josh Gaocbe70cb2016-10-18 18:17:52 -070086// Attach to a thread, and verify that it's still a member of the given process
Josh Gao2b2ae0c2017-08-21 14:31:17 -070087static bool ptrace_seize_thread(int pid_proc_fd, pid_t tid, std::string* error, int flags = 0) {
88 if (ptrace(PTRACE_SEIZE, tid, 0, flags) != 0) {
Josh Gaofd13bf02017-08-18 15:37:26 -070089 if (errno == EPERM) {
90 pid_t tracer = get_tracer(tid);
91 if (tracer != -1) {
92 *error = StringPrintf("failed to attach to thread %d, already traced by %d (%s)", tid,
93 tracer, get_process_name(tracer).c_str());
94 return false;
95 }
96 }
97
Josh Gao42fd74b2017-01-20 12:51:11 -080098 *error = StringPrintf("failed to attach to thread %d: %s", tid, strerror(errno));
Josh Gaocbe70cb2016-10-18 18:17:52 -070099 return false;
100 }
101
102 // Make sure that the task we attached to is actually part of the pid we're dumping.
Josh Gaofe902762017-02-01 16:31:43 -0800103 if (!pid_contains_tid(pid_proc_fd, tid)) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700104 if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700105 PLOG(WARNING) << "failed to detach from thread " << tid;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700106 }
Josh Gaofe902762017-02-01 16:31:43 -0800107 *error = StringPrintf("thread %d is not in process", tid);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700108 return false;
109 }
Josh Gao122479f2017-01-22 16:42:32 -0800110
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700111 return true;
112}
113
114static bool wait_for_stop(pid_t tid, int* received_signal) {
115 while (true) {
116 int status;
117 pid_t result = waitpid(tid, &status, __WALL);
118 if (result != tid) {
119 PLOG(ERROR) << "waitpid failed on " << tid << " while detaching";
120 return false;
121 }
122
123 if (WIFSTOPPED(status)) {
124 if (status >> 16 == PTRACE_EVENT_STOP) {
125 *received_signal = 0;
126 } else {
127 *received_signal = WSTOPSIG(status);
128 }
129 return true;
130 }
131 }
132}
133
134// Interrupt a process and wait for it to be interrupted.
135static bool ptrace_interrupt(pid_t tid, int* received_signal) {
136 if (ptrace(PTRACE_INTERRUPT, tid, 0, 0) == 0) {
137 return wait_for_stop(tid, received_signal);
Josh Gao3407d7c2017-06-13 17:21:12 +0000138 }
139
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700140 PLOG(ERROR) << "failed to interrupt " << tid << " to detach";
141 return false;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700142}
143
Josh Gaob0e51e32017-06-01 12:08:10 -0700144static bool activity_manager_notify(pid_t pid, int signal, const std::string& amfd_data) {
Josh Gao8bb03902017-05-30 15:31:02 -0700145 ATRACE_CALL();
Josh Gaob0e51e32017-06-01 12:08:10 -0700146 android::base::unique_fd amfd(socket_local_client(
147 "/data/system/ndebugsocket", ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700148 if (amfd.get() == -1) {
149 PLOG(ERROR) << "unable to connect to activity manager";
150 return false;
151 }
152
153 struct timeval tv = {
154 .tv_sec = 1,
155 .tv_usec = 0,
156 };
157 if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
158 PLOG(ERROR) << "failed to set send timeout on activity manager socket";
159 return false;
160 }
161 tv.tv_sec = 3; // 3 seconds on handshake read
162 if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
163 PLOG(ERROR) << "failed to set receive timeout on activity manager socket";
164 return false;
165 }
166
167 // Activity Manager protocol: binary 32-bit network-byte-order ints for the
168 // pid and signal number, followed by the raw text of the dump, culminating
169 // in a zero byte that marks end-of-data.
170 uint32_t datum = htonl(pid);
171 if (!android::base::WriteFully(amfd, &datum, 4)) {
172 PLOG(ERROR) << "AM pid write failed";
173 return false;
174 }
175 datum = htonl(signal);
176 if (!android::base::WriteFully(amfd, &datum, 4)) {
177 PLOG(ERROR) << "AM signal write failed";
178 return false;
179 }
180 if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size() + 1)) {
181 PLOG(ERROR) << "AM data write failed";
182 return false;
183 }
184
185 // 3 sec timeout reading the ack; we're fine if the read fails.
186 char ack;
187 android::base::ReadFully(amfd, &ack, 1);
188 return true;
189}
190
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700191// Globals used by the abort handler.
192static pid_t g_target_thread = -1;
193static bool g_tombstoned_connected = false;
194static unique_fd g_tombstoned_socket;
195static unique_fd g_output_fd;
Josh Gao57594112017-01-22 17:41:15 -0800196
Josh Gao38ac45d2018-04-27 13:31:47 -0700197static void DefuseSignalHandlers() {
198 // Don't try to dump ourselves.
199 struct sigaction action = {};
200 action.sa_handler = SIG_DFL;
201 debuggerd_register_handlers(&action);
202
203 sigset_t mask;
204 sigemptyset(&mask);
205 if (sigprocmask(SIG_SETMASK, &mask, nullptr) != 0) {
206 PLOG(FATAL) << "failed to set signal mask";
207 }
208}
209
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700210static void Initialize(char** argv) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700211 android::base::InitLogging(argv);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700212 android::base::SetAborter([](const char* abort_msg) {
213 // If we abort before we get an output fd, contact tombstoned to let any
214 // potential listeners know that we failed.
215 if (!g_tombstoned_connected) {
216 if (!tombstoned_connect(g_target_thread, &g_tombstoned_socket, &g_output_fd,
217 kDebuggerdAnyIntercept)) {
218 // We failed to connect, not much we can do.
219 LOG(ERROR) << "failed to connected to tombstoned to report failure";
220 _exit(1);
221 }
222 }
223
224 dprintf(g_output_fd.get(), "crash_dump failed to dump process");
225 if (g_target_thread != 1) {
226 dprintf(g_output_fd.get(), " %d: %s\n", g_target_thread, abort_msg);
227 } else {
228 dprintf(g_output_fd.get(), ": %s\n", abort_msg);
229 }
230
231 _exit(1);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700232 });
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700233}
Josh Gaoe7402502017-06-01 11:55:25 -0700234
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700235static void ParseArgs(int argc, char** argv, pid_t* pseudothread_tid, DebuggerdDumpType* dump_type) {
Narayan Kamatha73df602017-05-24 15:07:25 +0100236 if (argc != 4) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700237 LOG(FATAL) << "wrong number of args: " << argc << " (expected 4)";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700238 }
239
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700240 if (!android::base::ParseInt(argv[1], &g_target_thread, 1, std::numeric_limits<pid_t>::max())) {
241 LOG(FATAL) << "invalid target tid: " << argv[1];
Josh Gaocbe70cb2016-10-18 18:17:52 -0700242 }
243
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700244 if (!android::base::ParseInt(argv[2], pseudothread_tid, 1, std::numeric_limits<pid_t>::max())) {
Josh Gaof6ad5852017-02-15 12:21:11 -0800245 LOG(FATAL) << "invalid pseudothread tid: " << argv[2];
Josh Gao2f11a252017-02-13 14:46:19 -0800246 }
247
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700248 int dump_type_int;
249 if (!android::base::ParseInt(argv[3], &dump_type_int, 0, 1)) {
Narayan Kamatha73df602017-05-24 15:07:25 +0100250 LOG(FATAL) << "invalid requested dump type: " << argv[3];
251 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700252 *dump_type = static_cast<DebuggerdDumpType>(dump_type_int);
253}
Narayan Kamatha73df602017-05-24 15:07:25 +0100254
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700255static void ReadCrashInfo(unique_fd& fd, siginfo_t* siginfo,
Josh Gao9da1f512018-08-06 15:38:29 -0700256 std::unique_ptr<unwindstack::Regs>* regs, uintptr_t* abort_msg_address,
257 uintptr_t* fdsan_table_address) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700258 std::aligned_storage<sizeof(CrashInfo) + 1, alignof(CrashInfo)>::type buf;
Josh Gao9da1f512018-08-06 15:38:29 -0700259 CrashInfo* crash_info = reinterpret_cast<CrashInfo*>(&buf);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700260 ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), &buf, sizeof(buf)));
261 if (rc == -1) {
262 PLOG(FATAL) << "failed to read target ucontext";
Josh Gao9da1f512018-08-06 15:38:29 -0700263 } else {
264 ssize_t expected_size = 0;
265 switch (crash_info->header.version) {
266 case 1:
267 expected_size = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataV1);
268 break;
269
270 case 2:
271 expected_size = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataV2);
272 break;
273
274 default:
275 LOG(FATAL) << "unexpected CrashInfo version: " << crash_info->header.version;
276 break;
277 };
278
279 if (rc != expected_size) {
280 LOG(FATAL) << "read " << rc << " bytes when reading target crash information, expected "
281 << expected_size;
282 }
Josh Gaoc7fe0602017-03-13 14:13:29 -0700283 }
284
Josh Gao9da1f512018-08-06 15:38:29 -0700285 *fdsan_table_address = 0;
286 switch (crash_info->header.version) {
287 case 2:
288 *fdsan_table_address = crash_info->data.v2.fdsan_table_address;
Josh Gao8d44b142018-09-18 13:22:22 -0700289 FALLTHROUGH_INTENDED;
Josh Gao9da1f512018-08-06 15:38:29 -0700290 case 1:
291 *abort_msg_address = crash_info->data.v1.abort_msg_address;
292 *siginfo = crash_info->data.v1.siginfo;
Christopher Ferris60eb1972019-01-15 15:18:43 -0800293 regs->reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(),
294 &crash_info->data.v1.ucontext));
Josh Gao9da1f512018-08-06 15:38:29 -0700295 break;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700296
Josh Gao9da1f512018-08-06 15:38:29 -0700297 default:
298 __builtin_unreachable();
299 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700300}
301
302// Wait for a process to clone and return the child's pid.
303// Note: this leaves the parent in PTRACE_EVENT_STOP.
304static pid_t wait_for_clone(pid_t pid, bool resume_child) {
305 int status;
306 pid_t result = TEMP_FAILURE_RETRY(waitpid(pid, &status, __WALL));
307 if (result == -1) {
308 PLOG(FATAL) << "failed to waitpid";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700309 }
310
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700311 if (WIFEXITED(status)) {
312 LOG(FATAL) << "traced process exited with status " << WEXITSTATUS(status);
313 } else if (WIFSIGNALED(status)) {
314 LOG(FATAL) << "traced process exited with signal " << WTERMSIG(status);
315 } else if (!WIFSTOPPED(status)) {
316 LOG(FATAL) << "process didn't stop? (status = " << status << ")";
317 }
318
319 if (status >> 8 != (SIGTRAP | (PTRACE_EVENT_CLONE << 8))) {
320 LOG(FATAL) << "process didn't stop due to PTRACE_O_TRACECLONE (status = " << status << ")";
321 }
322
323 pid_t child;
324 if (ptrace(PTRACE_GETEVENTMSG, pid, 0, &child) != 0) {
325 PLOG(FATAL) << "failed to get child pid via PTRACE_GETEVENTMSG";
326 }
327
328 int stop_signal;
329 if (!wait_for_stop(child, &stop_signal)) {
330 PLOG(FATAL) << "failed to waitpid on child";
331 }
332
333 CHECK_EQ(0, stop_signal);
334
335 if (resume_child) {
336 if (ptrace(PTRACE_CONT, child, 0, 0) != 0) {
337 PLOG(FATAL) << "failed to resume child (pid = " << child << ")";
338 }
339 }
340
341 return child;
342}
343
344static pid_t wait_for_vm_process(pid_t pseudothread_tid) {
345 // The pseudothread will double-fork, we want its grandchild.
346 pid_t intermediate = wait_for_clone(pseudothread_tid, true);
347 pid_t vm_pid = wait_for_clone(intermediate, false);
348 if (ptrace(PTRACE_DETACH, intermediate, 0, 0) != 0) {
349 PLOG(FATAL) << "failed to detach from intermediate vm process";
350 }
351
352 return vm_pid;
353}
354
Jinguang Dong8ac2f272018-11-24 17:12:33 +0800355static void InstallSigPipeHandler() {
356 struct sigaction action = {};
357 action.sa_handler = SIG_IGN;
358 action.sa_flags = SA_RESTART;
359 sigaction(SIGPIPE, &action, nullptr);
360}
361
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700362int main(int argc, char** argv) {
Josh Gao38ac45d2018-04-27 13:31:47 -0700363 DefuseSignalHandlers();
Jinguang Dong8ac2f272018-11-24 17:12:33 +0800364 InstallSigPipeHandler();
Josh Gao38ac45d2018-04-27 13:31:47 -0700365
Josh Gao18cb6812019-04-16 13:17:08 -0700366 // There appears to be a bug in the kernel where our death causes SIGHUP to
367 // be sent to our process group if we exit while it has stopped jobs (e.g.
368 // because of wait_for_gdb). Use setsid to create a new process group to
369 // avoid hitting this.
370 setsid();
371
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700372 atrace_begin(ATRACE_TAG, "before reparent");
373 pid_t target_process = getppid();
374
375 // Open /proc/`getppid()` before we daemonize.
376 std::string target_proc_path = "/proc/" + std::to_string(target_process);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700377 int target_proc_fd = open(target_proc_path.c_str(), O_DIRECTORY | O_RDONLY);
378 if (target_proc_fd == -1) {
379 PLOG(FATAL) << "failed to open " << target_proc_path;
380 }
381
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700382 // Make sure getppid() hasn't changed.
383 if (getppid() != target_process) {
384 LOG(FATAL) << "parent died";
Josh Gao2a18b822017-02-16 19:17:28 -0800385 }
Josh Gao8bb03902017-05-30 15:31:02 -0700386 atrace_end(ATRACE_TAG);
387
Josh Gaocbe70cb2016-10-18 18:17:52 -0700388 // Reparent ourselves to init, so that the signal handler can waitpid on the
389 // original process to avoid leaving a zombie for non-fatal dumps.
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700390 // Move the input/output pipes off of stdout/stderr, out of paranoia.
391 unique_fd output_pipe(dup(STDOUT_FILENO));
392 unique_fd input_pipe(dup(STDIN_FILENO));
393
394 unique_fd fork_exit_read, fork_exit_write;
395 if (!Pipe(&fork_exit_read, &fork_exit_write)) {
396 PLOG(FATAL) << "failed to create pipe";
397 }
398
Josh Gaocbe70cb2016-10-18 18:17:52 -0700399 pid_t forkpid = fork();
400 if (forkpid == -1) {
401 PLOG(FATAL) << "fork failed";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700402 } else if (forkpid == 0) {
403 fork_exit_read.reset();
404 } else {
405 // We need the pseudothread to live until we get around to verifying the vm pid against it.
406 // The last thing it does is block on a waitpid on us, so wait until our child tells us to die.
407 fork_exit_write.reset();
408 char buf;
409 TEMP_FAILURE_RETRY(read(fork_exit_read.get(), &buf, sizeof(buf)));
410 _exit(0);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700411 }
412
Josh Gao8bb03902017-05-30 15:31:02 -0700413 ATRACE_NAME("after reparent");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700414 pid_t pseudothread_tid;
415 DebuggerdDumpType dump_type;
Josh Gao9da1f512018-08-06 15:38:29 -0700416 uintptr_t abort_msg_address = 0;
417 uintptr_t fdsan_table_address = 0;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700418
419 Initialize(argv);
420 ParseArgs(argc, argv, &pseudothread_tid, &dump_type);
Josh Gao8bb03902017-05-30 15:31:02 -0700421
Josh Gao7c6e3132017-01-22 17:59:02 -0800422 // Die if we take too long.
Andreas Gampeb02851a2017-06-22 19:45:53 -0700423 //
424 // Note: processes with many threads and minidebug-info can take a bit to
425 // unwind, do not make this too small. b/62828735
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700426 alarm(30);
Josh Gao7c6e3132017-01-22 17:59:02 -0800427
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700428 // Get the process name (aka cmdline).
429 std::string process_name = get_process_name(g_target_thread);
Josh Gao2f11a252017-02-13 14:46:19 -0800430
431 // Collect the list of open files.
432 OpenFilesList open_files;
Josh Gao8bb03902017-05-30 15:31:02 -0700433 {
434 ATRACE_NAME("open files");
Josh Gaoce841d92018-08-06 18:26:42 -0700435 populate_open_files_list(&open_files, g_target_thread);
Josh Gao8bb03902017-05-30 15:31:02 -0700436 }
Josh Gao2f11a252017-02-13 14:46:19 -0800437
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700438 // In order to reduce the duration that we pause the process for, we ptrace
439 // the threads, fetch their registers and associated information, and then
440 // fork a separate process as a snapshot of the process's address space.
441 std::set<pid_t> threads;
442 if (!android::procinfo::GetProcessTids(g_target_thread, &threads)) {
443 PLOG(FATAL) << "failed to get process threads";
444 }
Josh Gao3407d7c2017-06-13 17:21:12 +0000445
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700446 std::map<pid_t, ThreadInfo> thread_info;
447 siginfo_t siginfo;
448 std::string error;
449
450 {
451 ATRACE_NAME("ptrace");
452 for (pid_t thread : threads) {
453 // Trace the pseudothread separately, so we can use different options.
454 if (thread == pseudothread_tid) {
455 continue;
456 }
457
458 if (!ptrace_seize_thread(target_proc_fd, thread, &error)) {
459 bool fatal = thread == g_target_thread;
460 LOG(fatal ? FATAL : WARNING) << error;
461 }
462
463 ThreadInfo info;
464 info.pid = target_process;
465 info.tid = thread;
466 info.process_name = process_name;
467 info.thread_name = get_thread_name(thread);
468
469 if (!ptrace_interrupt(thread, &info.signo)) {
470 PLOG(WARNING) << "failed to ptrace interrupt thread " << thread;
471 ptrace(PTRACE_DETACH, thread, 0, 0);
472 continue;
473 }
474
475 if (thread == g_target_thread) {
476 // Read the thread's registers along with the rest of the crash info out of the pipe.
Josh Gao9da1f512018-08-06 15:38:29 -0700477 ReadCrashInfo(input_pipe, &siginfo, &info.registers, &abort_msg_address,
478 &fdsan_table_address);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700479 info.siginfo = &siginfo;
480 info.signo = info.siginfo->si_signo;
481 } else {
Christopher Ferris60eb1972019-01-15 15:18:43 -0800482 info.registers.reset(unwindstack::Regs::RemoteGet(thread));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700483 if (!info.registers) {
484 PLOG(WARNING) << "failed to fetch registers for thread " << thread;
485 ptrace(PTRACE_DETACH, thread, 0, 0);
486 continue;
487 }
488 }
489
490 thread_info[thread] = std::move(info);
491 }
492 }
493
494 // Trace the pseudothread with PTRACE_O_TRACECLONE and tell it to fork.
495 if (!ptrace_seize_thread(target_proc_fd, pseudothread_tid, &error, PTRACE_O_TRACECLONE)) {
496 LOG(FATAL) << "failed to seize pseudothread: " << error;
497 }
498
499 if (TEMP_FAILURE_RETRY(write(output_pipe.get(), "\1", 1)) != 1) {
500 PLOG(FATAL) << "failed to write to pseudothread";
501 }
502
503 pid_t vm_pid = wait_for_vm_process(pseudothread_tid);
504 if (ptrace(PTRACE_DETACH, pseudothread_tid, 0, 0) != 0) {
505 PLOG(FATAL) << "failed to detach from pseudothread";
506 }
507
508 // The pseudothread can die now.
509 fork_exit_write.reset();
510
511 // Defer the message until later, for readability.
512 bool wait_for_gdb = android::base::GetBoolProperty("debug.debuggerd.wait_for_gdb", false);
513 if (siginfo.si_signo == DEBUGGER_SIGNAL) {
514 wait_for_gdb = false;
515 }
516
517 // Detach from all of our attached threads before resuming.
518 for (const auto& [tid, thread] : thread_info) {
519 int resume_signal = thread.signo == DEBUGGER_SIGNAL ? 0 : thread.signo;
520 if (wait_for_gdb) {
521 resume_signal = 0;
522 if (tgkill(target_process, tid, SIGSTOP) != 0) {
523 PLOG(WARNING) << "failed to send SIGSTOP to " << tid;
524 }
525 }
526
527 LOG(DEBUG) << "detaching from thread " << tid;
528 if (ptrace(PTRACE_DETACH, tid, 0, resume_signal) != 0) {
529 PLOG(ERROR) << "failed to detach from thread " << tid;
530 }
531 }
532
533 // Drop our capabilities now that we've fetched all of the information we need.
Josh Gao2f11a252017-02-13 14:46:19 -0800534 drop_capabilities();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700535
Josh Gao8bb03902017-05-30 15:31:02 -0700536 {
537 ATRACE_NAME("tombstoned_connect");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700538 LOG(INFO) << "obtaining output fd from tombstoned, type: " << dump_type;
539 g_tombstoned_connected =
540 tombstoned_connect(g_target_thread, &g_tombstoned_socket, &g_output_fd, dump_type);
Josh Gao8bb03902017-05-30 15:31:02 -0700541 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700542
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700543 if (g_tombstoned_connected) {
544 if (TEMP_FAILURE_RETRY(dup2(g_output_fd.get(), STDOUT_FILENO)) == -1) {
545 PLOG(ERROR) << "failed to dup2 output fd (" << g_output_fd.get() << ") to STDOUT_FILENO";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700546 }
547 } else {
548 unique_fd devnull(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR)));
549 TEMP_FAILURE_RETRY(dup2(devnull.get(), STDOUT_FILENO));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700550 g_output_fd = std::move(devnull);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700551 }
552
Josh Gao9da1f512018-08-06 15:38:29 -0700553 LOG(INFO) << "performing dump of process " << target_process
554 << " (target tid = " << g_target_thread << ")";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700555
556 int signo = siginfo.si_signo;
Josh Gaofe902762017-02-01 16:31:43 -0800557 bool fatal_signal = signo != DEBUGGER_SIGNAL;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700558 bool backtrace = false;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700559
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700560 // si_value is special when used with DEBUGGER_SIGNAL.
Josh Gaocbe70cb2016-10-18 18:17:52 -0700561 // 0: dump tombstone
562 // 1: dump backtrace
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700563 if (!fatal_signal) {
564 int si_val = siginfo.si_value.sival_int;
565 if (si_val == 0) {
566 backtrace = false;
567 } else if (si_val == 1) {
568 backtrace = true;
569 } else {
570 LOG(WARNING) << "unknown si_value value " << si_val;
571 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700572 }
573
Josh Gaocbe70cb2016-10-18 18:17:52 -0700574 // TODO: Use seccomp to lock ourselves down.
Christopher Ferris60eb1972019-01-15 15:18:43 -0800575 unwindstack::UnwinderFromPid unwinder(256, vm_pid);
David Srbeckyb9cc4fb2019-04-05 18:23:32 +0000576 if (!unwinder.Init(unwindstack::Regs::CurrentArch())) {
Christopher Ferris60eb1972019-01-15 15:18:43 -0800577 LOG(FATAL) << "Failed to init unwinder object.";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700578 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700579
Josh Gaocbe70cb2016-10-18 18:17:52 -0700580 std::string amfd_data;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700581 if (backtrace) {
Josh Gao8bb03902017-05-30 15:31:02 -0700582 ATRACE_NAME("dump_backtrace");
Christopher Ferris60eb1972019-01-15 15:18:43 -0800583 dump_backtrace(std::move(g_output_fd), &unwinder, thread_info, g_target_thread);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700584 } else {
Josh Gaoce841d92018-08-06 18:26:42 -0700585 {
586 ATRACE_NAME("fdsan table dump");
Christopher Ferris60eb1972019-01-15 15:18:43 -0800587 populate_fdsan_table(&open_files, unwinder.GetProcessMemory(), fdsan_table_address);
Josh Gaoce841d92018-08-06 18:26:42 -0700588 }
589
590 {
591 ATRACE_NAME("engrave_tombstone");
Christopher Ferris60eb1972019-01-15 15:18:43 -0800592 engrave_tombstone(std::move(g_output_fd), &unwinder, thread_info, g_target_thread,
593 abort_msg_address, &open_files, &amfd_data);
Josh Gaoce841d92018-08-06 18:26:42 -0700594 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700595 }
596
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700597 if (fatal_signal) {
598 // Don't try to notify ActivityManager if it just crashed, or we might hang until timeout.
599 if (thread_info[target_process].thread_name != "system_server") {
600 activity_manager_notify(target_process, signo, amfd_data);
Josh Gao7c6e3132017-01-22 17:59:02 -0800601 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700602 }
603
604 if (wait_for_gdb) {
Josh Gao7c6e3132017-01-22 17:59:02 -0800605 // Use ALOGI to line up with output from engrave_tombstone.
606 ALOGI(
dimitry6429e202017-09-12 10:46:20 +0200607 "***********************************************************\n"
608 "* Process %d has been suspended while crashing.\n"
609 "* To attach gdbserver and start gdb, run this on the host:\n"
610 "*\n"
611 "* gdbclient.py -p %d\n"
612 "*\n"
613 "***********************************************************",
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700614 target_process, target_process);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700615 }
616
617 // Close stdout before we notify tombstoned of completion.
618 close(STDOUT_FILENO);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700619 if (g_tombstoned_connected && !tombstoned_notify_completion(g_tombstoned_socket.get())) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700620 LOG(ERROR) << "failed to notify tombstoned of completion";
621 }
622
623 return 0;
624}