blob: 1c1fb8a598e2019b172f341c9c8a95d5a462bdf9 [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
Sijie Chencb53fe12024-05-10 18:40:48 +000028#include <cstdint>
Josh Gaocbe70cb2016-10-18 18:17:52 -070029#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
Elliott Hughesa029d982021-06-21 12:36:47 -070035#include <android-base/errno_restorer.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070036#include <android-base/file.h>
37#include <android-base/logging.h>
Josh Gao8d44b142018-09-18 13:22:22 -070038#include <android-base/macros.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070039#include <android-base/parseint.h>
40#include <android-base/properties.h>
41#include <android-base/stringprintf.h>
Josh Gao57f58f82017-03-15 23:23:22 -070042#include <android-base/strings.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070043#include <android-base/unique_fd.h>
Mitch Phillipse4adff02021-01-21 20:41:50 -080044#include <bionic/macros.h>
Josh Gaoa48b41b2019-12-13 14:11:04 -080045#include <bionic/reserved_signals.h>
Sijie Chencb53fe12024-05-10 18:40:48 +000046#include <bionic/tls_defines.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070047#include <cutils/sockets.h>
Vijay Venkatramana95acea2017-01-23 20:11:51 -080048#include <log/log.h>
Josh Gaob0e51e32017-06-01 12:08:10 -070049#include <private/android_filesystem_config.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070050#include <procinfo/process.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070051
Josh Gao8bb03902017-05-30 15:31:02 -070052#define ATRACE_TAG ATRACE_TAG_BIONIC
53#include <utils/Trace.h>
54
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -070055#include <unwindstack/AndroidUnwinder.h>
56#include <unwindstack/Error.h>
Sijie Chencb53fe12024-05-10 18:40:48 +000057#include <unwindstack/MachineArm.h>
58#include <unwindstack/MachineArm64.h>
59#include <unwindstack/MachineRiscv64.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070060#include <unwindstack/Regs.h>
Sijie Chencb53fe12024-05-10 18:40:48 +000061#include <unwindstack/RegsArm.h>
62#include <unwindstack/RegsArm64.h>
63#include <unwindstack/RegsRiscv64.h>
64#include <unwindstack/UserArm.h>
65#include <unwindstack/UserArm64.h>
66#include <unwindstack/UserRiscv64.h>
67
68#include <native_bridge_support/guest_state_accessor/accessor.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070069
Josh Gaoc3706662017-08-29 13:08:32 -070070#include "libdebuggerd/backtrace.h"
71#include "libdebuggerd/tombstone.h"
72#include "libdebuggerd/utility.h"
Josh Gaocbe70cb2016-10-18 18:17:52 -070073
74#include "debuggerd/handler.h"
Shikha Panwarabde59e2023-02-03 15:05:18 +000075#include "tombstone_handler.h"
Josh Gaoc3706662017-08-29 13:08:32 -070076
77#include "protocol.h"
Narayan Kamath2d377cd2017-05-10 10:58:59 +010078#include "util.h"
Josh Gaocbe70cb2016-10-18 18:17:52 -070079
Elliott Hughesa029d982021-06-21 12:36:47 -070080using android::base::ErrnoRestorer;
Josh Gaocbe70cb2016-10-18 18:17:52 -070081using android::base::StringPrintf;
Elliott Hughesa029d982021-06-21 12:36:47 -070082using android::base::unique_fd;
Josh Gao57f58f82017-03-15 23:23:22 -070083
Sijie Chenc8027932024-05-10 18:40:48 +000084// This stores guest architecture. When the architecture is supported, tombstone file will output
85// guest state information.
86static Architecture g_guest_arch;
87
Josh Gaofe902762017-02-01 16:31:43 -080088static bool pid_contains_tid(int pid_proc_fd, pid_t tid) {
89 struct stat st;
90 std::string task_path = StringPrintf("task/%d", tid);
91 return fstatat(pid_proc_fd, task_path.c_str(), &st, 0) == 0;
Josh Gaocbe70cb2016-10-18 18:17:52 -070092}
93
Josh Gaofd13bf02017-08-18 15:37:26 -070094static pid_t get_tracer(pid_t tracee) {
95 // Check to see if the thread is being ptraced by another process.
96 android::procinfo::ProcessInfo process_info;
97 if (android::procinfo::GetProcessInfo(tracee, &process_info)) {
98 return process_info.tracer;
99 }
100 return -1;
101}
102
Josh Gaocbe70cb2016-10-18 18:17:52 -0700103// Attach to a thread, and verify that it's still a member of the given process
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700104static bool ptrace_seize_thread(int pid_proc_fd, pid_t tid, std::string* error, int flags = 0) {
105 if (ptrace(PTRACE_SEIZE, tid, 0, flags) != 0) {
Josh Gaofd13bf02017-08-18 15:37:26 -0700106 if (errno == EPERM) {
Elliott Hughesa029d982021-06-21 12:36:47 -0700107 ErrnoRestorer errno_restorer; // In case get_tracer() fails and we fall through.
108 pid_t tracer_pid = get_tracer(tid);
109 if (tracer_pid > 0) {
Josh Gaofd13bf02017-08-18 15:37:26 -0700110 *error = StringPrintf("failed to attach to thread %d, already traced by %d (%s)", tid,
Elliott Hughesa029d982021-06-21 12:36:47 -0700111 tracer_pid, get_process_name(tracer_pid).c_str());
Josh Gaofd13bf02017-08-18 15:37:26 -0700112 return false;
113 }
114 }
115
Josh Gao42fd74b2017-01-20 12:51:11 -0800116 *error = StringPrintf("failed to attach to thread %d: %s", tid, strerror(errno));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700117 return false;
118 }
119
120 // Make sure that the task we attached to is actually part of the pid we're dumping.
Josh Gaofe902762017-02-01 16:31:43 -0800121 if (!pid_contains_tid(pid_proc_fd, tid)) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700122 if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700123 PLOG(WARNING) << "failed to detach from thread " << tid;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700124 }
Josh Gaofe902762017-02-01 16:31:43 -0800125 *error = StringPrintf("thread %d is not in process", tid);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700126 return false;
127 }
Josh Gao122479f2017-01-22 16:42:32 -0800128
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700129 return true;
130}
131
132static bool wait_for_stop(pid_t tid, int* received_signal) {
133 while (true) {
134 int status;
135 pid_t result = waitpid(tid, &status, __WALL);
136 if (result != tid) {
137 PLOG(ERROR) << "waitpid failed on " << tid << " while detaching";
138 return false;
139 }
140
141 if (WIFSTOPPED(status)) {
142 if (status >> 16 == PTRACE_EVENT_STOP) {
143 *received_signal = 0;
144 } else {
145 *received_signal = WSTOPSIG(status);
146 }
147 return true;
148 }
149 }
150}
151
152// Interrupt a process and wait for it to be interrupted.
153static bool ptrace_interrupt(pid_t tid, int* received_signal) {
154 if (ptrace(PTRACE_INTERRUPT, tid, 0, 0) == 0) {
155 return wait_for_stop(tid, received_signal);
Josh Gao3407d7c2017-06-13 17:21:12 +0000156 }
157
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700158 PLOG(ERROR) << "failed to interrupt " << tid << " to detach";
159 return false;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700160}
161
Mitch Phillipsdd5a80d2023-03-13 15:08:17 -0700162static bool activity_manager_notify(pid_t pid, int signal, const std::string& amfd_data,
Mitch Phillips31407912024-03-14 14:18:47 +0100163 bool recoverable_crash) {
Josh Gao8bb03902017-05-30 15:31:02 -0700164 ATRACE_CALL();
Josh Gaob0e51e32017-06-01 12:08:10 -0700165 android::base::unique_fd amfd(socket_local_client(
166 "/data/system/ndebugsocket", ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700167 if (amfd.get() == -1) {
168 PLOG(ERROR) << "unable to connect to activity manager";
169 return false;
170 }
171
172 struct timeval tv = {
Peter Collingbournefb5eac92021-03-11 12:51:25 -0800173 .tv_sec = 1 * android::base::HwTimeoutMultiplier(),
Evgenii Stepanov2a55e1a2021-01-29 11:50:30 -0800174 .tv_usec = 0,
Josh Gaocbe70cb2016-10-18 18:17:52 -0700175 };
176 if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
177 PLOG(ERROR) << "failed to set send timeout on activity manager socket";
178 return false;
179 }
Peter Collingbournefb5eac92021-03-11 12:51:25 -0800180 tv.tv_sec = 3 * android::base::HwTimeoutMultiplier(); // 3 seconds on handshake read
Josh Gaocbe70cb2016-10-18 18:17:52 -0700181 if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
182 PLOG(ERROR) << "failed to set receive timeout on activity manager socket";
183 return false;
184 }
185
Mitch Phillipsdd5a80d2023-03-13 15:08:17 -0700186 // Activity Manager protocol:
187 // - 32-bit network-byte-order: pid
188 // - 32-bit network-byte-order: signal number
Mitch Phillips31407912024-03-14 14:18:47 +0100189 // - byte: recoverable_crash
Mitch Phillipsdd5a80d2023-03-13 15:08:17 -0700190 // - bytes: raw text of the dump
191 // - null terminator
192
Josh Gaocbe70cb2016-10-18 18:17:52 -0700193 uint32_t datum = htonl(pid);
Mitch Phillipsdd5a80d2023-03-13 15:08:17 -0700194 if (!android::base::WriteFully(amfd, &datum, sizeof(datum))) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700195 PLOG(ERROR) << "AM pid write failed";
196 return false;
197 }
Mitch Phillipsdd5a80d2023-03-13 15:08:17 -0700198
Josh Gaocbe70cb2016-10-18 18:17:52 -0700199 datum = htonl(signal);
Mitch Phillipsdd5a80d2023-03-13 15:08:17 -0700200 if (!android::base::WriteFully(amfd, &datum, sizeof(datum))) {
201 PLOG(ERROR) << "AM signo write failed";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700202 return false;
203 }
Mitch Phillipsdd5a80d2023-03-13 15:08:17 -0700204
Mitch Phillips31407912024-03-14 14:18:47 +0100205 uint8_t recoverable_crash_byte = recoverable_crash ? 1 : 0;
206 if (!android::base::WriteFully(amfd, &recoverable_crash_byte, sizeof(recoverable_crash_byte))) {
207 PLOG(ERROR) << "AM recoverable_crash_byte write failed";
Mitch Phillipsdd5a80d2023-03-13 15:08:17 -0700208 return false;
209 }
210
Josh Gaocbe70cb2016-10-18 18:17:52 -0700211 if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size() + 1)) {
212 PLOG(ERROR) << "AM data write failed";
213 return false;
214 }
215
216 // 3 sec timeout reading the ack; we're fine if the read fails.
217 char ack;
218 android::base::ReadFully(amfd, &ack, 1);
219 return true;
220}
221
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700222// Globals used by the abort handler.
223static pid_t g_target_thread = -1;
224static bool g_tombstoned_connected = false;
225static unique_fd g_tombstoned_socket;
226static unique_fd g_output_fd;
Josh Gao76e1e302021-01-26 15:53:11 -0800227static unique_fd g_proto_fd;
Josh Gao57594112017-01-22 17:41:15 -0800228
Josh Gao38ac45d2018-04-27 13:31:47 -0700229static void DefuseSignalHandlers() {
230 // Don't try to dump ourselves.
231 struct sigaction action = {};
232 action.sa_handler = SIG_DFL;
233 debuggerd_register_handlers(&action);
234
235 sigset_t mask;
236 sigemptyset(&mask);
237 if (sigprocmask(SIG_SETMASK, &mask, nullptr) != 0) {
238 PLOG(FATAL) << "failed to set signal mask";
239 }
240}
241
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700242static void Initialize(char** argv) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700243 android::base::InitLogging(argv);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700244 android::base::SetAborter([](const char* abort_msg) {
245 // If we abort before we get an output fd, contact tombstoned to let any
246 // potential listeners know that we failed.
247 if (!g_tombstoned_connected) {
Shikha Panwarabde59e2023-02-03 15:05:18 +0000248 if (!connect_tombstone_server(g_target_thread, &g_tombstoned_socket, &g_output_fd,
249 &g_proto_fd, kDebuggerdAnyIntercept)) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700250 // We failed to connect, not much we can do.
251 LOG(ERROR) << "failed to connected to tombstoned to report failure";
252 _exit(1);
253 }
254 }
255
256 dprintf(g_output_fd.get(), "crash_dump failed to dump process");
257 if (g_target_thread != 1) {
258 dprintf(g_output_fd.get(), " %d: %s\n", g_target_thread, abort_msg);
259 } else {
260 dprintf(g_output_fd.get(), ": %s\n", abort_msg);
261 }
262
263 _exit(1);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700264 });
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700265}
Josh Gaoe7402502017-06-01 11:55:25 -0700266
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700267static void ParseArgs(int argc, char** argv, pid_t* pseudothread_tid, DebuggerdDumpType* dump_type) {
Narayan Kamatha73df602017-05-24 15:07:25 +0100268 if (argc != 4) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700269 LOG(FATAL) << "wrong number of args: " << argc << " (expected 4)";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700270 }
271
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700272 if (!android::base::ParseInt(argv[1], &g_target_thread, 1, std::numeric_limits<pid_t>::max())) {
273 LOG(FATAL) << "invalid target tid: " << argv[1];
Josh Gaocbe70cb2016-10-18 18:17:52 -0700274 }
275
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700276 if (!android::base::ParseInt(argv[2], pseudothread_tid, 1, std::numeric_limits<pid_t>::max())) {
Josh Gaof6ad5852017-02-15 12:21:11 -0800277 LOG(FATAL) << "invalid pseudothread tid: " << argv[2];
Josh Gao2f11a252017-02-13 14:46:19 -0800278 }
279
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700280 int dump_type_int;
Josh Gao76e1e302021-01-26 15:53:11 -0800281 if (!android::base::ParseInt(argv[3], &dump_type_int, 0)) {
Narayan Kamatha73df602017-05-24 15:07:25 +0100282 LOG(FATAL) << "invalid requested dump type: " << argv[3];
283 }
Josh Gao76e1e302021-01-26 15:53:11 -0800284
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700285 *dump_type = static_cast<DebuggerdDumpType>(dump_type_int);
Josh Gao76e1e302021-01-26 15:53:11 -0800286 switch (*dump_type) {
287 case kDebuggerdNativeBacktrace:
288 case kDebuggerdTombstone:
289 case kDebuggerdTombstoneProto:
290 break;
291
292 default:
293 LOG(FATAL) << "invalid requested dump type: " << dump_type_int;
294 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700295}
Narayan Kamatha73df602017-05-24 15:07:25 +0100296
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700297static void ReadCrashInfo(unique_fd& fd, siginfo_t* siginfo,
Mitch Phillips8fe51272023-02-07 17:02:30 -0800298 std::unique_ptr<unwindstack::Regs>* regs, ProcessInfo* process_info,
Mitch Phillips31407912024-03-14 14:18:47 +0100299 bool* recoverable_crash) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700300 std::aligned_storage<sizeof(CrashInfo) + 1, alignof(CrashInfo)>::type buf;
Josh Gao9da1f512018-08-06 15:38:29 -0700301 CrashInfo* crash_info = reinterpret_cast<CrashInfo*>(&buf);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700302 ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), &buf, sizeof(buf)));
Mitch Phillips31407912024-03-14 14:18:47 +0100303 *recoverable_crash = false;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700304 if (rc == -1) {
305 PLOG(FATAL) << "failed to read target ucontext";
Florian Mayerc4b24f72024-05-16 22:59:16 +0000306 }
307 ssize_t expected_size = 0;
308 switch (crash_info->header.version) {
309 case 1:
310 case 2:
311 case 3:
312 expected_size = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataStatic);
313 break;
Josh Gao9da1f512018-08-06 15:38:29 -0700314
Florian Mayerc4b24f72024-05-16 22:59:16 +0000315 case 4:
316 expected_size = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataDynamic);
317 break;
Mitch Phillipse0b4bb12020-02-14 14:54:31 -0800318
Florian Mayerc4b24f72024-05-16 22:59:16 +0000319 default:
320 LOG(FATAL) << "unexpected CrashInfo version: " << crash_info->header.version;
321 break;
Elliott Hughesaed74ba2024-05-17 11:31:36 +0000322 }
Josh Gao9da1f512018-08-06 15:38:29 -0700323
Florian Mayerc4b24f72024-05-16 22:59:16 +0000324 if (rc < expected_size) {
325 LOG(FATAL) << "read " << rc << " bytes when reading target crash information, expected "
326 << expected_size;
Josh Gaoc7fe0602017-03-13 14:13:29 -0700327 }
328
Josh Gao9da1f512018-08-06 15:38:29 -0700329 switch (crash_info->header.version) {
Peter Collingbournef3d542f2020-03-05 16:46:15 -0800330 case 4:
331 process_info->fdsan_table_address = crash_info->data.d.fdsan_table_address;
332 process_info->gwp_asan_state = crash_info->data.d.gwp_asan_state;
333 process_info->gwp_asan_metadata = crash_info->data.d.gwp_asan_metadata;
Peter Collingbournef8622522020-04-07 14:07:32 -0700334 process_info->scudo_stack_depot = crash_info->data.d.scudo_stack_depot;
Florian Mayere8fcfee2023-12-04 16:39:15 -0800335 process_info->scudo_stack_depot_size = crash_info->data.d.scudo_stack_depot_size;
Peter Collingbournef8622522020-04-07 14:07:32 -0700336 process_info->scudo_region_info = crash_info->data.d.scudo_region_info;
Peter Collingbournebb4b49c2021-01-06 21:02:19 -0800337 process_info->scudo_ring_buffer = crash_info->data.d.scudo_ring_buffer;
Florian Mayerbd49c382022-12-22 16:15:23 -0800338 process_info->scudo_ring_buffer_size = crash_info->data.d.scudo_ring_buffer_size;
Mitch Phillips31407912024-03-14 14:18:47 +0100339 *recoverable_crash = crash_info->data.d.recoverable_crash;
Florian Mayer5fa66632024-02-07 16:42:23 -0800340 process_info->crash_detail_page = crash_info->data.d.crash_detail_page;
Josh Gao8d44b142018-09-18 13:22:22 -0700341 FALLTHROUGH_INTENDED;
Josh Gao9da1f512018-08-06 15:38:29 -0700342 case 1:
Peter Collingbournef3d542f2020-03-05 16:46:15 -0800343 case 2:
344 case 3:
345 process_info->abort_msg_address = crash_info->data.s.abort_msg_address;
346 *siginfo = crash_info->data.s.siginfo;
Peter Collingbournef03af882020-03-20 18:09:00 -0700347 if (signal_has_si_addr(siginfo)) {
Peter Collingbournef03af882020-03-20 18:09:00 -0700348 process_info->has_fault_address = true;
Mitch Phillipse4adff02021-01-21 20:41:50 -0800349 process_info->maybe_tagged_fault_address = reinterpret_cast<uintptr_t>(siginfo->si_addr);
350 process_info->untagged_fault_address =
351 untag_address(reinterpret_cast<uintptr_t>(siginfo->si_addr));
Peter Collingbournef03af882020-03-20 18:09:00 -0700352 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800353 regs->reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(),
Peter Collingbournef3d542f2020-03-05 16:46:15 -0800354 &crash_info->data.s.ucontext));
Josh Gao9da1f512018-08-06 15:38:29 -0700355 break;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700356
Josh Gao9da1f512018-08-06 15:38:29 -0700357 default:
358 __builtin_unreachable();
359 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700360}
361
362// Wait for a process to clone and return the child's pid.
363// Note: this leaves the parent in PTRACE_EVENT_STOP.
364static pid_t wait_for_clone(pid_t pid, bool resume_child) {
365 int status;
366 pid_t result = TEMP_FAILURE_RETRY(waitpid(pid, &status, __WALL));
367 if (result == -1) {
368 PLOG(FATAL) << "failed to waitpid";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700369 }
370
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700371 if (WIFEXITED(status)) {
372 LOG(FATAL) << "traced process exited with status " << WEXITSTATUS(status);
373 } else if (WIFSIGNALED(status)) {
374 LOG(FATAL) << "traced process exited with signal " << WTERMSIG(status);
375 } else if (!WIFSTOPPED(status)) {
376 LOG(FATAL) << "process didn't stop? (status = " << status << ")";
377 }
378
379 if (status >> 8 != (SIGTRAP | (PTRACE_EVENT_CLONE << 8))) {
380 LOG(FATAL) << "process didn't stop due to PTRACE_O_TRACECLONE (status = " << status << ")";
381 }
382
383 pid_t child;
384 if (ptrace(PTRACE_GETEVENTMSG, pid, 0, &child) != 0) {
385 PLOG(FATAL) << "failed to get child pid via PTRACE_GETEVENTMSG";
386 }
387
388 int stop_signal;
389 if (!wait_for_stop(child, &stop_signal)) {
390 PLOG(FATAL) << "failed to waitpid on child";
391 }
392
393 CHECK_EQ(0, stop_signal);
394
395 if (resume_child) {
396 if (ptrace(PTRACE_CONT, child, 0, 0) != 0) {
397 PLOG(FATAL) << "failed to resume child (pid = " << child << ")";
398 }
399 }
400
401 return child;
402}
403
404static pid_t wait_for_vm_process(pid_t pseudothread_tid) {
405 // The pseudothread will double-fork, we want its grandchild.
406 pid_t intermediate = wait_for_clone(pseudothread_tid, true);
407 pid_t vm_pid = wait_for_clone(intermediate, false);
408 if (ptrace(PTRACE_DETACH, intermediate, 0, 0) != 0) {
409 PLOG(FATAL) << "failed to detach from intermediate vm process";
410 }
411
412 return vm_pid;
413}
414
Jinguang Dong8ac2f272018-11-24 17:12:33 +0800415static void InstallSigPipeHandler() {
416 struct sigaction action = {};
417 action.sa_handler = SIG_IGN;
418 action.sa_flags = SA_RESTART;
419 sigaction(SIGPIPE, &action, nullptr);
420}
421
Sijie Chencb53fe12024-05-10 18:40:48 +0000422static bool PtracePeek(int request, pid_t tid, uintptr_t addr, void* data, std::string_view err_msg,
423 uintptr_t* result) {
424 errno = 0;
425 *result = ptrace(request, tid, addr, data);
426 if (errno != 0) {
427 PLOG(ERROR) << err_msg;
428 return false;
429 }
430 return true;
431}
432
433static bool GetGuestRegistersFromCrashedProcess([[maybe_unused]] pid_t tid,
434 NativeBridgeGuestRegs* guest_regs) {
435 auto process_memory = unwindstack::Memory::CreateProcessMemoryCached(tid);
436
437 uintptr_t header_ptr = 0;
438 uintptr_t base = 0;
439#if defined(__x86_64__)
440 if (!PtracePeek(PTRACE_PEEKUSER, tid, offsetof(user_regs_struct, fs_base), nullptr,
441 "failed to read thread register for thread " + std::to_string(tid), &base)) {
442 return false;
443 }
444#elif defined(__aarch64__)
445 // base is implicitly casted to uint64_t.
446 struct iovec pt_iov {
447 .iov_base = &base, .iov_len = sizeof(base),
448 };
449
450 if (ptrace(PTRACE_GETREGSET, tid, NT_ARM_TLS, &pt_iov) != 0) {
451 PLOG(ERROR) << "failed to read thread register for thread " << tid;
Sijie Chen75ed08f2024-05-23 00:10:07 +0000452 return false;
Sijie Chencb53fe12024-05-10 18:40:48 +0000453 }
Sijie Chen75ed08f2024-05-23 00:10:07 +0000454#elif defined(__riscv)
455 struct user_regs_struct regs;
456 struct iovec pt_iov = {.iov_base = &regs, .iov_len = sizeof(regs)};
457 if (ptrace(PTRACE_GETREGSET, tid, NT_PRSTATUS, &pt_iov) != 0) {
458 PLOG(ERROR) << "failed to read thread register for thread " << tid;
459 return false;
460 }
461 base = reinterpret_cast<uintptr_t>(regs.tp);
Sijie Chencb53fe12024-05-10 18:40:48 +0000462#else
463 // TODO(b/339287219): Add case for Riscv host.
464 return false;
465#endif
466 auto ptr_to_guest_slot = base + TLS_SLOT_NATIVE_BRIDGE_GUEST_STATE * sizeof(uintptr_t);
467 if (!process_memory->ReadFully(ptr_to_guest_slot, &header_ptr, sizeof(uintptr_t))) {
468 PLOG(ERROR) << "failed to get guest state TLS slot content for thread " << tid;
469 return false;
470 }
471
472 NativeBridgeGuestStateHeader header;
473 if (!process_memory->ReadFully(header_ptr, &header, sizeof(NativeBridgeGuestStateHeader))) {
474 PLOG(ERROR) << "failed to get the guest state header for thread " << tid;
475 return false;
476 }
477 if (header.signature != NATIVE_BRIDGE_GUEST_STATE_SIGNATURE) {
478 // Return when ptr points to unmapped memory or no valid guest state.
479 return false;
480 }
481 auto guest_state_data_copy = std::make_unique<unsigned char[]>(header.guest_state_data_size);
482 if (!process_memory->ReadFully(reinterpret_cast<uintptr_t>(header.guest_state_data),
483 guest_state_data_copy.get(), header.guest_state_data_size)) {
484 PLOG(ERROR) << "failed to read the guest state data for thread " << tid;
485 return false;
486 }
487
488 LoadGuestStateRegisters(guest_state_data_copy.get(), header.guest_state_data_size, guest_regs);
489 return true;
490}
491
492static void ReadGuestRegisters([[maybe_unused]] std::unique_ptr<unwindstack::Regs>* regs,
493 pid_t tid) {
494 // TODO: remove [[maybe_unused]], when the ARM32 case is removed from the native bridge support.
495 NativeBridgeGuestRegs guest_regs;
496 if (!GetGuestRegistersFromCrashedProcess(tid, &guest_regs)) {
497 return;
498 }
499
500 switch (guest_regs.guest_arch) {
501#if defined(__LP64__)
502 case NATIVE_BRIDGE_ARCH_ARM64: {
503 unwindstack::arm64_user_regs arm64_user_regs = {};
504 for (size_t i = 0; i < unwindstack::ARM64_REG_R31; i++) {
505 arm64_user_regs.regs[i] = guest_regs.regs_arm64.x[i];
506 }
507 arm64_user_regs.sp = guest_regs.regs_arm64.sp;
508 arm64_user_regs.pc = guest_regs.regs_arm64.ip;
509 regs->reset(unwindstack::RegsArm64::Read(&arm64_user_regs));
Sijie Chenc8027932024-05-10 18:40:48 +0000510
511 g_guest_arch = Architecture::ARM64;
Sijie Chencb53fe12024-05-10 18:40:48 +0000512 break;
513 }
514 case NATIVE_BRIDGE_ARCH_RISCV64: {
515 unwindstack::riscv64_user_regs riscv64_user_regs = {};
516 // RISCV64_REG_PC is at the first position.
517 riscv64_user_regs.regs[0] = guest_regs.regs_riscv64.ip;
518 for (size_t i = 1; i < unwindstack::RISCV64_REG_REAL_COUNT; i++) {
519 riscv64_user_regs.regs[i] = guest_regs.regs_riscv64.x[i];
520 }
521 regs->reset(unwindstack::RegsRiscv64::Read(&riscv64_user_regs, tid));
Sijie Chenc8027932024-05-10 18:40:48 +0000522
523 g_guest_arch = Architecture::RISCV64;
Sijie Chencb53fe12024-05-10 18:40:48 +0000524 break;
525 }
526#endif
527 default:
528 break;
529 }
530}
531
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700532int main(int argc, char** argv) {
Josh Gao38ac45d2018-04-27 13:31:47 -0700533 DefuseSignalHandlers();
Jinguang Dong8ac2f272018-11-24 17:12:33 +0800534 InstallSigPipeHandler();
Josh Gao38ac45d2018-04-27 13:31:47 -0700535
Josh Gao18cb6812019-04-16 13:17:08 -0700536 // There appears to be a bug in the kernel where our death causes SIGHUP to
537 // be sent to our process group if we exit while it has stopped jobs (e.g.
Elliott Hughese4781d52021-03-17 09:15:15 -0700538 // because of wait_for_debugger). Use setsid to create a new process group to
Josh Gao18cb6812019-04-16 13:17:08 -0700539 // avoid hitting this.
540 setsid();
541
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700542 atrace_begin(ATRACE_TAG, "before reparent");
543 pid_t target_process = getppid();
544
545 // Open /proc/`getppid()` before we daemonize.
546 std::string target_proc_path = "/proc/" + std::to_string(target_process);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700547 int target_proc_fd = open(target_proc_path.c_str(), O_DIRECTORY | O_RDONLY);
548 if (target_proc_fd == -1) {
549 PLOG(FATAL) << "failed to open " << target_proc_path;
550 }
551
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700552 // Make sure getppid() hasn't changed.
553 if (getppid() != target_process) {
554 LOG(FATAL) << "parent died";
Josh Gao2a18b822017-02-16 19:17:28 -0800555 }
Josh Gao8bb03902017-05-30 15:31:02 -0700556 atrace_end(ATRACE_TAG);
557
Josh Gaocbe70cb2016-10-18 18:17:52 -0700558 // Reparent ourselves to init, so that the signal handler can waitpid on the
559 // original process to avoid leaving a zombie for non-fatal dumps.
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700560 // Move the input/output pipes off of stdout/stderr, out of paranoia.
561 unique_fd output_pipe(dup(STDOUT_FILENO));
562 unique_fd input_pipe(dup(STDIN_FILENO));
563
564 unique_fd fork_exit_read, fork_exit_write;
565 if (!Pipe(&fork_exit_read, &fork_exit_write)) {
566 PLOG(FATAL) << "failed to create pipe";
567 }
568
Josh Gaocbe70cb2016-10-18 18:17:52 -0700569 pid_t forkpid = fork();
570 if (forkpid == -1) {
571 PLOG(FATAL) << "fork failed";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700572 } else if (forkpid == 0) {
573 fork_exit_read.reset();
574 } else {
575 // We need the pseudothread to live until we get around to verifying the vm pid against it.
576 // The last thing it does is block on a waitpid on us, so wait until our child tells us to die.
577 fork_exit_write.reset();
578 char buf;
579 TEMP_FAILURE_RETRY(read(fork_exit_read.get(), &buf, sizeof(buf)));
580 _exit(0);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700581 }
582
Josh Gao8bb03902017-05-30 15:31:02 -0700583 ATRACE_NAME("after reparent");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700584 pid_t pseudothread_tid;
585 DebuggerdDumpType dump_type;
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800586 ProcessInfo process_info;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700587
588 Initialize(argv);
589 ParseArgs(argc, argv, &pseudothread_tid, &dump_type);
Josh Gao8bb03902017-05-30 15:31:02 -0700590
Josh Gao7c6e3132017-01-22 17:59:02 -0800591 // Die if we take too long.
Andreas Gampeb02851a2017-06-22 19:45:53 -0700592 //
593 // Note: processes with many threads and minidebug-info can take a bit to
594 // unwind, do not make this too small. b/62828735
Peter Collingbournefb5eac92021-03-11 12:51:25 -0800595 alarm(30 * android::base::HwTimeoutMultiplier());
Josh Gao7c6e3132017-01-22 17:59:02 -0800596
Josh Gao2f11a252017-02-13 14:46:19 -0800597 // Collect the list of open files.
598 OpenFilesList open_files;
Josh Gao8bb03902017-05-30 15:31:02 -0700599 {
600 ATRACE_NAME("open files");
Josh Gaoce841d92018-08-06 18:26:42 -0700601 populate_open_files_list(&open_files, g_target_thread);
Josh Gao8bb03902017-05-30 15:31:02 -0700602 }
Josh Gao2f11a252017-02-13 14:46:19 -0800603
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700604 // In order to reduce the duration that we pause the process for, we ptrace
605 // the threads, fetch their registers and associated information, and then
606 // fork a separate process as a snapshot of the process's address space.
607 std::set<pid_t> threads;
608 if (!android::procinfo::GetProcessTids(g_target_thread, &threads)) {
609 PLOG(FATAL) << "failed to get process threads";
610 }
Josh Gao3407d7c2017-06-13 17:21:12 +0000611
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700612 std::map<pid_t, ThreadInfo> thread_info;
613 siginfo_t siginfo;
614 std::string error;
Mitch Phillips31407912024-03-14 14:18:47 +0100615 bool recoverable_crash = false;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700616
617 {
618 ATRACE_NAME("ptrace");
619 for (pid_t thread : threads) {
620 // Trace the pseudothread separately, so we can use different options.
621 if (thread == pseudothread_tid) {
622 continue;
623 }
624
625 if (!ptrace_seize_thread(target_proc_fd, thread, &error)) {
626 bool fatal = thread == g_target_thread;
627 LOG(fatal ? FATAL : WARNING) << error;
628 }
629
630 ThreadInfo info;
631 info.pid = target_process;
632 info.tid = thread;
Josh Gao5df504c2019-05-09 12:48:17 -0700633 info.uid = getuid();
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700634 info.thread_name = get_thread_name(thread);
635
Josh Gao76e1e302021-01-26 15:53:11 -0800636 unique_fd attr_fd(openat(target_proc_fd, "attr/current", O_RDONLY | O_CLOEXEC));
637 if (!android::base::ReadFdToString(attr_fd, &info.selinux_label)) {
638 PLOG(WARNING) << "failed to read selinux label";
639 }
640
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700641 if (!ptrace_interrupt(thread, &info.signo)) {
642 PLOG(WARNING) << "failed to ptrace interrupt thread " << thread;
643 ptrace(PTRACE_DETACH, thread, 0, 0);
644 continue;
645 }
646
Elliott Hughesd13ea522022-01-13 09:20:26 -0800647 struct iovec tagged_addr_iov = {
Peter Collingbourne864f15d2020-09-14 20:27:36 -0700648 &info.tagged_addr_ctrl,
649 sizeof(info.tagged_addr_ctrl),
650 };
651 if (ptrace(PTRACE_GETREGSET, thread, NT_ARM_TAGGED_ADDR_CTRL,
Elliott Hughesd13ea522022-01-13 09:20:26 -0800652 reinterpret_cast<void*>(&tagged_addr_iov)) == -1) {
Peter Collingbourne864f15d2020-09-14 20:27:36 -0700653 info.tagged_addr_ctrl = -1;
654 }
Peter Collingbourne864f15d2020-09-14 20:27:36 -0700655
Elliott Hughesd13ea522022-01-13 09:20:26 -0800656 struct iovec pac_enabled_keys_iov = {
657 &info.pac_enabled_keys,
658 sizeof(info.pac_enabled_keys),
659 };
660 if (ptrace(PTRACE_GETREGSET, thread, NT_ARM_PAC_ENABLED_KEYS,
661 reinterpret_cast<void*>(&pac_enabled_keys_iov)) == -1) {
662 info.pac_enabled_keys = -1;
663 }
664
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700665 if (thread == g_target_thread) {
666 // Read the thread's registers along with the rest of the crash info out of the pipe.
Mitch Phillips31407912024-03-14 14:18:47 +0100667 ReadCrashInfo(input_pipe, &siginfo, &info.registers, &process_info, &recoverable_crash);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700668 info.siginfo = &siginfo;
669 info.signo = info.siginfo->si_signo;
Josh Gao31348a72021-03-29 21:53:42 -0700670
671 info.command_line = get_command_line(g_target_thread);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700672 } else {
Christopher Ferris60eb1972019-01-15 15:18:43 -0800673 info.registers.reset(unwindstack::Regs::RemoteGet(thread));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700674 if (!info.registers) {
675 PLOG(WARNING) << "failed to fetch registers for thread " << thread;
676 ptrace(PTRACE_DETACH, thread, 0, 0);
677 continue;
678 }
679 }
Sijie Chencb53fe12024-05-10 18:40:48 +0000680 ReadGuestRegisters(&info.guest_registers, thread);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700681
682 thread_info[thread] = std::move(info);
683 }
684 }
685
686 // Trace the pseudothread with PTRACE_O_TRACECLONE and tell it to fork.
687 if (!ptrace_seize_thread(target_proc_fd, pseudothread_tid, &error, PTRACE_O_TRACECLONE)) {
688 LOG(FATAL) << "failed to seize pseudothread: " << error;
689 }
690
691 if (TEMP_FAILURE_RETRY(write(output_pipe.get(), "\1", 1)) != 1) {
692 PLOG(FATAL) << "failed to write to pseudothread";
693 }
694
695 pid_t vm_pid = wait_for_vm_process(pseudothread_tid);
696 if (ptrace(PTRACE_DETACH, pseudothread_tid, 0, 0) != 0) {
697 PLOG(FATAL) << "failed to detach from pseudothread";
698 }
699
700 // The pseudothread can die now.
701 fork_exit_write.reset();
702
703 // Defer the message until later, for readability.
Elliott Hughese4781d52021-03-17 09:15:15 -0700704 bool wait_for_debugger = android::base::GetBoolProperty(
705 "debug.debuggerd.wait_for_debugger",
706 android::base::GetBoolProperty("debug.debuggerd.wait_for_gdb", false));
Josh Gaoa48b41b2019-12-13 14:11:04 -0800707 if (siginfo.si_signo == BIONIC_SIGNAL_DEBUGGER) {
Elliott Hughese4781d52021-03-17 09:15:15 -0700708 wait_for_debugger = false;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700709 }
710
711 // Detach from all of our attached threads before resuming.
712 for (const auto& [tid, thread] : thread_info) {
Josh Gaoa48b41b2019-12-13 14:11:04 -0800713 int resume_signal = thread.signo == BIONIC_SIGNAL_DEBUGGER ? 0 : thread.signo;
Elliott Hughese4781d52021-03-17 09:15:15 -0700714 if (wait_for_debugger) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700715 resume_signal = 0;
716 if (tgkill(target_process, tid, SIGSTOP) != 0) {
717 PLOG(WARNING) << "failed to send SIGSTOP to " << tid;
718 }
719 }
720
721 LOG(DEBUG) << "detaching from thread " << tid;
722 if (ptrace(PTRACE_DETACH, tid, 0, resume_signal) != 0) {
723 PLOG(ERROR) << "failed to detach from thread " << tid;
724 }
725 }
726
727 // Drop our capabilities now that we've fetched all of the information we need.
Josh Gao2f11a252017-02-13 14:46:19 -0800728 drop_capabilities();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700729
Josh Gao8bb03902017-05-30 15:31:02 -0700730 {
731 ATRACE_NAME("tombstoned_connect");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700732 LOG(INFO) << "obtaining output fd from tombstoned, type: " << dump_type;
Shikha Panwarabde59e2023-02-03 15:05:18 +0000733 g_tombstoned_connected = connect_tombstone_server(g_target_thread, &g_tombstoned_socket,
734 &g_output_fd, &g_proto_fd, dump_type);
Josh Gao8bb03902017-05-30 15:31:02 -0700735 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700736
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700737 if (g_tombstoned_connected) {
738 if (TEMP_FAILURE_RETRY(dup2(g_output_fd.get(), STDOUT_FILENO)) == -1) {
739 PLOG(ERROR) << "failed to dup2 output fd (" << g_output_fd.get() << ") to STDOUT_FILENO";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700740 }
741 } else {
742 unique_fd devnull(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR)));
743 TEMP_FAILURE_RETRY(dup2(devnull.get(), STDOUT_FILENO));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700744 g_output_fd = std::move(devnull);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700745 }
746
Josh Gao9da1f512018-08-06 15:38:29 -0700747 LOG(INFO) << "performing dump of process " << target_process
748 << " (target tid = " << g_target_thread << ")";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700749
750 int signo = siginfo.si_signo;
Josh Gaoa48b41b2019-12-13 14:11:04 -0800751 bool fatal_signal = signo != BIONIC_SIGNAL_DEBUGGER;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700752 bool backtrace = false;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700753
Josh Gaoa48b41b2019-12-13 14:11:04 -0800754 // si_value is special when used with BIONIC_SIGNAL_DEBUGGER.
Josh Gaocbe70cb2016-10-18 18:17:52 -0700755 // 0: dump tombstone
756 // 1: dump backtrace
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700757 if (!fatal_signal) {
758 int si_val = siginfo.si_value.sival_int;
759 if (si_val == 0) {
760 backtrace = false;
761 } else if (si_val == 1) {
762 backtrace = true;
763 } else {
764 LOG(WARNING) << "unknown si_value value " << si_val;
765 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700766 }
767
Josh Gaocbe70cb2016-10-18 18:17:52 -0700768 // TODO: Use seccomp to lock ourselves down.
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700769
770 unwindstack::AndroidRemoteUnwinder unwinder(vm_pid, unwindstack::Regs::CurrentArch());
771 unwindstack::ErrorData error_data;
772 if (!unwinder.Initialize(error_data)) {
773 LOG(FATAL) << "Failed to initialize unwinder object: "
774 << unwindstack::GetErrorCodeString(error_data.code);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700775 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700776
Josh Gaocbe70cb2016-10-18 18:17:52 -0700777 std::string amfd_data;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700778 if (backtrace) {
Josh Gao8bb03902017-05-30 15:31:02 -0700779 ATRACE_NAME("dump_backtrace");
Christopher Ferris60eb1972019-01-15 15:18:43 -0800780 dump_backtrace(std::move(g_output_fd), &unwinder, thread_info, g_target_thread);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700781 } else {
Josh Gaoce841d92018-08-06 18:26:42 -0700782 {
783 ATRACE_NAME("fdsan table dump");
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800784 populate_fdsan_table(&open_files, unwinder.GetProcessMemory(),
785 process_info.fdsan_table_address);
Josh Gaoce841d92018-08-06 18:26:42 -0700786 }
787
788 {
789 ATRACE_NAME("engrave_tombstone");
Sijie Chenc8027932024-05-10 18:40:48 +0000790 unwindstack::ArchEnum regs_arch = unwindstack::ARCH_UNKNOWN;
791 switch (g_guest_arch) {
792 case Architecture::ARM32: {
793 regs_arch = unwindstack::ARCH_ARM;
794 break;
795 }
796 case Architecture::ARM64: {
797 regs_arch = unwindstack::ARCH_ARM64;
798 break;
799 }
800 case Architecture::RISCV64: {
801 regs_arch = unwindstack::ARCH_RISCV64;
802 break;
803 }
804 default: {
805 }
806 }
807 if (regs_arch == unwindstack::ARCH_UNKNOWN) {
808 engrave_tombstone(std::move(g_output_fd), std::move(g_proto_fd), &unwinder, thread_info,
809 g_target_thread, process_info, &open_files, &amfd_data);
810 } else {
811 unwindstack::AndroidRemoteUnwinder guest_unwinder(vm_pid, regs_arch);
812 engrave_tombstone(std::move(g_output_fd), std::move(g_proto_fd), &unwinder, thread_info,
813 g_target_thread, process_info, &open_files, &amfd_data, &g_guest_arch,
814 &guest_unwinder);
815 }
Josh Gaoce841d92018-08-06 18:26:42 -0700816 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700817 }
818
Mitch Phillipsdd5a80d2023-03-13 15:08:17 -0700819 if (fatal_signal) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700820 // Don't try to notify ActivityManager if it just crashed, or we might hang until timeout.
821 if (thread_info[target_process].thread_name != "system_server") {
Mitch Phillips31407912024-03-14 14:18:47 +0100822 activity_manager_notify(target_process, signo, amfd_data, recoverable_crash);
Josh Gao7c6e3132017-01-22 17:59:02 -0800823 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700824 }
825
Elliott Hughese4781d52021-03-17 09:15:15 -0700826 if (wait_for_debugger) {
Josh Gao7c6e3132017-01-22 17:59:02 -0800827 // Use ALOGI to line up with output from engrave_tombstone.
828 ALOGI(
dimitry6429e202017-09-12 10:46:20 +0200829 "***********************************************************\n"
830 "* Process %d has been suspended while crashing.\n"
Elliott Hughese4781d52021-03-17 09:15:15 -0700831 "* To attach the debugger, run this on the host:\n"
dimitry6429e202017-09-12 10:46:20 +0200832 "*\n"
Elliott Hughes5a4e6252023-02-28 22:16:19 +0000833 "* lldbclient.py -p %d\n"
dimitry6429e202017-09-12 10:46:20 +0200834 "*\n"
835 "***********************************************************",
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700836 target_process, target_process);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700837 }
838
839 // Close stdout before we notify tombstoned of completion.
840 close(STDOUT_FILENO);
Shikha Panwarabde59e2023-02-03 15:05:18 +0000841 if (g_tombstoned_connected &&
842 !notify_completion(g_tombstoned_socket.get(), g_output_fd.get(), g_proto_fd.get())) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700843 LOG(ERROR) << "failed to notify tombstoned of completion";
844 }
845
846 return 0;
847}