blob: 9b2091450a34d8c310694af8b34e67e668beaebf [file] [log] [blame]
Christopher Ferris20303f82014-01-10 16:33:16 -08001/*
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 Projectdd7bc332009-03-03 19:32:55 -080016
Pavel Chupinc6c194c2013-11-21 23:17:20 +040017#include "utility.h"
18
Jeff Brown053b8652012-06-06 16:25:03 -070019#include <errno.h>
Jeff Brown053b8652012-06-06 16:25:03 -070020#include <signal.h>
Pavel Chupinc6c194c2013-11-21 23:17:20 +040021#include <string.h>
22#include <unistd.h>
Jeff Brown13e715b2011-10-21 12:14:56 -070023#include <sys/ptrace.h>
Jeff Brown053b8652012-06-06 16:25:03 -070024#include <sys/wait.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025
Pavel Chupinc6c194c2013-11-21 23:17:20 +040026#include <backtrace/Backtrace.h>
27#include <log/logd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028
Christopher Ferris20303f82014-01-10 16:33:16 -080029const int sleep_time_usec = 50000; // 0.05 seconds
30const int max_total_sleep_usec = 10000000; // 10 seconds
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031
Christopher Tateded2e5a2013-03-19 13:12:23 -070032static int write_to_am(int fd, const char* buf, int len) {
Christopher Ferris20303f82014-01-10 16:33:16 -080033 int to_write = len;
34 while (to_write > 0) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +040035 int written = TEMP_FAILURE_RETRY(write(fd, buf + len - to_write, to_write));
Christopher Ferris20303f82014-01-10 16:33:16 -080036 if (written < 0) {
37 // hard failure
38 LOG("AM write failure (%d / %s)\n", errno, strerror(errno));
39 return -1;
Christopher Tateded2e5a2013-03-19 13:12:23 -070040 }
Christopher Ferris20303f82014-01-10 16:33:16 -080041 to_write -= written;
42 }
43 return len;
Christopher Tateded2e5a2013-03-19 13:12:23 -070044}
45
Christopher Ferris20303f82014-01-10 16:33:16 -080046void _LOG(log_t* log, int scopeFlags, const char* fmt, ...) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +040047 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 Projectdd7bc332009-03-03 19:32:55 -080050
Pavel Chupinc6c194c2013-11-21 23:17:20 +040051 char buf[512];
Christopher Ferris20303f82014-01-10 16:33:16 -080052 va_list ap;
53 va_start(ap, fmt);
Pavel Chupinc6c194c2013-11-21 23:17:20 +040054 vsnprintf(buf, sizeof(buf), fmt, ap);
55 va_end(ap);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056
Pavel Chupinc6c194c2013-11-21 23:17:20 +040057 size_t len = strlen(buf);
58 if (len <= 0) {
59 return;
Christopher Ferris20303f82014-01-10 16:33:16 -080060 }
61
62 if (want_tfd_write) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +040063 TEMP_FAILURE_RETRY(write(log->tfd, buf, len));
Christopher Ferris20303f82014-01-10 16:33:16 -080064 }
65
66 if (want_log_write) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +040067 __android_log_write(ANDROID_LOG_INFO, "DEBUG", buf);
68 if (want_amfd_write) {
Christopher Ferris20303f82014-01-10 16:33:16 -080069 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 Tateded2e5a2013-03-19 13:12:23 -070074 }
Christopher Ferris20303f82014-01-10 16:33:16 -080075 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076}
77
Jeff Brown053b8652012-06-06 16:25:03 -070078int wait_for_signal(pid_t tid, int* total_sleep_time_usec) {
Christopher Ferris20303f82014-01-10 16:33:16 -080079 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 Brown13e715b2011-10-21 12:14:56 -070095 }
Christopher Ferris20303f82014-01-10 16:33:16 -080096
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 Brown13e715b2011-10-21 12:14:56 -0700107}
108
Jeff Brown053b8652012-06-06 16:25:03 -0700109void wait_for_stop(pid_t tid, int* total_sleep_time_usec) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800110 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 Brown13e715b2011-10-21 12:14:56 -0700115 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800116
117 usleep(sleep_time_usec);
118 *total_sleep_time_usec += sleep_time_usec;
119 }
Jeff Brown13e715b2011-10-21 12:14:56 -0700120}
Kévin PETIT4bb47722013-12-18 16:44:24 +0000121
122#if defined (__mips__)
123#define DUMP_MEMORY_AS_ASCII 1
124#else
125#define DUMP_MEMORY_AS_ASCII 0
126#endif
127
128void 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}