blob: a1633448a243ea3053553e6dac2b52366e6c9637 [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
Brigid Smith62ba4892014-06-10 11:53:08 -070017#define LOG_TAG "DEBUG"
18
Pavel Chupinc6c194c2013-11-21 23:17:20 +040019#include "utility.h"
20
Jeff Brown053b8652012-06-06 16:25:03 -070021#include <errno.h>
Jeff Brown053b8652012-06-06 16:25:03 -070022#include <signal.h>
Pavel Chupinc6c194c2013-11-21 23:17:20 +040023#include <string.h>
24#include <unistd.h>
Jeff Brown13e715b2011-10-21 12:14:56 -070025#include <sys/ptrace.h>
Jeff Brown053b8652012-06-06 16:25:03 -070026#include <sys/wait.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027
Pavel Chupinc6c194c2013-11-21 23:17:20 +040028#include <backtrace/Backtrace.h>
Mark Salyzyn99f47a92014-04-07 14:58:08 -070029#include <log/log.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030
Christopher Ferris20303f82014-01-10 16:33:16 -080031const int sleep_time_usec = 50000; // 0.05 seconds
32const int max_total_sleep_usec = 10000000; // 10 seconds
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033
Christopher Tateded2e5a2013-03-19 13:12:23 -070034static int write_to_am(int fd, const char* buf, int len) {
Christopher Ferris20303f82014-01-10 16:33:16 -080035 int to_write = len;
36 while (to_write > 0) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +040037 int written = TEMP_FAILURE_RETRY(write(fd, buf + len - to_write, to_write));
Christopher Ferris20303f82014-01-10 16:33:16 -080038 if (written < 0) {
39 // hard failure
Brigid Smith50eb5462014-06-18 14:17:57 -070040 ALOGE("AM write failure (%d / %s)\n", errno, strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -080041 return -1;
Christopher Tateded2e5a2013-03-19 13:12:23 -070042 }
Christopher Ferris20303f82014-01-10 16:33:16 -080043 to_write -= written;
44 }
45 return len;
Christopher Tateded2e5a2013-03-19 13:12:23 -070046}
47
Brigid Smith62ba4892014-06-10 11:53:08 -070048// Whitelist output desired in the logcat output.
49bool 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
59void _LOG(log_t* log, enum logtype ltype, const char* fmt, ...) {
Brigid Smith50eb5462014-06-18 14:17:57 -070060 bool write_to_tombstone = (log->tfd != -1);
61 bool write_to_logcat = is_allowed_in_logcat(ltype)
62 && (log->crashed_tid == log->current_tid);
63 bool write_to_activitymanager = (log->amfd != -1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064
Pavel Chupinc6c194c2013-11-21 23:17:20 +040065 char buf[512];
Christopher Ferris20303f82014-01-10 16:33:16 -080066 va_list ap;
67 va_start(ap, fmt);
Pavel Chupinc6c194c2013-11-21 23:17:20 +040068 vsnprintf(buf, sizeof(buf), fmt, ap);
69 va_end(ap);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070
Pavel Chupinc6c194c2013-11-21 23:17:20 +040071 size_t len = strlen(buf);
72 if (len <= 0) {
73 return;
Christopher Ferris20303f82014-01-10 16:33:16 -080074 }
75
Brigid Smith62ba4892014-06-10 11:53:08 -070076 if (write_to_tombstone) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +040077 TEMP_FAILURE_RETRY(write(log->tfd, buf, len));
Christopher Ferris20303f82014-01-10 16:33:16 -080078 }
79
Brigid Smith62ba4892014-06-10 11:53:08 -070080 if (write_to_logcat) {
81 __android_log_buf_write(LOG_ID_CRASH, ANDROID_LOG_INFO, LOG_TAG, buf);
82 if (write_to_activitymanager) {
Christopher Ferris20303f82014-01-10 16:33:16 -080083 int written = write_to_am(log->amfd, buf, len);
84 if (written <= 0) {
85 // timeout or other failure on write; stop informing the activity manager
86 log->amfd = -1;
87 }
Christopher Tateded2e5a2013-03-19 13:12:23 -070088 }
Christopher Ferris20303f82014-01-10 16:33:16 -080089 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090}
91
Jeff Brown053b8652012-06-06 16:25:03 -070092int wait_for_signal(pid_t tid, int* total_sleep_time_usec) {
Christopher Ferris20303f82014-01-10 16:33:16 -080093 for (;;) {
94 int status;
95 pid_t n = waitpid(tid, &status, __WALL | WNOHANG);
96 if (n < 0) {
97 if (errno == EAGAIN)
98 continue;
Brigid Smith50eb5462014-06-18 14:17:57 -070099 ALOGE("waitpid failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800100 return -1;
101 } else if (n > 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700102 ALOGV("waitpid: n=%d status=%08x\n", n, status);
Christopher Ferris20303f82014-01-10 16:33:16 -0800103 if (WIFSTOPPED(status)) {
104 return WSTOPSIG(status);
105 } else {
Brigid Smith50eb5462014-06-18 14:17:57 -0700106 ALOGE("unexpected waitpid response: n=%d, status=%08x\n", n, status);
Christopher Ferris20303f82014-01-10 16:33:16 -0800107 return -1;
108 }
Jeff Brown13e715b2011-10-21 12:14:56 -0700109 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800110
111 if (*total_sleep_time_usec > max_total_sleep_usec) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700112 ALOGE("timed out waiting for tid=%d to die\n", tid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800113 return -1;
114 }
115
116 // not ready yet
Brigid Smith50eb5462014-06-18 14:17:57 -0700117 ALOGV("not ready yet\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800118 usleep(sleep_time_usec);
119 *total_sleep_time_usec += sleep_time_usec;
120 }
Jeff Brown13e715b2011-10-21 12:14:56 -0700121}
122
Jeff Brown053b8652012-06-06 16:25:03 -0700123void wait_for_stop(pid_t tid, int* total_sleep_time_usec) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800124 siginfo_t si;
125 while (TEMP_FAILURE_RETRY(ptrace(PTRACE_GETSIGINFO, tid, 0, &si)) < 0 && errno == ESRCH) {
126 if (*total_sleep_time_usec > max_total_sleep_usec) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700127 ALOGE("timed out waiting for tid=%d to stop\n", tid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800128 break;
Jeff Brown13e715b2011-10-21 12:14:56 -0700129 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800130
131 usleep(sleep_time_usec);
132 *total_sleep_time_usec += sleep_time_usec;
133 }
Jeff Brown13e715b2011-10-21 12:14:56 -0700134}
Kévin PETIT4bb47722013-12-18 16:44:24 +0000135
136#if defined (__mips__)
137#define DUMP_MEMORY_AS_ASCII 1
138#else
139#define DUMP_MEMORY_AS_ASCII 0
140#endif
141
Brigid Smith62ba4892014-06-10 11:53:08 -0700142void dump_memory(log_t* log, pid_t tid, uintptr_t addr) {
Kévin PETIT4bb47722013-12-18 16:44:24 +0000143 char code_buffer[64];
144 char ascii_buffer[32];
145 uintptr_t p, end;
146
147 p = addr & ~(sizeof(long) - 1);
148 /* Dump 32 bytes before addr */
149 p -= 32;
150 if (p > addr) {
151 /* catch underflow */
152 p = 0;
153 }
154 /* Dump 256 bytes */
155 end = p + 256;
156 /* catch overflow; 'end - p' has to be multiples of 16 */
157 while (end < p) {
158 end -= 16;
159 }
160
161 /* Dump the code around PC as:
162 * addr contents ascii
163 * 0000000000008d34 ef000000e8bd0090 e1b00000512fff1e ............../Q
164 * 0000000000008d44 ea00b1f9e92d0090 e3a070fcef000000 ......-..p......
165 * On 32-bit machines, there are still 16 bytes per line but addresses and
166 * words are of course presented differently.
167 */
168 while (p < end) {
169 char* asc_out = ascii_buffer;
170
171 int len = snprintf(code_buffer, sizeof(code_buffer), "%" PRIPTR " ", p);
172
173 for (size_t i = 0; i < 16/sizeof(long); i++) {
174 long data = ptrace(PTRACE_PEEKTEXT, tid, (void*)p, NULL);
175 if (data == -1 && errno != 0) {
176 // ptrace failed, probably because we're dumping memory in an
177 // unmapped or inaccessible page.
178#ifdef __LP64__
179 len += sprintf(code_buffer + len, "---------------- ");
180#else
181 len += sprintf(code_buffer + len, "-------- ");
182#endif
183 } else {
184 len += sprintf(code_buffer + len, "%" PRIPTR " ",
185 static_cast<uintptr_t>(data));
186 }
187
188#if DUMP_MEMORY_AS_ASCII
189 for (size_t j = 0; j < sizeof(long); j++) {
190 /*
191 * Our isprint() allows high-ASCII characters that display
192 * differently (often badly) in different viewers, so we
193 * just use a simpler test.
194 */
195 char val = (data >> (j*8)) & 0xff;
196 if (val >= 0x20 && val < 0x7f) {
197 *asc_out++ = val;
198 } else {
199 *asc_out++ = '.';
200 }
201 }
202#endif
203 p += sizeof(long);
204 }
205 *asc_out = '\0';
Brigid Smith62ba4892014-06-10 11:53:08 -0700206 _LOG(log, logtype::MEMORY, " %s %s\n", code_buffer, ascii_buffer);
Kévin PETIT4bb47722013-12-18 16:44:24 +0000207 }
208}