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 | |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 17 | #include "utility.h" |
| 18 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 19 | #include <errno.h> |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 20 | #include <signal.h> |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 21 | #include <string.h> |
| 22 | #include <unistd.h> |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 23 | #include <sys/ptrace.h> |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 24 | #include <sys/wait.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 | |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 26 | #include <backtrace/Backtrace.h> |
| 27 | #include <log/logd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 29 | const int sleep_time_usec = 50000; // 0.05 seconds |
| 30 | const int max_total_sleep_usec = 10000000; // 10 seconds |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 31 | |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 32 | static int write_to_am(int fd, const char* buf, int len) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 33 | int to_write = len; |
| 34 | while (to_write > 0) { |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 35 | int written = TEMP_FAILURE_RETRY(write(fd, buf + len - to_write, to_write)); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 36 | if (written < 0) { |
| 37 | // hard failure |
| 38 | LOG("AM write failure (%d / %s)\n", errno, strerror(errno)); |
| 39 | return -1; |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 40 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 41 | to_write -= written; |
| 42 | } |
| 43 | return len; |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 46 | void _LOG(log_t* log, int scopeFlags, const char* fmt, ...) { |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 47 | bool want_tfd_write = log && log->tfd >= 0; |
| 48 | bool want_log_write = IS_AT_FAULT(scopeFlags) && (!log || !log->quiet); |
| 49 | bool want_amfd_write = IS_AT_FAULT(scopeFlags) && !IS_SENSITIVE(scopeFlags) && log && log->amfd >= 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 50 | |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 51 | char buf[512]; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 52 | va_list ap; |
| 53 | va_start(ap, fmt); |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 54 | vsnprintf(buf, sizeof(buf), fmt, ap); |
| 55 | va_end(ap); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 56 | |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 57 | size_t len = strlen(buf); |
| 58 | if (len <= 0) { |
| 59 | return; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | if (want_tfd_write) { |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 63 | TEMP_FAILURE_RETRY(write(log->tfd, buf, len)); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | if (want_log_write) { |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 67 | __android_log_write(ANDROID_LOG_INFO, "DEBUG", buf); |
| 68 | if (want_amfd_write) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 69 | int written = write_to_am(log->amfd, buf, len); |
| 70 | if (written <= 0) { |
| 71 | // timeout or other failure on write; stop informing the activity manager |
| 72 | log->amfd = -1; |
| 73 | } |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 74 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 75 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 78 | int wait_for_signal(pid_t tid, int* total_sleep_time_usec) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 79 | for (;;) { |
| 80 | int status; |
| 81 | pid_t n = waitpid(tid, &status, __WALL | WNOHANG); |
| 82 | if (n < 0) { |
| 83 | if (errno == EAGAIN) |
| 84 | continue; |
| 85 | LOG("waitpid failed: %s\n", strerror(errno)); |
| 86 | return -1; |
| 87 | } else if (n > 0) { |
| 88 | XLOG("waitpid: n=%d status=%08x\n", n, status); |
| 89 | if (WIFSTOPPED(status)) { |
| 90 | return WSTOPSIG(status); |
| 91 | } else { |
| 92 | LOG("unexpected waitpid response: n=%d, status=%08x\n", n, status); |
| 93 | return -1; |
| 94 | } |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 95 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 96 | |
| 97 | if (*total_sleep_time_usec > max_total_sleep_usec) { |
| 98 | LOG("timed out waiting for tid=%d to die\n", tid); |
| 99 | return -1; |
| 100 | } |
| 101 | |
| 102 | // not ready yet |
| 103 | XLOG("not ready yet\n"); |
| 104 | usleep(sleep_time_usec); |
| 105 | *total_sleep_time_usec += sleep_time_usec; |
| 106 | } |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 109 | void wait_for_stop(pid_t tid, int* total_sleep_time_usec) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 110 | siginfo_t si; |
| 111 | while (TEMP_FAILURE_RETRY(ptrace(PTRACE_GETSIGINFO, tid, 0, &si)) < 0 && errno == ESRCH) { |
| 112 | if (*total_sleep_time_usec > max_total_sleep_usec) { |
| 113 | LOG("timed out waiting for tid=%d to stop\n", tid); |
| 114 | break; |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 115 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 116 | |
| 117 | usleep(sleep_time_usec); |
| 118 | *total_sleep_time_usec += sleep_time_usec; |
| 119 | } |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 120 | } |
Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 121 | |
| 122 | #if defined (__mips__) |
| 123 | #define DUMP_MEMORY_AS_ASCII 1 |
| 124 | #else |
| 125 | #define DUMP_MEMORY_AS_ASCII 0 |
| 126 | #endif |
| 127 | |
| 128 | void dump_memory(log_t* log, pid_t tid, uintptr_t addr, int scope_flags) { |
| 129 | char code_buffer[64]; |
| 130 | char ascii_buffer[32]; |
| 131 | uintptr_t p, end; |
| 132 | |
| 133 | p = addr & ~(sizeof(long) - 1); |
| 134 | /* Dump 32 bytes before addr */ |
| 135 | p -= 32; |
| 136 | if (p > addr) { |
| 137 | /* catch underflow */ |
| 138 | p = 0; |
| 139 | } |
| 140 | /* Dump 256 bytes */ |
| 141 | end = p + 256; |
| 142 | /* catch overflow; 'end - p' has to be multiples of 16 */ |
| 143 | while (end < p) { |
| 144 | end -= 16; |
| 145 | } |
| 146 | |
| 147 | /* Dump the code around PC as: |
| 148 | * addr contents ascii |
| 149 | * 0000000000008d34 ef000000e8bd0090 e1b00000512fff1e ............../Q |
| 150 | * 0000000000008d44 ea00b1f9e92d0090 e3a070fcef000000 ......-..p...... |
| 151 | * On 32-bit machines, there are still 16 bytes per line but addresses and |
| 152 | * words are of course presented differently. |
| 153 | */ |
| 154 | while (p < end) { |
| 155 | char* asc_out = ascii_buffer; |
| 156 | |
| 157 | int len = snprintf(code_buffer, sizeof(code_buffer), "%" PRIPTR " ", p); |
| 158 | |
| 159 | for (size_t i = 0; i < 16/sizeof(long); i++) { |
| 160 | long data = ptrace(PTRACE_PEEKTEXT, tid, (void*)p, NULL); |
| 161 | if (data == -1 && errno != 0) { |
| 162 | // ptrace failed, probably because we're dumping memory in an |
| 163 | // unmapped or inaccessible page. |
| 164 | #ifdef __LP64__ |
| 165 | len += sprintf(code_buffer + len, "---------------- "); |
| 166 | #else |
| 167 | len += sprintf(code_buffer + len, "-------- "); |
| 168 | #endif |
| 169 | } else { |
| 170 | len += sprintf(code_buffer + len, "%" PRIPTR " ", |
| 171 | static_cast<uintptr_t>(data)); |
| 172 | } |
| 173 | |
| 174 | #if DUMP_MEMORY_AS_ASCII |
| 175 | for (size_t j = 0; j < sizeof(long); j++) { |
| 176 | /* |
| 177 | * Our isprint() allows high-ASCII characters that display |
| 178 | * differently (often badly) in different viewers, so we |
| 179 | * just use a simpler test. |
| 180 | */ |
| 181 | char val = (data >> (j*8)) & 0xff; |
| 182 | if (val >= 0x20 && val < 0x7f) { |
| 183 | *asc_out++ = val; |
| 184 | } else { |
| 185 | *asc_out++ = '.'; |
| 186 | } |
| 187 | } |
| 188 | #endif |
| 189 | p += sizeof(long); |
| 190 | } |
| 191 | *asc_out = '\0'; |
| 192 | _LOG(log, scope_flags, " %s %s\n", code_buffer, ascii_buffer); |
| 193 | } |
| 194 | } |