blob: d4c252f2db8ac658bf3f481c88249c20f7e5bde5 [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>
Mark Salyzyn99f47a92014-04-07 14:58:08 -070027#include <log/log.h>
Pavel Chupinc6c194c2013-11-21 23:17:20 +040028#include <log/logd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
Christopher Ferris20303f82014-01-10 16:33:16 -080030const int sleep_time_usec = 50000; // 0.05 seconds
31const int max_total_sleep_usec = 10000000; // 10 seconds
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032
Christopher Tateded2e5a2013-03-19 13:12:23 -070033static int write_to_am(int fd, const char* buf, int len) {
Christopher Ferris20303f82014-01-10 16:33:16 -080034 int to_write = len;
35 while (to_write > 0) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +040036 int written = TEMP_FAILURE_RETRY(write(fd, buf + len - to_write, to_write));
Christopher Ferris20303f82014-01-10 16:33:16 -080037 if (written < 0) {
38 // hard failure
39 LOG("AM write failure (%d / %s)\n", errno, strerror(errno));
40 return -1;
Christopher Tateded2e5a2013-03-19 13:12:23 -070041 }
Christopher Ferris20303f82014-01-10 16:33:16 -080042 to_write -= written;
43 }
44 return len;
Christopher Tateded2e5a2013-03-19 13:12:23 -070045}
46
Christopher Ferris20303f82014-01-10 16:33:16 -080047void _LOG(log_t* log, int scopeFlags, const char* fmt, ...) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +040048 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 Projectdd7bc332009-03-03 19:32:55 -080051
Pavel Chupinc6c194c2013-11-21 23:17:20 +040052 char buf[512];
Christopher Ferris20303f82014-01-10 16:33:16 -080053 va_list ap;
54 va_start(ap, fmt);
Pavel Chupinc6c194c2013-11-21 23:17:20 +040055 vsnprintf(buf, sizeof(buf), fmt, ap);
56 va_end(ap);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
Pavel Chupinc6c194c2013-11-21 23:17:20 +040058 size_t len = strlen(buf);
59 if (len <= 0) {
60 return;
Christopher Ferris20303f82014-01-10 16:33:16 -080061 }
62
63 if (want_tfd_write) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +040064 TEMP_FAILURE_RETRY(write(log->tfd, buf, len));
Christopher Ferris20303f82014-01-10 16:33:16 -080065 }
66
67 if (want_log_write) {
Mark Salyzyn99f47a92014-04-07 14:58:08 -070068 __android_log_buf_write(LOG_ID_CRASH, ANDROID_LOG_INFO, "DEBUG", buf);
Pavel Chupinc6c194c2013-11-21 23:17:20 +040069 if (want_amfd_write) {
Christopher Ferris20303f82014-01-10 16:33:16 -080070 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 Tateded2e5a2013-03-19 13:12:23 -070075 }
Christopher Ferris20303f82014-01-10 16:33:16 -080076 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077}
78
Jeff Brown053b8652012-06-06 16:25:03 -070079int wait_for_signal(pid_t tid, int* total_sleep_time_usec) {
Christopher Ferris20303f82014-01-10 16:33:16 -080080 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 Brown13e715b2011-10-21 12:14:56 -070096 }
Christopher Ferris20303f82014-01-10 16:33:16 -080097
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 Brown13e715b2011-10-21 12:14:56 -0700108}
109
Jeff Brown053b8652012-06-06 16:25:03 -0700110void wait_for_stop(pid_t tid, int* total_sleep_time_usec) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800111 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 Brown13e715b2011-10-21 12:14:56 -0700116 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800117
118 usleep(sleep_time_usec);
119 *total_sleep_time_usec += sleep_time_usec;
120 }
Jeff Brown13e715b2011-10-21 12:14:56 -0700121}
Kévin PETIT4bb47722013-12-18 16:44:24 +0000122
123#if defined (__mips__)
124#define DUMP_MEMORY_AS_ASCII 1
125#else
126#define DUMP_MEMORY_AS_ASCII 0
127#endif
128
129void 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}