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