blob: 68a43cffb2eeffa712e5badaf9ef2fc11a4e6477 [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>
Mitch Phillipse4adff02021-01-21 20:41:50 -080043#include <bionic/macros.h>
Josh Gaoa48b41b2019-12-13 14:11:04 -080044#include <bionic/reserved_signals.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070045#include <cutils/sockets.h>
Vijay Venkatramana95acea2017-01-23 20:11:51 -080046#include <log/log.h>
Josh Gaob0e51e32017-06-01 12:08:10 -070047#include <private/android_filesystem_config.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070048#include <procinfo/process.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070049
Josh Gao8bb03902017-05-30 15:31:02 -070050#define ATRACE_TAG ATRACE_TAG_BIONIC
51#include <utils/Trace.h>
52
David Srbeckyb9cc4fb2019-04-05 18:23:32 +000053#include <unwindstack/DexFiles.h>
Christopher Ferris60eb1972019-01-15 15:18:43 -080054#include <unwindstack/JitDebug.h>
55#include <unwindstack/Maps.h>
56#include <unwindstack/Memory.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070057#include <unwindstack/Regs.h>
Christopher Ferris60eb1972019-01-15 15:18:43 -080058#include <unwindstack/Unwinder.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070059
Josh Gaoc3706662017-08-29 13:08:32 -070060#include "libdebuggerd/backtrace.h"
61#include "libdebuggerd/tombstone.h"
62#include "libdebuggerd/utility.h"
Josh Gaocbe70cb2016-10-18 18:17:52 -070063
64#include "debuggerd/handler.h"
Narayan Kamath2d377cd2017-05-10 10:58:59 +010065#include "tombstoned/tombstoned.h"
Josh Gaoc3706662017-08-29 13:08:32 -070066
67#include "protocol.h"
Narayan Kamath2d377cd2017-05-10 10:58:59 +010068#include "util.h"
Josh Gaocbe70cb2016-10-18 18:17:52 -070069
70using android::base::unique_fd;
71using android::base::StringPrintf;
Josh Gao57f58f82017-03-15 23:23:22 -070072
Josh Gaofe902762017-02-01 16:31:43 -080073static bool pid_contains_tid(int pid_proc_fd, pid_t tid) {
74 struct stat st;
75 std::string task_path = StringPrintf("task/%d", tid);
76 return fstatat(pid_proc_fd, task_path.c_str(), &st, 0) == 0;
Josh Gaocbe70cb2016-10-18 18:17:52 -070077}
78
Josh Gaofd13bf02017-08-18 15:37:26 -070079static pid_t get_tracer(pid_t tracee) {
80 // Check to see if the thread is being ptraced by another process.
81 android::procinfo::ProcessInfo process_info;
82 if (android::procinfo::GetProcessInfo(tracee, &process_info)) {
83 return process_info.tracer;
84 }
85 return -1;
86}
87
Josh Gaocbe70cb2016-10-18 18:17:52 -070088// Attach to a thread, and verify that it's still a member of the given process
Josh Gao2b2ae0c2017-08-21 14:31:17 -070089static bool ptrace_seize_thread(int pid_proc_fd, pid_t tid, std::string* error, int flags = 0) {
90 if (ptrace(PTRACE_SEIZE, tid, 0, flags) != 0) {
Josh Gaofd13bf02017-08-18 15:37:26 -070091 if (errno == EPERM) {
92 pid_t tracer = get_tracer(tid);
93 if (tracer != -1) {
94 *error = StringPrintf("failed to attach to thread %d, already traced by %d (%s)", tid,
95 tracer, get_process_name(tracer).c_str());
96 return false;
97 }
98 }
99
Josh Gao42fd74b2017-01-20 12:51:11 -0800100 *error = StringPrintf("failed to attach to thread %d: %s", tid, strerror(errno));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700101 return false;
102 }
103
104 // Make sure that the task we attached to is actually part of the pid we're dumping.
Josh Gaofe902762017-02-01 16:31:43 -0800105 if (!pid_contains_tid(pid_proc_fd, tid)) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700106 if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700107 PLOG(WARNING) << "failed to detach from thread " << tid;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700108 }
Josh Gaofe902762017-02-01 16:31:43 -0800109 *error = StringPrintf("thread %d is not in process", tid);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700110 return false;
111 }
Josh Gao122479f2017-01-22 16:42:32 -0800112
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700113 return true;
114}
115
116static bool wait_for_stop(pid_t tid, int* received_signal) {
117 while (true) {
118 int status;
119 pid_t result = waitpid(tid, &status, __WALL);
120 if (result != tid) {
121 PLOG(ERROR) << "waitpid failed on " << tid << " while detaching";
122 return false;
123 }
124
125 if (WIFSTOPPED(status)) {
126 if (status >> 16 == PTRACE_EVENT_STOP) {
127 *received_signal = 0;
128 } else {
129 *received_signal = WSTOPSIG(status);
130 }
131 return true;
132 }
133 }
134}
135
136// Interrupt a process and wait for it to be interrupted.
137static bool ptrace_interrupt(pid_t tid, int* received_signal) {
138 if (ptrace(PTRACE_INTERRUPT, tid, 0, 0) == 0) {
139 return wait_for_stop(tid, received_signal);
Josh Gao3407d7c2017-06-13 17:21:12 +0000140 }
141
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700142 PLOG(ERROR) << "failed to interrupt " << tid << " to detach";
143 return false;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700144}
145
Josh Gaob0e51e32017-06-01 12:08:10 -0700146static bool activity_manager_notify(pid_t pid, int signal, const std::string& amfd_data) {
Josh Gao8bb03902017-05-30 15:31:02 -0700147 ATRACE_CALL();
Josh Gaob0e51e32017-06-01 12:08:10 -0700148 android::base::unique_fd amfd(socket_local_client(
149 "/data/system/ndebugsocket", ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700150 if (amfd.get() == -1) {
151 PLOG(ERROR) << "unable to connect to activity manager";
152 return false;
153 }
154
155 struct timeval tv = {
Evgenii Stepanov2a55e1a2021-01-29 11:50:30 -0800156 .tv_sec = 1 * android::base::TimeoutMultiplier(),
157 .tv_usec = 0,
Josh Gaocbe70cb2016-10-18 18:17:52 -0700158 };
159 if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
160 PLOG(ERROR) << "failed to set send timeout on activity manager socket";
161 return false;
162 }
Evgenii Stepanov2a55e1a2021-01-29 11:50:30 -0800163 tv.tv_sec = 3 * android::base::TimeoutMultiplier(); // 3 seconds on handshake read
Josh Gaocbe70cb2016-10-18 18:17:52 -0700164 if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
165 PLOG(ERROR) << "failed to set receive timeout on activity manager socket";
166 return false;
167 }
168
169 // Activity Manager protocol: binary 32-bit network-byte-order ints for the
170 // pid and signal number, followed by the raw text of the dump, culminating
171 // in a zero byte that marks end-of-data.
172 uint32_t datum = htonl(pid);
173 if (!android::base::WriteFully(amfd, &datum, 4)) {
174 PLOG(ERROR) << "AM pid write failed";
175 return false;
176 }
177 datum = htonl(signal);
178 if (!android::base::WriteFully(amfd, &datum, 4)) {
179 PLOG(ERROR) << "AM signal write failed";
180 return false;
181 }
182 if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size() + 1)) {
183 PLOG(ERROR) << "AM data write failed";
184 return false;
185 }
186
187 // 3 sec timeout reading the ack; we're fine if the read fails.
188 char ack;
189 android::base::ReadFully(amfd, &ack, 1);
190 return true;
191}
192
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700193// Globals used by the abort handler.
194static pid_t g_target_thread = -1;
195static bool g_tombstoned_connected = false;
196static unique_fd g_tombstoned_socket;
197static unique_fd g_output_fd;
Josh Gao76e1e302021-01-26 15:53:11 -0800198static unique_fd g_proto_fd;
Josh Gao57594112017-01-22 17:41:15 -0800199
Josh Gao38ac45d2018-04-27 13:31:47 -0700200static void DefuseSignalHandlers() {
201 // Don't try to dump ourselves.
202 struct sigaction action = {};
203 action.sa_handler = SIG_DFL;
204 debuggerd_register_handlers(&action);
205
206 sigset_t mask;
207 sigemptyset(&mask);
208 if (sigprocmask(SIG_SETMASK, &mask, nullptr) != 0) {
209 PLOG(FATAL) << "failed to set signal mask";
210 }
211}
212
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700213static void Initialize(char** argv) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700214 android::base::InitLogging(argv);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700215 android::base::SetAborter([](const char* abort_msg) {
216 // If we abort before we get an output fd, contact tombstoned to let any
217 // potential listeners know that we failed.
218 if (!g_tombstoned_connected) {
Josh Gao76e1e302021-01-26 15:53:11 -0800219 if (!tombstoned_connect(g_target_thread, &g_tombstoned_socket, &g_output_fd, &g_proto_fd,
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700220 kDebuggerdAnyIntercept)) {
221 // We failed to connect, not much we can do.
222 LOG(ERROR) << "failed to connected to tombstoned to report failure";
223 _exit(1);
224 }
225 }
226
227 dprintf(g_output_fd.get(), "crash_dump failed to dump process");
228 if (g_target_thread != 1) {
229 dprintf(g_output_fd.get(), " %d: %s\n", g_target_thread, abort_msg);
230 } else {
231 dprintf(g_output_fd.get(), ": %s\n", abort_msg);
232 }
233
234 _exit(1);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700235 });
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700236}
Josh Gaoe7402502017-06-01 11:55:25 -0700237
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700238static void ParseArgs(int argc, char** argv, pid_t* pseudothread_tid, DebuggerdDumpType* dump_type) {
Narayan Kamatha73df602017-05-24 15:07:25 +0100239 if (argc != 4) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700240 LOG(FATAL) << "wrong number of args: " << argc << " (expected 4)";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700241 }
242
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700243 if (!android::base::ParseInt(argv[1], &g_target_thread, 1, std::numeric_limits<pid_t>::max())) {
244 LOG(FATAL) << "invalid target tid: " << argv[1];
Josh Gaocbe70cb2016-10-18 18:17:52 -0700245 }
246
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700247 if (!android::base::ParseInt(argv[2], pseudothread_tid, 1, std::numeric_limits<pid_t>::max())) {
Josh Gaof6ad5852017-02-15 12:21:11 -0800248 LOG(FATAL) << "invalid pseudothread tid: " << argv[2];
Josh Gao2f11a252017-02-13 14:46:19 -0800249 }
250
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700251 int dump_type_int;
Josh Gao76e1e302021-01-26 15:53:11 -0800252 if (!android::base::ParseInt(argv[3], &dump_type_int, 0)) {
Narayan Kamatha73df602017-05-24 15:07:25 +0100253 LOG(FATAL) << "invalid requested dump type: " << argv[3];
254 }
Josh Gao76e1e302021-01-26 15:53:11 -0800255
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700256 *dump_type = static_cast<DebuggerdDumpType>(dump_type_int);
Josh Gao76e1e302021-01-26 15:53:11 -0800257 switch (*dump_type) {
258 case kDebuggerdNativeBacktrace:
259 case kDebuggerdTombstone:
260 case kDebuggerdTombstoneProto:
261 break;
262
263 default:
264 LOG(FATAL) << "invalid requested dump type: " << dump_type_int;
265 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700266}
Narayan Kamatha73df602017-05-24 15:07:25 +0100267
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700268static void ReadCrashInfo(unique_fd& fd, siginfo_t* siginfo,
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800269 std::unique_ptr<unwindstack::Regs>* regs, ProcessInfo* process_info) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700270 std::aligned_storage<sizeof(CrashInfo) + 1, alignof(CrashInfo)>::type buf;
Josh Gao9da1f512018-08-06 15:38:29 -0700271 CrashInfo* crash_info = reinterpret_cast<CrashInfo*>(&buf);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700272 ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), &buf, sizeof(buf)));
273 if (rc == -1) {
274 PLOG(FATAL) << "failed to read target ucontext";
Josh Gao9da1f512018-08-06 15:38:29 -0700275 } else {
276 ssize_t expected_size = 0;
277 switch (crash_info->header.version) {
278 case 1:
Josh Gao9da1f512018-08-06 15:38:29 -0700279 case 2:
Peter Collingbournef3d542f2020-03-05 16:46:15 -0800280 case 3:
281 expected_size = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataStatic);
Josh Gao9da1f512018-08-06 15:38:29 -0700282 break;
283
Peter Collingbournef3d542f2020-03-05 16:46:15 -0800284 case 4:
285 expected_size = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataDynamic);
Mitch Phillipse0b4bb12020-02-14 14:54:31 -0800286 break;
287
Josh Gao9da1f512018-08-06 15:38:29 -0700288 default:
289 LOG(FATAL) << "unexpected CrashInfo version: " << crash_info->header.version;
290 break;
291 };
292
Peter Collingbournef3d542f2020-03-05 16:46:15 -0800293 if (rc < expected_size) {
Josh Gao9da1f512018-08-06 15:38:29 -0700294 LOG(FATAL) << "read " << rc << " bytes when reading target crash information, expected "
295 << expected_size;
296 }
Josh Gaoc7fe0602017-03-13 14:13:29 -0700297 }
298
Josh Gao9da1f512018-08-06 15:38:29 -0700299 switch (crash_info->header.version) {
Peter Collingbournef3d542f2020-03-05 16:46:15 -0800300 case 4:
301 process_info->fdsan_table_address = crash_info->data.d.fdsan_table_address;
302 process_info->gwp_asan_state = crash_info->data.d.gwp_asan_state;
303 process_info->gwp_asan_metadata = crash_info->data.d.gwp_asan_metadata;
Peter Collingbournef8622522020-04-07 14:07:32 -0700304 process_info->scudo_stack_depot = crash_info->data.d.scudo_stack_depot;
305 process_info->scudo_region_info = crash_info->data.d.scudo_region_info;
Josh Gao8d44b142018-09-18 13:22:22 -0700306 FALLTHROUGH_INTENDED;
Josh Gao9da1f512018-08-06 15:38:29 -0700307 case 1:
Peter Collingbournef3d542f2020-03-05 16:46:15 -0800308 case 2:
309 case 3:
310 process_info->abort_msg_address = crash_info->data.s.abort_msg_address;
311 *siginfo = crash_info->data.s.siginfo;
Peter Collingbournef03af882020-03-20 18:09:00 -0700312 if (signal_has_si_addr(siginfo)) {
Peter Collingbournef03af882020-03-20 18:09:00 -0700313 process_info->has_fault_address = true;
Mitch Phillipse4adff02021-01-21 20:41:50 -0800314 process_info->maybe_tagged_fault_address = reinterpret_cast<uintptr_t>(siginfo->si_addr);
315 process_info->untagged_fault_address =
316 untag_address(reinterpret_cast<uintptr_t>(siginfo->si_addr));
Peter Collingbournef03af882020-03-20 18:09:00 -0700317 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800318 regs->reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(),
Peter Collingbournef3d542f2020-03-05 16:46:15 -0800319 &crash_info->data.s.ucontext));
Josh Gao9da1f512018-08-06 15:38:29 -0700320 break;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700321
Josh Gao9da1f512018-08-06 15:38:29 -0700322 default:
323 __builtin_unreachable();
324 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700325}
326
327// Wait for a process to clone and return the child's pid.
328// Note: this leaves the parent in PTRACE_EVENT_STOP.
329static pid_t wait_for_clone(pid_t pid, bool resume_child) {
330 int status;
331 pid_t result = TEMP_FAILURE_RETRY(waitpid(pid, &status, __WALL));
332 if (result == -1) {
333 PLOG(FATAL) << "failed to waitpid";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700334 }
335
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700336 if (WIFEXITED(status)) {
337 LOG(FATAL) << "traced process exited with status " << WEXITSTATUS(status);
338 } else if (WIFSIGNALED(status)) {
339 LOG(FATAL) << "traced process exited with signal " << WTERMSIG(status);
340 } else if (!WIFSTOPPED(status)) {
341 LOG(FATAL) << "process didn't stop? (status = " << status << ")";
342 }
343
344 if (status >> 8 != (SIGTRAP | (PTRACE_EVENT_CLONE << 8))) {
345 LOG(FATAL) << "process didn't stop due to PTRACE_O_TRACECLONE (status = " << status << ")";
346 }
347
348 pid_t child;
349 if (ptrace(PTRACE_GETEVENTMSG, pid, 0, &child) != 0) {
350 PLOG(FATAL) << "failed to get child pid via PTRACE_GETEVENTMSG";
351 }
352
353 int stop_signal;
354 if (!wait_for_stop(child, &stop_signal)) {
355 PLOG(FATAL) << "failed to waitpid on child";
356 }
357
358 CHECK_EQ(0, stop_signal);
359
360 if (resume_child) {
361 if (ptrace(PTRACE_CONT, child, 0, 0) != 0) {
362 PLOG(FATAL) << "failed to resume child (pid = " << child << ")";
363 }
364 }
365
366 return child;
367}
368
369static pid_t wait_for_vm_process(pid_t pseudothread_tid) {
370 // The pseudothread will double-fork, we want its grandchild.
371 pid_t intermediate = wait_for_clone(pseudothread_tid, true);
372 pid_t vm_pid = wait_for_clone(intermediate, false);
373 if (ptrace(PTRACE_DETACH, intermediate, 0, 0) != 0) {
374 PLOG(FATAL) << "failed to detach from intermediate vm process";
375 }
376
377 return vm_pid;
378}
379
Jinguang Dong8ac2f272018-11-24 17:12:33 +0800380static void InstallSigPipeHandler() {
381 struct sigaction action = {};
382 action.sa_handler = SIG_IGN;
383 action.sa_flags = SA_RESTART;
384 sigaction(SIGPIPE, &action, nullptr);
385}
386
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700387int main(int argc, char** argv) {
Josh Gao38ac45d2018-04-27 13:31:47 -0700388 DefuseSignalHandlers();
Jinguang Dong8ac2f272018-11-24 17:12:33 +0800389 InstallSigPipeHandler();
Josh Gao38ac45d2018-04-27 13:31:47 -0700390
Josh Gao18cb6812019-04-16 13:17:08 -0700391 // There appears to be a bug in the kernel where our death causes SIGHUP to
392 // be sent to our process group if we exit while it has stopped jobs (e.g.
393 // because of wait_for_gdb). Use setsid to create a new process group to
394 // avoid hitting this.
395 setsid();
396
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700397 atrace_begin(ATRACE_TAG, "before reparent");
398 pid_t target_process = getppid();
399
400 // Open /proc/`getppid()` before we daemonize.
401 std::string target_proc_path = "/proc/" + std::to_string(target_process);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700402 int target_proc_fd = open(target_proc_path.c_str(), O_DIRECTORY | O_RDONLY);
403 if (target_proc_fd == -1) {
404 PLOG(FATAL) << "failed to open " << target_proc_path;
405 }
406
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700407 // Make sure getppid() hasn't changed.
408 if (getppid() != target_process) {
409 LOG(FATAL) << "parent died";
Josh Gao2a18b822017-02-16 19:17:28 -0800410 }
Josh Gao8bb03902017-05-30 15:31:02 -0700411 atrace_end(ATRACE_TAG);
412
Josh Gaocbe70cb2016-10-18 18:17:52 -0700413 // Reparent ourselves to init, so that the signal handler can waitpid on the
414 // original process to avoid leaving a zombie for non-fatal dumps.
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700415 // Move the input/output pipes off of stdout/stderr, out of paranoia.
416 unique_fd output_pipe(dup(STDOUT_FILENO));
417 unique_fd input_pipe(dup(STDIN_FILENO));
418
419 unique_fd fork_exit_read, fork_exit_write;
420 if (!Pipe(&fork_exit_read, &fork_exit_write)) {
421 PLOG(FATAL) << "failed to create pipe";
422 }
423
Josh Gaocbe70cb2016-10-18 18:17:52 -0700424 pid_t forkpid = fork();
425 if (forkpid == -1) {
426 PLOG(FATAL) << "fork failed";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700427 } else if (forkpid == 0) {
428 fork_exit_read.reset();
429 } else {
430 // We need the pseudothread to live until we get around to verifying the vm pid against it.
431 // The last thing it does is block on a waitpid on us, so wait until our child tells us to die.
432 fork_exit_write.reset();
433 char buf;
434 TEMP_FAILURE_RETRY(read(fork_exit_read.get(), &buf, sizeof(buf)));
435 _exit(0);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700436 }
437
Josh Gao8bb03902017-05-30 15:31:02 -0700438 ATRACE_NAME("after reparent");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700439 pid_t pseudothread_tid;
440 DebuggerdDumpType dump_type;
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800441 ProcessInfo process_info;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700442
443 Initialize(argv);
444 ParseArgs(argc, argv, &pseudothread_tid, &dump_type);
Josh Gao8bb03902017-05-30 15:31:02 -0700445
Josh Gao7c6e3132017-01-22 17:59:02 -0800446 // Die if we take too long.
Andreas Gampeb02851a2017-06-22 19:45:53 -0700447 //
448 // Note: processes with many threads and minidebug-info can take a bit to
449 // unwind, do not make this too small. b/62828735
Evgenii Stepanov2a55e1a2021-01-29 11:50:30 -0800450 alarm(30 * android::base::TimeoutMultiplier());
Josh Gao7c6e3132017-01-22 17:59:02 -0800451
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700452 // Get the process name (aka cmdline).
453 std::string process_name = get_process_name(g_target_thread);
Josh Gao2f11a252017-02-13 14:46:19 -0800454
455 // Collect the list of open files.
456 OpenFilesList open_files;
Josh Gao8bb03902017-05-30 15:31:02 -0700457 {
458 ATRACE_NAME("open files");
Josh Gaoce841d92018-08-06 18:26:42 -0700459 populate_open_files_list(&open_files, g_target_thread);
Josh Gao8bb03902017-05-30 15:31:02 -0700460 }
Josh Gao2f11a252017-02-13 14:46:19 -0800461
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700462 // In order to reduce the duration that we pause the process for, we ptrace
463 // the threads, fetch their registers and associated information, and then
464 // fork a separate process as a snapshot of the process's address space.
465 std::set<pid_t> threads;
466 if (!android::procinfo::GetProcessTids(g_target_thread, &threads)) {
467 PLOG(FATAL) << "failed to get process threads";
468 }
Josh Gao3407d7c2017-06-13 17:21:12 +0000469
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700470 std::map<pid_t, ThreadInfo> thread_info;
471 siginfo_t siginfo;
472 std::string error;
473
474 {
475 ATRACE_NAME("ptrace");
476 for (pid_t thread : threads) {
477 // Trace the pseudothread separately, so we can use different options.
478 if (thread == pseudothread_tid) {
479 continue;
480 }
481
482 if (!ptrace_seize_thread(target_proc_fd, thread, &error)) {
483 bool fatal = thread == g_target_thread;
484 LOG(fatal ? FATAL : WARNING) << error;
485 }
486
487 ThreadInfo info;
488 info.pid = target_process;
489 info.tid = thread;
Josh Gao5df504c2019-05-09 12:48:17 -0700490 info.uid = getuid();
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700491 info.process_name = process_name;
492 info.thread_name = get_thread_name(thread);
493
Josh Gao76e1e302021-01-26 15:53:11 -0800494 unique_fd attr_fd(openat(target_proc_fd, "attr/current", O_RDONLY | O_CLOEXEC));
495 if (!android::base::ReadFdToString(attr_fd, &info.selinux_label)) {
496 PLOG(WARNING) << "failed to read selinux label";
497 }
498
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700499 if (!ptrace_interrupt(thread, &info.signo)) {
500 PLOG(WARNING) << "failed to ptrace interrupt thread " << thread;
501 ptrace(PTRACE_DETACH, thread, 0, 0);
502 continue;
503 }
504
Peter Collingbourne864f15d2020-09-14 20:27:36 -0700505 struct iovec iov = {
506 &info.tagged_addr_ctrl,
507 sizeof(info.tagged_addr_ctrl),
508 };
509 if (ptrace(PTRACE_GETREGSET, thread, NT_ARM_TAGGED_ADDR_CTRL,
510 reinterpret_cast<void*>(&iov)) == -1) {
511 info.tagged_addr_ctrl = -1;
512 }
Peter Collingbourne864f15d2020-09-14 20:27:36 -0700513
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700514 if (thread == g_target_thread) {
515 // Read the thread's registers along with the rest of the crash info out of the pipe.
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800516 ReadCrashInfo(input_pipe, &siginfo, &info.registers, &process_info);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700517 info.siginfo = &siginfo;
518 info.signo = info.siginfo->si_signo;
519 } else {
Christopher Ferris60eb1972019-01-15 15:18:43 -0800520 info.registers.reset(unwindstack::Regs::RemoteGet(thread));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700521 if (!info.registers) {
522 PLOG(WARNING) << "failed to fetch registers for thread " << thread;
523 ptrace(PTRACE_DETACH, thread, 0, 0);
524 continue;
525 }
526 }
527
528 thread_info[thread] = std::move(info);
529 }
530 }
531
532 // Trace the pseudothread with PTRACE_O_TRACECLONE and tell it to fork.
533 if (!ptrace_seize_thread(target_proc_fd, pseudothread_tid, &error, PTRACE_O_TRACECLONE)) {
534 LOG(FATAL) << "failed to seize pseudothread: " << error;
535 }
536
537 if (TEMP_FAILURE_RETRY(write(output_pipe.get(), "\1", 1)) != 1) {
538 PLOG(FATAL) << "failed to write to pseudothread";
539 }
540
541 pid_t vm_pid = wait_for_vm_process(pseudothread_tid);
542 if (ptrace(PTRACE_DETACH, pseudothread_tid, 0, 0) != 0) {
543 PLOG(FATAL) << "failed to detach from pseudothread";
544 }
545
546 // The pseudothread can die now.
547 fork_exit_write.reset();
548
549 // Defer the message until later, for readability.
550 bool wait_for_gdb = android::base::GetBoolProperty("debug.debuggerd.wait_for_gdb", false);
Josh Gaoa48b41b2019-12-13 14:11:04 -0800551 if (siginfo.si_signo == BIONIC_SIGNAL_DEBUGGER) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700552 wait_for_gdb = false;
553 }
554
555 // Detach from all of our attached threads before resuming.
556 for (const auto& [tid, thread] : thread_info) {
Josh Gaoa48b41b2019-12-13 14:11:04 -0800557 int resume_signal = thread.signo == BIONIC_SIGNAL_DEBUGGER ? 0 : thread.signo;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700558 if (wait_for_gdb) {
559 resume_signal = 0;
560 if (tgkill(target_process, tid, SIGSTOP) != 0) {
561 PLOG(WARNING) << "failed to send SIGSTOP to " << tid;
562 }
563 }
564
565 LOG(DEBUG) << "detaching from thread " << tid;
566 if (ptrace(PTRACE_DETACH, tid, 0, resume_signal) != 0) {
567 PLOG(ERROR) << "failed to detach from thread " << tid;
568 }
569 }
570
571 // Drop our capabilities now that we've fetched all of the information we need.
Josh Gao2f11a252017-02-13 14:46:19 -0800572 drop_capabilities();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700573
Josh Gao8bb03902017-05-30 15:31:02 -0700574 {
575 ATRACE_NAME("tombstoned_connect");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700576 LOG(INFO) << "obtaining output fd from tombstoned, type: " << dump_type;
Josh Gao76e1e302021-01-26 15:53:11 -0800577 g_tombstoned_connected = tombstoned_connect(g_target_thread, &g_tombstoned_socket, &g_output_fd,
578 &g_proto_fd, dump_type);
Josh Gao8bb03902017-05-30 15:31:02 -0700579 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700580
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700581 if (g_tombstoned_connected) {
582 if (TEMP_FAILURE_RETRY(dup2(g_output_fd.get(), STDOUT_FILENO)) == -1) {
583 PLOG(ERROR) << "failed to dup2 output fd (" << g_output_fd.get() << ") to STDOUT_FILENO";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700584 }
585 } else {
586 unique_fd devnull(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR)));
587 TEMP_FAILURE_RETRY(dup2(devnull.get(), STDOUT_FILENO));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700588 g_output_fd = std::move(devnull);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700589 }
590
Josh Gao9da1f512018-08-06 15:38:29 -0700591 LOG(INFO) << "performing dump of process " << target_process
592 << " (target tid = " << g_target_thread << ")";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700593
594 int signo = siginfo.si_signo;
Josh Gaoa48b41b2019-12-13 14:11:04 -0800595 bool fatal_signal = signo != BIONIC_SIGNAL_DEBUGGER;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700596 bool backtrace = false;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700597
Josh Gaoa48b41b2019-12-13 14:11:04 -0800598 // si_value is special when used with BIONIC_SIGNAL_DEBUGGER.
Josh Gaocbe70cb2016-10-18 18:17:52 -0700599 // 0: dump tombstone
600 // 1: dump backtrace
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700601 if (!fatal_signal) {
602 int si_val = siginfo.si_value.sival_int;
603 if (si_val == 0) {
604 backtrace = false;
605 } else if (si_val == 1) {
606 backtrace = true;
607 } else {
608 LOG(WARNING) << "unknown si_value value " << si_val;
609 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700610 }
611
Josh Gaocbe70cb2016-10-18 18:17:52 -0700612 // TODO: Use seccomp to lock ourselves down.
Christopher Ferrisb05c4722020-09-23 15:51:46 -0700613 unwindstack::UnwinderFromPid unwinder(256, vm_pid, unwindstack::Regs::CurrentArch());
614 if (!unwinder.Init()) {
Christopher Ferris60eb1972019-01-15 15:18:43 -0800615 LOG(FATAL) << "Failed to init unwinder object.";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700616 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700617
Josh Gaocbe70cb2016-10-18 18:17:52 -0700618 std::string amfd_data;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700619 if (backtrace) {
Josh Gao8bb03902017-05-30 15:31:02 -0700620 ATRACE_NAME("dump_backtrace");
Christopher Ferris60eb1972019-01-15 15:18:43 -0800621 dump_backtrace(std::move(g_output_fd), &unwinder, thread_info, g_target_thread);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700622 } else {
Josh Gaoce841d92018-08-06 18:26:42 -0700623 {
624 ATRACE_NAME("fdsan table dump");
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800625 populate_fdsan_table(&open_files, unwinder.GetProcessMemory(),
626 process_info.fdsan_table_address);
Josh Gaoce841d92018-08-06 18:26:42 -0700627 }
628
629 {
630 ATRACE_NAME("engrave_tombstone");
Josh Gao76e1e302021-01-26 15:53:11 -0800631 engrave_tombstone(std::move(g_output_fd), std::move(g_proto_fd), &unwinder, thread_info,
632 g_target_thread, process_info, &open_files, &amfd_data);
Josh Gaoce841d92018-08-06 18:26:42 -0700633 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700634 }
635
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700636 if (fatal_signal) {
637 // Don't try to notify ActivityManager if it just crashed, or we might hang until timeout.
638 if (thread_info[target_process].thread_name != "system_server") {
639 activity_manager_notify(target_process, signo, amfd_data);
Josh Gao7c6e3132017-01-22 17:59:02 -0800640 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700641 }
642
643 if (wait_for_gdb) {
Josh Gao7c6e3132017-01-22 17:59:02 -0800644 // Use ALOGI to line up with output from engrave_tombstone.
645 ALOGI(
dimitry6429e202017-09-12 10:46:20 +0200646 "***********************************************************\n"
647 "* Process %d has been suspended while crashing.\n"
648 "* To attach gdbserver and start gdb, run this on the host:\n"
649 "*\n"
650 "* gdbclient.py -p %d\n"
651 "*\n"
652 "***********************************************************",
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700653 target_process, target_process);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700654 }
655
656 // Close stdout before we notify tombstoned of completion.
657 close(STDOUT_FILENO);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700658 if (g_tombstoned_connected && !tombstoned_notify_completion(g_tombstoned_socket.get())) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700659 LOG(ERROR) << "failed to notify tombstoned of completion";
660 }
661
662 return 0;
663}