1 /*
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>
21 #include <sys/prctl.h>
22 #include <sys/ptrace.h>
23 #include <sys/types.h>
24 #include <sys/un.h>
25 #include <sys/wait.h>
26 #include <syscall.h>
27 #include <unistd.h>
28 
29 #include <limits>
30 #include <map>
31 #include <memory>
32 #include <set>
33 #include <vector>
34 
35 #include <android-base/file.h>
36 #include <android-base/logging.h>
37 #include <android-base/macros.h>
38 #include <android-base/parseint.h>
39 #include <android-base/properties.h>
40 #include <android-base/stringprintf.h>
41 #include <android-base/strings.h>
42 #include <android-base/unique_fd.h>
43 #include <bionic/reserved_signals.h>
44 #include <cutils/sockets.h>
45 #include <log/log.h>
46 #include <private/android_filesystem_config.h>
47 #include <procinfo/process.h>
48 
49 #define ATRACE_TAG ATRACE_TAG_BIONIC
50 #include <utils/Trace.h>
51 
52 #include <unwindstack/DexFiles.h>
53 #include <unwindstack/JitDebug.h>
54 #include <unwindstack/Maps.h>
55 #include <unwindstack/Memory.h>
56 #include <unwindstack/Regs.h>
57 #include <unwindstack/Unwinder.h>
58 
59 #include "libdebuggerd/backtrace.h"
60 #include "libdebuggerd/tombstone.h"
61 #include "libdebuggerd/utility.h"
62 
63 #include "debuggerd/handler.h"
64 #include "tombstoned/tombstoned.h"
65 
66 #include "protocol.h"
67 #include "util.h"
68 
69 using android::base::unique_fd;
70 using android::base::StringPrintf;
71 
pid_contains_tid(int pid_proc_fd,pid_t tid)72 static bool pid_contains_tid(int pid_proc_fd, pid_t tid) {
73   struct stat st;
74   std::string task_path = StringPrintf("task/%d", tid);
75   return fstatat(pid_proc_fd, task_path.c_str(), &st, 0) == 0;
76 }
77 
get_tracer(pid_t tracee)78 static pid_t get_tracer(pid_t tracee) {
79   // Check to see if the thread is being ptraced by another process.
80   android::procinfo::ProcessInfo process_info;
81   if (android::procinfo::GetProcessInfo(tracee, &process_info)) {
82     return process_info.tracer;
83   }
84   return -1;
85 }
86 
87 // Attach to a thread, and verify that it's still a member of the given process
ptrace_seize_thread(int pid_proc_fd,pid_t tid,std::string * error,int flags=0)88 static bool ptrace_seize_thread(int pid_proc_fd, pid_t tid, std::string* error, int flags = 0) {
89   if (ptrace(PTRACE_SEIZE, tid, 0, flags) != 0) {
90     if (errno == EPERM) {
91       pid_t tracer = get_tracer(tid);
92       if (tracer != -1) {
93         *error = StringPrintf("failed to attach to thread %d, already traced by %d (%s)", tid,
94                               tracer, get_process_name(tracer).c_str());
95         return false;
96       }
97     }
98 
99     *error = StringPrintf("failed to attach to thread %d: %s", tid, strerror(errno));
100     return false;
101   }
102 
103   // Make sure that the task we attached to is actually part of the pid we're dumping.
104   if (!pid_contains_tid(pid_proc_fd, tid)) {
105     if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
106       PLOG(WARNING) << "failed to detach from thread " << tid;
107     }
108     *error = StringPrintf("thread %d is not in process", tid);
109     return false;
110   }
111 
112   return true;
113 }
114 
wait_for_stop(pid_t tid,int * received_signal)115 static 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.
ptrace_interrupt(pid_t tid,int * received_signal)136 static 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);
139   }
140 
141   PLOG(ERROR) << "failed to interrupt " << tid << " to detach";
142   return false;
143 }
144 
activity_manager_notify(pid_t pid,int signal,const std::string & amfd_data)145 static bool activity_manager_notify(pid_t pid, int signal, const std::string& amfd_data) {
146   ATRACE_CALL();
147   android::base::unique_fd amfd(socket_local_client(
148       "/data/system/ndebugsocket", ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM));
149   if (amfd.get() == -1) {
150     PLOG(ERROR) << "unable to connect to activity manager";
151     return false;
152   }
153 
154   struct timeval tv = {
155     .tv_sec = 1,
156     .tv_usec = 0,
157   };
158   if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
159     PLOG(ERROR) << "failed to set send timeout on activity manager socket";
160     return false;
161   }
162   tv.tv_sec = 3;  // 3 seconds on handshake read
163   if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
164     PLOG(ERROR) << "failed to set receive timeout on activity manager socket";
165     return false;
166   }
167 
168   // Activity Manager protocol: binary 32-bit network-byte-order ints for the
169   // pid and signal number, followed by the raw text of the dump, culminating
170   // in a zero byte that marks end-of-data.
171   uint32_t datum = htonl(pid);
172   if (!android::base::WriteFully(amfd, &datum, 4)) {
173     PLOG(ERROR) << "AM pid write failed";
174     return false;
175   }
176   datum = htonl(signal);
177   if (!android::base::WriteFully(amfd, &datum, 4)) {
178     PLOG(ERROR) << "AM signal write failed";
179     return false;
180   }
181   if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size() + 1)) {
182     PLOG(ERROR) << "AM data write failed";
183     return false;
184   }
185 
186   // 3 sec timeout reading the ack; we're fine if the read fails.
187   char ack;
188   android::base::ReadFully(amfd, &ack, 1);
189   return true;
190 }
191 
192 // Globals used by the abort handler.
193 static pid_t g_target_thread = -1;
194 static bool g_tombstoned_connected = false;
195 static unique_fd g_tombstoned_socket;
196 static unique_fd g_output_fd;
197 
DefuseSignalHandlers()198 static void DefuseSignalHandlers() {
199   // Don't try to dump ourselves.
200   struct sigaction action = {};
201   action.sa_handler = SIG_DFL;
202   debuggerd_register_handlers(&action);
203 
204   sigset_t mask;
205   sigemptyset(&mask);
206   if (sigprocmask(SIG_SETMASK, &mask, nullptr) != 0) {
207     PLOG(FATAL) << "failed to set signal mask";
208   }
209 }
210 
Initialize(char ** argv)211 static void Initialize(char** argv) {
212   android::base::InitLogging(argv);
213   android::base::SetAborter([](const char* abort_msg) {
214     // If we abort before we get an output fd, contact tombstoned to let any
215     // potential listeners know that we failed.
216     if (!g_tombstoned_connected) {
217       if (!tombstoned_connect(g_target_thread, &g_tombstoned_socket, &g_output_fd,
218                               kDebuggerdAnyIntercept)) {
219         // We failed to connect, not much we can do.
220         LOG(ERROR) << "failed to connected to tombstoned to report failure";
221         _exit(1);
222       }
223     }
224 
225     dprintf(g_output_fd.get(), "crash_dump failed to dump process");
226     if (g_target_thread != 1) {
227       dprintf(g_output_fd.get(), " %d: %s\n", g_target_thread, abort_msg);
228     } else {
229       dprintf(g_output_fd.get(), ": %s\n", abort_msg);
230     }
231 
232     _exit(1);
233   });
234 }
235 
ParseArgs(int argc,char ** argv,pid_t * pseudothread_tid,DebuggerdDumpType * dump_type)236 static void ParseArgs(int argc, char** argv, pid_t* pseudothread_tid, DebuggerdDumpType* dump_type) {
237   if (argc != 4) {
238     LOG(FATAL) << "wrong number of args: " << argc << " (expected 4)";
239   }
240 
241   if (!android::base::ParseInt(argv[1], &g_target_thread, 1, std::numeric_limits<pid_t>::max())) {
242     LOG(FATAL) << "invalid target tid: " << argv[1];
243   }
244 
245   if (!android::base::ParseInt(argv[2], pseudothread_tid, 1, std::numeric_limits<pid_t>::max())) {
246     LOG(FATAL) << "invalid pseudothread tid: " << argv[2];
247   }
248 
249   int dump_type_int;
250   if (!android::base::ParseInt(argv[3], &dump_type_int, 0, 1)) {
251     LOG(FATAL) << "invalid requested dump type: " << argv[3];
252   }
253   *dump_type = static_cast<DebuggerdDumpType>(dump_type_int);
254 }
255 
ReadCrashInfo(unique_fd & fd,siginfo_t * siginfo,std::unique_ptr<unwindstack::Regs> * regs,ProcessInfo * process_info)256 static void ReadCrashInfo(unique_fd& fd, siginfo_t* siginfo,
257                           std::unique_ptr<unwindstack::Regs>* regs, ProcessInfo* process_info) {
258   std::aligned_storage<sizeof(CrashInfo) + 1, alignof(CrashInfo)>::type buf;
259   CrashInfo* crash_info = reinterpret_cast<CrashInfo*>(&buf);
260   ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), &buf, sizeof(buf)));
261   if (rc == -1) {
262     PLOG(FATAL) << "failed to read target ucontext";
263   } else {
264     ssize_t expected_size = 0;
265     switch (crash_info->header.version) {
266       case 1:
267       case 2:
268       case 3:
269         expected_size = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataStatic);
270         break;
271 
272       case 4:
273         expected_size = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataDynamic);
274         break;
275 
276       default:
277         LOG(FATAL) << "unexpected CrashInfo version: " << crash_info->header.version;
278         break;
279     };
280 
281     if (rc < expected_size) {
282       LOG(FATAL) << "read " << rc << " bytes when reading target crash information, expected "
283                  << expected_size;
284     }
285   }
286 
287   switch (crash_info->header.version) {
288     case 4:
289       process_info->fdsan_table_address = crash_info->data.d.fdsan_table_address;
290       process_info->gwp_asan_state = crash_info->data.d.gwp_asan_state;
291       process_info->gwp_asan_metadata = crash_info->data.d.gwp_asan_metadata;
292       process_info->scudo_stack_depot = crash_info->data.d.scudo_stack_depot;
293       process_info->scudo_region_info = crash_info->data.d.scudo_region_info;
294       FALLTHROUGH_INTENDED;
295     case 1:
296     case 2:
297     case 3:
298       process_info->abort_msg_address = crash_info->data.s.abort_msg_address;
299       *siginfo = crash_info->data.s.siginfo;
300       if (signal_has_si_addr(siginfo)) {
301         // Make a copy of the ucontext field because otherwise it is not aligned enough (due to
302         // being in a packed struct) and clang complains about that.
303         ucontext_t ucontext = crash_info->data.s.ucontext;
304         process_info->has_fault_address = true;
305         process_info->fault_address = get_fault_address(siginfo, &ucontext);
306       }
307       regs->reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(),
308                                                         &crash_info->data.s.ucontext));
309       break;
310 
311     default:
312       __builtin_unreachable();
313   }
314 }
315 
316 // Wait for a process to clone and return the child's pid.
317 // Note: this leaves the parent in PTRACE_EVENT_STOP.
wait_for_clone(pid_t pid,bool resume_child)318 static pid_t wait_for_clone(pid_t pid, bool resume_child) {
319   int status;
320   pid_t result = TEMP_FAILURE_RETRY(waitpid(pid, &status, __WALL));
321   if (result == -1) {
322     PLOG(FATAL) << "failed to waitpid";
323   }
324 
325   if (WIFEXITED(status)) {
326     LOG(FATAL) << "traced process exited with status " << WEXITSTATUS(status);
327   } else if (WIFSIGNALED(status)) {
328     LOG(FATAL) << "traced process exited with signal " << WTERMSIG(status);
329   } else if (!WIFSTOPPED(status)) {
330     LOG(FATAL) << "process didn't stop? (status = " << status << ")";
331   }
332 
333   if (status >> 8 != (SIGTRAP | (PTRACE_EVENT_CLONE << 8))) {
334     LOG(FATAL) << "process didn't stop due to PTRACE_O_TRACECLONE (status = " << status << ")";
335   }
336 
337   pid_t child;
338   if (ptrace(PTRACE_GETEVENTMSG, pid, 0, &child) != 0) {
339     PLOG(FATAL) << "failed to get child pid via PTRACE_GETEVENTMSG";
340   }
341 
342   int stop_signal;
343   if (!wait_for_stop(child, &stop_signal)) {
344     PLOG(FATAL) << "failed to waitpid on child";
345   }
346 
347   CHECK_EQ(0, stop_signal);
348 
349   if (resume_child) {
350     if (ptrace(PTRACE_CONT, child, 0, 0) != 0) {
351       PLOG(FATAL) << "failed to resume child (pid = " << child << ")";
352     }
353   }
354 
355   return child;
356 }
357 
wait_for_vm_process(pid_t pseudothread_tid)358 static pid_t wait_for_vm_process(pid_t pseudothread_tid) {
359   // The pseudothread will double-fork, we want its grandchild.
360   pid_t intermediate = wait_for_clone(pseudothread_tid, true);
361   pid_t vm_pid = wait_for_clone(intermediate, false);
362   if (ptrace(PTRACE_DETACH, intermediate, 0, 0) != 0) {
363     PLOG(FATAL) << "failed to detach from intermediate vm process";
364   }
365 
366   return vm_pid;
367 }
368 
InstallSigPipeHandler()369 static void InstallSigPipeHandler() {
370   struct sigaction action = {};
371   action.sa_handler = SIG_IGN;
372   action.sa_flags = SA_RESTART;
373   sigaction(SIGPIPE, &action, nullptr);
374 }
375 
main(int argc,char ** argv)376 int main(int argc, char** argv) {
377   DefuseSignalHandlers();
378   InstallSigPipeHandler();
379 
380   // There appears to be a bug in the kernel where our death causes SIGHUP to
381   // be sent to our process group if we exit while it has stopped jobs (e.g.
382   // because of wait_for_gdb). Use setsid to create a new process group to
383   // avoid hitting this.
384   setsid();
385 
386   atrace_begin(ATRACE_TAG, "before reparent");
387   pid_t target_process = getppid();
388 
389   // Open /proc/`getppid()` before we daemonize.
390   std::string target_proc_path = "/proc/" + std::to_string(target_process);
391   int target_proc_fd = open(target_proc_path.c_str(), O_DIRECTORY | O_RDONLY);
392   if (target_proc_fd == -1) {
393     PLOG(FATAL) << "failed to open " << target_proc_path;
394   }
395 
396   // Make sure getppid() hasn't changed.
397   if (getppid() != target_process) {
398     LOG(FATAL) << "parent died";
399   }
400   atrace_end(ATRACE_TAG);
401 
402   // Reparent ourselves to init, so that the signal handler can waitpid on the
403   // original process to avoid leaving a zombie for non-fatal dumps.
404   // Move the input/output pipes off of stdout/stderr, out of paranoia.
405   unique_fd output_pipe(dup(STDOUT_FILENO));
406   unique_fd input_pipe(dup(STDIN_FILENO));
407 
408   unique_fd fork_exit_read, fork_exit_write;
409   if (!Pipe(&fork_exit_read, &fork_exit_write)) {
410     PLOG(FATAL) << "failed to create pipe";
411   }
412 
413   pid_t forkpid = fork();
414   if (forkpid == -1) {
415     PLOG(FATAL) << "fork failed";
416   } else if (forkpid == 0) {
417     fork_exit_read.reset();
418   } else {
419     // We need the pseudothread to live until we get around to verifying the vm pid against it.
420     // The last thing it does is block on a waitpid on us, so wait until our child tells us to die.
421     fork_exit_write.reset();
422     char buf;
423     TEMP_FAILURE_RETRY(read(fork_exit_read.get(), &buf, sizeof(buf)));
424     _exit(0);
425   }
426 
427   ATRACE_NAME("after reparent");
428   pid_t pseudothread_tid;
429   DebuggerdDumpType dump_type;
430   ProcessInfo process_info;
431 
432   Initialize(argv);
433   ParseArgs(argc, argv, &pseudothread_tid, &dump_type);
434 
435   // Die if we take too long.
436   //
437   // Note: processes with many threads and minidebug-info can take a bit to
438   //       unwind, do not make this too small. b/62828735
439   alarm(30);
440 
441   // Get the process name (aka cmdline).
442   std::string process_name = get_process_name(g_target_thread);
443 
444   // Collect the list of open files.
445   OpenFilesList open_files;
446   {
447     ATRACE_NAME("open files");
448     populate_open_files_list(&open_files, g_target_thread);
449   }
450 
451   // In order to reduce the duration that we pause the process for, we ptrace
452   // the threads, fetch their registers and associated information, and then
453   // fork a separate process as a snapshot of the process's address space.
454   std::set<pid_t> threads;
455   if (!android::procinfo::GetProcessTids(g_target_thread, &threads)) {
456     PLOG(FATAL) << "failed to get process threads";
457   }
458 
459   std::map<pid_t, ThreadInfo> thread_info;
460   siginfo_t siginfo;
461   std::string error;
462 
463   {
464     ATRACE_NAME("ptrace");
465     for (pid_t thread : threads) {
466       // Trace the pseudothread separately, so we can use different options.
467       if (thread == pseudothread_tid) {
468         continue;
469       }
470 
471       if (!ptrace_seize_thread(target_proc_fd, thread, &error)) {
472         bool fatal = thread == g_target_thread;
473         LOG(fatal ? FATAL : WARNING) << error;
474       }
475 
476       ThreadInfo info;
477       info.pid = target_process;
478       info.tid = thread;
479       info.uid = getuid();
480       info.process_name = process_name;
481       info.thread_name = get_thread_name(thread);
482 
483       if (!ptrace_interrupt(thread, &info.signo)) {
484         PLOG(WARNING) << "failed to ptrace interrupt thread " << thread;
485         ptrace(PTRACE_DETACH, thread, 0, 0);
486         continue;
487       }
488 
489       if (thread == g_target_thread) {
490         // Read the thread's registers along with the rest of the crash info out of the pipe.
491         ReadCrashInfo(input_pipe, &siginfo, &info.registers, &process_info);
492         info.siginfo = &siginfo;
493         info.signo = info.siginfo->si_signo;
494       } else {
495         info.registers.reset(unwindstack::Regs::RemoteGet(thread));
496         if (!info.registers) {
497           PLOG(WARNING) << "failed to fetch registers for thread " << thread;
498           ptrace(PTRACE_DETACH, thread, 0, 0);
499           continue;
500         }
501       }
502 
503       thread_info[thread] = std::move(info);
504     }
505   }
506 
507   // Trace the pseudothread with PTRACE_O_TRACECLONE and tell it to fork.
508   if (!ptrace_seize_thread(target_proc_fd, pseudothread_tid, &error, PTRACE_O_TRACECLONE)) {
509     LOG(FATAL) << "failed to seize pseudothread: " << error;
510   }
511 
512   if (TEMP_FAILURE_RETRY(write(output_pipe.get(), "\1", 1)) != 1) {
513     PLOG(FATAL) << "failed to write to pseudothread";
514   }
515 
516   pid_t vm_pid = wait_for_vm_process(pseudothread_tid);
517   if (ptrace(PTRACE_DETACH, pseudothread_tid, 0, 0) != 0) {
518     PLOG(FATAL) << "failed to detach from pseudothread";
519   }
520 
521   // The pseudothread can die now.
522   fork_exit_write.reset();
523 
524   // Defer the message until later, for readability.
525   bool wait_for_gdb = android::base::GetBoolProperty("debug.debuggerd.wait_for_gdb", false);
526   if (siginfo.si_signo == BIONIC_SIGNAL_DEBUGGER) {
527     wait_for_gdb = false;
528   }
529 
530   // Detach from all of our attached threads before resuming.
531   for (const auto& [tid, thread] : thread_info) {
532     int resume_signal = thread.signo == BIONIC_SIGNAL_DEBUGGER ? 0 : thread.signo;
533     if (wait_for_gdb) {
534       resume_signal = 0;
535       if (tgkill(target_process, tid, SIGSTOP) != 0) {
536         PLOG(WARNING) << "failed to send SIGSTOP to " << tid;
537       }
538     }
539 
540     LOG(DEBUG) << "detaching from thread " << tid;
541     if (ptrace(PTRACE_DETACH, tid, 0, resume_signal) != 0) {
542       PLOG(ERROR) << "failed to detach from thread " << tid;
543     }
544   }
545 
546   // Drop our capabilities now that we've fetched all of the information we need.
547   drop_capabilities();
548 
549   {
550     ATRACE_NAME("tombstoned_connect");
551     LOG(INFO) << "obtaining output fd from tombstoned, type: " << dump_type;
552     g_tombstoned_connected =
553         tombstoned_connect(g_target_thread, &g_tombstoned_socket, &g_output_fd, dump_type);
554   }
555 
556   if (g_tombstoned_connected) {
557     if (TEMP_FAILURE_RETRY(dup2(g_output_fd.get(), STDOUT_FILENO)) == -1) {
558       PLOG(ERROR) << "failed to dup2 output fd (" << g_output_fd.get() << ") to STDOUT_FILENO";
559     }
560   } else {
561     unique_fd devnull(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR)));
562     TEMP_FAILURE_RETRY(dup2(devnull.get(), STDOUT_FILENO));
563     g_output_fd = std::move(devnull);
564   }
565 
566   LOG(INFO) << "performing dump of process " << target_process
567             << " (target tid = " << g_target_thread << ")";
568 
569   int signo = siginfo.si_signo;
570   bool fatal_signal = signo != BIONIC_SIGNAL_DEBUGGER;
571   bool backtrace = false;
572 
573   // si_value is special when used with BIONIC_SIGNAL_DEBUGGER.
574   //   0: dump tombstone
575   //   1: dump backtrace
576   if (!fatal_signal) {
577     int si_val = siginfo.si_value.sival_int;
578     if (si_val == 0) {
579       backtrace = false;
580     } else if (si_val == 1) {
581       backtrace = true;
582     } else {
583       LOG(WARNING) << "unknown si_value value " << si_val;
584     }
585   }
586 
587   // TODO: Use seccomp to lock ourselves down.
588   unwindstack::UnwinderFromPid unwinder(256, vm_pid);
589   if (!unwinder.Init(unwindstack::Regs::CurrentArch())) {
590     LOG(FATAL) << "Failed to init unwinder object.";
591   }
592 
593   std::string amfd_data;
594   if (backtrace) {
595     ATRACE_NAME("dump_backtrace");
596     dump_backtrace(std::move(g_output_fd), &unwinder, thread_info, g_target_thread);
597   } else {
598     {
599       ATRACE_NAME("fdsan table dump");
600       populate_fdsan_table(&open_files, unwinder.GetProcessMemory(),
601                            process_info.fdsan_table_address);
602     }
603 
604     {
605       ATRACE_NAME("engrave_tombstone");
606       engrave_tombstone(std::move(g_output_fd), &unwinder, thread_info, g_target_thread, process_info,
607                         &open_files, &amfd_data);
608     }
609   }
610 
611   if (fatal_signal) {
612     // Don't try to notify ActivityManager if it just crashed, or we might hang until timeout.
613     if (thread_info[target_process].thread_name != "system_server") {
614       activity_manager_notify(target_process, signo, amfd_data);
615     }
616   }
617 
618   if (wait_for_gdb) {
619     // Use ALOGI to line up with output from engrave_tombstone.
620     ALOGI(
621         "***********************************************************\n"
622         "* Process %d has been suspended while crashing.\n"
623         "* To attach gdbserver and start gdb, run this on the host:\n"
624         "*\n"
625         "*     gdbclient.py -p %d\n"
626         "*\n"
627         "***********************************************************",
628         target_process, target_process);
629   }
630 
631   // Close stdout before we notify tombstoned of completion.
632   close(STDOUT_FILENO);
633   if (g_tombstoned_connected && !tombstoned_notify_completion(g_tombstoned_socket.get())) {
634     LOG(ERROR) << "failed to notify tombstoned of completion";
635   }
636 
637   return 0;
638 }
639