blob: a23a26925d4dbb62e78a037caeabf16ff9ef5586 [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 Gaocbe70cb2016-10-18 18:17:52 -070026#include <unistd.h>
27
28#include <limits>
Josh Gao57f58f82017-03-15 23:23:22 -070029#include <map>
Josh Gaocbe70cb2016-10-18 18:17:52 -070030#include <memory>
31#include <set>
32#include <vector>
33
Elliott Hughesa029d982021-06-21 12:36:47 -070034#include <android-base/errno_restorer.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070035#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
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -070053#include <unwindstack/AndroidUnwinder.h>
54#include <unwindstack/Error.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070055#include <unwindstack/Regs.h>
56
Josh Gaoc3706662017-08-29 13:08:32 -070057#include "libdebuggerd/backtrace.h"
58#include "libdebuggerd/tombstone.h"
59#include "libdebuggerd/utility.h"
Josh Gaocbe70cb2016-10-18 18:17:52 -070060
61#include "debuggerd/handler.h"
Shikha Panwarabde59e2023-02-03 15:05:18 +000062#include "tombstone_handler.h"
Josh Gaoc3706662017-08-29 13:08:32 -070063
64#include "protocol.h"
Narayan Kamath2d377cd2017-05-10 10:58:59 +010065#include "util.h"
Josh Gaocbe70cb2016-10-18 18:17:52 -070066
Elliott Hughesa029d982021-06-21 12:36:47 -070067using android::base::ErrnoRestorer;
Josh Gaocbe70cb2016-10-18 18:17:52 -070068using android::base::StringPrintf;
Elliott Hughesa029d982021-06-21 12:36:47 -070069using android::base::unique_fd;
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) {
Elliott Hughesa029d982021-06-21 12:36:47 -070090 ErrnoRestorer errno_restorer; // In case get_tracer() fails and we fall through.
91 pid_t tracer_pid = get_tracer(tid);
92 if (tracer_pid > 0) {
Josh Gaofd13bf02017-08-18 15:37:26 -070093 *error = StringPrintf("failed to attach to thread %d, already traced by %d (%s)", tid,
Elliott Hughesa029d982021-06-21 12:36:47 -070094 tracer_pid, get_process_name(tracer_pid).c_str());
Josh Gaofd13bf02017-08-18 15:37:26 -070095 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
Mitch Phillipsdd5a80d2023-03-13 15:08:17 -0700145static bool activity_manager_notify(pid_t pid, int signal, const std::string& amfd_data,
146 bool recoverable_gwp_asan_crash) {
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 = {
Peter Collingbournefb5eac92021-03-11 12:51:25 -0800156 .tv_sec = 1 * android::base::HwTimeoutMultiplier(),
Evgenii Stepanov2a55e1a2021-01-29 11:50:30 -0800157 .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 }
Peter Collingbournefb5eac92021-03-11 12:51:25 -0800163 tv.tv_sec = 3 * android::base::HwTimeoutMultiplier(); // 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
Mitch Phillipsdd5a80d2023-03-13 15:08:17 -0700169 // Activity Manager protocol:
170 // - 32-bit network-byte-order: pid
171 // - 32-bit network-byte-order: signal number
172 // - byte: recoverable_gwp_asan_crash
173 // - bytes: raw text of the dump
174 // - null terminator
175
Josh Gaocbe70cb2016-10-18 18:17:52 -0700176 uint32_t datum = htonl(pid);
Mitch Phillipsdd5a80d2023-03-13 15:08:17 -0700177 if (!android::base::WriteFully(amfd, &datum, sizeof(datum))) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700178 PLOG(ERROR) << "AM pid write failed";
179 return false;
180 }
Mitch Phillipsdd5a80d2023-03-13 15:08:17 -0700181
Josh Gaocbe70cb2016-10-18 18:17:52 -0700182 datum = htonl(signal);
Mitch Phillipsdd5a80d2023-03-13 15:08:17 -0700183 if (!android::base::WriteFully(amfd, &datum, sizeof(datum))) {
184 PLOG(ERROR) << "AM signo write failed";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700185 return false;
186 }
Mitch Phillipsdd5a80d2023-03-13 15:08:17 -0700187
188 uint8_t recoverable_gwp_asan_crash_byte = recoverable_gwp_asan_crash ? 1 : 0;
189 if (!android::base::WriteFully(amfd, &recoverable_gwp_asan_crash_byte,
190 sizeof(recoverable_gwp_asan_crash_byte))) {
191 PLOG(ERROR) << "AM recoverable_gwp_asan_crash_byte write failed";
192 return false;
193 }
194
Josh Gaocbe70cb2016-10-18 18:17:52 -0700195 if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size() + 1)) {
196 PLOG(ERROR) << "AM data write failed";
197 return false;
198 }
199
200 // 3 sec timeout reading the ack; we're fine if the read fails.
201 char ack;
202 android::base::ReadFully(amfd, &ack, 1);
203 return true;
204}
205
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700206// Globals used by the abort handler.
207static pid_t g_target_thread = -1;
208static bool g_tombstoned_connected = false;
209static unique_fd g_tombstoned_socket;
210static unique_fd g_output_fd;
Josh Gao76e1e302021-01-26 15:53:11 -0800211static unique_fd g_proto_fd;
Josh Gao57594112017-01-22 17:41:15 -0800212
Josh Gao38ac45d2018-04-27 13:31:47 -0700213static void DefuseSignalHandlers() {
214 // Don't try to dump ourselves.
215 struct sigaction action = {};
216 action.sa_handler = SIG_DFL;
217 debuggerd_register_handlers(&action);
218
219 sigset_t mask;
220 sigemptyset(&mask);
221 if (sigprocmask(SIG_SETMASK, &mask, nullptr) != 0) {
222 PLOG(FATAL) << "failed to set signal mask";
223 }
224}
225
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700226static void Initialize(char** argv) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700227 android::base::InitLogging(argv);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700228 android::base::SetAborter([](const char* abort_msg) {
229 // If we abort before we get an output fd, contact tombstoned to let any
230 // potential listeners know that we failed.
231 if (!g_tombstoned_connected) {
Shikha Panwarabde59e2023-02-03 15:05:18 +0000232 if (!connect_tombstone_server(g_target_thread, &g_tombstoned_socket, &g_output_fd,
233 &g_proto_fd, kDebuggerdAnyIntercept)) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700234 // We failed to connect, not much we can do.
235 LOG(ERROR) << "failed to connected to tombstoned to report failure";
236 _exit(1);
237 }
238 }
239
240 dprintf(g_output_fd.get(), "crash_dump failed to dump process");
241 if (g_target_thread != 1) {
242 dprintf(g_output_fd.get(), " %d: %s\n", g_target_thread, abort_msg);
243 } else {
244 dprintf(g_output_fd.get(), ": %s\n", abort_msg);
245 }
246
247 _exit(1);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700248 });
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700249}
Josh Gaoe7402502017-06-01 11:55:25 -0700250
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700251static void ParseArgs(int argc, char** argv, pid_t* pseudothread_tid, DebuggerdDumpType* dump_type) {
Narayan Kamatha73df602017-05-24 15:07:25 +0100252 if (argc != 4) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700253 LOG(FATAL) << "wrong number of args: " << argc << " (expected 4)";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700254 }
255
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700256 if (!android::base::ParseInt(argv[1], &g_target_thread, 1, std::numeric_limits<pid_t>::max())) {
257 LOG(FATAL) << "invalid target tid: " << argv[1];
Josh Gaocbe70cb2016-10-18 18:17:52 -0700258 }
259
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700260 if (!android::base::ParseInt(argv[2], pseudothread_tid, 1, std::numeric_limits<pid_t>::max())) {
Josh Gaof6ad5852017-02-15 12:21:11 -0800261 LOG(FATAL) << "invalid pseudothread tid: " << argv[2];
Josh Gao2f11a252017-02-13 14:46:19 -0800262 }
263
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700264 int dump_type_int;
Josh Gao76e1e302021-01-26 15:53:11 -0800265 if (!android::base::ParseInt(argv[3], &dump_type_int, 0)) {
Narayan Kamatha73df602017-05-24 15:07:25 +0100266 LOG(FATAL) << "invalid requested dump type: " << argv[3];
267 }
Josh Gao76e1e302021-01-26 15:53:11 -0800268
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700269 *dump_type = static_cast<DebuggerdDumpType>(dump_type_int);
Josh Gao76e1e302021-01-26 15:53:11 -0800270 switch (*dump_type) {
271 case kDebuggerdNativeBacktrace:
272 case kDebuggerdTombstone:
273 case kDebuggerdTombstoneProto:
274 break;
275
276 default:
277 LOG(FATAL) << "invalid requested dump type: " << dump_type_int;
278 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700279}
Narayan Kamatha73df602017-05-24 15:07:25 +0100280
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700281static void ReadCrashInfo(unique_fd& fd, siginfo_t* siginfo,
Mitch Phillips8fe51272023-02-07 17:02:30 -0800282 std::unique_ptr<unwindstack::Regs>* regs, ProcessInfo* process_info,
283 bool* recoverable_gwp_asan_crash) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700284 std::aligned_storage<sizeof(CrashInfo) + 1, alignof(CrashInfo)>::type buf;
Josh Gao9da1f512018-08-06 15:38:29 -0700285 CrashInfo* crash_info = reinterpret_cast<CrashInfo*>(&buf);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700286 ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), &buf, sizeof(buf)));
Mitch Phillips8fe51272023-02-07 17:02:30 -0800287 *recoverable_gwp_asan_crash = false;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700288 if (rc == -1) {
289 PLOG(FATAL) << "failed to read target ucontext";
Josh Gao9da1f512018-08-06 15:38:29 -0700290 } else {
291 ssize_t expected_size = 0;
292 switch (crash_info->header.version) {
293 case 1:
Josh Gao9da1f512018-08-06 15:38:29 -0700294 case 2:
Peter Collingbournef3d542f2020-03-05 16:46:15 -0800295 case 3:
296 expected_size = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataStatic);
Josh Gao9da1f512018-08-06 15:38:29 -0700297 break;
298
Peter Collingbournef3d542f2020-03-05 16:46:15 -0800299 case 4:
300 expected_size = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataDynamic);
Mitch Phillipse0b4bb12020-02-14 14:54:31 -0800301 break;
302
Josh Gao9da1f512018-08-06 15:38:29 -0700303 default:
304 LOG(FATAL) << "unexpected CrashInfo version: " << crash_info->header.version;
305 break;
306 };
307
Peter Collingbournef3d542f2020-03-05 16:46:15 -0800308 if (rc < expected_size) {
Josh Gao9da1f512018-08-06 15:38:29 -0700309 LOG(FATAL) << "read " << rc << " bytes when reading target crash information, expected "
310 << expected_size;
311 }
Josh Gaoc7fe0602017-03-13 14:13:29 -0700312 }
313
Josh Gao9da1f512018-08-06 15:38:29 -0700314 switch (crash_info->header.version) {
Peter Collingbournef3d542f2020-03-05 16:46:15 -0800315 case 4:
316 process_info->fdsan_table_address = crash_info->data.d.fdsan_table_address;
317 process_info->gwp_asan_state = crash_info->data.d.gwp_asan_state;
318 process_info->gwp_asan_metadata = crash_info->data.d.gwp_asan_metadata;
Peter Collingbournef8622522020-04-07 14:07:32 -0700319 process_info->scudo_stack_depot = crash_info->data.d.scudo_stack_depot;
Florian Mayere8fcfee2023-12-04 16:39:15 -0800320 process_info->scudo_stack_depot_size = crash_info->data.d.scudo_stack_depot_size;
Peter Collingbournef8622522020-04-07 14:07:32 -0700321 process_info->scudo_region_info = crash_info->data.d.scudo_region_info;
Peter Collingbournebb4b49c2021-01-06 21:02:19 -0800322 process_info->scudo_ring_buffer = crash_info->data.d.scudo_ring_buffer;
Florian Mayerbd49c382022-12-22 16:15:23 -0800323 process_info->scudo_ring_buffer_size = crash_info->data.d.scudo_ring_buffer_size;
Mitch Phillips8fe51272023-02-07 17:02:30 -0800324 *recoverable_gwp_asan_crash = crash_info->data.d.recoverable_gwp_asan_crash;
Florian Mayer5fa66632024-02-07 16:42:23 -0800325 process_info->crash_detail_page = crash_info->data.d.crash_detail_page;
Josh Gao8d44b142018-09-18 13:22:22 -0700326 FALLTHROUGH_INTENDED;
Josh Gao9da1f512018-08-06 15:38:29 -0700327 case 1:
Peter Collingbournef3d542f2020-03-05 16:46:15 -0800328 case 2:
329 case 3:
330 process_info->abort_msg_address = crash_info->data.s.abort_msg_address;
331 *siginfo = crash_info->data.s.siginfo;
Peter Collingbournef03af882020-03-20 18:09:00 -0700332 if (signal_has_si_addr(siginfo)) {
Peter Collingbournef03af882020-03-20 18:09:00 -0700333 process_info->has_fault_address = true;
Mitch Phillipse4adff02021-01-21 20:41:50 -0800334 process_info->maybe_tagged_fault_address = reinterpret_cast<uintptr_t>(siginfo->si_addr);
335 process_info->untagged_fault_address =
336 untag_address(reinterpret_cast<uintptr_t>(siginfo->si_addr));
Peter Collingbournef03af882020-03-20 18:09:00 -0700337 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800338 regs->reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(),
Peter Collingbournef3d542f2020-03-05 16:46:15 -0800339 &crash_info->data.s.ucontext));
Josh Gao9da1f512018-08-06 15:38:29 -0700340 break;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700341
Josh Gao9da1f512018-08-06 15:38:29 -0700342 default:
343 __builtin_unreachable();
344 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700345}
346
347// Wait for a process to clone and return the child's pid.
348// Note: this leaves the parent in PTRACE_EVENT_STOP.
349static pid_t wait_for_clone(pid_t pid, bool resume_child) {
350 int status;
351 pid_t result = TEMP_FAILURE_RETRY(waitpid(pid, &status, __WALL));
352 if (result == -1) {
353 PLOG(FATAL) << "failed to waitpid";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700354 }
355
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700356 if (WIFEXITED(status)) {
357 LOG(FATAL) << "traced process exited with status " << WEXITSTATUS(status);
358 } else if (WIFSIGNALED(status)) {
359 LOG(FATAL) << "traced process exited with signal " << WTERMSIG(status);
360 } else if (!WIFSTOPPED(status)) {
361 LOG(FATAL) << "process didn't stop? (status = " << status << ")";
362 }
363
364 if (status >> 8 != (SIGTRAP | (PTRACE_EVENT_CLONE << 8))) {
365 LOG(FATAL) << "process didn't stop due to PTRACE_O_TRACECLONE (status = " << status << ")";
366 }
367
368 pid_t child;
369 if (ptrace(PTRACE_GETEVENTMSG, pid, 0, &child) != 0) {
370 PLOG(FATAL) << "failed to get child pid via PTRACE_GETEVENTMSG";
371 }
372
373 int stop_signal;
374 if (!wait_for_stop(child, &stop_signal)) {
375 PLOG(FATAL) << "failed to waitpid on child";
376 }
377
378 CHECK_EQ(0, stop_signal);
379
380 if (resume_child) {
381 if (ptrace(PTRACE_CONT, child, 0, 0) != 0) {
382 PLOG(FATAL) << "failed to resume child (pid = " << child << ")";
383 }
384 }
385
386 return child;
387}
388
389static pid_t wait_for_vm_process(pid_t pseudothread_tid) {
390 // The pseudothread will double-fork, we want its grandchild.
391 pid_t intermediate = wait_for_clone(pseudothread_tid, true);
392 pid_t vm_pid = wait_for_clone(intermediate, false);
393 if (ptrace(PTRACE_DETACH, intermediate, 0, 0) != 0) {
394 PLOG(FATAL) << "failed to detach from intermediate vm process";
395 }
396
397 return vm_pid;
398}
399
Jinguang Dong8ac2f272018-11-24 17:12:33 +0800400static void InstallSigPipeHandler() {
401 struct sigaction action = {};
402 action.sa_handler = SIG_IGN;
403 action.sa_flags = SA_RESTART;
404 sigaction(SIGPIPE, &action, nullptr);
405}
406
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700407int main(int argc, char** argv) {
Josh Gao38ac45d2018-04-27 13:31:47 -0700408 DefuseSignalHandlers();
Jinguang Dong8ac2f272018-11-24 17:12:33 +0800409 InstallSigPipeHandler();
Josh Gao38ac45d2018-04-27 13:31:47 -0700410
Josh Gao18cb6812019-04-16 13:17:08 -0700411 // There appears to be a bug in the kernel where our death causes SIGHUP to
412 // be sent to our process group if we exit while it has stopped jobs (e.g.
Elliott Hughese4781d52021-03-17 09:15:15 -0700413 // because of wait_for_debugger). Use setsid to create a new process group to
Josh Gao18cb6812019-04-16 13:17:08 -0700414 // avoid hitting this.
415 setsid();
416
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700417 atrace_begin(ATRACE_TAG, "before reparent");
418 pid_t target_process = getppid();
419
420 // Open /proc/`getppid()` before we daemonize.
421 std::string target_proc_path = "/proc/" + std::to_string(target_process);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700422 int target_proc_fd = open(target_proc_path.c_str(), O_DIRECTORY | O_RDONLY);
423 if (target_proc_fd == -1) {
424 PLOG(FATAL) << "failed to open " << target_proc_path;
425 }
426
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700427 // Make sure getppid() hasn't changed.
428 if (getppid() != target_process) {
429 LOG(FATAL) << "parent died";
Josh Gao2a18b822017-02-16 19:17:28 -0800430 }
Josh Gao8bb03902017-05-30 15:31:02 -0700431 atrace_end(ATRACE_TAG);
432
Josh Gaocbe70cb2016-10-18 18:17:52 -0700433 // Reparent ourselves to init, so that the signal handler can waitpid on the
434 // original process to avoid leaving a zombie for non-fatal dumps.
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700435 // Move the input/output pipes off of stdout/stderr, out of paranoia.
436 unique_fd output_pipe(dup(STDOUT_FILENO));
437 unique_fd input_pipe(dup(STDIN_FILENO));
438
439 unique_fd fork_exit_read, fork_exit_write;
440 if (!Pipe(&fork_exit_read, &fork_exit_write)) {
441 PLOG(FATAL) << "failed to create pipe";
442 }
443
Josh Gaocbe70cb2016-10-18 18:17:52 -0700444 pid_t forkpid = fork();
445 if (forkpid == -1) {
446 PLOG(FATAL) << "fork failed";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700447 } else if (forkpid == 0) {
448 fork_exit_read.reset();
449 } else {
450 // We need the pseudothread to live until we get around to verifying the vm pid against it.
451 // The last thing it does is block on a waitpid on us, so wait until our child tells us to die.
452 fork_exit_write.reset();
453 char buf;
454 TEMP_FAILURE_RETRY(read(fork_exit_read.get(), &buf, sizeof(buf)));
455 _exit(0);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700456 }
457
Josh Gao8bb03902017-05-30 15:31:02 -0700458 ATRACE_NAME("after reparent");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700459 pid_t pseudothread_tid;
460 DebuggerdDumpType dump_type;
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800461 ProcessInfo process_info;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700462
463 Initialize(argv);
464 ParseArgs(argc, argv, &pseudothread_tid, &dump_type);
Josh Gao8bb03902017-05-30 15:31:02 -0700465
Josh Gao7c6e3132017-01-22 17:59:02 -0800466 // Die if we take too long.
Andreas Gampeb02851a2017-06-22 19:45:53 -0700467 //
468 // Note: processes with many threads and minidebug-info can take a bit to
469 // unwind, do not make this too small. b/62828735
Peter Collingbournefb5eac92021-03-11 12:51:25 -0800470 alarm(30 * android::base::HwTimeoutMultiplier());
Josh Gao7c6e3132017-01-22 17:59:02 -0800471
Josh Gao2f11a252017-02-13 14:46:19 -0800472 // Collect the list of open files.
473 OpenFilesList open_files;
Josh Gao8bb03902017-05-30 15:31:02 -0700474 {
475 ATRACE_NAME("open files");
Josh Gaoce841d92018-08-06 18:26:42 -0700476 populate_open_files_list(&open_files, g_target_thread);
Josh Gao8bb03902017-05-30 15:31:02 -0700477 }
Josh Gao2f11a252017-02-13 14:46:19 -0800478
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700479 // In order to reduce the duration that we pause the process for, we ptrace
480 // the threads, fetch their registers and associated information, and then
481 // fork a separate process as a snapshot of the process's address space.
482 std::set<pid_t> threads;
483 if (!android::procinfo::GetProcessTids(g_target_thread, &threads)) {
484 PLOG(FATAL) << "failed to get process threads";
485 }
Josh Gao3407d7c2017-06-13 17:21:12 +0000486
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700487 std::map<pid_t, ThreadInfo> thread_info;
488 siginfo_t siginfo;
489 std::string error;
Mitch Phillips8fe51272023-02-07 17:02:30 -0800490 bool recoverable_gwp_asan_crash = false;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700491
492 {
493 ATRACE_NAME("ptrace");
494 for (pid_t thread : threads) {
495 // Trace the pseudothread separately, so we can use different options.
496 if (thread == pseudothread_tid) {
497 continue;
498 }
499
500 if (!ptrace_seize_thread(target_proc_fd, thread, &error)) {
501 bool fatal = thread == g_target_thread;
502 LOG(fatal ? FATAL : WARNING) << error;
503 }
504
505 ThreadInfo info;
506 info.pid = target_process;
507 info.tid = thread;
Josh Gao5df504c2019-05-09 12:48:17 -0700508 info.uid = getuid();
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700509 info.thread_name = get_thread_name(thread);
510
Josh Gao76e1e302021-01-26 15:53:11 -0800511 unique_fd attr_fd(openat(target_proc_fd, "attr/current", O_RDONLY | O_CLOEXEC));
512 if (!android::base::ReadFdToString(attr_fd, &info.selinux_label)) {
513 PLOG(WARNING) << "failed to read selinux label";
514 }
515
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700516 if (!ptrace_interrupt(thread, &info.signo)) {
517 PLOG(WARNING) << "failed to ptrace interrupt thread " << thread;
518 ptrace(PTRACE_DETACH, thread, 0, 0);
519 continue;
520 }
521
Elliott Hughesd13ea522022-01-13 09:20:26 -0800522 struct iovec tagged_addr_iov = {
Peter Collingbourne864f15d2020-09-14 20:27:36 -0700523 &info.tagged_addr_ctrl,
524 sizeof(info.tagged_addr_ctrl),
525 };
526 if (ptrace(PTRACE_GETREGSET, thread, NT_ARM_TAGGED_ADDR_CTRL,
Elliott Hughesd13ea522022-01-13 09:20:26 -0800527 reinterpret_cast<void*>(&tagged_addr_iov)) == -1) {
Peter Collingbourne864f15d2020-09-14 20:27:36 -0700528 info.tagged_addr_ctrl = -1;
529 }
Peter Collingbourne864f15d2020-09-14 20:27:36 -0700530
Elliott Hughesd13ea522022-01-13 09:20:26 -0800531 struct iovec pac_enabled_keys_iov = {
532 &info.pac_enabled_keys,
533 sizeof(info.pac_enabled_keys),
534 };
535 if (ptrace(PTRACE_GETREGSET, thread, NT_ARM_PAC_ENABLED_KEYS,
536 reinterpret_cast<void*>(&pac_enabled_keys_iov)) == -1) {
537 info.pac_enabled_keys = -1;
538 }
539
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700540 if (thread == g_target_thread) {
541 // Read the thread's registers along with the rest of the crash info out of the pipe.
Mitch Phillips8fe51272023-02-07 17:02:30 -0800542 ReadCrashInfo(input_pipe, &siginfo, &info.registers, &process_info,
543 &recoverable_gwp_asan_crash);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700544 info.siginfo = &siginfo;
545 info.signo = info.siginfo->si_signo;
Josh Gao31348a72021-03-29 21:53:42 -0700546
547 info.command_line = get_command_line(g_target_thread);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700548 } else {
Christopher Ferris60eb1972019-01-15 15:18:43 -0800549 info.registers.reset(unwindstack::Regs::RemoteGet(thread));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700550 if (!info.registers) {
551 PLOG(WARNING) << "failed to fetch registers for thread " << thread;
552 ptrace(PTRACE_DETACH, thread, 0, 0);
553 continue;
554 }
555 }
556
557 thread_info[thread] = std::move(info);
558 }
559 }
560
561 // Trace the pseudothread with PTRACE_O_TRACECLONE and tell it to fork.
562 if (!ptrace_seize_thread(target_proc_fd, pseudothread_tid, &error, PTRACE_O_TRACECLONE)) {
563 LOG(FATAL) << "failed to seize pseudothread: " << error;
564 }
565
566 if (TEMP_FAILURE_RETRY(write(output_pipe.get(), "\1", 1)) != 1) {
567 PLOG(FATAL) << "failed to write to pseudothread";
568 }
569
570 pid_t vm_pid = wait_for_vm_process(pseudothread_tid);
571 if (ptrace(PTRACE_DETACH, pseudothread_tid, 0, 0) != 0) {
572 PLOG(FATAL) << "failed to detach from pseudothread";
573 }
574
575 // The pseudothread can die now.
576 fork_exit_write.reset();
577
578 // Defer the message until later, for readability.
Elliott Hughese4781d52021-03-17 09:15:15 -0700579 bool wait_for_debugger = android::base::GetBoolProperty(
580 "debug.debuggerd.wait_for_debugger",
581 android::base::GetBoolProperty("debug.debuggerd.wait_for_gdb", false));
Josh Gaoa48b41b2019-12-13 14:11:04 -0800582 if (siginfo.si_signo == BIONIC_SIGNAL_DEBUGGER) {
Elliott Hughese4781d52021-03-17 09:15:15 -0700583 wait_for_debugger = false;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700584 }
585
586 // Detach from all of our attached threads before resuming.
587 for (const auto& [tid, thread] : thread_info) {
Josh Gaoa48b41b2019-12-13 14:11:04 -0800588 int resume_signal = thread.signo == BIONIC_SIGNAL_DEBUGGER ? 0 : thread.signo;
Elliott Hughese4781d52021-03-17 09:15:15 -0700589 if (wait_for_debugger) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700590 resume_signal = 0;
591 if (tgkill(target_process, tid, SIGSTOP) != 0) {
592 PLOG(WARNING) << "failed to send SIGSTOP to " << tid;
593 }
594 }
595
596 LOG(DEBUG) << "detaching from thread " << tid;
597 if (ptrace(PTRACE_DETACH, tid, 0, resume_signal) != 0) {
598 PLOG(ERROR) << "failed to detach from thread " << tid;
599 }
600 }
601
602 // Drop our capabilities now that we've fetched all of the information we need.
Josh Gao2f11a252017-02-13 14:46:19 -0800603 drop_capabilities();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700604
Josh Gao8bb03902017-05-30 15:31:02 -0700605 {
606 ATRACE_NAME("tombstoned_connect");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700607 LOG(INFO) << "obtaining output fd from tombstoned, type: " << dump_type;
Shikha Panwarabde59e2023-02-03 15:05:18 +0000608 g_tombstoned_connected = connect_tombstone_server(g_target_thread, &g_tombstoned_socket,
609 &g_output_fd, &g_proto_fd, dump_type);
Josh Gao8bb03902017-05-30 15:31:02 -0700610 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700611
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700612 if (g_tombstoned_connected) {
613 if (TEMP_FAILURE_RETRY(dup2(g_output_fd.get(), STDOUT_FILENO)) == -1) {
614 PLOG(ERROR) << "failed to dup2 output fd (" << g_output_fd.get() << ") to STDOUT_FILENO";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700615 }
616 } else {
617 unique_fd devnull(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR)));
618 TEMP_FAILURE_RETRY(dup2(devnull.get(), STDOUT_FILENO));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700619 g_output_fd = std::move(devnull);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700620 }
621
Josh Gao9da1f512018-08-06 15:38:29 -0700622 LOG(INFO) << "performing dump of process " << target_process
623 << " (target tid = " << g_target_thread << ")";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700624
625 int signo = siginfo.si_signo;
Josh Gaoa48b41b2019-12-13 14:11:04 -0800626 bool fatal_signal = signo != BIONIC_SIGNAL_DEBUGGER;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700627 bool backtrace = false;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700628
Josh Gaoa48b41b2019-12-13 14:11:04 -0800629 // si_value is special when used with BIONIC_SIGNAL_DEBUGGER.
Josh Gaocbe70cb2016-10-18 18:17:52 -0700630 // 0: dump tombstone
631 // 1: dump backtrace
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700632 if (!fatal_signal) {
633 int si_val = siginfo.si_value.sival_int;
634 if (si_val == 0) {
635 backtrace = false;
636 } else if (si_val == 1) {
637 backtrace = true;
638 } else {
639 LOG(WARNING) << "unknown si_value value " << si_val;
640 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700641 }
642
Josh Gaocbe70cb2016-10-18 18:17:52 -0700643 // TODO: Use seccomp to lock ourselves down.
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700644
645 unwindstack::AndroidRemoteUnwinder unwinder(vm_pid, unwindstack::Regs::CurrentArch());
646 unwindstack::ErrorData error_data;
647 if (!unwinder.Initialize(error_data)) {
648 LOG(FATAL) << "Failed to initialize unwinder object: "
649 << unwindstack::GetErrorCodeString(error_data.code);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700650 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700651
Josh Gaocbe70cb2016-10-18 18:17:52 -0700652 std::string amfd_data;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700653 if (backtrace) {
Josh Gao8bb03902017-05-30 15:31:02 -0700654 ATRACE_NAME("dump_backtrace");
Christopher Ferris60eb1972019-01-15 15:18:43 -0800655 dump_backtrace(std::move(g_output_fd), &unwinder, thread_info, g_target_thread);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700656 } else {
Josh Gaoce841d92018-08-06 18:26:42 -0700657 {
658 ATRACE_NAME("fdsan table dump");
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800659 populate_fdsan_table(&open_files, unwinder.GetProcessMemory(),
660 process_info.fdsan_table_address);
Josh Gaoce841d92018-08-06 18:26:42 -0700661 }
662
663 {
664 ATRACE_NAME("engrave_tombstone");
Josh Gao76e1e302021-01-26 15:53:11 -0800665 engrave_tombstone(std::move(g_output_fd), std::move(g_proto_fd), &unwinder, thread_info,
666 g_target_thread, process_info, &open_files, &amfd_data);
Josh Gaoce841d92018-08-06 18:26:42 -0700667 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700668 }
669
Mitch Phillipsdd5a80d2023-03-13 15:08:17 -0700670 if (fatal_signal) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700671 // Don't try to notify ActivityManager if it just crashed, or we might hang until timeout.
672 if (thread_info[target_process].thread_name != "system_server") {
Mitch Phillipsdd5a80d2023-03-13 15:08:17 -0700673 activity_manager_notify(target_process, signo, amfd_data, recoverable_gwp_asan_crash);
Josh Gao7c6e3132017-01-22 17:59:02 -0800674 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700675 }
676
Elliott Hughese4781d52021-03-17 09:15:15 -0700677 if (wait_for_debugger) {
Josh Gao7c6e3132017-01-22 17:59:02 -0800678 // Use ALOGI to line up with output from engrave_tombstone.
679 ALOGI(
dimitry6429e202017-09-12 10:46:20 +0200680 "***********************************************************\n"
681 "* Process %d has been suspended while crashing.\n"
Elliott Hughese4781d52021-03-17 09:15:15 -0700682 "* To attach the debugger, run this on the host:\n"
dimitry6429e202017-09-12 10:46:20 +0200683 "*\n"
Elliott Hughes5a4e6252023-02-28 22:16:19 +0000684 "* lldbclient.py -p %d\n"
dimitry6429e202017-09-12 10:46:20 +0200685 "*\n"
686 "***********************************************************",
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700687 target_process, target_process);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700688 }
689
690 // Close stdout before we notify tombstoned of completion.
691 close(STDOUT_FILENO);
Shikha Panwarabde59e2023-02-03 15:05:18 +0000692 if (g_tombstoned_connected &&
693 !notify_completion(g_tombstoned_socket.get(), g_output_fd.get(), g_proto_fd.get())) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700694 LOG(ERROR) << "failed to notify tombstoned of completion";
695 }
696
697 return 0;
698}