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