1 /*
2  * Copyright (C) 2018 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 <dirent.h>
18 #include <poll.h>
19 #include <sys/prctl.h>
20 #include <sys/ptrace.h>
21 #include <sys/types.h>
22 #include <sys/wait.h>
23 #include <unistd.h>
24 
25 #include <csignal>
26 #include <cstdlib>
27 #include <cstring>
28 #include <iostream>
29 #include <thread>
30 #include <memory>
31 #include <set>
32 #include <string>
33 
34 #include <android-base/file.h>
35 #include <android-base/logging.h>
36 #include <android-base/macros.h>
37 #include <android-base/parseint.h>
38 #include <android-base/stringprintf.h>
39 #include <android-base/strings.h>
40 #include <android-base/unique_fd.h>
41 #include <backtrace/Backtrace.h>
42 #include <backtrace/BacktraceMap.h>
43 
44 namespace art {
45 namespace {
46 
47 using android::base::StringPrintf;
48 using android::base::unique_fd;
49 
50 constexpr bool kUseAddr2line = true;
51 
52 namespace timeout_signal {
53 
54 class SignalSet {
55  public:
SignalSet()56   SignalSet() {
57     if (sigemptyset(&set_) == -1) {
58       PLOG(FATAL) << "sigemptyset failed";
59     }
60   }
61 
Add(int signal)62   void Add(int signal) {
63     if (sigaddset(&set_, signal) == -1) {
64       PLOG(FATAL) << "sigaddset " << signal << " failed";
65     }
66   }
67 
Block()68   void Block() {
69     if (pthread_sigmask(SIG_BLOCK, &set_, nullptr) != 0) {
70       PLOG(FATAL) << "pthread_sigmask failed";
71     }
72   }
73 
Wait()74   int Wait() {
75     // Sleep in sigwait() until a signal arrives. gdb causes EINTR failures.
76     int signal_number;
77     int rc = TEMP_FAILURE_RETRY(sigwait(&set_, &signal_number));
78     if (rc != 0) {
79       PLOG(FATAL) << "sigwait failed";
80     }
81     return signal_number;
82   }
83 
84  private:
85   sigset_t set_;
86 };
87 
88 }  // namespace timeout_signal
89 
90 namespace addr2line {
91 
92 constexpr const char* kAddr2linePath =
93     "/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8/bin/x86_64-linux-addr2line";
94 
FindAddr2line()95 std::unique_ptr<std::string> FindAddr2line() {
96   const char* env_value = getenv("ANDROID_BUILD_TOP");
97   if (env_value != nullptr) {
98     std::string path = std::string(env_value) + kAddr2linePath;
99     if (access(path.c_str(), X_OK) == 0) {
100       return std::make_unique<std::string>(path);
101     }
102   }
103 
104   {
105     std::string path = std::string(".") + kAddr2linePath;
106     if (access(path.c_str(), X_OK) == 0) {
107       return std::make_unique<std::string>(path);
108     }
109   }
110 
111   {
112     using android::base::Dirname;
113 
114     std::string exec_dir = android::base::GetExecutableDirectory();
115     std::string derived_top = Dirname(Dirname(Dirname(Dirname(exec_dir))));
116     std::string path = derived_top + kAddr2linePath;
117     if (access(path.c_str(), X_OK) == 0) {
118       return std::make_unique<std::string>(path);
119     }
120   }
121 
122   constexpr const char* kHostAddr2line = "/usr/bin/addr2line";
123   if (access(kHostAddr2line, F_OK) == 0) {
124     return std::make_unique<std::string>(kHostAddr2line);
125   }
126 
127   return nullptr;
128 }
129 
130 // The state of an open pipe to addr2line. In "server" mode, addr2line takes input on stdin
131 // and prints the result to stdout. This struct keeps the state of the open connection.
132 struct Addr2linePipe {
Addr2linePipeart::__anon25001a8c0111::addr2line::Addr2linePipe133   Addr2linePipe(int in_fd, int out_fd, const std::string& file_name, pid_t pid)
134       : in(in_fd), out(out_fd), file(file_name), child_pid(pid), odd(true) {}
135 
~Addr2linePipeart::__anon25001a8c0111::addr2line::Addr2linePipe136   ~Addr2linePipe() {
137     kill(child_pid, SIGKILL);
138   }
139 
140   unique_fd in;      // The file descriptor that is connected to the output of addr2line.
141   unique_fd out;     // The file descriptor that is connected to the input of addr2line.
142 
143   const std::string file;     // The file addr2line is working on, so that we know when to close
144                               // and restart.
145   const pid_t child_pid;      // The pid of the child, which we should kill when we're done.
146   bool odd;                   // Print state for indentation of lines.
147 };
148 
Connect(const std::string & name,const char * args[])149 std::unique_ptr<Addr2linePipe> Connect(const std::string& name, const char* args[]) {
150   int caller_to_addr2line[2];
151   int addr2line_to_caller[2];
152 
153   if (pipe(caller_to_addr2line) == -1) {
154     return nullptr;
155   }
156   if (pipe(addr2line_to_caller) == -1) {
157     close(caller_to_addr2line[0]);
158     close(caller_to_addr2line[1]);
159     return nullptr;
160   }
161 
162   pid_t pid = fork();
163   if (pid == -1) {
164     close(caller_to_addr2line[0]);
165     close(caller_to_addr2line[1]);
166     close(addr2line_to_caller[0]);
167     close(addr2line_to_caller[1]);
168     return nullptr;
169   }
170 
171   if (pid == 0) {
172     dup2(caller_to_addr2line[0], STDIN_FILENO);
173     dup2(addr2line_to_caller[1], STDOUT_FILENO);
174 
175     close(caller_to_addr2line[0]);
176     close(caller_to_addr2line[1]);
177     close(addr2line_to_caller[0]);
178     close(addr2line_to_caller[1]);
179 
180     execv(args[0], const_cast<char* const*>(args));
181     exit(1);
182   } else {
183     close(caller_to_addr2line[0]);
184     close(addr2line_to_caller[1]);
185     return std::make_unique<Addr2linePipe>(addr2line_to_caller[0],
186                                            caller_to_addr2line[1],
187                                            name,
188                                            pid);
189   }
190 }
191 
WritePrefix(std::ostream & os,const char * prefix,bool odd)192 void WritePrefix(std::ostream& os, const char* prefix, bool odd) {
193   if (prefix != nullptr) {
194     os << prefix;
195   }
196   os << "  ";
197   if (!odd) {
198     os << " ";
199   }
200 }
201 
Drain(size_t expected,const char * prefix,std::unique_ptr<Addr2linePipe> * pipe,std::ostream & os)202 void Drain(size_t expected,
203            const char* prefix,
204            std::unique_ptr<Addr2linePipe>* pipe /* inout */,
205            std::ostream& os) {
206   DCHECK(pipe != nullptr);
207   DCHECK(pipe->get() != nullptr);
208   int in = pipe->get()->in.get();
209   DCHECK_GE(in, 0);
210 
211   bool prefix_written = false;
212 
213   for (;;) {
214     constexpr uint32_t kWaitTimeExpectedMilli = 500;
215     constexpr uint32_t kWaitTimeUnexpectedMilli = 50;
216 
217     int timeout = expected > 0 ? kWaitTimeExpectedMilli : kWaitTimeUnexpectedMilli;
218     struct pollfd read_fd{in, POLLIN, 0};
219     int retval = TEMP_FAILURE_RETRY(poll(&read_fd, 1, timeout));
220     if (retval == -1) {
221       // An error occurred.
222       pipe->reset();
223       return;
224     }
225 
226     if (retval == 0) {
227       // Timeout.
228       return;
229     }
230 
231     if (!(read_fd.revents & POLLIN)) {
232       // addr2line call exited.
233       pipe->reset();
234       return;
235     }
236 
237     constexpr size_t kMaxBuffer = 128;  // Relatively small buffer. Should be OK as we're on an
238     // alt stack, but just to be sure...
239     char buffer[kMaxBuffer];
240     memset(buffer, 0, kMaxBuffer);
241     int bytes_read = TEMP_FAILURE_RETRY(read(in, buffer, kMaxBuffer - 1));
242     if (bytes_read <= 0) {
243       // This should not really happen...
244       pipe->reset();
245       return;
246     }
247     buffer[bytes_read] = '\0';
248 
249     char* tmp = buffer;
250     while (*tmp != 0) {
251       if (!prefix_written) {
252         WritePrefix(os, prefix, (*pipe)->odd);
253         prefix_written = true;
254       }
255       char* new_line = strchr(tmp, '\n');
256       if (new_line == nullptr) {
257         os << tmp;
258 
259         break;
260       } else {
261         os << std::string(tmp, new_line - tmp + 1);
262 
263         tmp = new_line + 1;
264         prefix_written = false;
265         (*pipe)->odd = !(*pipe)->odd;
266 
267         if (expected > 0) {
268           expected--;
269         }
270       }
271     }
272   }
273 }
274 
Addr2line(const std::string & addr2line,const std::string & map_src,uintptr_t offset,std::ostream & os,const char * prefix,std::unique_ptr<Addr2linePipe> * pipe)275 void Addr2line(const std::string& addr2line,
276                const std::string& map_src,
277                uintptr_t offset,
278                std::ostream& os,
279                const char* prefix,
280                std::unique_ptr<Addr2linePipe>* pipe /* inout */) {
281   DCHECK(pipe != nullptr);
282 
283   if (map_src == "[vdso]" || android::base::EndsWith(map_src, ".vdex")) {
284     // addr2line will not work on the vdso.
285     // vdex files are special frames injected for the interpreter
286     // so they don't have any line number information available.
287     return;
288   }
289 
290   if (*pipe == nullptr || (*pipe)->file != map_src) {
291     if (*pipe != nullptr) {
292       Drain(0, prefix, pipe, os);
293     }
294     pipe->reset();  // Close early.
295 
296     const char* args[] = {
297         addr2line.c_str(),
298         "--functions",
299         "--inlines",
300         "--demangle",
301         "-e",
302         map_src.c_str(),
303         nullptr
304     };
305     *pipe = Connect(map_src, args);
306   }
307 
308   Addr2linePipe* pipe_ptr = pipe->get();
309   if (pipe_ptr == nullptr) {
310     // Failed...
311     return;
312   }
313 
314   // Send the offset.
315   const std::string hex_offset = StringPrintf("%zx\n", offset);
316 
317   if (!android::base::WriteFully(pipe_ptr->out.get(), hex_offset.data(), hex_offset.length())) {
318     // Error. :-(
319     pipe->reset();
320     return;
321   }
322 
323   // Now drain (expecting two lines).
324   Drain(2U, prefix, pipe, os);
325 }
326 
327 }  // namespace addr2line
328 
329 namespace ptrace {
330 
PtraceSiblings(pid_t pid)331 std::set<pid_t> PtraceSiblings(pid_t pid) {
332   std::set<pid_t> ret;
333   std::string task_path = android::base::StringPrintf("/proc/%d/task", pid);
334 
335   std::unique_ptr<DIR, int (*)(DIR*)> d(opendir(task_path.c_str()), closedir);
336 
337   // Bail early if the task directory cannot be opened.
338   if (d == nullptr) {
339     PLOG(ERROR) << "Failed to scan task folder";
340     return ret;
341   }
342 
343   struct dirent* de;
344   while ((de = readdir(d.get())) != nullptr) {
345     // Ignore "." and "..".
346     if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
347       continue;
348     }
349 
350     char* end;
351     pid_t tid = strtoul(de->d_name, &end, 10);
352     if (*end) {
353       continue;
354     }
355 
356     if (tid == pid) {
357       continue;
358     }
359 
360     if (::ptrace(PTRACE_ATTACH, tid, 0, 0) != 0) {
361       PLOG(ERROR) << "Failed to attach to tid " << tid;
362       continue;
363     }
364 
365     ret.insert(tid);
366   }
367   return ret;
368 }
369 
DumpABI(pid_t forked_pid)370 void DumpABI(pid_t forked_pid) {
371   enum class ABI { kArm, kArm64, kX86, kX86_64 };
372 #if defined(__arm__)
373   constexpr ABI kDumperABI = ABI::kArm;
374 #elif defined(__aarch64__)
375   constexpr ABI kDumperABI = ABI::kArm64;
376 #elif defined(__i386__)
377   constexpr ABI kDumperABI = ABI::kX86;
378 #elif defined(__x86_64__)
379   constexpr ABI kDumperABI = ABI::kX86_64;
380 #else
381 #error Unsupported architecture
382 #endif
383 
384   char data[1024];  // Should be more than enough.
385   struct iovec io_vec;
386   io_vec.iov_base = &data;
387   io_vec.iov_len = 1024;
388   ABI to_print;
389   if (0 != ::ptrace(PTRACE_GETREGSET, forked_pid, /* NT_PRSTATUS */ 1, &io_vec)) {
390     LOG(ERROR) << "Could not get registers to determine abi.";
391     // Use 64-bit as default.
392     switch (kDumperABI) {
393       case ABI::kArm:
394       case ABI::kArm64:
395         to_print = ABI::kArm64;
396         break;
397       case ABI::kX86:
398       case ABI::kX86_64:
399         to_print = ABI::kX86_64;
400         break;
401       default:
402         __builtin_unreachable();
403     }
404   } else {
405     // Check the length of the data. Assume that it's the same arch as the tool.
406     switch (kDumperABI) {
407       case ABI::kArm:
408       case ABI::kArm64:
409         to_print = io_vec.iov_len == 18 * sizeof(uint32_t) ? ABI::kArm : ABI::kArm64;
410         break;
411       case ABI::kX86:
412       case ABI::kX86_64:
413         to_print = io_vec.iov_len == 17 * sizeof(uint32_t) ? ABI::kX86 : ABI::kX86_64;
414         break;
415       default:
416         __builtin_unreachable();
417     }
418   }
419   std::string abi_str;
420   switch (to_print) {
421     case ABI::kArm:
422       abi_str = "arm";
423       break;
424     case ABI::kArm64:
425       abi_str = "arm64";
426       break;
427     case ABI::kX86:
428       abi_str = "x86";
429       break;
430     case ABI::kX86_64:
431       abi_str = "x86_64";
432       break;
433   }
434   LOG(ERROR) << "ABI: '" << abi_str << "'" << std::endl;
435 }
436 
437 }  // namespace ptrace
438 
439 template <typename T>
WaitLoop(uint32_t max_wait_micros,const T & handler)440 bool WaitLoop(uint32_t max_wait_micros, const T& handler) {
441   constexpr uint32_t kWaitMicros = 10;
442   const size_t kMaxLoopCount = max_wait_micros / kWaitMicros;
443 
444   for (size_t loop_count = 1; loop_count <= kMaxLoopCount; ++loop_count) {
445     bool ret;
446     if (handler(&ret)) {
447       return ret;
448     }
449     usleep(kWaitMicros);
450   }
451   return false;
452 }
453 
WaitForMainSigStop(const std::atomic<bool> & saw_wif_stopped_for_main)454 bool WaitForMainSigStop(const std::atomic<bool>& saw_wif_stopped_for_main) {
455   auto handler = [&](bool* res) {
456     if (saw_wif_stopped_for_main) {
457       *res = true;
458       return true;
459     }
460     return false;
461   };
462   constexpr uint32_t kMaxWaitMicros = 30 * 1000 * 1000;  // 30s wait.
463   return WaitLoop(kMaxWaitMicros, handler);
464 }
465 
WaitForSigStopped(pid_t pid,uint32_t max_wait_micros)466 bool WaitForSigStopped(pid_t pid, uint32_t max_wait_micros) {
467   auto handler = [&](bool* res) {
468     int status;
469     pid_t rc = TEMP_FAILURE_RETRY(waitpid(pid, &status, WNOHANG));
470     if (rc == -1) {
471       PLOG(ERROR) << "Failed to waitpid for " << pid;
472       *res = false;
473       return true;
474     }
475     if (rc == pid) {
476       if (!(WIFSTOPPED(status))) {
477         LOG(ERROR) << "Did not get expected stopped signal for " << pid;
478         *res = false;
479       } else {
480         *res = true;
481       }
482       return true;
483     }
484     return false;
485   };
486   return WaitLoop(max_wait_micros, handler);
487 }
488 
489 #ifdef __LP64__
490 constexpr bool kIs64Bit = true;
491 #else
492 constexpr bool kIs64Bit = false;
493 #endif
494 
DumpThread(pid_t pid,pid_t tid,const std::string * addr2line_path,const char * prefix,BacktraceMap * map)495 void DumpThread(pid_t pid,
496                 pid_t tid,
497                 const std::string* addr2line_path,
498                 const char* prefix,
499                 BacktraceMap* map) {
500   LOG(ERROR) << std::endl << "=== pid: " << pid << " tid: " << tid << " ===" << std::endl;
501 
502   constexpr uint32_t kMaxWaitMicros = 1000 * 1000;  // 1s.
503   if (pid != tid && !WaitForSigStopped(tid, kMaxWaitMicros)) {
504     LOG(ERROR) << "Failed to wait for sigstop on " << tid;
505   }
506 
507   std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, tid, map));
508   if (backtrace == nullptr) {
509     LOG(ERROR) << prefix << "(failed to create Backtrace for thread " << tid << ")";
510     return;
511   }
512   backtrace->SetSkipFrames(false);
513   if (!backtrace->Unwind(0, nullptr)) {
514     LOG(ERROR) << prefix << "(backtrace::Unwind failed for thread " << tid
515                << ": " <<  backtrace->GetErrorString(backtrace->GetError()) << ")";
516     return;
517   }
518   if (backtrace->NumFrames() == 0) {
519     LOG(ERROR) << prefix << "(no native stack frames for thread " << tid << ")";
520     return;
521   }
522 
523   std::unique_ptr<addr2line::Addr2linePipe> addr2line_state;
524 
525   for (Backtrace::const_iterator it = backtrace->begin();
526       it != backtrace->end(); ++it) {
527     std::ostringstream oss;
528     oss << prefix << StringPrintf("#%02zu pc ", it->num);
529     bool try_addr2line = false;
530     if (!BacktraceMap::IsValid(it->map)) {
531       oss << StringPrintf(kIs64Bit ? "%016" PRIx64 "  ???" : "%08" PRIx64 "  ???", it->pc);
532     } else {
533       oss << StringPrintf(kIs64Bit ? "%016" PRIx64 "  " : "%08" PRIx64 "  ", it->rel_pc);
534       if (it->map.name.empty()) {
535         oss << StringPrintf("<anonymous:%" PRIx64 ">", it->map.start);
536       } else {
537         oss << it->map.name;
538       }
539       if (it->map.offset != 0) {
540         oss << StringPrintf(" (offset %" PRIx64 ")", it->map.offset);
541       }
542       oss << " (";
543       if (!it->func_name.empty()) {
544         oss << it->func_name;
545         if (it->func_offset != 0) {
546           oss << "+" << it->func_offset;
547         }
548         // Functions found using the gdb jit interface will be in an empty
549         // map that cannot be found using addr2line.
550         if (!it->map.name.empty()) {
551           try_addr2line = true;
552         }
553       } else {
554         oss << "???";
555       }
556       oss << ")";
557     }
558     LOG(ERROR) << oss.str() << std::endl;
559     if (try_addr2line && addr2line_path != nullptr) {
560       addr2line::Addr2line(*addr2line_path,
561                            it->map.name,
562                            it->rel_pc,
563                            LOG_STREAM(ERROR),
564                            prefix,
565                            &addr2line_state);
566     }
567   }
568 
569   if (addr2line_state != nullptr) {
570     addr2line::Drain(0, prefix, &addr2line_state, LOG_STREAM(ERROR));
571   }
572 }
573 
DumpProcess(pid_t forked_pid,const std::atomic<bool> & saw_wif_stopped_for_main)574 void DumpProcess(pid_t forked_pid, const std::atomic<bool>& saw_wif_stopped_for_main) {
575   LOG(ERROR) << "Timeout for process " << forked_pid;
576 
577   CHECK_EQ(0, ::ptrace(PTRACE_ATTACH, forked_pid, 0, 0));
578   std::set<pid_t> tids = ptrace::PtraceSiblings(forked_pid);
579   tids.insert(forked_pid);
580 
581   ptrace::DumpABI(forked_pid);
582 
583   // Check whether we have and should use addr2line.
584   std::unique_ptr<std::string> addr2line_path;
585   if (kUseAddr2line) {
586     addr2line_path = addr2line::FindAddr2line();
587     if (addr2line_path == nullptr) {
588       LOG(ERROR) << "Did not find usable addr2line";
589     }
590   }
591 
592   if (!WaitForMainSigStop(saw_wif_stopped_for_main)) {
593     LOG(ERROR) << "Did not receive SIGSTOP for pid " << forked_pid;
594   }
595 
596   std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(forked_pid));
597   if (backtrace_map == nullptr) {
598     LOG(ERROR) << "Could not create BacktraceMap";
599     return;
600   }
601 
602   for (pid_t tid : tids) {
603     DumpThread(forked_pid, tid, addr2line_path.get(), "  ", backtrace_map.get());
604   }
605 }
606 
607 [[noreturn]]
WaitMainLoop(pid_t forked_pid,std::atomic<bool> * saw_wif_stopped_for_main)608 void WaitMainLoop(pid_t forked_pid, std::atomic<bool>* saw_wif_stopped_for_main) {
609   for (;;) {
610     // Consider switching to waitid to not get woken up for WIFSTOPPED.
611     int status;
612     pid_t res = TEMP_FAILURE_RETRY(waitpid(forked_pid, &status, 0));
613     if (res == -1) {
614       PLOG(FATAL) << "Failure during waitpid";
615       __builtin_unreachable();
616     }
617 
618     if (WIFEXITED(status)) {
619       _exit(WEXITSTATUS(status));
620       __builtin_unreachable();
621     }
622     if (WIFSIGNALED(status)) {
623       _exit(1);
624       __builtin_unreachable();
625     }
626     if (WIFSTOPPED(status)) {
627       *saw_wif_stopped_for_main = true;
628       continue;
629     }
630     if (WIFCONTINUED(status)) {
631       continue;
632     }
633 
634     LOG(FATAL) << "Unknown status " << std::hex << status;
635   }
636 }
637 
638 [[noreturn]]
SetupAndWait(pid_t forked_pid,int signal,int timeout_exit_code)639 void SetupAndWait(pid_t forked_pid, int signal, int timeout_exit_code) {
640   timeout_signal::SignalSet signals;
641   signals.Add(signal);
642   signals.Block();
643 
644   std::atomic<bool> saw_wif_stopped_for_main(false);
645 
646   std::thread signal_catcher([&]() {
647     signals.Block();
648     int sig = signals.Wait();
649     CHECK_EQ(sig, signal);
650 
651     DumpProcess(forked_pid, saw_wif_stopped_for_main);
652 
653     // Don't clean up. Just kill the child and exit.
654     kill(forked_pid, SIGKILL);
655     _exit(timeout_exit_code);
656   });
657 
658   WaitMainLoop(forked_pid, &saw_wif_stopped_for_main);
659 }
660 
661 }  // namespace
662 }  // namespace art
663 
main(int argc ATTRIBUTE_UNUSED,char ** argv)664 int main(int argc ATTRIBUTE_UNUSED, char** argv) {
665   android::base::InitLogging(argv);
666 
667   int signal = SIGRTMIN + 2;
668   int timeout_exit_code = 1;
669 
670   size_t index = 1u;
671   CHECK(argv[index] != nullptr);
672 
673   bool to_logcat = false;
674 #ifdef __ANDROID__
675   if (strcmp(argv[index], "-l") == 0) {
676     index++;
677     CHECK(argv[index] != nullptr);
678     to_logcat = true;
679   }
680 #endif
681   if (!to_logcat) {
682     android::base::SetLogger(android::base::StderrLogger);
683   }
684 
685   if (strcmp(argv[index], "-s") == 0) {
686     index++;
687     CHECK(argv[index] != nullptr);
688     uint32_t signal_uint;
689     CHECK(android::base::ParseUint(argv[index], &signal_uint)) << "Signal not a number.";
690     signal = signal_uint;
691     index++;
692     CHECK(argv[index] != nullptr);
693   }
694 
695   if (strcmp(argv[index], "-e") == 0) {
696     index++;
697     CHECK(argv[index] != nullptr);
698     uint32_t timeout_exit_code_uint;
699     CHECK(android::base::ParseUint(argv[index], &timeout_exit_code_uint))
700         << "Exit code not a number.";
701     timeout_exit_code = timeout_exit_code_uint;
702     index++;
703     CHECK(argv[index] != nullptr);
704   }
705 
706   pid_t orig_ppid = getpid();
707 
708   pid_t pid = fork();
709   if (pid == 0) {
710     if (prctl(PR_SET_PDEATHSIG, SIGTERM) == -1) {
711       _exit(1);
712     }
713 
714     if (getppid() != orig_ppid) {
715       _exit(2);
716     }
717 
718     execvp(argv[index], &argv[index]);
719 
720     _exit(3);
721     __builtin_unreachable();
722   }
723 
724   art::SetupAndWait(pid, signal, timeout_exit_code);
725   __builtin_unreachable();
726 }
727