blob: 247d806ba27a84735a14b0981e0ab973b2613b56 [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
Josh Gaoc3706662017-08-29 13:08:32 -070019#include "libdebuggerd/utility.h"
Pavel Chupinc6c194c2013-11-21 23:17:20 +040020
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>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070024#include <sys/capability.h>
25#include <sys/prctl.h>
Jeff Brown13e715b2011-10-21 12:14:56 -070026#include <sys/ptrace.h>
Josh Gaobf2dd482017-03-28 13:07:15 -070027#include <sys/uio.h>
Jeff Brown053b8652012-06-06 16:25:03 -070028#include <sys/wait.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070029#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030
Christopher Ferris99235e92016-05-03 16:32:13 -070031#include <string>
32
Josh Gaobf2dd482017-03-28 13:07:15 -070033#include <android-base/logging.h>
34#include <android-base/properties.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080035#include <android-base/stringprintf.h>
Josh Gaobf2dd482017-03-28 13:07:15 -070036#include <android-base/strings.h>
Josh Gao57f58f82017-03-15 23:23:22 -070037#include <android-base/unique_fd.h>
Pavel Chupinc6c194c2013-11-21 23:17:20 +040038#include <backtrace/Backtrace.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070039#include <debuggerd/handler.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070040#include <log/log.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070041#include <unwindstack/Memory.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
Josh Gaobf2dd482017-03-28 13:07:15 -070043using android::base::unique_fd;
44
Brigid Smith62ba4892014-06-10 11:53:08 -070045// Whitelist output desired in the logcat output.
46bool is_allowed_in_logcat(enum logtype ltype) {
Christopher Ferrisb36b5922015-06-17 18:35:59 -070047 if ((ltype == HEADER)
Brigid Smith62ba4892014-06-10 11:53:08 -070048 || (ltype == REGISTERS)
49 || (ltype == BACKTRACE)) {
50 return true;
51 }
52 return false;
53}
54
Josh Gaobf2dd482017-03-28 13:07:15 -070055static bool should_write_to_kmsg() {
56 // Write to kmsg if tombstoned isn't up, and we're able to do so.
57 if (!android::base::GetBoolProperty("ro.debuggable", false)) {
58 return false;
59 }
60
61 if (android::base::GetProperty("init.svc.tombstoned", "") == "running") {
62 return false;
63 }
64
65 return true;
66}
67
Chenjie Luo97258aa2017-03-06 12:12:07 -080068__attribute__((__weak__, visibility("default")))
Brigid Smith62ba4892014-06-10 11:53:08 -070069void _LOG(log_t* log, enum logtype ltype, const char* fmt, ...) {
Brigid Smith50eb5462014-06-18 14:17:57 -070070 bool write_to_tombstone = (log->tfd != -1);
71 bool write_to_logcat = is_allowed_in_logcat(ltype)
Brigid Smithc75a02f2014-07-17 14:52:33 -070072 && log->crashed_tid != -1
73 && log->current_tid != -1
Brigid Smith50eb5462014-06-18 14:17:57 -070074 && (log->crashed_tid == log->current_tid);
Josh Gaobf2dd482017-03-28 13:07:15 -070075 static bool write_to_kmsg = should_write_to_kmsg();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076
Pavel Chupinc6c194c2013-11-21 23:17:20 +040077 char buf[512];
Christopher Ferris20303f82014-01-10 16:33:16 -080078 va_list ap;
79 va_start(ap, fmt);
Pavel Chupinc6c194c2013-11-21 23:17:20 +040080 vsnprintf(buf, sizeof(buf), fmt, ap);
81 va_end(ap);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082
Pavel Chupinc6c194c2013-11-21 23:17:20 +040083 size_t len = strlen(buf);
84 if (len <= 0) {
85 return;
Christopher Ferris20303f82014-01-10 16:33:16 -080086 }
87
Brigid Smith62ba4892014-06-10 11:53:08 -070088 if (write_to_tombstone) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +040089 TEMP_FAILURE_RETRY(write(log->tfd, buf, len));
Christopher Ferris20303f82014-01-10 16:33:16 -080090 }
91
Brigid Smith62ba4892014-06-10 11:53:08 -070092 if (write_to_logcat) {
Christopher Ferrisb0412a52015-05-05 12:23:06 -070093 __android_log_buf_write(LOG_ID_CRASH, ANDROID_LOG_FATAL, LOG_TAG, buf);
Christopher Ferris99235e92016-05-03 16:32:13 -070094 if (log->amfd_data != nullptr) {
95 *log->amfd_data += buf;
Christopher Tateded2e5a2013-03-19 13:12:23 -070096 }
Josh Gaobf2dd482017-03-28 13:07:15 -070097
98 if (write_to_kmsg) {
99 unique_fd kmsg_fd(open("/dev/kmsg_debug", O_WRONLY | O_APPEND | O_CLOEXEC));
100 if (kmsg_fd.get() >= 0) {
101 // Our output might contain newlines which would otherwise be handled by the android logger.
102 // Split the lines up ourselves before sending to the kernel logger.
103 if (buf[len - 1] == '\n') {
104 buf[len - 1] = '\0';
105 }
106
107 std::vector<std::string> fragments = android::base::Split(buf, "\n");
108 for (const std::string& fragment : fragments) {
109 static constexpr char prefix[] = "<3>DEBUG: ";
110 struct iovec iov[3];
111 iov[0].iov_base = const_cast<char*>(prefix);
112 iov[0].iov_len = strlen(prefix);
113 iov[1].iov_base = const_cast<char*>(fragment.c_str());
114 iov[1].iov_len = fragment.length();
115 iov[2].iov_base = const_cast<char*>("\n");
116 iov[2].iov_len = 1;
117 TEMP_FAILURE_RETRY(writev(kmsg_fd.get(), iov, 3));
118 }
119 }
120 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800121 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122}
123
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700124#define MEMORY_BYTES_TO_DUMP 256
125#define MEMORY_BYTES_PER_LINE 16
Kévin PETIT4bb47722013-12-18 16:44:24 +0000126
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700127void dump_memory(log_t* log, unwindstack::Memory* memory, uintptr_t addr, const char* fmt, ...) {
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700128 std::string log_msg;
129 va_list ap;
130 va_start(ap, fmt);
131 android::base::StringAppendV(&log_msg, fmt, ap);
132 va_end(ap);
Kévin PETIT4bb47722013-12-18 16:44:24 +0000133
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700134 // Align the address to sizeof(long) and start 32 bytes before the address.
135 addr &= ~(sizeof(long) - 1);
136 if (addr >= 4128) {
137 addr -= 32;
138 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000139
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700140 // Don't bother if the address looks too low, or looks too high.
141 if (addr < 4096 ||
142#if defined(__LP64__)
143 addr > 0x4000000000000000UL - MEMORY_BYTES_TO_DUMP) {
Kévin PETIT4bb47722013-12-18 16:44:24 +0000144#else
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700145 addr > 0xffff0000 - MEMORY_BYTES_TO_DUMP) {
Kévin PETIT4bb47722013-12-18 16:44:24 +0000146#endif
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700147 return;
148 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000149
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700150 _LOG(log, logtype::MEMORY, "\n%s\n", log_msg.c_str());
151
152 // Dump 256 bytes
153 uintptr_t data[MEMORY_BYTES_TO_DUMP/sizeof(uintptr_t)];
154 memset(data, 0, MEMORY_BYTES_TO_DUMP);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700155 size_t bytes = memory->Read(addr, reinterpret_cast<uint8_t*>(data), sizeof(data));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700156 if (bytes % sizeof(uintptr_t) != 0) {
157 // This should never happen, but just in case.
158 ALOGE("Bytes read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t));
159 bytes &= ~(sizeof(uintptr_t) - 1);
160 }
161
Christopher Ferris456abba2015-07-09 15:35:47 -0700162 uintptr_t start = 0;
163 bool skip_2nd_read = false;
164 if (bytes == 0) {
165 // In this case, we might want to try another read at the beginning of
166 // the next page only if it's within the amount of memory we would have
167 // read.
168 size_t page_size = sysconf(_SC_PAGE_SIZE);
169 start = ((addr + (page_size - 1)) & ~(page_size - 1)) - addr;
170 if (start == 0 || start >= MEMORY_BYTES_TO_DUMP) {
171 skip_2nd_read = true;
172 }
173 }
174
175 if (bytes < MEMORY_BYTES_TO_DUMP && !skip_2nd_read) {
176 // Try to do one more read. This could happen if a read crosses a map,
177 // but the maps do not have any break between them. Or it could happen
178 // if reading from an unreadable map, but the read would cross back
179 // into a readable map. Only requires one extra read because a map has
180 // to contain at least one page, and the total number of bytes to dump
181 // is smaller than a page.
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700182 size_t bytes2 = memory->Read(addr + start + bytes, reinterpret_cast<uint8_t*>(data) + bytes,
183 sizeof(data) - bytes - start);
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700184 bytes += bytes2;
185 if (bytes2 > 0 && bytes % sizeof(uintptr_t) != 0) {
186 // This should never happen, but we'll try and continue any way.
187 ALOGE("Bytes after second read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t));
188 bytes &= ~(sizeof(uintptr_t) - 1);
Kévin PETIT4bb47722013-12-18 16:44:24 +0000189 }
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700190 }
191
192 // Dump the code around memory as:
193 // addr contents ascii
194 // 0000000000008d34 ef000000e8bd0090 e1b00000512fff1e ............../Q
195 // 0000000000008d44 ea00b1f9e92d0090 e3a070fcef000000 ......-..p......
196 // On 32-bit machines, there are still 16 bytes per line but addresses and
197 // words are of course presented differently.
198 uintptr_t* data_ptr = data;
Christopher Ferris456abba2015-07-09 15:35:47 -0700199 size_t current = 0;
200 size_t total_bytes = start + bytes;
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700201 for (size_t line = 0; line < MEMORY_BYTES_TO_DUMP / MEMORY_BYTES_PER_LINE; line++) {
202 std::string logline;
203 android::base::StringAppendF(&logline, " %" PRIPTR, addr);
204
205 addr += MEMORY_BYTES_PER_LINE;
206 std::string ascii;
Christopher Ferris456abba2015-07-09 15:35:47 -0700207 for (size_t i = 0; i < MEMORY_BYTES_PER_LINE / sizeof(uintptr_t); i++) {
208 if (current >= start && current + sizeof(uintptr_t) <= total_bytes) {
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700209 android::base::StringAppendF(&logline, " %" PRIPTR, *data_ptr);
210
211 // Fill out the ascii string from the data.
212 uint8_t* ptr = reinterpret_cast<uint8_t*>(data_ptr);
213 for (size_t val = 0; val < sizeof(uintptr_t); val++, ptr++) {
214 if (*ptr >= 0x20 && *ptr < 0x7f) {
215 ascii += *ptr;
216 } else {
217 ascii += '.';
218 }
219 }
Christopher Ferris456abba2015-07-09 15:35:47 -0700220 data_ptr++;
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700221 } else {
222 logline += ' ' + std::string(sizeof(uintptr_t) * 2, '-');
223 ascii += std::string(sizeof(uintptr_t), '.');
224 }
Christopher Ferris456abba2015-07-09 15:35:47 -0700225 current += sizeof(uintptr_t);
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700226 }
227 _LOG(log, logtype::MEMORY, "%s %s\n", logline.c_str(), ascii.c_str());
228 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000229}
Josh Gao57f58f82017-03-15 23:23:22 -0700230
231void read_with_default(const char* path, char* buf, size_t len, const char* default_value) {
Josh Gaobf2dd482017-03-28 13:07:15 -0700232 unique_fd fd(open(path, O_RDONLY | O_CLOEXEC));
Josh Gao57f58f82017-03-15 23:23:22 -0700233 if (fd != -1) {
234 int rc = TEMP_FAILURE_RETRY(read(fd.get(), buf, len - 1));
235 if (rc != -1) {
236 buf[rc] = '\0';
237
238 // Trim trailing newlines.
239 if (rc > 0 && buf[rc - 1] == '\n') {
240 buf[rc - 1] = '\0';
241 }
242 return;
243 }
244 }
245 strcpy(buf, default_value);
246}
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700247
248void drop_capabilities() {
249 __user_cap_header_struct capheader;
250 memset(&capheader, 0, sizeof(capheader));
251 capheader.version = _LINUX_CAPABILITY_VERSION_3;
252 capheader.pid = 0;
253
254 __user_cap_data_struct capdata[2];
255 memset(&capdata, 0, sizeof(capdata));
256
257 if (capset(&capheader, &capdata[0]) == -1) {
258 PLOG(FATAL) << "failed to drop capabilities";
259 }
260
261 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) {
262 PLOG(FATAL) << "failed to set PR_SET_NO_NEW_PRIVS";
263 }
264}
265
266bool signal_has_si_addr(int si_signo, int si_code) {
267 // Manually sent signals won't have si_addr.
268 if (si_code == SI_USER || si_code == SI_QUEUE || si_code == SI_TKILL) {
269 return false;
270 }
271
272 switch (si_signo) {
273 case SIGBUS:
274 case SIGFPE:
275 case SIGILL:
276 case SIGSEGV:
277 case SIGTRAP:
278 return true;
279 default:
280 return false;
281 }
282}
283
284const char* get_signame(int sig) {
285 switch (sig) {
286 case SIGABRT: return "SIGABRT";
287 case SIGBUS: return "SIGBUS";
288 case SIGFPE: return "SIGFPE";
289 case SIGILL: return "SIGILL";
290 case SIGSEGV: return "SIGSEGV";
291#if defined(SIGSTKFLT)
292 case SIGSTKFLT: return "SIGSTKFLT";
293#endif
294 case SIGSTOP: return "SIGSTOP";
295 case SIGSYS: return "SIGSYS";
296 case SIGTRAP: return "SIGTRAP";
297 case DEBUGGER_SIGNAL: return "<debuggerd signal>";
298 default: return "?";
299 }
300}
301
302const char* get_sigcode(int signo, int code) {
303 // Try the signal-specific codes...
304 switch (signo) {
305 case SIGILL:
306 switch (code) {
307 case ILL_ILLOPC: return "ILL_ILLOPC";
308 case ILL_ILLOPN: return "ILL_ILLOPN";
309 case ILL_ILLADR: return "ILL_ILLADR";
310 case ILL_ILLTRP: return "ILL_ILLTRP";
311 case ILL_PRVOPC: return "ILL_PRVOPC";
312 case ILL_PRVREG: return "ILL_PRVREG";
313 case ILL_COPROC: return "ILL_COPROC";
314 case ILL_BADSTK: return "ILL_BADSTK";
315 }
316 static_assert(NSIGILL == ILL_BADSTK, "missing ILL_* si_code");
317 break;
318 case SIGBUS:
319 switch (code) {
320 case BUS_ADRALN: return "BUS_ADRALN";
321 case BUS_ADRERR: return "BUS_ADRERR";
322 case BUS_OBJERR: return "BUS_OBJERR";
323 case BUS_MCEERR_AR: return "BUS_MCEERR_AR";
324 case BUS_MCEERR_AO: return "BUS_MCEERR_AO";
325 }
326 static_assert(NSIGBUS == BUS_MCEERR_AO, "missing BUS_* si_code");
327 break;
328 case SIGFPE:
329 switch (code) {
330 case FPE_INTDIV: return "FPE_INTDIV";
331 case FPE_INTOVF: return "FPE_INTOVF";
332 case FPE_FLTDIV: return "FPE_FLTDIV";
333 case FPE_FLTOVF: return "FPE_FLTOVF";
334 case FPE_FLTUND: return "FPE_FLTUND";
335 case FPE_FLTRES: return "FPE_FLTRES";
336 case FPE_FLTINV: return "FPE_FLTINV";
337 case FPE_FLTSUB: return "FPE_FLTSUB";
338 }
339 static_assert(NSIGFPE == FPE_FLTSUB, "missing FPE_* si_code");
340 break;
341 case SIGSEGV:
342 switch (code) {
343 case SEGV_MAPERR: return "SEGV_MAPERR";
344 case SEGV_ACCERR: return "SEGV_ACCERR";
345#if defined(SEGV_BNDERR)
346 case SEGV_BNDERR: return "SEGV_BNDERR";
347#endif
348#if defined(SEGV_PKUERR)
349 case SEGV_PKUERR: return "SEGV_PKUERR";
350#endif
351 }
352#if defined(SEGV_PKUERR)
353 static_assert(NSIGSEGV == SEGV_PKUERR, "missing SEGV_* si_code");
354#elif defined(SEGV_BNDERR)
355 static_assert(NSIGSEGV == SEGV_BNDERR, "missing SEGV_* si_code");
356#else
357 static_assert(NSIGSEGV == SEGV_ACCERR, "missing SEGV_* si_code");
358#endif
359 break;
360#if defined(SYS_SECCOMP) // Our glibc is too old, and we build this for the host too.
361 case SIGSYS:
362 switch (code) {
363 case SYS_SECCOMP: return "SYS_SECCOMP";
364 }
365 static_assert(NSIGSYS == SYS_SECCOMP, "missing SYS_* si_code");
366 break;
367#endif
368 case SIGTRAP:
369 switch (code) {
370 case TRAP_BRKPT: return "TRAP_BRKPT";
371 case TRAP_TRACE: return "TRAP_TRACE";
372 case TRAP_BRANCH: return "TRAP_BRANCH";
373 case TRAP_HWBKPT: return "TRAP_HWBKPT";
374 }
375 if ((code & 0xff) == SIGTRAP) {
376 switch ((code >> 8) & 0xff) {
377 case PTRACE_EVENT_FORK:
378 return "PTRACE_EVENT_FORK";
379 case PTRACE_EVENT_VFORK:
380 return "PTRACE_EVENT_VFORK";
381 case PTRACE_EVENT_CLONE:
382 return "PTRACE_EVENT_CLONE";
383 case PTRACE_EVENT_EXEC:
384 return "PTRACE_EVENT_EXEC";
385 case PTRACE_EVENT_VFORK_DONE:
386 return "PTRACE_EVENT_VFORK_DONE";
387 case PTRACE_EVENT_EXIT:
388 return "PTRACE_EVENT_EXIT";
389 case PTRACE_EVENT_SECCOMP:
390 return "PTRACE_EVENT_SECCOMP";
391 case PTRACE_EVENT_STOP:
392 return "PTRACE_EVENT_STOP";
393 }
394 }
395 static_assert(NSIGTRAP == TRAP_HWBKPT, "missing TRAP_* si_code");
396 break;
397 }
398 // Then the other codes...
399 switch (code) {
400 case SI_USER: return "SI_USER";
401 case SI_KERNEL: return "SI_KERNEL";
402 case SI_QUEUE: return "SI_QUEUE";
403 case SI_TIMER: return "SI_TIMER";
404 case SI_MESGQ: return "SI_MESGQ";
405 case SI_ASYNCIO: return "SI_ASYNCIO";
406 case SI_SIGIO: return "SI_SIGIO";
407 case SI_TKILL: return "SI_TKILL";
408 case SI_DETHREAD: return "SI_DETHREAD";
409 }
410 // Then give up...
411 return "?";
412}