Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008, 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 | */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 16 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 17 | #include <stddef.h> |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 18 | #include <stdio.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 19 | #include <string.h> |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | #include <unistd.h> |
| 22 | #include <signal.h> |
Colin Cross | 9227bd3 | 2013-07-23 16:59:20 -0700 | [diff] [blame] | 23 | #include <log/logd.h> |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 24 | #include <sys/ptrace.h> |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 25 | #include <sys/wait.h> |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 26 | #include <arpa/inet.h> |
| 27 | #include <assert.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | |
| 29 | #include "utility.h" |
| 30 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 31 | const int sleep_time_usec = 50000; // 0.05 seconds |
| 32 | const int max_total_sleep_usec = 10000000; // 10 seconds |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 33 | |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 34 | static int write_to_am(int fd, const char* buf, int len) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 35 | int to_write = len; |
| 36 | while (to_write > 0) { |
| 37 | int written = TEMP_FAILURE_RETRY( write(fd, buf + len - to_write, to_write) ); |
| 38 | if (written < 0) { |
| 39 | // hard failure |
| 40 | LOG("AM write failure (%d / %s)\n", errno, strerror(errno)); |
| 41 | return -1; |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 42 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 43 | to_write -= written; |
| 44 | } |
| 45 | return len; |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 48 | void _LOG(log_t* log, int scopeFlags, const char* fmt, ...) { |
| 49 | char buf[512]; |
| 50 | bool want_tfd_write; |
| 51 | bool want_log_write; |
| 52 | bool want_amfd_write; |
| 53 | int len = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 54 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 55 | va_list ap; |
| 56 | va_start(ap, fmt); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 57 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 58 | // where is the information going to go? |
| 59 | want_tfd_write = log && log->tfd >= 0; |
| 60 | want_log_write = IS_AT_FAULT(scopeFlags) && (!log || !log->quiet); |
| 61 | want_amfd_write = IS_AT_FAULT(scopeFlags) && !IS_SENSITIVE(scopeFlags) && log && log->amfd >= 0; |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 62 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 63 | // if we're going to need the literal string, generate it once here |
| 64 | if (want_tfd_write || want_amfd_write) { |
| 65 | vsnprintf(buf, sizeof(buf), fmt, ap); |
| 66 | len = strlen(buf); |
| 67 | } |
| 68 | |
| 69 | if (want_tfd_write) { |
| 70 | write(log->tfd, buf, len); |
| 71 | } |
| 72 | |
| 73 | if (want_log_write) { |
| 74 | // whatever goes to logcat also goes to the Activity Manager |
| 75 | __android_log_vprint(ANDROID_LOG_INFO, "DEBUG", fmt, ap); |
| 76 | if (want_amfd_write && len > 0) { |
| 77 | int written = write_to_am(log->amfd, buf, len); |
| 78 | if (written <= 0) { |
| 79 | // timeout or other failure on write; stop informing the activity manager |
| 80 | log->amfd = -1; |
| 81 | } |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 82 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 83 | } |
| 84 | va_end(ap); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 87 | int wait_for_signal(pid_t tid, int* total_sleep_time_usec) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 88 | for (;;) { |
| 89 | int status; |
| 90 | pid_t n = waitpid(tid, &status, __WALL | WNOHANG); |
| 91 | if (n < 0) { |
| 92 | if (errno == EAGAIN) |
| 93 | continue; |
| 94 | LOG("waitpid failed: %s\n", strerror(errno)); |
| 95 | return -1; |
| 96 | } else if (n > 0) { |
| 97 | XLOG("waitpid: n=%d status=%08x\n", n, status); |
| 98 | if (WIFSTOPPED(status)) { |
| 99 | return WSTOPSIG(status); |
| 100 | } else { |
| 101 | LOG("unexpected waitpid response: n=%d, status=%08x\n", n, status); |
| 102 | return -1; |
| 103 | } |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 104 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 105 | |
| 106 | if (*total_sleep_time_usec > max_total_sleep_usec) { |
| 107 | LOG("timed out waiting for tid=%d to die\n", tid); |
| 108 | return -1; |
| 109 | } |
| 110 | |
| 111 | // not ready yet |
| 112 | XLOG("not ready yet\n"); |
| 113 | usleep(sleep_time_usec); |
| 114 | *total_sleep_time_usec += sleep_time_usec; |
| 115 | } |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 118 | void wait_for_stop(pid_t tid, int* total_sleep_time_usec) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 119 | siginfo_t si; |
| 120 | while (TEMP_FAILURE_RETRY(ptrace(PTRACE_GETSIGINFO, tid, 0, &si)) < 0 && errno == ESRCH) { |
| 121 | if (*total_sleep_time_usec > max_total_sleep_usec) { |
| 122 | LOG("timed out waiting for tid=%d to stop\n", tid); |
| 123 | break; |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 124 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 125 | |
| 126 | usleep(sleep_time_usec); |
| 127 | *total_sleep_time_usec += sleep_time_usec; |
| 128 | } |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 129 | } |