Andreas Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 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> |
Andreas Gampe | 48f8a24 | 2018-11-06 15:34:38 -0800 | [diff] [blame] | 28 | #include <iostream> |
Andreas Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 29 | #include <thread> |
| 30 | #include <memory> |
| 31 | #include <set> |
Andreas Gampe | f0d4cfe | 2018-12-13 16:00:15 -0800 | [diff] [blame] | 32 | #include <string> |
Andreas Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 33 | |
| 34 | #include <android-base/file.h> |
| 35 | #include <android-base/logging.h> |
| 36 | #include <android-base/macros.h> |
Andreas Gampe | 298dc0f | 2019-06-10 16:41:40 -0700 | [diff] [blame^] | 37 | #include <android-base/parseint.h> |
Andreas Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 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: |
| 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 Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 88 | } // namespace timeout_signal |
| 89 | |
| 90 | namespace addr2line { |
| 91 | |
| 92 | constexpr const char* kAddr2linePath = |
David Srbecky | 87da30e | 2019-01-30 15:51:23 +0000 | [diff] [blame] | 93 | "/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8/bin/x86_64-linux-addr2line"; |
Andreas Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 94 | |
| 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 | |
Andreas Gampe | f0d4cfe | 2018-12-13 16:00:15 -0800 | [diff] [blame] | 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 | } |
Andreas Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 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 { |
| 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 | |
| 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 | |
| 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 | |
| 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 { |
Andreas Gampe | 48f8a24 | 2018-11-06 15:34:38 -0800 | [diff] [blame] | 261 | os << std::string(tmp, new_line - tmp + 1); |
Andreas Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 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 | |
| 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 | |
Andreas Gampe | 48f8a24 | 2018-11-06 15:34:38 -0800 | [diff] [blame] | 296 | const char* args[] = { |
Andreas Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 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 | |
| 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 | |
Andreas Gampe | d406b62 | 2018-12-13 16:01:33 -0800 | [diff] [blame] | 370 | void 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 Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 455 | } // namespace ptrace |
| 456 | |
Andreas Gampe | 5f04f7f | 2018-11-06 15:32:33 -0800 | [diff] [blame] | 457 | template <typename T> |
| 458 | bool 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 Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 468 | } |
Andreas Gampe | 5f04f7f | 2018-11-06 15:32:33 -0800 | [diff] [blame] | 469 | return false; |
| 470 | } |
| 471 | |
| 472 | bool 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 Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 478 | return false; |
Andreas Gampe | 5f04f7f | 2018-11-06 15:32:33 -0800 | [diff] [blame] | 479 | }; |
| 480 | constexpr uint32_t kMaxWaitMicros = 30 * 1000 * 1000; // 30s wait. |
| 481 | return WaitLoop(kMaxWaitMicros, handler); |
| 482 | } |
| 483 | |
| 484 | bool 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 Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | #ifdef __LP64__ |
| 508 | constexpr bool kIs64Bit = true; |
| 509 | #else |
| 510 | constexpr bool kIs64Bit = false; |
| 511 | #endif |
| 512 | |
| 513 | void DumpThread(pid_t pid, |
| 514 | pid_t tid, |
| 515 | const std::string* addr2line_path, |
| 516 | const char* prefix, |
| 517 | BacktraceMap* map) { |
Andreas Gampe | 48f8a24 | 2018-11-06 15:34:38 -0800 | [diff] [blame] | 518 | // Use std::cerr to avoid the LOG prefix. |
| 519 | std::cerr << std::endl << "=== pid: " << pid << " tid: " << tid << " ===" << std::endl; |
| 520 | |
Andreas Gampe | 5f04f7f | 2018-11-06 15:32:33 -0800 | [diff] [blame] | 521 | 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 Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 524 | } |
| 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 Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 531 | backtrace->SetSkipFrames(false); |
Andreas Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 532 | 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 Gampe | 48f8a24 | 2018-11-06 15:34:38 -0800 | [diff] [blame] | 577 | std::cerr << oss.str() << std::endl; |
Andreas Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 578 | if (try_addr2line && addr2line_path != nullptr) { |
| 579 | addr2line::Addr2line(*addr2line_path, |
| 580 | it->map.name, |
Andreas Gampe | 204d1a9 | 2018-11-06 15:29:20 -0800 | [diff] [blame] | 581 | it->rel_pc, |
Andreas Gampe | 48f8a24 | 2018-11-06 15:34:38 -0800 | [diff] [blame] | 582 | std::cerr, |
Andreas Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 583 | prefix, |
| 584 | &addr2line_state); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | if (addr2line_state != nullptr) { |
Andreas Gampe | 48f8a24 | 2018-11-06 15:34:38 -0800 | [diff] [blame] | 589 | addr2line::Drain(0, prefix, &addr2line_state, std::cerr); |
Andreas Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
Andreas Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 593 | void DumpProcess(pid_t forked_pid, const std::atomic<bool>& saw_wif_stopped_for_main) { |
Andreas Gampe | d406b62 | 2018-12-13 16:01:33 -0800 | [diff] [blame] | 594 | LOG(ERROR) << "Timeout for process " << forked_pid; |
| 595 | |
Andreas Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 596 | 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 Gampe | d406b62 | 2018-12-13 16:01:33 -0800 | [diff] [blame] | 600 | ptrace::DumpABI(forked_pid); |
| 601 | |
Andreas Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 602 | // Check whether we have and should use addr2line. |
Andreas Gampe | f0d4cfe | 2018-12-13 16:00:15 -0800 | [diff] [blame] | 603 | 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 Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 609 | } |
Andreas Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 610 | |
| 611 | if (!WaitForMainSigStop(saw_wif_stopped_for_main)) { |
Andreas Gampe | 5f04f7f | 2018-11-06 15:32:33 -0800 | [diff] [blame] | 612 | LOG(ERROR) << "Did not receive SIGSTOP for pid " << forked_pid; |
Andreas Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 613 | } |
| 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 Gampe | f0d4cfe | 2018-12-13 16:00:15 -0800 | [diff] [blame] | 622 | DumpThread(forked_pid, tid, addr2line_path.get(), " ", backtrace_map.get()); |
Andreas Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | |
| 626 | [[noreturn]] |
| 627 | void 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 Gampe | 298dc0f | 2019-06-10 16:41:40 -0700 | [diff] [blame^] | 658 | void SetupAndWait(pid_t forked_pid, int signal) { |
Andreas Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 659 | timeout_signal::SignalSet signals; |
Andreas Gampe | 298dc0f | 2019-06-10 16:41:40 -0700 | [diff] [blame^] | 660 | signals.Add(signal); |
Andreas Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 661 | 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 Gampe | 298dc0f | 2019-06-10 16:41:40 -0700 | [diff] [blame^] | 668 | CHECK_EQ(sig, signal); |
Andreas Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 669 | |
| 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 | |
| 683 | int main(int argc ATTRIBUTE_UNUSED, char** argv) { |
Andreas Gampe | 298dc0f | 2019-06-10 16:41:40 -0700 | [diff] [blame^] | 684 | 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 Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 698 | 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 Gampe | 298dc0f | 2019-06-10 16:41:40 -0700 | [diff] [blame^] | 710 | execvp(argv[index], &argv[index]); |
Andreas Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 711 | |
| 712 | _exit(3); |
| 713 | __builtin_unreachable(); |
| 714 | } |
| 715 | |
Andreas Gampe | 298dc0f | 2019-06-10 16:41:40 -0700 | [diff] [blame^] | 716 | art::SetupAndWait(pid, signal); |
Andreas Gampe | a5bd61e | 2018-11-02 15:20:53 -0700 | [diff] [blame] | 717 | __builtin_unreachable(); |
| 718 | } |