blob: e8f366fbe14b6863c4ce431b0f4e952e695bce8e [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>
Josh Gaoa48b41b2019-12-13 14:11:04 -080043#include <bionic/reserved_signals.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070044#include <cutils/sockets.h>
Vijay Venkatramana95acea2017-01-23 20:11:51 -080045#include <log/log.h>
Josh Gaob0e51e32017-06-01 12:08:10 -070046#include <private/android_filesystem_config.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070047#include <procinfo/process.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070048
Josh Gao8bb03902017-05-30 15:31:02 -070049#define ATRACE_TAG ATRACE_TAG_BIONIC
50#include <utils/Trace.h>
51
David Srbeckyb9cc4fb2019-04-05 18:23:32 +000052#include <unwindstack/DexFiles.h>
Christopher Ferris60eb1972019-01-15 15:18:43 -080053#include <unwindstack/JitDebug.h>
54#include <unwindstack/Maps.h>
55#include <unwindstack/Memory.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070056#include <unwindstack/Regs.h>
Christopher Ferris60eb1972019-01-15 15:18:43 -080057#include <unwindstack/Unwinder.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070058
Josh Gaoc3706662017-08-29 13:08:32 -070059#include "libdebuggerd/backtrace.h"
60#include "libdebuggerd/tombstone.h"
61#include "libdebuggerd/utility.h"
Josh Gaocbe70cb2016-10-18 18:17:52 -070062
63#include "debuggerd/handler.h"
Narayan Kamath2d377cd2017-05-10 10:58:59 +010064#include "tombstoned/tombstoned.h"
Josh Gaoc3706662017-08-29 13:08:32 -070065
66#include "protocol.h"
Narayan Kamath2d377cd2017-05-10 10:58:59 +010067#include "util.h"
Josh Gaocbe70cb2016-10-18 18:17:52 -070068
69using android::base::unique_fd;
70using android::base::StringPrintf;
Josh Gao57f58f82017-03-15 23:23:22 -070071
Josh Gaofe902762017-02-01 16:31:43 -080072static 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 Gaocbe70cb2016-10-18 18:17:52 -070076}
77
Josh Gaofd13bf02017-08-18 15:37:26 -070078static pid_t get_tracer(pid_t tracee) {
79 // Check to see if the thread is being ptraced by another process.
80 android::procinfo::ProcessInfo process_info;
81 if (android::procinfo::GetProcessInfo(tracee, &process_info)) {
82 return process_info.tracer;
83 }
84 return -1;
85}
86
Josh Gaocbe70cb2016-10-18 18:17:52 -070087// Attach to a thread, and verify that it's still a member of the given process
Josh Gao2b2ae0c2017-08-21 14:31:17 -070088static bool ptrace_seize_thread(int pid_proc_fd, pid_t tid, std::string* error, int flags = 0) {
89 if (ptrace(PTRACE_SEIZE, tid, 0, flags) != 0) {
Josh Gaofd13bf02017-08-18 15:37:26 -070090 if (errno == EPERM) {
91 pid_t tracer = get_tracer(tid);
92 if (tracer != -1) {
93 *error = StringPrintf("failed to attach to thread %d, already traced by %d (%s)", tid,
94 tracer, get_process_name(tracer).c_str());
95 return false;
96 }
97 }
98
Josh Gao42fd74b2017-01-20 12:51:11 -080099 *error = StringPrintf("failed to attach to thread %d: %s", tid, strerror(errno));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700100 return false;
101 }
102
103 // Make sure that the task we attached to is actually part of the pid we're dumping.
Josh Gaofe902762017-02-01 16:31:43 -0800104 if (!pid_contains_tid(pid_proc_fd, tid)) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700105 if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700106 PLOG(WARNING) << "failed to detach from thread " << tid;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700107 }
Josh Gaofe902762017-02-01 16:31:43 -0800108 *error = StringPrintf("thread %d is not in process", tid);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700109 return false;
110 }
Josh Gao122479f2017-01-22 16:42:32 -0800111
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700112 return true;
113}
114
115static bool wait_for_stop(pid_t tid, int* received_signal) {
116 while (true) {
117 int status;
118 pid_t result = waitpid(tid, &status, __WALL);
119 if (result != tid) {
120 PLOG(ERROR) << "waitpid failed on " << tid << " while detaching";
121 return false;
122 }
123
124 if (WIFSTOPPED(status)) {
125 if (status >> 16 == PTRACE_EVENT_STOP) {
126 *received_signal = 0;
127 } else {
128 *received_signal = WSTOPSIG(status);
129 }
130 return true;
131 }
132 }
133}
134
135// Interrupt a process and wait for it to be interrupted.
136static bool ptrace_interrupt(pid_t tid, int* received_signal) {
137 if (ptrace(PTRACE_INTERRUPT, tid, 0, 0) == 0) {
138 return wait_for_stop(tid, received_signal);
Josh Gao3407d7c2017-06-13 17:21:12 +0000139 }
140
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700141 PLOG(ERROR) << "failed to interrupt " << tid << " to detach";
142 return false;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700143}
144
Josh Gaob0e51e32017-06-01 12:08:10 -0700145static bool activity_manager_notify(pid_t pid, int signal, const std::string& amfd_data) {
Josh Gao8bb03902017-05-30 15:31:02 -0700146 ATRACE_CALL();
Josh Gaob0e51e32017-06-01 12:08:10 -0700147 android::base::unique_fd amfd(socket_local_client(
148 "/data/system/ndebugsocket", ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700149 if (amfd.get() == -1) {
150 PLOG(ERROR) << "unable to connect to activity manager";
151 return false;
152 }
153
154 struct timeval tv = {
155 .tv_sec = 1,
156 .tv_usec = 0,
157 };
158 if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
159 PLOG(ERROR) << "failed to set send timeout on activity manager socket";
160 return false;
161 }
162 tv.tv_sec = 3; // 3 seconds on handshake read
163 if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
164 PLOG(ERROR) << "failed to set receive timeout on activity manager socket";
165 return false;
166 }
167
168 // Activity Manager protocol: binary 32-bit network-byte-order ints for the
169 // pid and signal number, followed by the raw text of the dump, culminating
170 // in a zero byte that marks end-of-data.
171 uint32_t datum = htonl(pid);
172 if (!android::base::WriteFully(amfd, &datum, 4)) {
173 PLOG(ERROR) << "AM pid write failed";
174 return false;
175 }
176 datum = htonl(signal);
177 if (!android::base::WriteFully(amfd, &datum, 4)) {
178 PLOG(ERROR) << "AM signal write failed";
179 return false;
180 }
181 if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size() + 1)) {
182 PLOG(ERROR) << "AM data write failed";
183 return false;
184 }
185
186 // 3 sec timeout reading the ack; we're fine if the read fails.
187 char ack;
188 android::base::ReadFully(amfd, &ack, 1);
189 return true;
190}
191
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700192// Globals used by the abort handler.
193static pid_t g_target_thread = -1;
194static bool g_tombstoned_connected = false;
195static unique_fd g_tombstoned_socket;
196static unique_fd g_output_fd;
Josh Gao57594112017-01-22 17:41:15 -0800197
Josh Gao38ac45d2018-04-27 13:31:47 -0700198static void DefuseSignalHandlers() {
199 // Don't try to dump ourselves.
200 struct sigaction action = {};
201 action.sa_handler = SIG_DFL;
202 debuggerd_register_handlers(&action);
203
204 sigset_t mask;
205 sigemptyset(&mask);
206 if (sigprocmask(SIG_SETMASK, &mask, nullptr) != 0) {
207 PLOG(FATAL) << "failed to set signal mask";
208 }
209}
210
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700211static void Initialize(char** argv) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700212 android::base::InitLogging(argv);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700213 android::base::SetAborter([](const char* abort_msg) {
214 // If we abort before we get an output fd, contact tombstoned to let any
215 // potential listeners know that we failed.
216 if (!g_tombstoned_connected) {
217 if (!tombstoned_connect(g_target_thread, &g_tombstoned_socket, &g_output_fd,
218 kDebuggerdAnyIntercept)) {
219 // We failed to connect, not much we can do.
220 LOG(ERROR) << "failed to connected to tombstoned to report failure";
221 _exit(1);
222 }
223 }
224
225 dprintf(g_output_fd.get(), "crash_dump failed to dump process");
226 if (g_target_thread != 1) {
227 dprintf(g_output_fd.get(), " %d: %s\n", g_target_thread, abort_msg);
228 } else {
229 dprintf(g_output_fd.get(), ": %s\n", abort_msg);
230 }
231
232 _exit(1);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700233 });
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700234}
Josh Gaoe7402502017-06-01 11:55:25 -0700235
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700236static void ParseArgs(int argc, char** argv, pid_t* pseudothread_tid, DebuggerdDumpType* dump_type) {
Narayan Kamatha73df602017-05-24 15:07:25 +0100237 if (argc != 4) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700238 LOG(FATAL) << "wrong number of args: " << argc << " (expected 4)";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700239 }
240
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700241 if (!android::base::ParseInt(argv[1], &g_target_thread, 1, std::numeric_limits<pid_t>::max())) {
242 LOG(FATAL) << "invalid target tid: " << argv[1];
Josh Gaocbe70cb2016-10-18 18:17:52 -0700243 }
244
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700245 if (!android::base::ParseInt(argv[2], pseudothread_tid, 1, std::numeric_limits<pid_t>::max())) {
Josh Gaof6ad5852017-02-15 12:21:11 -0800246 LOG(FATAL) << "invalid pseudothread tid: " << argv[2];
Josh Gao2f11a252017-02-13 14:46:19 -0800247 }
248
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700249 int dump_type_int;
250 if (!android::base::ParseInt(argv[3], &dump_type_int, 0, 1)) {
Narayan Kamatha73df602017-05-24 15:07:25 +0100251 LOG(FATAL) << "invalid requested dump type: " << argv[3];
252 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700253 *dump_type = static_cast<DebuggerdDumpType>(dump_type_int);
254}
Narayan Kamatha73df602017-05-24 15:07:25 +0100255
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700256static void ReadCrashInfo(unique_fd& fd, siginfo_t* siginfo,
Josh Gao9da1f512018-08-06 15:38:29 -0700257 std::unique_ptr<unwindstack::Regs>* regs, uintptr_t* abort_msg_address,
258 uintptr_t* fdsan_table_address) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700259 std::aligned_storage<sizeof(CrashInfo) + 1, alignof(CrashInfo)>::type buf;
Josh Gao9da1f512018-08-06 15:38:29 -0700260 CrashInfo* crash_info = reinterpret_cast<CrashInfo*>(&buf);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700261 ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), &buf, sizeof(buf)));
262 if (rc == -1) {
263 PLOG(FATAL) << "failed to read target ucontext";
Josh Gao9da1f512018-08-06 15:38:29 -0700264 } else {
265 ssize_t expected_size = 0;
266 switch (crash_info->header.version) {
267 case 1:
268 expected_size = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataV1);
269 break;
270
271 case 2:
272 expected_size = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataV2);
273 break;
274
275 default:
276 LOG(FATAL) << "unexpected CrashInfo version: " << crash_info->header.version;
277 break;
278 };
279
280 if (rc != expected_size) {
281 LOG(FATAL) << "read " << rc << " bytes when reading target crash information, expected "
282 << expected_size;
283 }
Josh Gaoc7fe0602017-03-13 14:13:29 -0700284 }
285
Josh Gao9da1f512018-08-06 15:38:29 -0700286 *fdsan_table_address = 0;
287 switch (crash_info->header.version) {
288 case 2:
289 *fdsan_table_address = crash_info->data.v2.fdsan_table_address;
Josh Gao8d44b142018-09-18 13:22:22 -0700290 FALLTHROUGH_INTENDED;
Josh Gao9da1f512018-08-06 15:38:29 -0700291 case 1:
292 *abort_msg_address = crash_info->data.v1.abort_msg_address;
293 *siginfo = crash_info->data.v1.siginfo;
Christopher Ferris60eb1972019-01-15 15:18:43 -0800294 regs->reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(),
295 &crash_info->data.v1.ucontext));
Josh Gao9da1f512018-08-06 15:38:29 -0700296 break;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700297
Josh Gao9da1f512018-08-06 15:38:29 -0700298 default:
299 __builtin_unreachable();
300 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700301}
302
303// Wait for a process to clone and return the child's pid.
304// Note: this leaves the parent in PTRACE_EVENT_STOP.
305static pid_t wait_for_clone(pid_t pid, bool resume_child) {
306 int status;
307 pid_t result = TEMP_FAILURE_RETRY(waitpid(pid, &status, __WALL));
308 if (result == -1) {
309 PLOG(FATAL) << "failed to waitpid";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700310 }
311
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700312 if (WIFEXITED(status)) {
313 LOG(FATAL) << "traced process exited with status " << WEXITSTATUS(status);
314 } else if (WIFSIGNALED(status)) {
315 LOG(FATAL) << "traced process exited with signal " << WTERMSIG(status);
316 } else if (!WIFSTOPPED(status)) {
317 LOG(FATAL) << "process didn't stop? (status = " << status << ")";
318 }
319
320 if (status >> 8 != (SIGTRAP | (PTRACE_EVENT_CLONE << 8))) {
321 LOG(FATAL) << "process didn't stop due to PTRACE_O_TRACECLONE (status = " << status << ")";
322 }
323
324 pid_t child;
325 if (ptrace(PTRACE_GETEVENTMSG, pid, 0, &child) != 0) {
326 PLOG(FATAL) << "failed to get child pid via PTRACE_GETEVENTMSG";
327 }
328
329 int stop_signal;
330 if (!wait_for_stop(child, &stop_signal)) {
331 PLOG(FATAL) << "failed to waitpid on child";
332 }
333
334 CHECK_EQ(0, stop_signal);
335
336 if (resume_child) {
337 if (ptrace(PTRACE_CONT, child, 0, 0) != 0) {
338 PLOG(FATAL) << "failed to resume child (pid = " << child << ")";
339 }
340 }
341
342 return child;
343}
344
345static pid_t wait_for_vm_process(pid_t pseudothread_tid) {
346 // The pseudothread will double-fork, we want its grandchild.
347 pid_t intermediate = wait_for_clone(pseudothread_tid, true);
348 pid_t vm_pid = wait_for_clone(intermediate, false);
349 if (ptrace(PTRACE_DETACH, intermediate, 0, 0) != 0) {
350 PLOG(FATAL) << "failed to detach from intermediate vm process";
351 }
352
353 return vm_pid;
354}
355
Jinguang Dong8ac2f272018-11-24 17:12:33 +0800356static void InstallSigPipeHandler() {
357 struct sigaction action = {};
358 action.sa_handler = SIG_IGN;
359 action.sa_flags = SA_RESTART;
360 sigaction(SIGPIPE, &action, nullptr);
361}
362
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700363int main(int argc, char** argv) {
Josh Gao38ac45d2018-04-27 13:31:47 -0700364 DefuseSignalHandlers();
Jinguang Dong8ac2f272018-11-24 17:12:33 +0800365 InstallSigPipeHandler();
Josh Gao38ac45d2018-04-27 13:31:47 -0700366
Josh Gao18cb6812019-04-16 13:17:08 -0700367 // There appears to be a bug in the kernel where our death causes SIGHUP to
368 // be sent to our process group if we exit while it has stopped jobs (e.g.
369 // because of wait_for_gdb). Use setsid to create a new process group to
370 // avoid hitting this.
371 setsid();
372
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700373 atrace_begin(ATRACE_TAG, "before reparent");
374 pid_t target_process = getppid();
375
376 // Open /proc/`getppid()` before we daemonize.
377 std::string target_proc_path = "/proc/" + std::to_string(target_process);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700378 int target_proc_fd = open(target_proc_path.c_str(), O_DIRECTORY | O_RDONLY);
379 if (target_proc_fd == -1) {
380 PLOG(FATAL) << "failed to open " << target_proc_path;
381 }
382
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700383 // Make sure getppid() hasn't changed.
384 if (getppid() != target_process) {
385 LOG(FATAL) << "parent died";
Josh Gao2a18b822017-02-16 19:17:28 -0800386 }
Josh Gao8bb03902017-05-30 15:31:02 -0700387 atrace_end(ATRACE_TAG);
388
Josh Gaocbe70cb2016-10-18 18:17:52 -0700389 // Reparent ourselves to init, so that the signal handler can waitpid on the
390 // original process to avoid leaving a zombie for non-fatal dumps.
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700391 // Move the input/output pipes off of stdout/stderr, out of paranoia.
392 unique_fd output_pipe(dup(STDOUT_FILENO));
393 unique_fd input_pipe(dup(STDIN_FILENO));
394
395 unique_fd fork_exit_read, fork_exit_write;
396 if (!Pipe(&fork_exit_read, &fork_exit_write)) {
397 PLOG(FATAL) << "failed to create pipe";
398 }
399
Josh Gaocbe70cb2016-10-18 18:17:52 -0700400 pid_t forkpid = fork();
401 if (forkpid == -1) {
402 PLOG(FATAL) << "fork failed";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700403 } else if (forkpid == 0) {
404 fork_exit_read.reset();
405 } else {
406 // We need the pseudothread to live until we get around to verifying the vm pid against it.
407 // The last thing it does is block on a waitpid on us, so wait until our child tells us to die.
408 fork_exit_write.reset();
409 char buf;
410 TEMP_FAILURE_RETRY(read(fork_exit_read.get(), &buf, sizeof(buf)));
411 _exit(0);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700412 }
413
Josh Gao8bb03902017-05-30 15:31:02 -0700414 ATRACE_NAME("after reparent");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700415 pid_t pseudothread_tid;
416 DebuggerdDumpType dump_type;
Josh Gao9da1f512018-08-06 15:38:29 -0700417 uintptr_t abort_msg_address = 0;
418 uintptr_t fdsan_table_address = 0;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700419
420 Initialize(argv);
421 ParseArgs(argc, argv, &pseudothread_tid, &dump_type);
Josh Gao8bb03902017-05-30 15:31:02 -0700422
Josh Gao7c6e3132017-01-22 17:59:02 -0800423 // Die if we take too long.
Andreas Gampeb02851a2017-06-22 19:45:53 -0700424 //
425 // Note: processes with many threads and minidebug-info can take a bit to
426 // unwind, do not make this too small. b/62828735
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700427 alarm(30);
Josh Gao7c6e3132017-01-22 17:59:02 -0800428
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700429 // Get the process name (aka cmdline).
430 std::string process_name = get_process_name(g_target_thread);
Josh Gao2f11a252017-02-13 14:46:19 -0800431
432 // Collect the list of open files.
433 OpenFilesList open_files;
Josh Gao8bb03902017-05-30 15:31:02 -0700434 {
435 ATRACE_NAME("open files");
Josh Gaoce841d92018-08-06 18:26:42 -0700436 populate_open_files_list(&open_files, g_target_thread);
Josh Gao8bb03902017-05-30 15:31:02 -0700437 }
Josh Gao2f11a252017-02-13 14:46:19 -0800438
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700439 // In order to reduce the duration that we pause the process for, we ptrace
440 // the threads, fetch their registers and associated information, and then
441 // fork a separate process as a snapshot of the process's address space.
442 std::set<pid_t> threads;
443 if (!android::procinfo::GetProcessTids(g_target_thread, &threads)) {
444 PLOG(FATAL) << "failed to get process threads";
445 }
Josh Gao3407d7c2017-06-13 17:21:12 +0000446
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700447 std::map<pid_t, ThreadInfo> thread_info;
448 siginfo_t siginfo;
449 std::string error;
450
451 {
452 ATRACE_NAME("ptrace");
453 for (pid_t thread : threads) {
454 // Trace the pseudothread separately, so we can use different options.
455 if (thread == pseudothread_tid) {
456 continue;
457 }
458
459 if (!ptrace_seize_thread(target_proc_fd, thread, &error)) {
460 bool fatal = thread == g_target_thread;
461 LOG(fatal ? FATAL : WARNING) << error;
462 }
463
464 ThreadInfo info;
465 info.pid = target_process;
466 info.tid = thread;
Josh Gao5df504c2019-05-09 12:48:17 -0700467 info.uid = getuid();
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700468 info.process_name = process_name;
469 info.thread_name = get_thread_name(thread);
470
471 if (!ptrace_interrupt(thread, &info.signo)) {
472 PLOG(WARNING) << "failed to ptrace interrupt thread " << thread;
473 ptrace(PTRACE_DETACH, thread, 0, 0);
474 continue;
475 }
476
477 if (thread == g_target_thread) {
478 // Read the thread's registers along with the rest of the crash info out of the pipe.
Josh Gao9da1f512018-08-06 15:38:29 -0700479 ReadCrashInfo(input_pipe, &siginfo, &info.registers, &abort_msg_address,
480 &fdsan_table_address);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700481 info.siginfo = &siginfo;
482 info.signo = info.siginfo->si_signo;
483 } else {
Christopher Ferris60eb1972019-01-15 15:18:43 -0800484 info.registers.reset(unwindstack::Regs::RemoteGet(thread));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700485 if (!info.registers) {
486 PLOG(WARNING) << "failed to fetch registers for thread " << thread;
487 ptrace(PTRACE_DETACH, thread, 0, 0);
488 continue;
489 }
490 }
491
492 thread_info[thread] = std::move(info);
493 }
494 }
495
496 // Trace the pseudothread with PTRACE_O_TRACECLONE and tell it to fork.
497 if (!ptrace_seize_thread(target_proc_fd, pseudothread_tid, &error, PTRACE_O_TRACECLONE)) {
498 LOG(FATAL) << "failed to seize pseudothread: " << error;
499 }
500
501 if (TEMP_FAILURE_RETRY(write(output_pipe.get(), "\1", 1)) != 1) {
502 PLOG(FATAL) << "failed to write to pseudothread";
503 }
504
505 pid_t vm_pid = wait_for_vm_process(pseudothread_tid);
506 if (ptrace(PTRACE_DETACH, pseudothread_tid, 0, 0) != 0) {
507 PLOG(FATAL) << "failed to detach from pseudothread";
508 }
509
510 // The pseudothread can die now.
511 fork_exit_write.reset();
512
513 // Defer the message until later, for readability.
514 bool wait_for_gdb = android::base::GetBoolProperty("debug.debuggerd.wait_for_gdb", false);
Josh Gaoa48b41b2019-12-13 14:11:04 -0800515 if (siginfo.si_signo == BIONIC_SIGNAL_DEBUGGER) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700516 wait_for_gdb = false;
517 }
518
519 // Detach from all of our attached threads before resuming.
520 for (const auto& [tid, thread] : thread_info) {
Josh Gaoa48b41b2019-12-13 14:11:04 -0800521 int resume_signal = thread.signo == BIONIC_SIGNAL_DEBUGGER ? 0 : thread.signo;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700522 if (wait_for_gdb) {
523 resume_signal = 0;
524 if (tgkill(target_process, tid, SIGSTOP) != 0) {
525 PLOG(WARNING) << "failed to send SIGSTOP to " << tid;
526 }
527 }
528
529 LOG(DEBUG) << "detaching from thread " << tid;
530 if (ptrace(PTRACE_DETACH, tid, 0, resume_signal) != 0) {
531 PLOG(ERROR) << "failed to detach from thread " << tid;
532 }
533 }
534
535 // Drop our capabilities now that we've fetched all of the information we need.
Josh Gao2f11a252017-02-13 14:46:19 -0800536 drop_capabilities();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700537
Josh Gao8bb03902017-05-30 15:31:02 -0700538 {
539 ATRACE_NAME("tombstoned_connect");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700540 LOG(INFO) << "obtaining output fd from tombstoned, type: " << dump_type;
541 g_tombstoned_connected =
542 tombstoned_connect(g_target_thread, &g_tombstoned_socket, &g_output_fd, dump_type);
Josh Gao8bb03902017-05-30 15:31:02 -0700543 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700544
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700545 if (g_tombstoned_connected) {
546 if (TEMP_FAILURE_RETRY(dup2(g_output_fd.get(), STDOUT_FILENO)) == -1) {
547 PLOG(ERROR) << "failed to dup2 output fd (" << g_output_fd.get() << ") to STDOUT_FILENO";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700548 }
549 } else {
550 unique_fd devnull(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR)));
551 TEMP_FAILURE_RETRY(dup2(devnull.get(), STDOUT_FILENO));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700552 g_output_fd = std::move(devnull);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700553 }
554
Josh Gao9da1f512018-08-06 15:38:29 -0700555 LOG(INFO) << "performing dump of process " << target_process
556 << " (target tid = " << g_target_thread << ")";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700557
558 int signo = siginfo.si_signo;
Josh Gaoa48b41b2019-12-13 14:11:04 -0800559 bool fatal_signal = signo != BIONIC_SIGNAL_DEBUGGER;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700560 bool backtrace = false;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700561
Josh Gaoa48b41b2019-12-13 14:11:04 -0800562 // si_value is special when used with BIONIC_SIGNAL_DEBUGGER.
Josh Gaocbe70cb2016-10-18 18:17:52 -0700563 // 0: dump tombstone
564 // 1: dump backtrace
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700565 if (!fatal_signal) {
566 int si_val = siginfo.si_value.sival_int;
567 if (si_val == 0) {
568 backtrace = false;
569 } else if (si_val == 1) {
570 backtrace = true;
571 } else {
572 LOG(WARNING) << "unknown si_value value " << si_val;
573 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700574 }
575
Josh Gaocbe70cb2016-10-18 18:17:52 -0700576 // TODO: Use seccomp to lock ourselves down.
Christopher Ferris60eb1972019-01-15 15:18:43 -0800577 unwindstack::UnwinderFromPid unwinder(256, vm_pid);
David Srbeckyb9cc4fb2019-04-05 18:23:32 +0000578 if (!unwinder.Init(unwindstack::Regs::CurrentArch())) {
Christopher Ferris60eb1972019-01-15 15:18:43 -0800579 LOG(FATAL) << "Failed to init unwinder object.";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700580 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700581
Josh Gaocbe70cb2016-10-18 18:17:52 -0700582 std::string amfd_data;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700583 if (backtrace) {
Josh Gao8bb03902017-05-30 15:31:02 -0700584 ATRACE_NAME("dump_backtrace");
Christopher Ferris60eb1972019-01-15 15:18:43 -0800585 dump_backtrace(std::move(g_output_fd), &unwinder, thread_info, g_target_thread);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700586 } else {
Josh Gaoce841d92018-08-06 18:26:42 -0700587 {
588 ATRACE_NAME("fdsan table dump");
Christopher Ferris60eb1972019-01-15 15:18:43 -0800589 populate_fdsan_table(&open_files, unwinder.GetProcessMemory(), fdsan_table_address);
Josh Gaoce841d92018-08-06 18:26:42 -0700590 }
591
592 {
593 ATRACE_NAME("engrave_tombstone");
Christopher Ferris60eb1972019-01-15 15:18:43 -0800594 engrave_tombstone(std::move(g_output_fd), &unwinder, thread_info, g_target_thread,
595 abort_msg_address, &open_files, &amfd_data);
Josh Gaoce841d92018-08-06 18:26:42 -0700596 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700597 }
598
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700599 if (fatal_signal) {
600 // Don't try to notify ActivityManager if it just crashed, or we might hang until timeout.
601 if (thread_info[target_process].thread_name != "system_server") {
602 activity_manager_notify(target_process, signo, amfd_data);
Josh Gao7c6e3132017-01-22 17:59:02 -0800603 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700604 }
605
606 if (wait_for_gdb) {
Josh Gao7c6e3132017-01-22 17:59:02 -0800607 // Use ALOGI to line up with output from engrave_tombstone.
608 ALOGI(
dimitry6429e202017-09-12 10:46:20 +0200609 "***********************************************************\n"
610 "* Process %d has been suspended while crashing.\n"
611 "* To attach gdbserver and start gdb, run this on the host:\n"
612 "*\n"
613 "* gdbclient.py -p %d\n"
614 "*\n"
615 "***********************************************************",
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700616 target_process, target_process);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700617 }
618
619 // Close stdout before we notify tombstoned of completion.
620 close(STDOUT_FILENO);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700621 if (g_tombstoned_connected && !tombstoned_notify_completion(g_tombstoned_socket.get())) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700622 LOG(ERROR) << "failed to notify tombstoned of completion";
623 }
624
625 return 0;
626}