blob: 504d01fc4128f2b9ab29bb2a824674928ee38b34 [file] [log] [blame]
Andreas Gampea5bd61e2018-11-02 15:20:53 -07001/*
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>
Andreas Gampe48f8a242018-11-06 15:34:38 -080028#include <iostream>
Andreas Gampea5bd61e2018-11-02 15:20:53 -070029#include <thread>
30#include <memory>
31#include <set>
Andreas Gampef0d4cfe2018-12-13 16:00:15 -080032#include <string>
Andreas Gampea5bd61e2018-11-02 15:20:53 -070033
34#include <android-base/file.h>
35#include <android-base/logging.h>
36#include <android-base/macros.h>
Andreas Gampe298dc0f2019-06-10 16:41:40 -070037#include <android-base/parseint.h>
Andreas Gampea5bd61e2018-11-02 15:20:53 -070038#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
44namespace art {
45namespace {
46
47using android::base::StringPrintf;
48using android::base::unique_fd;
49
50constexpr bool kUseAddr2line = true;
51
52namespace timeout_signal {
53
54class SignalSet {
55 public:
56 SignalSet() {
57 if (sigemptyset(&set_) == -1) {
58 PLOG(FATAL) << "sigemptyset failed";
59 }
60 }
61
62 void Add(int signal) {
63 if (sigaddset(&set_, signal) == -1) {
64 PLOG(FATAL) << "sigaddset " << signal << " failed";
65 }
66 }
67
68 void Block() {
69 if (pthread_sigmask(SIG_BLOCK, &set_, nullptr) != 0) {
70 PLOG(FATAL) << "pthread_sigmask failed";
71 }
72 }
73
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
Andreas Gampea5bd61e2018-11-02 15:20:53 -070088} // namespace timeout_signal
89
90namespace addr2line {
91
92constexpr const char* kAddr2linePath =
David Srbecky87da30e2019-01-30 15:51:23 +000093 "/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8/bin/x86_64-linux-addr2line";
Andreas Gampea5bd61e2018-11-02 15:20:53 -070094
95std::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
Andreas Gampef0d4cfe2018-12-13 16:00:15 -0800104 {
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 }
Andreas Gampea5bd61e2018-11-02 15:20:53 -0700120 }
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.
132struct Addr2linePipe {
133 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
136 ~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
149std::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
192void 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
202void 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 {
Andreas Gampe48f8a242018-11-06 15:34:38 -0800261 os << std::string(tmp, new_line - tmp + 1);
Andreas Gampea5bd61e2018-11-02 15:20:53 -0700262
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
275void 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
Andreas Gampe48f8a242018-11-06 15:34:38 -0800296 const char* args[] = {
Andreas Gampea5bd61e2018-11-02 15:20:53 -0700297 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
329namespace ptrace {
330
331std::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
Andreas Gamped406b622018-12-13 16:01:33 -0800370void DumpABI(pid_t forked_pid) {
371 enum class ABI { kArm, kArm64, kMips, kMips64, 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(__mips__) && !defined(__LP64__)
377 constexpr ABI kDumperABI = ABI::kMips;
378#elif defined(__mips__) && defined(__LP64__)
379 constexpr ABI kDumperABI = ABI::kMips64;
380#elif defined(__i386__)
381 constexpr ABI kDumperABI = ABI::kX86;
382#elif defined(__x86_64__)
383 constexpr ABI kDumperABI = ABI::kX86_64;
384#else
385#error Unsupported architecture
386#endif
387
388 char data[1024]; // Should be more than enough.
389 struct iovec io_vec;
390 io_vec.iov_base = &data;
391 io_vec.iov_len = 1024;
392 ABI to_print;
393 if (0 != ::ptrace(PTRACE_GETREGSET, forked_pid, /* NT_PRSTATUS */ 1, &io_vec)) {
394 LOG(ERROR) << "Could not get registers to determine abi.";
395 // Use 64-bit as default.
396 switch (kDumperABI) {
397 case ABI::kArm:
398 case ABI::kArm64:
399 to_print = ABI::kArm64;
400 break;
401 case ABI::kMips:
402 case ABI::kMips64:
403 to_print = ABI::kMips64;
404 break;
405 case ABI::kX86:
406 case ABI::kX86_64:
407 to_print = ABI::kX86_64;
408 break;
409 default:
410 __builtin_unreachable();
411 }
412 } else {
413 // Check the length of the data. Assume that it's the same arch as the tool.
414 switch (kDumperABI) {
415 case ABI::kArm:
416 case ABI::kArm64:
417 to_print = io_vec.iov_len == 18 * sizeof(uint32_t) ? ABI::kArm : ABI::kArm64;
418 break;
419 case ABI::kMips:
420 case ABI::kMips64:
421 to_print = ABI::kMips64; // TODO Figure out how this should work.
422 break;
423 case ABI::kX86:
424 case ABI::kX86_64:
425 to_print = io_vec.iov_len == 17 * sizeof(uint32_t) ? ABI::kX86 : ABI::kX86_64;
426 break;
427 default:
428 __builtin_unreachable();
429 }
430 }
431 std::string abi_str;
432 switch (to_print) {
433 case ABI::kArm:
434 abi_str = "arm";
435 break;
436 case ABI::kArm64:
437 abi_str = "arm64";
438 break;
439 case ABI::kMips:
440 abi_str = "mips";
441 break;
442 case ABI::kMips64:
443 abi_str = "mips64";
444 break;
445 case ABI::kX86:
446 abi_str = "x86";
447 break;
448 case ABI::kX86_64:
449 abi_str = "x86_64";
450 break;
451 }
452 std::cerr << "ABI: '" << abi_str << "'" << std::endl;
453}
454
Andreas Gampea5bd61e2018-11-02 15:20:53 -0700455} // namespace ptrace
456
Andreas Gampe5f04f7f2018-11-06 15:32:33 -0800457template <typename T>
458bool WaitLoop(uint32_t max_wait_micros, const T& handler) {
459 constexpr uint32_t kWaitMicros = 10;
460 const size_t kMaxLoopCount = max_wait_micros / kWaitMicros;
461
462 for (size_t loop_count = 1; loop_count <= kMaxLoopCount; ++loop_count) {
463 bool ret;
464 if (handler(&ret)) {
465 return ret;
466 }
467 usleep(kWaitMicros);
Andreas Gampea5bd61e2018-11-02 15:20:53 -0700468 }
Andreas Gampe5f04f7f2018-11-06 15:32:33 -0800469 return false;
470}
471
472bool WaitForMainSigStop(const std::atomic<bool>& saw_wif_stopped_for_main) {
473 auto handler = [&](bool* res) {
474 if (saw_wif_stopped_for_main) {
475 *res = true;
476 return true;
477 }
Andreas Gampea5bd61e2018-11-02 15:20:53 -0700478 return false;
Andreas Gampe5f04f7f2018-11-06 15:32:33 -0800479 };
480 constexpr uint32_t kMaxWaitMicros = 30 * 1000 * 1000; // 30s wait.
481 return WaitLoop(kMaxWaitMicros, handler);
482}
483
484bool WaitForSigStopped(pid_t pid, uint32_t max_wait_micros) {
485 auto handler = [&](bool* res) {
486 int status;
487 pid_t rc = TEMP_FAILURE_RETRY(waitpid(pid, &status, WNOHANG));
488 if (rc == -1) {
489 PLOG(ERROR) << "Failed to waitpid for " << pid;
490 *res = false;
491 return true;
492 }
493 if (rc == pid) {
494 if (!(WIFSTOPPED(status))) {
495 LOG(ERROR) << "Did not get expected stopped signal for " << pid;
496 *res = false;
497 } else {
498 *res = true;
499 }
500 return true;
501 }
502 return false;
503 };
504 return WaitLoop(max_wait_micros, handler);
Andreas Gampea5bd61e2018-11-02 15:20:53 -0700505}
506
507#ifdef __LP64__
508constexpr bool kIs64Bit = true;
509#else
510constexpr bool kIs64Bit = false;
511#endif
512
513void DumpThread(pid_t pid,
514 pid_t tid,
515 const std::string* addr2line_path,
516 const char* prefix,
517 BacktraceMap* map) {
Andreas Gampe48f8a242018-11-06 15:34:38 -0800518 // Use std::cerr to avoid the LOG prefix.
519 std::cerr << std::endl << "=== pid: " << pid << " tid: " << tid << " ===" << std::endl;
520
Andreas Gampe5f04f7f2018-11-06 15:32:33 -0800521 constexpr uint32_t kMaxWaitMicros = 1000 * 1000; // 1s.
522 if (pid != tid && !WaitForSigStopped(tid, kMaxWaitMicros)) {
523 LOG(ERROR) << "Failed to wait for sigstop on " << tid;
Andreas Gampea5bd61e2018-11-02 15:20:53 -0700524 }
525
526 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, tid, map));
527 if (backtrace == nullptr) {
528 LOG(ERROR) << prefix << "(failed to create Backtrace for thread " << tid << ")";
529 return;
530 }
Elliott Hughesc1896c92018-11-29 11:33:18 -0800531 backtrace->SetSkipFrames(false);
Andreas Gampea5bd61e2018-11-02 15:20:53 -0700532 if (!backtrace->Unwind(0, nullptr)) {
533 LOG(ERROR) << prefix << "(backtrace::Unwind failed for thread " << tid
534 << ": " << backtrace->GetErrorString(backtrace->GetError()) << ")";
535 return;
536 }
537 if (backtrace->NumFrames() == 0) {
538 LOG(ERROR) << prefix << "(no native stack frames for thread " << tid << ")";
539 return;
540 }
541
542 std::unique_ptr<addr2line::Addr2linePipe> addr2line_state;
543
544 for (Backtrace::const_iterator it = backtrace->begin();
545 it != backtrace->end(); ++it) {
546 std::ostringstream oss;
547 oss << prefix << StringPrintf("#%02zu pc ", it->num);
548 bool try_addr2line = false;
549 if (!BacktraceMap::IsValid(it->map)) {
550 oss << StringPrintf(kIs64Bit ? "%016" PRIx64 " ???" : "%08" PRIx64 " ???", it->pc);
551 } else {
552 oss << StringPrintf(kIs64Bit ? "%016" PRIx64 " " : "%08" PRIx64 " ", it->rel_pc);
553 if (it->map.name.empty()) {
554 oss << StringPrintf("<anonymous:%" PRIx64 ">", it->map.start);
555 } else {
556 oss << it->map.name;
557 }
558 if (it->map.offset != 0) {
559 oss << StringPrintf(" (offset %" PRIx64 ")", it->map.offset);
560 }
561 oss << " (";
562 if (!it->func_name.empty()) {
563 oss << it->func_name;
564 if (it->func_offset != 0) {
565 oss << "+" << it->func_offset;
566 }
567 // Functions found using the gdb jit interface will be in an empty
568 // map that cannot be found using addr2line.
569 if (!it->map.name.empty()) {
570 try_addr2line = true;
571 }
572 } else {
573 oss << "???";
574 }
575 oss << ")";
576 }
Andreas Gampe48f8a242018-11-06 15:34:38 -0800577 std::cerr << oss.str() << std::endl;
Andreas Gampea5bd61e2018-11-02 15:20:53 -0700578 if (try_addr2line && addr2line_path != nullptr) {
579 addr2line::Addr2line(*addr2line_path,
580 it->map.name,
Andreas Gampe204d1a92018-11-06 15:29:20 -0800581 it->rel_pc,
Andreas Gampe48f8a242018-11-06 15:34:38 -0800582 std::cerr,
Andreas Gampea5bd61e2018-11-02 15:20:53 -0700583 prefix,
584 &addr2line_state);
585 }
586 }
587
588 if (addr2line_state != nullptr) {
Andreas Gampe48f8a242018-11-06 15:34:38 -0800589 addr2line::Drain(0, prefix, &addr2line_state, std::cerr);
Andreas Gampea5bd61e2018-11-02 15:20:53 -0700590 }
591}
592
Andreas Gampea5bd61e2018-11-02 15:20:53 -0700593void DumpProcess(pid_t forked_pid, const std::atomic<bool>& saw_wif_stopped_for_main) {
Andreas Gamped406b622018-12-13 16:01:33 -0800594 LOG(ERROR) << "Timeout for process " << forked_pid;
595
Andreas Gampea5bd61e2018-11-02 15:20:53 -0700596 CHECK_EQ(0, ::ptrace(PTRACE_ATTACH, forked_pid, 0, 0));
597 std::set<pid_t> tids = ptrace::PtraceSiblings(forked_pid);
598 tids.insert(forked_pid);
599
Andreas Gamped406b622018-12-13 16:01:33 -0800600 ptrace::DumpABI(forked_pid);
601
Andreas Gampea5bd61e2018-11-02 15:20:53 -0700602 // Check whether we have and should use addr2line.
Andreas Gampef0d4cfe2018-12-13 16:00:15 -0800603 std::unique_ptr<std::string> addr2line_path;
604 if (kUseAddr2line) {
605 addr2line_path = addr2line::FindAddr2line();
606 if (addr2line_path == nullptr) {
607 LOG(ERROR) << "Did not find usable addr2line";
608 }
Andreas Gampea5bd61e2018-11-02 15:20:53 -0700609 }
Andreas Gampea5bd61e2018-11-02 15:20:53 -0700610
611 if (!WaitForMainSigStop(saw_wif_stopped_for_main)) {
Andreas Gampe5f04f7f2018-11-06 15:32:33 -0800612 LOG(ERROR) << "Did not receive SIGSTOP for pid " << forked_pid;
Andreas Gampea5bd61e2018-11-02 15:20:53 -0700613 }
614
615 std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(forked_pid));
616 if (backtrace_map == nullptr) {
617 LOG(ERROR) << "Could not create BacktraceMap";
618 return;
619 }
620
621 for (pid_t tid : tids) {
Andreas Gampef0d4cfe2018-12-13 16:00:15 -0800622 DumpThread(forked_pid, tid, addr2line_path.get(), " ", backtrace_map.get());
Andreas Gampea5bd61e2018-11-02 15:20:53 -0700623 }
624}
625
626[[noreturn]]
627void WaitMainLoop(pid_t forked_pid, std::atomic<bool>* saw_wif_stopped_for_main) {
628 for (;;) {
629 // Consider switching to waitid to not get woken up for WIFSTOPPED.
630 int status;
631 pid_t res = TEMP_FAILURE_RETRY(waitpid(forked_pid, &status, 0));
632 if (res == -1) {
633 PLOG(FATAL) << "Failure during waitpid";
634 __builtin_unreachable();
635 }
636
637 if (WIFEXITED(status)) {
638 _exit(WEXITSTATUS(status));
639 __builtin_unreachable();
640 }
641 if (WIFSIGNALED(status)) {
642 _exit(1);
643 __builtin_unreachable();
644 }
645 if (WIFSTOPPED(status)) {
646 *saw_wif_stopped_for_main = true;
647 continue;
648 }
649 if (WIFCONTINUED(status)) {
650 continue;
651 }
652
653 LOG(FATAL) << "Unknown status " << std::hex << status;
654 }
655}
656
657[[noreturn]]
Andreas Gampe298dc0f2019-06-10 16:41:40 -0700658void SetupAndWait(pid_t forked_pid, int signal) {
Andreas Gampea5bd61e2018-11-02 15:20:53 -0700659 timeout_signal::SignalSet signals;
Andreas Gampe298dc0f2019-06-10 16:41:40 -0700660 signals.Add(signal);
Andreas Gampea5bd61e2018-11-02 15:20:53 -0700661 signals.Block();
662
663 std::atomic<bool> saw_wif_stopped_for_main(false);
664
665 std::thread signal_catcher([&]() {
666 signals.Block();
667 int sig = signals.Wait();
Andreas Gampe298dc0f2019-06-10 16:41:40 -0700668 CHECK_EQ(sig, signal);
Andreas Gampea5bd61e2018-11-02 15:20:53 -0700669
670 DumpProcess(forked_pid, saw_wif_stopped_for_main);
671
672 // Don't clean up. Just kill the child and exit.
673 kill(forked_pid, SIGKILL);
674 _exit(1);
675 });
676
677 WaitMainLoop(forked_pid, &saw_wif_stopped_for_main);
678}
679
680} // namespace
681} // namespace art
682
683int main(int argc ATTRIBUTE_UNUSED, char** argv) {
Andreas Gampe298dc0f2019-06-10 16:41:40 -0700684 int signal = SIGRTMIN + 2;
685
686 size_t index = 1u;
687 CHECK(argv[index] != nullptr);
688 if (strcmp(argv[index], "-s") == 0) {
689 index++;
690 CHECK(argv[index] != nullptr);
691 uint32_t signal_uint;
692 CHECK(android::base::ParseUint(argv[index], &signal_uint)) << "Signal not a number.";
693 signal = signal_uint;
694 index++;
695 CHECK(argv[index] != nullptr);
696 }
697
Andreas Gampea5bd61e2018-11-02 15:20:53 -0700698 pid_t orig_ppid = getpid();
699
700 pid_t pid = fork();
701 if (pid == 0) {
702 if (prctl(PR_SET_PDEATHSIG, SIGTERM) == -1) {
703 _exit(1);
704 }
705
706 if (getppid() != orig_ppid) {
707 _exit(2);
708 }
709
Andreas Gampe298dc0f2019-06-10 16:41:40 -0700710 execvp(argv[index], &argv[index]);
Andreas Gampea5bd61e2018-11-02 15:20:53 -0700711
712 _exit(3);
713 __builtin_unreachable();
714 }
715
Andreas Gampe298dc0f2019-06-10 16:41:40 -0700716 art::SetupAndWait(pid, signal);
Andreas Gampea5bd61e2018-11-02 15:20:53 -0700717 __builtin_unreachable();
718}