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 */
16
17 #define LOG_TAG "DEBUG"
18
19 #include "utility.h"
20
21 #include <errno.h>
22 #include <signal.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/ptrace.h>
26 #include <sys/wait.h>
27
28 #include <backtrace/Backtrace.h>
29 #include <log/log.h>
30
31 const int sleep_time_usec = 50000; // 0.05 seconds
32 const int max_total_sleep_usec = 10000000; // 10 seconds
33
write_to_am(int fd,const char * buf,int len)34 static int write_to_am(int fd, const char* buf, int len) {
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 ALOGE("AM write failure (%d / %s)\n", errno, strerror(errno));
41 return -1;
42 }
43 to_write -= written;
44 }
45 return len;
46 }
47
48 // Whitelist output desired in the logcat output.
is_allowed_in_logcat(enum logtype ltype)49 bool is_allowed_in_logcat(enum logtype ltype) {
50 if ((ltype == ERROR)
51 || (ltype == HEADER)
52 || (ltype == REGISTERS)
53 || (ltype == BACKTRACE)) {
54 return true;
55 }
56 return false;
57 }
58
_LOG(log_t * log,enum logtype ltype,const char * fmt,...)59 void _LOG(log_t* log, enum logtype ltype, const char* fmt, ...) {
60 bool write_to_tombstone = (log->tfd != -1);
61 bool write_to_logcat = is_allowed_in_logcat(ltype)
62 && log->crashed_tid != -1
63 && log->current_tid != -1
64 && (log->crashed_tid == log->current_tid);
65 bool write_to_activitymanager = (log->amfd != -1);
66
67 char buf[512];
68 va_list ap;
69 va_start(ap, fmt);
70 vsnprintf(buf, sizeof(buf), fmt, ap);
71 va_end(ap);
72
73 size_t len = strlen(buf);
74 if (len <= 0) {
75 return;
76 }
77
78 if (write_to_tombstone) {
79 TEMP_FAILURE_RETRY(write(log->tfd, buf, len));
80 }
81
82 if (write_to_logcat) {
83 __android_log_buf_write(LOG_ID_CRASH, ANDROID_LOG_INFO, LOG_TAG, buf);
84 if (write_to_activitymanager) {
85 int written = write_to_am(log->amfd, buf, len);
86 if (written <= 0) {
87 // timeout or other failure on write; stop informing the activity manager
88 log->amfd = -1;
89 }
90 }
91 }
92 }
93
wait_for_signal(pid_t tid,int * total_sleep_time_usec)94 int wait_for_signal(pid_t tid, int* total_sleep_time_usec) {
95 for (;;) {
96 int status;
97 pid_t n = waitpid(tid, &status, __WALL | WNOHANG);
98 if (n < 0) {
99 if (errno == EAGAIN)
100 continue;
101 ALOGE("waitpid failed: %s\n", strerror(errno));
102 return -1;
103 } else if (n > 0) {
104 ALOGV("waitpid: n=%d status=%08x\n", n, status);
105 if (WIFSTOPPED(status)) {
106 return WSTOPSIG(status);
107 } else {
108 ALOGE("unexpected waitpid response: n=%d, status=%08x\n", n, status);
109 return -1;
110 }
111 }
112
113 if (*total_sleep_time_usec > max_total_sleep_usec) {
114 ALOGE("timed out waiting for tid=%d to die\n", tid);
115 return -1;
116 }
117
118 // not ready yet
119 ALOGV("not ready yet\n");
120 usleep(sleep_time_usec);
121 *total_sleep_time_usec += sleep_time_usec;
122 }
123 }
124
wait_for_stop(pid_t tid,int * total_sleep_time_usec)125 void wait_for_stop(pid_t tid, int* total_sleep_time_usec) {
126 siginfo_t si;
127 while (TEMP_FAILURE_RETRY(ptrace(PTRACE_GETSIGINFO, tid, 0, &si)) < 0 && errno == ESRCH) {
128 if (*total_sleep_time_usec > max_total_sleep_usec) {
129 ALOGE("timed out waiting for tid=%d to stop\n", tid);
130 break;
131 }
132
133 usleep(sleep_time_usec);
134 *total_sleep_time_usec += sleep_time_usec;
135 }
136 }
137
138 #if defined (__mips__)
139 #define DUMP_MEMORY_AS_ASCII 1
140 #else
141 #define DUMP_MEMORY_AS_ASCII 0
142 #endif
143
dump_memory(log_t * log,pid_t tid,uintptr_t addr)144 void dump_memory(log_t* log, pid_t tid, uintptr_t addr) {
145 char code_buffer[64];
146 char ascii_buffer[32];
147 uintptr_t p, end;
148
149 p = addr & ~(sizeof(long) - 1);
150 /* Dump 32 bytes before addr */
151 p -= 32;
152 if (p > addr) {
153 /* catch underflow */
154 p = 0;
155 }
156 /* Dump 256 bytes */
157 end = p + 256;
158 /* catch overflow; 'end - p' has to be multiples of 16 */
159 while (end < p) {
160 end -= 16;
161 }
162
163 /* Dump the code around PC as:
164 * addr contents ascii
165 * 0000000000008d34 ef000000e8bd0090 e1b00000512fff1e ............../Q
166 * 0000000000008d44 ea00b1f9e92d0090 e3a070fcef000000 ......-..p......
167 * On 32-bit machines, there are still 16 bytes per line but addresses and
168 * words are of course presented differently.
169 */
170 while (p < end) {
171 char* asc_out = ascii_buffer;
172
173 int len = snprintf(code_buffer, sizeof(code_buffer), "%" PRIPTR " ", p);
174
175 for (size_t i = 0; i < 16/sizeof(long); i++) {
176 long data = ptrace(PTRACE_PEEKTEXT, tid, (void*)p, NULL);
177 if (data == -1 && errno != 0) {
178 // ptrace failed, probably because we're dumping memory in an
179 // unmapped or inaccessible page.
180 #ifdef __LP64__
181 len += sprintf(code_buffer + len, "---------------- ");
182 #else
183 len += sprintf(code_buffer + len, "-------- ");
184 #endif
185 } else {
186 len += sprintf(code_buffer + len, "%" PRIPTR " ",
187 static_cast<uintptr_t>(data));
188 }
189
190 #if DUMP_MEMORY_AS_ASCII
191 for (size_t j = 0; j < sizeof(long); j++) {
192 /*
193 * Our isprint() allows high-ASCII characters that display
194 * differently (often badly) in different viewers, so we
195 * just use a simpler test.
196 */
197 char val = (data >> (j*8)) & 0xff;
198 if (val >= 0x20 && val < 0x7f) {
199 *asc_out++ = val;
200 } else {
201 *asc_out++ = '.';
202 }
203 }
204 #endif
205 p += sizeof(long);
206 }
207 *asc_out = '\0';
208 _LOG(log, logtype::MEMORY, " %s %s\n", code_buffer, ascii_buffer);
209 }
210 }
211