blob: 4640aebd5e74b280e935d7949bfaf7bb1f944a7a [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 Chen3ff250f2024-03-25 20:06:18 +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>
Sijie Chen4c3a9df2024-04-26 00:40:22 +000033#include <string>
Josh Gaocbe70cb2016-10-18 18:17:52 -070034#include <vector>
35
Elliott Hughesa029d982021-06-21 12:36:47 -070036#include <android-base/errno_restorer.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070037#include <android-base/file.h>
38#include <android-base/logging.h>
Josh Gao8d44b142018-09-18 13:22:22 -070039#include <android-base/macros.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070040#include <android-base/parseint.h>
41#include <android-base/properties.h>
42#include <android-base/stringprintf.h>
Josh Gao57f58f82017-03-15 23:23:22 -070043#include <android-base/strings.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070044#include <android-base/unique_fd.h>
Mitch Phillipse4adff02021-01-21 20:41:50 -080045#include <bionic/macros.h>
Josh Gaoa48b41b2019-12-13 14:11:04 -080046#include <bionic/reserved_signals.h>
Sijie Chen3ff250f2024-03-25 20:06:18 +000047#include <bionic/tls_defines.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070048#include <cutils/sockets.h>
Vijay Venkatramana95acea2017-01-23 20:11:51 -080049#include <log/log.h>
Josh Gaob0e51e32017-06-01 12:08:10 -070050#include <private/android_filesystem_config.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070051#include <procinfo/process.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070052
Josh Gao8bb03902017-05-30 15:31:02 -070053#define ATRACE_TAG ATRACE_TAG_BIONIC
54#include <utils/Trace.h>
55
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -070056#include <unwindstack/AndroidUnwinder.h>
57#include <unwindstack/Error.h>
Sijie Chen3ff250f2024-03-25 20:06:18 +000058#include <unwindstack/MachineArm.h>
59#include <unwindstack/MachineArm64.h>
60#include <unwindstack/MachineRiscv64.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070061#include <unwindstack/Regs.h>
Sijie Chen3ff250f2024-03-25 20:06:18 +000062#include <unwindstack/RegsArm.h>
63#include <unwindstack/RegsArm64.h>
64#include <unwindstack/RegsRiscv64.h>
65#include <unwindstack/UserArm.h>
66#include <unwindstack/UserArm64.h>
67#include <unwindstack/UserRiscv64.h>
68
69#include <native_bridge_support/guest_state_accessor/accessor.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070070
Josh Gaoc3706662017-08-29 13:08:32 -070071#include "libdebuggerd/backtrace.h"
72#include "libdebuggerd/tombstone.h"
73#include "libdebuggerd/utility.h"
Josh Gaocbe70cb2016-10-18 18:17:52 -070074
75#include "debuggerd/handler.h"
Shikha Panwarabde59e2023-02-03 15:05:18 +000076#include "tombstone_handler.h"
Josh Gaoc3706662017-08-29 13:08:32 -070077
78#include "protocol.h"
Narayan Kamath2d377cd2017-05-10 10:58:59 +010079#include "util.h"
Josh Gaocbe70cb2016-10-18 18:17:52 -070080
Elliott Hughesa029d982021-06-21 12:36:47 -070081using android::base::ErrnoRestorer;
Josh Gaocbe70cb2016-10-18 18:17:52 -070082using android::base::StringPrintf;
Elliott Hughesa029d982021-06-21 12:36:47 -070083using android::base::unique_fd;
Josh Gao57f58f82017-03-15 23:23:22 -070084
Sijie Chen4c3a9df2024-04-26 00:40:22 +000085// This stores guest architecture. When the architecture is supported, tombstone file will output
86// guest state information.
87static Architecture g_guest_arch;
88
Josh Gaofe902762017-02-01 16:31:43 -080089static bool pid_contains_tid(int pid_proc_fd, pid_t tid) {
90 struct stat st;
91 std::string task_path = StringPrintf("task/%d", tid);
92 return fstatat(pid_proc_fd, task_path.c_str(), &st, 0) == 0;
Josh Gaocbe70cb2016-10-18 18:17:52 -070093}
94
Josh Gaofd13bf02017-08-18 15:37:26 -070095static pid_t get_tracer(pid_t tracee) {
96 // Check to see if the thread is being ptraced by another process.
97 android::procinfo::ProcessInfo process_info;
98 if (android::procinfo::GetProcessInfo(tracee, &process_info)) {
99 return process_info.tracer;
100 }
101 return -1;
102}
103
Josh Gaocbe70cb2016-10-18 18:17:52 -0700104// Attach to a thread, and verify that it's still a member of the given process
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700105static bool ptrace_seize_thread(int pid_proc_fd, pid_t tid, std::string* error, int flags = 0) {
106 if (ptrace(PTRACE_SEIZE, tid, 0, flags) != 0) {
Josh Gaofd13bf02017-08-18 15:37:26 -0700107 if (errno == EPERM) {
Elliott Hughesa029d982021-06-21 12:36:47 -0700108 ErrnoRestorer errno_restorer; // In case get_tracer() fails and we fall through.
109 pid_t tracer_pid = get_tracer(tid);
110 if (tracer_pid > 0) {
Josh Gaofd13bf02017-08-18 15:37:26 -0700111 *error = StringPrintf("failed to attach to thread %d, already traced by %d (%s)", tid,
Elliott Hughesa029d982021-06-21 12:36:47 -0700112 tracer_pid, get_process_name(tracer_pid).c_str());
Josh Gaofd13bf02017-08-18 15:37:26 -0700113 return false;
114 }
115 }
116
Josh Gao42fd74b2017-01-20 12:51:11 -0800117 *error = StringPrintf("failed to attach to thread %d: %s", tid, strerror(errno));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700118 return false;
119 }
120
121 // Make sure that the task we attached to is actually part of the pid we're dumping.
Josh Gaofe902762017-02-01 16:31:43 -0800122 if (!pid_contains_tid(pid_proc_fd, tid)) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700123 if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700124 PLOG(WARNING) << "failed to detach from thread " << tid;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700125 }
Josh Gaofe902762017-02-01 16:31:43 -0800126 *error = StringPrintf("thread %d is not in process", tid);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700127 return false;
128 }
Josh Gao122479f2017-01-22 16:42:32 -0800129
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700130 return true;
131}
132
133static bool wait_for_stop(pid_t tid, int* received_signal) {
134 while (true) {
135 int status;
136 pid_t result = waitpid(tid, &status, __WALL);
137 if (result != tid) {
138 PLOG(ERROR) << "waitpid failed on " << tid << " while detaching";
139 return false;
140 }
141
142 if (WIFSTOPPED(status)) {
143 if (status >> 16 == PTRACE_EVENT_STOP) {
144 *received_signal = 0;
145 } else {
146 *received_signal = WSTOPSIG(status);
147 }
148 return true;
149 }
150 }
151}
152
153// Interrupt a process and wait for it to be interrupted.
154static bool ptrace_interrupt(pid_t tid, int* received_signal) {
155 if (ptrace(PTRACE_INTERRUPT, tid, 0, 0) == 0) {
156 return wait_for_stop(tid, received_signal);
Josh Gao3407d7c2017-06-13 17:21:12 +0000157 }
158
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700159 PLOG(ERROR) << "failed to interrupt " << tid << " to detach";
160 return false;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700161}
162
Mitch Phillipsdd5a80d2023-03-13 15:08:17 -0700163static bool activity_manager_notify(pid_t pid, int signal, const std::string& amfd_data,
Mitch Phillips31407912024-03-14 14:18:47 +0100164 bool recoverable_crash) {
Josh Gao8bb03902017-05-30 15:31:02 -0700165 ATRACE_CALL();
Josh Gaob0e51e32017-06-01 12:08:10 -0700166 android::base::unique_fd amfd(socket_local_client(
167 "/data/system/ndebugsocket", ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700168 if (amfd.get() == -1) {
169 PLOG(ERROR) << "unable to connect to activity manager";
170 return false;
171 }
172
173 struct timeval tv = {
Peter Collingbournefb5eac92021-03-11 12:51:25 -0800174 .tv_sec = 1 * android::base::HwTimeoutMultiplier(),
Evgenii Stepanov2a55e1a2021-01-29 11:50:30 -0800175 .tv_usec = 0,
Josh Gaocbe70cb2016-10-18 18:17:52 -0700176 };
177 if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
178 PLOG(ERROR) << "failed to set send timeout on activity manager socket";
179 return false;
180 }
Peter Collingbournefb5eac92021-03-11 12:51:25 -0800181 tv.tv_sec = 3 * android::base::HwTimeoutMultiplier(); // 3 seconds on handshake read
Josh Gaocbe70cb2016-10-18 18:17:52 -0700182 if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
183 PLOG(ERROR) << "failed to set receive timeout on activity manager socket";
184 return false;
185 }
186
Mitch Phillipsdd5a80d2023-03-13 15:08:17 -0700187 // Activity Manager protocol:
188 // - 32-bit network-byte-order: pid
189 // - 32-bit network-byte-order: signal number
Mitch Phillips31407912024-03-14 14:18:47 +0100190 // - byte: recoverable_crash
Mitch Phillipsdd5a80d2023-03-13 15:08:17 -0700191 // - bytes: raw text of the dump
192 // - null terminator
193
Josh Gaocbe70cb2016-10-18 18:17:52 -0700194 uint32_t datum = htonl(pid);
Mitch Phillipsdd5a80d2023-03-13 15:08:17 -0700195 if (!android::base::WriteFully(amfd, &datum, sizeof(datum))) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700196 PLOG(ERROR) << "AM pid write failed";
197 return false;
198 }
Mitch Phillipsdd5a80d2023-03-13 15:08:17 -0700199
Josh Gaocbe70cb2016-10-18 18:17:52 -0700200 datum = htonl(signal);
Mitch Phillipsdd5a80d2023-03-13 15:08:17 -0700201 if (!android::base::WriteFully(amfd, &datum, sizeof(datum))) {
202 PLOG(ERROR) << "AM signo write failed";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700203 return false;
204 }
Mitch Phillipsdd5a80d2023-03-13 15:08:17 -0700205
Mitch Phillips31407912024-03-14 14:18:47 +0100206 uint8_t recoverable_crash_byte = recoverable_crash ? 1 : 0;
207 if (!android::base::WriteFully(amfd, &recoverable_crash_byte, sizeof(recoverable_crash_byte))) {
208 PLOG(ERROR) << "AM recoverable_crash_byte write failed";
Mitch Phillipsdd5a80d2023-03-13 15:08:17 -0700209 return false;
210 }
211
Josh Gaocbe70cb2016-10-18 18:17:52 -0700212 if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size() + 1)) {
213 PLOG(ERROR) << "AM data write failed";
214 return false;
215 }
216
217 // 3 sec timeout reading the ack; we're fine if the read fails.
218 char ack;
219 android::base::ReadFully(amfd, &ack, 1);
220 return true;
221}
222
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700223// Globals used by the abort handler.
224static pid_t g_target_thread = -1;
225static bool g_tombstoned_connected = false;
226static unique_fd g_tombstoned_socket;
227static unique_fd g_output_fd;
Josh Gao76e1e302021-01-26 15:53:11 -0800228static unique_fd g_proto_fd;
Josh Gao57594112017-01-22 17:41:15 -0800229
Josh Gao38ac45d2018-04-27 13:31:47 -0700230static void DefuseSignalHandlers() {
231 // Don't try to dump ourselves.
232 struct sigaction action = {};
233 action.sa_handler = SIG_DFL;
234 debuggerd_register_handlers(&action);
235
236 sigset_t mask;
237 sigemptyset(&mask);
238 if (sigprocmask(SIG_SETMASK, &mask, nullptr) != 0) {
239 PLOG(FATAL) << "failed to set signal mask";
240 }
241}
242
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700243static void Initialize(char** argv) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700244 android::base::InitLogging(argv);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700245 android::base::SetAborter([](const char* abort_msg) {
246 // If we abort before we get an output fd, contact tombstoned to let any
247 // potential listeners know that we failed.
248 if (!g_tombstoned_connected) {
Shikha Panwarabde59e2023-02-03 15:05:18 +0000249 if (!connect_tombstone_server(g_target_thread, &g_tombstoned_socket, &g_output_fd,
250 &g_proto_fd, kDebuggerdAnyIntercept)) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700251 // We failed to connect, not much we can do.
252 LOG(ERROR) << "failed to connected to tombstoned to report failure";
253 _exit(1);
254 }
255 }
256
257 dprintf(g_output_fd.get(), "crash_dump failed to dump process");
258 if (g_target_thread != 1) {
259 dprintf(g_output_fd.get(), " %d: %s\n", g_target_thread, abort_msg);
260 } else {
261 dprintf(g_output_fd.get(), ": %s\n", abort_msg);
262 }
263
264 _exit(1);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700265 });
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700266}
Josh Gaoe7402502017-06-01 11:55:25 -0700267
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700268static void ParseArgs(int argc, char** argv, pid_t* pseudothread_tid, DebuggerdDumpType* dump_type) {
Narayan Kamatha73df602017-05-24 15:07:25 +0100269 if (argc != 4) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700270 LOG(FATAL) << "wrong number of args: " << argc << " (expected 4)";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700271 }
272
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700273 if (!android::base::ParseInt(argv[1], &g_target_thread, 1, std::numeric_limits<pid_t>::max())) {
274 LOG(FATAL) << "invalid target tid: " << argv[1];
Josh Gaocbe70cb2016-10-18 18:17:52 -0700275 }
276
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700277 if (!android::base::ParseInt(argv[2], pseudothread_tid, 1, std::numeric_limits<pid_t>::max())) {
Josh Gaof6ad5852017-02-15 12:21:11 -0800278 LOG(FATAL) << "invalid pseudothread tid: " << argv[2];
Josh Gao2f11a252017-02-13 14:46:19 -0800279 }
280
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700281 int dump_type_int;
Josh Gao76e1e302021-01-26 15:53:11 -0800282 if (!android::base::ParseInt(argv[3], &dump_type_int, 0)) {
Narayan Kamatha73df602017-05-24 15:07:25 +0100283 LOG(FATAL) << "invalid requested dump type: " << argv[3];
284 }
Josh Gao76e1e302021-01-26 15:53:11 -0800285
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700286 *dump_type = static_cast<DebuggerdDumpType>(dump_type_int);
Josh Gao76e1e302021-01-26 15:53:11 -0800287 switch (*dump_type) {
288 case kDebuggerdNativeBacktrace:
289 case kDebuggerdTombstone:
290 case kDebuggerdTombstoneProto:
291 break;
292
293 default:
294 LOG(FATAL) << "invalid requested dump type: " << dump_type_int;
295 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700296}
Narayan Kamatha73df602017-05-24 15:07:25 +0100297
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700298static void ReadCrashInfo(unique_fd& fd, siginfo_t* siginfo,
Mitch Phillips8fe51272023-02-07 17:02:30 -0800299 std::unique_ptr<unwindstack::Regs>* regs, ProcessInfo* process_info,
Mitch Phillips31407912024-03-14 14:18:47 +0100300 bool* recoverable_crash) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700301 std::aligned_storage<sizeof(CrashInfo) + 1, alignof(CrashInfo)>::type buf;
Josh Gao9da1f512018-08-06 15:38:29 -0700302 CrashInfo* crash_info = reinterpret_cast<CrashInfo*>(&buf);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700303 ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), &buf, sizeof(buf)));
Mitch Phillips31407912024-03-14 14:18:47 +0100304 *recoverable_crash = false;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700305 if (rc == -1) {
306 PLOG(FATAL) << "failed to read target ucontext";
Josh Gao9da1f512018-08-06 15:38:29 -0700307 } else {
308 ssize_t expected_size = 0;
309 switch (crash_info->header.version) {
310 case 1:
Josh Gao9da1f512018-08-06 15:38:29 -0700311 case 2:
Peter Collingbournef3d542f2020-03-05 16:46:15 -0800312 case 3:
313 expected_size = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataStatic);
Josh Gao9da1f512018-08-06 15:38:29 -0700314 break;
315
Peter Collingbournef3d542f2020-03-05 16:46:15 -0800316 case 4:
317 expected_size = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataDynamic);
Mitch Phillipse0b4bb12020-02-14 14:54:31 -0800318 break;
319
Josh Gao9da1f512018-08-06 15:38:29 -0700320 default:
321 LOG(FATAL) << "unexpected CrashInfo version: " << crash_info->header.version;
322 break;
323 };
324
Peter Collingbournef3d542f2020-03-05 16:46:15 -0800325 if (rc < expected_size) {
Josh Gao9da1f512018-08-06 15:38:29 -0700326 LOG(FATAL) << "read " << rc << " bytes when reading target crash information, expected "
327 << expected_size;
328 }
Josh Gaoc7fe0602017-03-13 14:13:29 -0700329 }
330
Josh Gao9da1f512018-08-06 15:38:29 -0700331 switch (crash_info->header.version) {
Peter Collingbournef3d542f2020-03-05 16:46:15 -0800332 case 4:
333 process_info->fdsan_table_address = crash_info->data.d.fdsan_table_address;
334 process_info->gwp_asan_state = crash_info->data.d.gwp_asan_state;
335 process_info->gwp_asan_metadata = crash_info->data.d.gwp_asan_metadata;
Peter Collingbournef8622522020-04-07 14:07:32 -0700336 process_info->scudo_stack_depot = crash_info->data.d.scudo_stack_depot;
Florian Mayere8fcfee2023-12-04 16:39:15 -0800337 process_info->scudo_stack_depot_size = crash_info->data.d.scudo_stack_depot_size;
Peter Collingbournef8622522020-04-07 14:07:32 -0700338 process_info->scudo_region_info = crash_info->data.d.scudo_region_info;
Peter Collingbournebb4b49c2021-01-06 21:02:19 -0800339 process_info->scudo_ring_buffer = crash_info->data.d.scudo_ring_buffer;
Florian Mayerbd49c382022-12-22 16:15:23 -0800340 process_info->scudo_ring_buffer_size = crash_info->data.d.scudo_ring_buffer_size;
Mitch Phillips31407912024-03-14 14:18:47 +0100341 *recoverable_crash = crash_info->data.d.recoverable_crash;
Florian Mayer5fa66632024-02-07 16:42:23 -0800342 process_info->crash_detail_page = crash_info->data.d.crash_detail_page;
Josh Gao8d44b142018-09-18 13:22:22 -0700343 FALLTHROUGH_INTENDED;
Josh Gao9da1f512018-08-06 15:38:29 -0700344 case 1:
Peter Collingbournef3d542f2020-03-05 16:46:15 -0800345 case 2:
346 case 3:
347 process_info->abort_msg_address = crash_info->data.s.abort_msg_address;
348 *siginfo = crash_info->data.s.siginfo;
Peter Collingbournef03af882020-03-20 18:09:00 -0700349 if (signal_has_si_addr(siginfo)) {
Peter Collingbournef03af882020-03-20 18:09:00 -0700350 process_info->has_fault_address = true;
Mitch Phillipse4adff02021-01-21 20:41:50 -0800351 process_info->maybe_tagged_fault_address = reinterpret_cast<uintptr_t>(siginfo->si_addr);
352 process_info->untagged_fault_address =
353 untag_address(reinterpret_cast<uintptr_t>(siginfo->si_addr));
Peter Collingbournef03af882020-03-20 18:09:00 -0700354 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800355 regs->reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(),
Peter Collingbournef3d542f2020-03-05 16:46:15 -0800356 &crash_info->data.s.ucontext));
Josh Gao9da1f512018-08-06 15:38:29 -0700357 break;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700358
Josh Gao9da1f512018-08-06 15:38:29 -0700359 default:
360 __builtin_unreachable();
361 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700362}
363
364// Wait for a process to clone and return the child's pid.
365// Note: this leaves the parent in PTRACE_EVENT_STOP.
366static pid_t wait_for_clone(pid_t pid, bool resume_child) {
367 int status;
368 pid_t result = TEMP_FAILURE_RETRY(waitpid(pid, &status, __WALL));
369 if (result == -1) {
370 PLOG(FATAL) << "failed to waitpid";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700371 }
372
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700373 if (WIFEXITED(status)) {
374 LOG(FATAL) << "traced process exited with status " << WEXITSTATUS(status);
375 } else if (WIFSIGNALED(status)) {
376 LOG(FATAL) << "traced process exited with signal " << WTERMSIG(status);
377 } else if (!WIFSTOPPED(status)) {
378 LOG(FATAL) << "process didn't stop? (status = " << status << ")";
379 }
380
381 if (status >> 8 != (SIGTRAP | (PTRACE_EVENT_CLONE << 8))) {
382 LOG(FATAL) << "process didn't stop due to PTRACE_O_TRACECLONE (status = " << status << ")";
383 }
384
385 pid_t child;
386 if (ptrace(PTRACE_GETEVENTMSG, pid, 0, &child) != 0) {
387 PLOG(FATAL) << "failed to get child pid via PTRACE_GETEVENTMSG";
388 }
389
390 int stop_signal;
391 if (!wait_for_stop(child, &stop_signal)) {
392 PLOG(FATAL) << "failed to waitpid on child";
393 }
394
395 CHECK_EQ(0, stop_signal);
396
397 if (resume_child) {
398 if (ptrace(PTRACE_CONT, child, 0, 0) != 0) {
399 PLOG(FATAL) << "failed to resume child (pid = " << child << ")";
400 }
401 }
402
403 return child;
404}
405
406static pid_t wait_for_vm_process(pid_t pseudothread_tid) {
407 // The pseudothread will double-fork, we want its grandchild.
408 pid_t intermediate = wait_for_clone(pseudothread_tid, true);
409 pid_t vm_pid = wait_for_clone(intermediate, false);
410 if (ptrace(PTRACE_DETACH, intermediate, 0, 0) != 0) {
411 PLOG(FATAL) << "failed to detach from intermediate vm process";
412 }
413
414 return vm_pid;
415}
416
Jinguang Dong8ac2f272018-11-24 17:12:33 +0800417static void InstallSigPipeHandler() {
418 struct sigaction action = {};
419 action.sa_handler = SIG_IGN;
420 action.sa_flags = SA_RESTART;
421 sigaction(SIGPIPE, &action, nullptr);
422}
423
Sijie Chen3ff250f2024-03-25 20:06:18 +0000424static bool PtracePeek(int request, pid_t tid, uintptr_t addr, void* data, std::string err_msg,
425 uintptr_t* result) {
426 errno = 0;
427 *result = ptrace(request, tid, addr, data);
428 if (errno != 0) {
429 PLOG(ERROR) << err_msg;
430 return false;
431 }
432 return true;
433}
434
435static bool GetGuestRegistersFromCrashedProcess([[maybe_unused]] pid_t tid,
436 NativeBridgeGuestRegs* guest_regs) {
437 auto process_memory = unwindstack::Memory::CreateProcessMemoryCached(tid);
438
439 uintptr_t header_ptr = 0;
440 uintptr_t base = 0;
441#if defined(__x86_64__)
442 if (!PtracePeek(PTRACE_PEEKUSER, tid, offsetof(user_regs_struct, fs_base), nullptr,
443 "failed to read thread register for thread " + std::to_string(tid), &base)) {
444 return false;
445 }
446#elif defined(__aarch64__)
447 // base is implicitly casted to uint64_t.
448 struct iovec pt_iov {
449 .iov_base = &base, .iov_len = sizeof(base),
450 };
451
452 if (ptrace(PTRACE_GETREGSET, tid, NT_ARM_TLS, &pt_iov) != 0) {
453 PLOG(ERROR) << "failed to read thread register for thread " << tid;
454 }
455#else
456 // TODO(b/339287219): Add case for Riscv host.
457 return false;
458#endif
459 auto ptr_to_guest_slot = base + TLS_SLOT_NATIVE_BRIDGE_GUEST_STATE * sizeof(uintptr_t);
460 if (!process_memory->ReadFully(ptr_to_guest_slot, &header_ptr, sizeof(uintptr_t))) {
461 PLOG(ERROR) << "failed to get guest state TLS slot content for thread " << tid;
462 return false;
463 }
464
465 NativeBridgeGuestStateHeader header;
466 if (!process_memory->ReadFully(header_ptr, &header, sizeof(NativeBridgeGuestStateHeader))) {
467 PLOG(ERROR) << "failed to get the guest state header for thread " << tid;
468 return false;
469 }
470 if (header.signature != NATIVE_BRIDGE_GUEST_STATE_SIGNATURE) {
471 // Return when ptr points to unmapped memory or no valid guest state.
472 return false;
473 }
474 auto guest_state_data_copy = std::make_unique<unsigned char[]>(header.guest_state_data_size);
475 if (!process_memory->ReadFully(reinterpret_cast<uintptr_t>(header.guest_state_data),
476 guest_state_data_copy.get(), header.guest_state_data_size)) {
477 PLOG(ERROR) << "failed to read the guest state data for thread " << tid;
478 return false;
479 }
480
481 LoadGuestStateRegisters(guest_state_data_copy.get(), header.guest_state_data_size, guest_regs);
482 return true;
483}
484
485static void ReadGuestRegisters(std::unique_ptr<unwindstack::Regs>* regs, pid_t tid) {
486 NativeBridgeGuestRegs guest_regs;
487 if (!GetGuestRegistersFromCrashedProcess(tid, &guest_regs)) {
488 return;
489 }
490
491 switch (guest_regs.guest_arch) {
492 case NATIVE_BRIDGE_ARCH_ARM: {
493 unwindstack::arm_user_regs arm_user_regs = {};
494 for (size_t i = 0; i < unwindstack::ARM_REG_LAST; i++) {
495 arm_user_regs.regs[i] = guest_regs.regs_arm.r[i];
496 }
497 regs->reset(unwindstack::RegsArm::Read(&arm_user_regs));
Sijie Chen4c3a9df2024-04-26 00:40:22 +0000498
499 g_guest_arch = Architecture::ARM32;
Sijie Chen3ff250f2024-03-25 20:06:18 +0000500 break;
501 }
502#if defined(__LP64__)
503 case NATIVE_BRIDGE_ARCH_ARM64: {
504 unwindstack::arm64_user_regs arm64_user_regs = {};
505 for (size_t i = 0; i < unwindstack::ARM64_REG_R31; i++) {
506 arm64_user_regs.regs[i] = guest_regs.regs_arm64.x[i];
507 }
508 arm64_user_regs.sp = guest_regs.regs_arm64.sp;
509 arm64_user_regs.pc = guest_regs.regs_arm64.ip;
510 regs->reset(unwindstack::RegsArm64::Read(&arm64_user_regs));
Sijie Chen4c3a9df2024-04-26 00:40:22 +0000511
512 g_guest_arch = Architecture::ARM64;
Sijie Chen3ff250f2024-03-25 20:06:18 +0000513 break;
514 }
515 case NATIVE_BRIDGE_ARCH_RISCV64: {
516 unwindstack::riscv64_user_regs riscv64_user_regs = {};
517 // RISCV64_REG_PC is at the first position.
518 riscv64_user_regs.regs[0] = guest_regs.regs_riscv64.ip;
519 for (size_t i = 1; i < unwindstack::RISCV64_REG_REAL_COUNT; i++) {
520 riscv64_user_regs.regs[i] = guest_regs.regs_riscv64.x[i];
521 }
522 regs->reset(unwindstack::RegsRiscv64::Read(&riscv64_user_regs, tid));
Sijie Chen4c3a9df2024-04-26 00:40:22 +0000523
524 g_guest_arch = Architecture::RISCV64;
Sijie Chen3ff250f2024-03-25 20:06:18 +0000525 break;
526 }
527#endif
528 }
529}
530
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700531int main(int argc, char** argv) {
Josh Gao38ac45d2018-04-27 13:31:47 -0700532 DefuseSignalHandlers();
Jinguang Dong8ac2f272018-11-24 17:12:33 +0800533 InstallSigPipeHandler();
Josh Gao38ac45d2018-04-27 13:31:47 -0700534
Josh Gao18cb6812019-04-16 13:17:08 -0700535 // There appears to be a bug in the kernel where our death causes SIGHUP to
536 // be sent to our process group if we exit while it has stopped jobs (e.g.
Elliott Hughese4781d52021-03-17 09:15:15 -0700537 // because of wait_for_debugger). Use setsid to create a new process group to
Josh Gao18cb6812019-04-16 13:17:08 -0700538 // avoid hitting this.
539 setsid();
540
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700541 atrace_begin(ATRACE_TAG, "before reparent");
542 pid_t target_process = getppid();
543
544 // Open /proc/`getppid()` before we daemonize.
545 std::string target_proc_path = "/proc/" + std::to_string(target_process);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700546 int target_proc_fd = open(target_proc_path.c_str(), O_DIRECTORY | O_RDONLY);
547 if (target_proc_fd == -1) {
548 PLOG(FATAL) << "failed to open " << target_proc_path;
549 }
550
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700551 // Make sure getppid() hasn't changed.
552 if (getppid() != target_process) {
553 LOG(FATAL) << "parent died";
Josh Gao2a18b822017-02-16 19:17:28 -0800554 }
Josh Gao8bb03902017-05-30 15:31:02 -0700555 atrace_end(ATRACE_TAG);
556
Josh Gaocbe70cb2016-10-18 18:17:52 -0700557 // Reparent ourselves to init, so that the signal handler can waitpid on the
558 // original process to avoid leaving a zombie for non-fatal dumps.
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700559 // Move the input/output pipes off of stdout/stderr, out of paranoia.
560 unique_fd output_pipe(dup(STDOUT_FILENO));
561 unique_fd input_pipe(dup(STDIN_FILENO));
562
563 unique_fd fork_exit_read, fork_exit_write;
564 if (!Pipe(&fork_exit_read, &fork_exit_write)) {
565 PLOG(FATAL) << "failed to create pipe";
566 }
567
Josh Gaocbe70cb2016-10-18 18:17:52 -0700568 pid_t forkpid = fork();
569 if (forkpid == -1) {
570 PLOG(FATAL) << "fork failed";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700571 } else if (forkpid == 0) {
572 fork_exit_read.reset();
573 } else {
574 // We need the pseudothread to live until we get around to verifying the vm pid against it.
575 // The last thing it does is block on a waitpid on us, so wait until our child tells us to die.
576 fork_exit_write.reset();
577 char buf;
578 TEMP_FAILURE_RETRY(read(fork_exit_read.get(), &buf, sizeof(buf)));
579 _exit(0);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700580 }
581
Josh Gao8bb03902017-05-30 15:31:02 -0700582 ATRACE_NAME("after reparent");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700583 pid_t pseudothread_tid;
584 DebuggerdDumpType dump_type;
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800585 ProcessInfo process_info;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700586
587 Initialize(argv);
588 ParseArgs(argc, argv, &pseudothread_tid, &dump_type);
Josh Gao8bb03902017-05-30 15:31:02 -0700589
Josh Gao7c6e3132017-01-22 17:59:02 -0800590 // Die if we take too long.
Andreas Gampeb02851a2017-06-22 19:45:53 -0700591 //
592 // Note: processes with many threads and minidebug-info can take a bit to
593 // unwind, do not make this too small. b/62828735
Peter Collingbournefb5eac92021-03-11 12:51:25 -0800594 alarm(30 * android::base::HwTimeoutMultiplier());
Josh Gao7c6e3132017-01-22 17:59:02 -0800595
Josh Gao2f11a252017-02-13 14:46:19 -0800596 // Collect the list of open files.
597 OpenFilesList open_files;
Josh Gao8bb03902017-05-30 15:31:02 -0700598 {
599 ATRACE_NAME("open files");
Josh Gaoce841d92018-08-06 18:26:42 -0700600 populate_open_files_list(&open_files, g_target_thread);
Josh Gao8bb03902017-05-30 15:31:02 -0700601 }
Josh Gao2f11a252017-02-13 14:46:19 -0800602
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700603 // In order to reduce the duration that we pause the process for, we ptrace
604 // the threads, fetch their registers and associated information, and then
605 // fork a separate process as a snapshot of the process's address space.
606 std::set<pid_t> threads;
607 if (!android::procinfo::GetProcessTids(g_target_thread, &threads)) {
608 PLOG(FATAL) << "failed to get process threads";
609 }
Josh Gao3407d7c2017-06-13 17:21:12 +0000610
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700611 std::map<pid_t, ThreadInfo> thread_info;
612 siginfo_t siginfo;
613 std::string error;
Mitch Phillips31407912024-03-14 14:18:47 +0100614 bool recoverable_crash = false;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700615
616 {
617 ATRACE_NAME("ptrace");
618 for (pid_t thread : threads) {
619 // Trace the pseudothread separately, so we can use different options.
620 if (thread == pseudothread_tid) {
621 continue;
622 }
623
624 if (!ptrace_seize_thread(target_proc_fd, thread, &error)) {
625 bool fatal = thread == g_target_thread;
626 LOG(fatal ? FATAL : WARNING) << error;
627 }
628
629 ThreadInfo info;
630 info.pid = target_process;
631 info.tid = thread;
Josh Gao5df504c2019-05-09 12:48:17 -0700632 info.uid = getuid();
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700633 info.thread_name = get_thread_name(thread);
634
Josh Gao76e1e302021-01-26 15:53:11 -0800635 unique_fd attr_fd(openat(target_proc_fd, "attr/current", O_RDONLY | O_CLOEXEC));
636 if (!android::base::ReadFdToString(attr_fd, &info.selinux_label)) {
637 PLOG(WARNING) << "failed to read selinux label";
638 }
639
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700640 if (!ptrace_interrupt(thread, &info.signo)) {
641 PLOG(WARNING) << "failed to ptrace interrupt thread " << thread;
642 ptrace(PTRACE_DETACH, thread, 0, 0);
643 continue;
644 }
645
Elliott Hughesd13ea522022-01-13 09:20:26 -0800646 struct iovec tagged_addr_iov = {
Peter Collingbourne864f15d2020-09-14 20:27:36 -0700647 &info.tagged_addr_ctrl,
648 sizeof(info.tagged_addr_ctrl),
649 };
650 if (ptrace(PTRACE_GETREGSET, thread, NT_ARM_TAGGED_ADDR_CTRL,
Elliott Hughesd13ea522022-01-13 09:20:26 -0800651 reinterpret_cast<void*>(&tagged_addr_iov)) == -1) {
Peter Collingbourne864f15d2020-09-14 20:27:36 -0700652 info.tagged_addr_ctrl = -1;
653 }
Peter Collingbourne864f15d2020-09-14 20:27:36 -0700654
Elliott Hughesd13ea522022-01-13 09:20:26 -0800655 struct iovec pac_enabled_keys_iov = {
656 &info.pac_enabled_keys,
657 sizeof(info.pac_enabled_keys),
658 };
659 if (ptrace(PTRACE_GETREGSET, thread, NT_ARM_PAC_ENABLED_KEYS,
660 reinterpret_cast<void*>(&pac_enabled_keys_iov)) == -1) {
661 info.pac_enabled_keys = -1;
662 }
663
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700664 if (thread == g_target_thread) {
Sijie Chen3ff250f2024-03-25 20:06:18 +0000665 ReadGuestRegisters(&info.guest_registers, thread);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700666 // 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 }
680
681 thread_info[thread] = std::move(info);
682 }
683 }
684
685 // Trace the pseudothread with PTRACE_O_TRACECLONE and tell it to fork.
686 if (!ptrace_seize_thread(target_proc_fd, pseudothread_tid, &error, PTRACE_O_TRACECLONE)) {
687 LOG(FATAL) << "failed to seize pseudothread: " << error;
688 }
689
690 if (TEMP_FAILURE_RETRY(write(output_pipe.get(), "\1", 1)) != 1) {
691 PLOG(FATAL) << "failed to write to pseudothread";
692 }
693
694 pid_t vm_pid = wait_for_vm_process(pseudothread_tid);
695 if (ptrace(PTRACE_DETACH, pseudothread_tid, 0, 0) != 0) {
696 PLOG(FATAL) << "failed to detach from pseudothread";
697 }
698
699 // The pseudothread can die now.
700 fork_exit_write.reset();
701
702 // Defer the message until later, for readability.
Elliott Hughese4781d52021-03-17 09:15:15 -0700703 bool wait_for_debugger = android::base::GetBoolProperty(
704 "debug.debuggerd.wait_for_debugger",
705 android::base::GetBoolProperty("debug.debuggerd.wait_for_gdb", false));
Josh Gaoa48b41b2019-12-13 14:11:04 -0800706 if (siginfo.si_signo == BIONIC_SIGNAL_DEBUGGER) {
Elliott Hughese4781d52021-03-17 09:15:15 -0700707 wait_for_debugger = false;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700708 }
709
710 // Detach from all of our attached threads before resuming.
711 for (const auto& [tid, thread] : thread_info) {
Josh Gaoa48b41b2019-12-13 14:11:04 -0800712 int resume_signal = thread.signo == BIONIC_SIGNAL_DEBUGGER ? 0 : thread.signo;
Elliott Hughese4781d52021-03-17 09:15:15 -0700713 if (wait_for_debugger) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700714 resume_signal = 0;
715 if (tgkill(target_process, tid, SIGSTOP) != 0) {
716 PLOG(WARNING) << "failed to send SIGSTOP to " << tid;
717 }
718 }
719
720 LOG(DEBUG) << "detaching from thread " << tid;
721 if (ptrace(PTRACE_DETACH, tid, 0, resume_signal) != 0) {
722 PLOG(ERROR) << "failed to detach from thread " << tid;
723 }
724 }
725
726 // Drop our capabilities now that we've fetched all of the information we need.
Josh Gao2f11a252017-02-13 14:46:19 -0800727 drop_capabilities();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700728
Josh Gao8bb03902017-05-30 15:31:02 -0700729 {
730 ATRACE_NAME("tombstoned_connect");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700731 LOG(INFO) << "obtaining output fd from tombstoned, type: " << dump_type;
Shikha Panwarabde59e2023-02-03 15:05:18 +0000732 g_tombstoned_connected = connect_tombstone_server(g_target_thread, &g_tombstoned_socket,
733 &g_output_fd, &g_proto_fd, dump_type);
Josh Gao8bb03902017-05-30 15:31:02 -0700734 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700735
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700736 if (g_tombstoned_connected) {
737 if (TEMP_FAILURE_RETRY(dup2(g_output_fd.get(), STDOUT_FILENO)) == -1) {
738 PLOG(ERROR) << "failed to dup2 output fd (" << g_output_fd.get() << ") to STDOUT_FILENO";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700739 }
740 } else {
741 unique_fd devnull(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR)));
742 TEMP_FAILURE_RETRY(dup2(devnull.get(), STDOUT_FILENO));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700743 g_output_fd = std::move(devnull);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700744 }
745
Josh Gao9da1f512018-08-06 15:38:29 -0700746 LOG(INFO) << "performing dump of process " << target_process
747 << " (target tid = " << g_target_thread << ")";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700748
749 int signo = siginfo.si_signo;
Josh Gaoa48b41b2019-12-13 14:11:04 -0800750 bool fatal_signal = signo != BIONIC_SIGNAL_DEBUGGER;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700751 bool backtrace = false;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700752
Josh Gaoa48b41b2019-12-13 14:11:04 -0800753 // si_value is special when used with BIONIC_SIGNAL_DEBUGGER.
Josh Gaocbe70cb2016-10-18 18:17:52 -0700754 // 0: dump tombstone
755 // 1: dump backtrace
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700756 if (!fatal_signal) {
757 int si_val = siginfo.si_value.sival_int;
758 if (si_val == 0) {
759 backtrace = false;
760 } else if (si_val == 1) {
761 backtrace = true;
762 } else {
763 LOG(WARNING) << "unknown si_value value " << si_val;
764 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700765 }
766
Josh Gaocbe70cb2016-10-18 18:17:52 -0700767 // TODO: Use seccomp to lock ourselves down.
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700768
769 unwindstack::AndroidRemoteUnwinder unwinder(vm_pid, unwindstack::Regs::CurrentArch());
770 unwindstack::ErrorData error_data;
771 if (!unwinder.Initialize(error_data)) {
772 LOG(FATAL) << "Failed to initialize unwinder object: "
773 << unwindstack::GetErrorCodeString(error_data.code);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700774 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700775
Josh Gaocbe70cb2016-10-18 18:17:52 -0700776 std::string amfd_data;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700777 if (backtrace) {
Josh Gao8bb03902017-05-30 15:31:02 -0700778 ATRACE_NAME("dump_backtrace");
Christopher Ferris60eb1972019-01-15 15:18:43 -0800779 dump_backtrace(std::move(g_output_fd), &unwinder, thread_info, g_target_thread);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700780 } else {
Josh Gaoce841d92018-08-06 18:26:42 -0700781 {
782 ATRACE_NAME("fdsan table dump");
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800783 populate_fdsan_table(&open_files, unwinder.GetProcessMemory(),
784 process_info.fdsan_table_address);
Josh Gaoce841d92018-08-06 18:26:42 -0700785 }
786
787 {
788 ATRACE_NAME("engrave_tombstone");
Sijie Chen4c3a9df2024-04-26 00:40:22 +0000789 unwindstack::ArchEnum regs_arch = unwindstack::ARCH_UNKNOWN;
790 switch (g_guest_arch) {
791 case Architecture::ARM32: {
792 regs_arch = unwindstack::ARCH_ARM;
793 break;
794 }
795 case Architecture::ARM64: {
796 regs_arch = unwindstack::ARCH_ARM64;
797 break;
798 }
799 case Architecture::RISCV64: {
800 regs_arch = unwindstack::ARCH_RISCV64;
801 break;
802 }
803 default: {
804 }
805 }
806 if (regs_arch == unwindstack::ARCH_UNKNOWN) {
807 engrave_tombstone(std::move(g_output_fd), std::move(g_proto_fd), &unwinder, thread_info,
808 g_target_thread, process_info, &open_files, &amfd_data);
809 } else {
810 unwindstack::AndroidRemoteUnwinder guest_unwinder(vm_pid, regs_arch);
811 engrave_tombstone(std::move(g_output_fd), std::move(g_proto_fd), &unwinder, thread_info,
812 g_target_thread, process_info, &open_files, &amfd_data, &g_guest_arch,
813 &guest_unwinder);
814 }
Josh Gaoce841d92018-08-06 18:26:42 -0700815 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700816 }
817
Mitch Phillipsdd5a80d2023-03-13 15:08:17 -0700818 if (fatal_signal) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700819 // Don't try to notify ActivityManager if it just crashed, or we might hang until timeout.
820 if (thread_info[target_process].thread_name != "system_server") {
Mitch Phillips31407912024-03-14 14:18:47 +0100821 activity_manager_notify(target_process, signo, amfd_data, recoverable_crash);
Josh Gao7c6e3132017-01-22 17:59:02 -0800822 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700823 }
824
Elliott Hughese4781d52021-03-17 09:15:15 -0700825 if (wait_for_debugger) {
Josh Gao7c6e3132017-01-22 17:59:02 -0800826 // Use ALOGI to line up with output from engrave_tombstone.
827 ALOGI(
dimitry6429e202017-09-12 10:46:20 +0200828 "***********************************************************\n"
829 "* Process %d has been suspended while crashing.\n"
Elliott Hughese4781d52021-03-17 09:15:15 -0700830 "* To attach the debugger, run this on the host:\n"
dimitry6429e202017-09-12 10:46:20 +0200831 "*\n"
Elliott Hughes5a4e6252023-02-28 22:16:19 +0000832 "* lldbclient.py -p %d\n"
dimitry6429e202017-09-12 10:46:20 +0200833 "*\n"
834 "***********************************************************",
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700835 target_process, target_process);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700836 }
837
838 // Close stdout before we notify tombstoned of completion.
839 close(STDOUT_FILENO);
Shikha Panwarabde59e2023-02-03 15:05:18 +0000840 if (g_tombstoned_connected &&
841 !notify_completion(g_tombstoned_socket.get(), g_output_fd.get(), g_proto_fd.get())) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700842 LOG(ERROR) << "failed to notify tombstoned of completion";
843 }
844
845 return 0;
846}