blob: 57209aaefeffa4165d375aaadec42a75cc92cfd6 [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>
Jeff Brown13e715b2011-10-21 12:14:56 -070024#include <sys/ptrace.h>
Jeff Brown053b8652012-06-06 16:25:03 -070025#include <sys/wait.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070026#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027
Christopher Ferris99235e92016-05-03 16:32:13 -070028#include <string>
29
Elliott Hughes4f713192015-12-04 22:00:26 -080030#include <android-base/stringprintf.h>
Pavel Chupinc6c194c2013-11-21 23:17:20 +040031#include <backtrace/Backtrace.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070032#include <log/log.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033
Brigid Smith62ba4892014-06-10 11:53:08 -070034// Whitelist output desired in the logcat output.
35bool is_allowed_in_logcat(enum logtype ltype) {
Christopher Ferrisb36b5922015-06-17 18:35:59 -070036 if ((ltype == HEADER)
Brigid Smith62ba4892014-06-10 11:53:08 -070037 || (ltype == REGISTERS)
38 || (ltype == BACKTRACE)) {
39 return true;
40 }
41 return false;
42}
43
44void _LOG(log_t* log, enum logtype ltype, const char* fmt, ...) {
Brigid Smith50eb5462014-06-18 14:17:57 -070045 bool write_to_tombstone = (log->tfd != -1);
46 bool write_to_logcat = is_allowed_in_logcat(ltype)
Brigid Smithc75a02f2014-07-17 14:52:33 -070047 && log->crashed_tid != -1
48 && log->current_tid != -1
Brigid Smith50eb5462014-06-18 14:17:57 -070049 && (log->crashed_tid == log->current_tid);
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
Brigid Smith62ba4892014-06-10 11:53:08 -070062 if (write_to_tombstone) {
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
Brigid Smith62ba4892014-06-10 11:53:08 -070066 if (write_to_logcat) {
Christopher Ferrisb0412a52015-05-05 12:23:06 -070067 __android_log_buf_write(LOG_ID_CRASH, ANDROID_LOG_FATAL, LOG_TAG, buf);
Christopher Ferris99235e92016-05-03 16:32:13 -070068 if (log->amfd_data != nullptr) {
69 *log->amfd_data += buf;
Christopher Tateded2e5a2013-03-19 13:12:23 -070070 }
Christopher Ferris20303f82014-01-10 16:33:16 -080071 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072}
73
Josh Gaocbe70cb2016-10-18 18:17:52 -070074bool wait_for_signal(pid_t tid, siginfo_t* siginfo) {
Josh Gao7c89f9e2016-01-13 17:57:14 -080075 while (true) {
Christopher Ferris20303f82014-01-10 16:33:16 -080076 int status;
Josh Gaof5a960a2016-08-10 17:57:01 -070077 pid_t n = TEMP_FAILURE_RETRY(waitpid(tid, &status, __WALL));
Christopher Ferris1072f912014-10-31 21:34:38 -070078 if (n == -1) {
79 ALOGE("waitpid failed: tid %d, %s", tid, strerror(errno));
Josh Gaocbe70cb2016-10-18 18:17:52 -070080 return false;
Christopher Ferris1072f912014-10-31 21:34:38 -070081 } else if (n == tid) {
Christopher Ferris20303f82014-01-10 16:33:16 -080082 if (WIFSTOPPED(status)) {
Josh Gaocbe70cb2016-10-18 18:17:52 -070083 if (ptrace(PTRACE_GETSIGINFO, tid, nullptr, siginfo) != 0) {
84 ALOGE("PTRACE_GETSIGINFO failed: %s", strerror(errno));
85 return false;
86 }
87 return true;
Christopher Ferris20303f82014-01-10 16:33:16 -080088 } else {
Brigid Smith50eb5462014-06-18 14:17:57 -070089 ALOGE("unexpected waitpid response: n=%d, status=%08x\n", n, status);
Christopher Ferris1072f912014-10-31 21:34:38 -070090 // This is the only circumstance under which we can allow a detach
91 // to fail with ESRCH, which indicates the tid has exited.
Josh Gaocbe70cb2016-10-18 18:17:52 -070092 return false;
Christopher Ferris20303f82014-01-10 16:33:16 -080093 }
Jeff Brown13e715b2011-10-21 12:14:56 -070094 }
Christopher Ferris20303f82014-01-10 16:33:16 -080095 }
Jeff Brown13e715b2011-10-21 12:14:56 -070096}
Kévin PETIT4bb47722013-12-18 16:44:24 +000097
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070098#define MEMORY_BYTES_TO_DUMP 256
99#define MEMORY_BYTES_PER_LINE 16
Kévin PETIT4bb47722013-12-18 16:44:24 +0000100
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700101void dump_memory(log_t* log, Backtrace* backtrace, uintptr_t addr, const char* fmt, ...) {
102 std::string log_msg;
103 va_list ap;
104 va_start(ap, fmt);
105 android::base::StringAppendV(&log_msg, fmt, ap);
106 va_end(ap);
Kévin PETIT4bb47722013-12-18 16:44:24 +0000107
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700108 // Align the address to sizeof(long) and start 32 bytes before the address.
109 addr &= ~(sizeof(long) - 1);
110 if (addr >= 4128) {
111 addr -= 32;
112 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000113
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700114 // Don't bother if the address looks too low, or looks too high.
115 if (addr < 4096 ||
116#if defined(__LP64__)
117 addr > 0x4000000000000000UL - MEMORY_BYTES_TO_DUMP) {
Kévin PETIT4bb47722013-12-18 16:44:24 +0000118#else
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700119 addr > 0xffff0000 - MEMORY_BYTES_TO_DUMP) {
Kévin PETIT4bb47722013-12-18 16:44:24 +0000120#endif
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700121 return;
122 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000123
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700124 _LOG(log, logtype::MEMORY, "\n%s\n", log_msg.c_str());
125
126 // Dump 256 bytes
127 uintptr_t data[MEMORY_BYTES_TO_DUMP/sizeof(uintptr_t)];
128 memset(data, 0, MEMORY_BYTES_TO_DUMP);
129 size_t bytes = backtrace->Read(addr, reinterpret_cast<uint8_t*>(data), sizeof(data));
130 if (bytes % sizeof(uintptr_t) != 0) {
131 // This should never happen, but just in case.
132 ALOGE("Bytes read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t));
133 bytes &= ~(sizeof(uintptr_t) - 1);
134 }
135
Christopher Ferris456abba2015-07-09 15:35:47 -0700136 uintptr_t start = 0;
137 bool skip_2nd_read = false;
138 if (bytes == 0) {
139 // In this case, we might want to try another read at the beginning of
140 // the next page only if it's within the amount of memory we would have
141 // read.
142 size_t page_size = sysconf(_SC_PAGE_SIZE);
143 start = ((addr + (page_size - 1)) & ~(page_size - 1)) - addr;
144 if (start == 0 || start >= MEMORY_BYTES_TO_DUMP) {
145 skip_2nd_read = true;
146 }
147 }
148
149 if (bytes < MEMORY_BYTES_TO_DUMP && !skip_2nd_read) {
150 // Try to do one more read. This could happen if a read crosses a map,
151 // but the maps do not have any break between them. Or it could happen
152 // if reading from an unreadable map, but the read would cross back
153 // into a readable map. Only requires one extra read because a map has
154 // to contain at least one page, and the total number of bytes to dump
155 // is smaller than a page.
156 size_t bytes2 = backtrace->Read(addr + start + bytes, reinterpret_cast<uint8_t*>(data) + bytes,
157 sizeof(data) - bytes - start);
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700158 bytes += bytes2;
159 if (bytes2 > 0 && bytes % sizeof(uintptr_t) != 0) {
160 // This should never happen, but we'll try and continue any way.
161 ALOGE("Bytes after second read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t));
162 bytes &= ~(sizeof(uintptr_t) - 1);
Kévin PETIT4bb47722013-12-18 16:44:24 +0000163 }
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700164 }
165
166 // Dump the code around memory as:
167 // addr contents ascii
168 // 0000000000008d34 ef000000e8bd0090 e1b00000512fff1e ............../Q
169 // 0000000000008d44 ea00b1f9e92d0090 e3a070fcef000000 ......-..p......
170 // On 32-bit machines, there are still 16 bytes per line but addresses and
171 // words are of course presented differently.
172 uintptr_t* data_ptr = data;
Christopher Ferris456abba2015-07-09 15:35:47 -0700173 size_t current = 0;
174 size_t total_bytes = start + bytes;
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700175 for (size_t line = 0; line < MEMORY_BYTES_TO_DUMP / MEMORY_BYTES_PER_LINE; line++) {
176 std::string logline;
177 android::base::StringAppendF(&logline, " %" PRIPTR, addr);
178
179 addr += MEMORY_BYTES_PER_LINE;
180 std::string ascii;
Christopher Ferris456abba2015-07-09 15:35:47 -0700181 for (size_t i = 0; i < MEMORY_BYTES_PER_LINE / sizeof(uintptr_t); i++) {
182 if (current >= start && current + sizeof(uintptr_t) <= total_bytes) {
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700183 android::base::StringAppendF(&logline, " %" PRIPTR, *data_ptr);
184
185 // Fill out the ascii string from the data.
186 uint8_t* ptr = reinterpret_cast<uint8_t*>(data_ptr);
187 for (size_t val = 0; val < sizeof(uintptr_t); val++, ptr++) {
188 if (*ptr >= 0x20 && *ptr < 0x7f) {
189 ascii += *ptr;
190 } else {
191 ascii += '.';
192 }
193 }
Christopher Ferris456abba2015-07-09 15:35:47 -0700194 data_ptr++;
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700195 } else {
196 logline += ' ' + std::string(sizeof(uintptr_t) * 2, '-');
197 ascii += std::string(sizeof(uintptr_t), '.');
198 }
Christopher Ferris456abba2015-07-09 15:35:47 -0700199 current += sizeof(uintptr_t);
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700200 }
201 _LOG(log, logtype::MEMORY, "%s %s\n", logline.c_str(), ascii.c_str());
202 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000203}