blob: 0a491bbf6b84d8bc19e6fd34f7746d7ed2a2587f [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>
Peter Collingbournef8622522020-04-07 14:07:32 -070038#include <bionic/mte_kernel.h>
Josh Gaoa48b41b2019-12-13 14:11:04 -080039#include <bionic/reserved_signals.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070040#include <debuggerd/handler.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070041#include <log/log.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070042#include <unwindstack/Memory.h>
Christopher Ferris4ae266c2019-04-03 09:27:12 -070043#include <unwindstack/Unwinder.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044
Josh Gaobf2dd482017-03-28 13:07:15 -070045using android::base::unique_fd;
46
Brigid Smith62ba4892014-06-10 11:53:08 -070047// Whitelist output desired in the logcat output.
48bool is_allowed_in_logcat(enum logtype ltype) {
Christopher Ferrisb36b5922015-06-17 18:35:59 -070049 if ((ltype == HEADER)
Brigid Smith62ba4892014-06-10 11:53:08 -070050 || (ltype == REGISTERS)
51 || (ltype == BACKTRACE)) {
52 return true;
53 }
54 return false;
55}
56
Josh Gaobf2dd482017-03-28 13:07:15 -070057static bool should_write_to_kmsg() {
58 // Write to kmsg if tombstoned isn't up, and we're able to do so.
59 if (!android::base::GetBoolProperty("ro.debuggable", false)) {
60 return false;
61 }
62
63 if (android::base::GetProperty("init.svc.tombstoned", "") == "running") {
64 return false;
65 }
66
67 return true;
68}
69
Chenjie Luo97258aa2017-03-06 12:12:07 -080070__attribute__((__weak__, visibility("default")))
Brigid Smith62ba4892014-06-10 11:53:08 -070071void _LOG(log_t* log, enum logtype ltype, const char* fmt, ...) {
Mitch Phillipsaadebd82019-11-18 15:17:18 -080072 va_list ap;
73 va_start(ap, fmt);
74 _VLOG(log, ltype, fmt, ap);
75 va_end(ap);
76}
77
78__attribute__((__weak__, visibility("default")))
79void _VLOG(log_t* log, enum logtype ltype, const char* fmt, va_list ap) {
Brigid Smith50eb5462014-06-18 14:17:57 -070080 bool write_to_tombstone = (log->tfd != -1);
81 bool write_to_logcat = is_allowed_in_logcat(ltype)
Brigid Smithc75a02f2014-07-17 14:52:33 -070082 && log->crashed_tid != -1
83 && log->current_tid != -1
Brigid Smith50eb5462014-06-18 14:17:57 -070084 && (log->crashed_tid == log->current_tid);
Josh Gaobf2dd482017-03-28 13:07:15 -070085 static bool write_to_kmsg = should_write_to_kmsg();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080086
Josh Gao1cc7bd82018-02-13 13:16:17 -080087 std::string msg;
Josh Gao1cc7bd82018-02-13 13:16:17 -080088 android::base::StringAppendV(&msg, fmt, ap);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089
Josh Gao1cc7bd82018-02-13 13:16:17 -080090 if (msg.empty()) return;
Christopher Ferris20303f82014-01-10 16:33:16 -080091
Brigid Smith62ba4892014-06-10 11:53:08 -070092 if (write_to_tombstone) {
Josh Gao1cc7bd82018-02-13 13:16:17 -080093 TEMP_FAILURE_RETRY(write(log->tfd, msg.c_str(), msg.size()));
Christopher Ferris20303f82014-01-10 16:33:16 -080094 }
95
Brigid Smith62ba4892014-06-10 11:53:08 -070096 if (write_to_logcat) {
Josh Gao1cc7bd82018-02-13 13:16:17 -080097 __android_log_buf_write(LOG_ID_CRASH, ANDROID_LOG_FATAL, LOG_TAG, msg.c_str());
Christopher Ferris99235e92016-05-03 16:32:13 -070098 if (log->amfd_data != nullptr) {
Josh Gao1cc7bd82018-02-13 13:16:17 -080099 *log->amfd_data += msg;
Christopher Tateded2e5a2013-03-19 13:12:23 -0700100 }
Josh Gaobf2dd482017-03-28 13:07:15 -0700101
102 if (write_to_kmsg) {
103 unique_fd kmsg_fd(open("/dev/kmsg_debug", O_WRONLY | O_APPEND | O_CLOEXEC));
104 if (kmsg_fd.get() >= 0) {
105 // Our output might contain newlines which would otherwise be handled by the android logger.
106 // Split the lines up ourselves before sending to the kernel logger.
Josh Gao1cc7bd82018-02-13 13:16:17 -0800107 if (msg.back() == '\n') {
108 msg.back() = '\0';
Josh Gaobf2dd482017-03-28 13:07:15 -0700109 }
110
Josh Gao1cc7bd82018-02-13 13:16:17 -0800111 std::vector<std::string> fragments = android::base::Split(msg, "\n");
Josh Gaobf2dd482017-03-28 13:07:15 -0700112 for (const std::string& fragment : fragments) {
113 static constexpr char prefix[] = "<3>DEBUG: ";
114 struct iovec iov[3];
115 iov[0].iov_base = const_cast<char*>(prefix);
116 iov[0].iov_len = strlen(prefix);
117 iov[1].iov_base = const_cast<char*>(fragment.c_str());
118 iov[1].iov_len = fragment.length();
119 iov[2].iov_base = const_cast<char*>("\n");
120 iov[2].iov_len = 1;
121 TEMP_FAILURE_RETRY(writev(kmsg_fd.get(), iov, 3));
122 }
123 }
124 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800125 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126}
127
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700128#define MEMORY_BYTES_TO_DUMP 256
129#define MEMORY_BYTES_PER_LINE 16
Kévin PETIT4bb47722013-12-18 16:44:24 +0000130
Elliott Hughese1415a52018-02-15 09:18:21 -0800131void dump_memory(log_t* log, unwindstack::Memory* memory, uint64_t addr, const std::string& label) {
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700132 // Align the address to sizeof(long) and start 32 bytes before the address.
133 addr &= ~(sizeof(long) - 1);
134 if (addr >= 4128) {
135 addr -= 32;
136 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000137
Peter Collingbourne10e428d2020-07-17 14:49:31 -0700138 // We don't want the address tag to interfere with the bounds check below or appear in the
139 // addresses in the memory dump.
140 addr = untag_address(addr);
141
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700142 // Don't bother if the address looks too low, or looks too high.
143 if (addr < 4096 ||
144#if defined(__LP64__)
145 addr > 0x4000000000000000UL - MEMORY_BYTES_TO_DUMP) {
Kévin PETIT4bb47722013-12-18 16:44:24 +0000146#else
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700147 addr > 0xffff0000 - MEMORY_BYTES_TO_DUMP) {
Kévin PETIT4bb47722013-12-18 16:44:24 +0000148#endif
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700149 return;
150 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000151
Elliott Hughese1415a52018-02-15 09:18:21 -0800152 _LOG(log, logtype::MEMORY, "\n%s:\n", label.c_str());
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700153
154 // Dump 256 bytes
155 uintptr_t data[MEMORY_BYTES_TO_DUMP/sizeof(uintptr_t)];
156 memset(data, 0, MEMORY_BYTES_TO_DUMP);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700157 size_t bytes = memory->Read(addr, reinterpret_cast<uint8_t*>(data), sizeof(data));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700158 if (bytes % sizeof(uintptr_t) != 0) {
159 // This should never happen, but just in case.
160 ALOGE("Bytes read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t));
161 bytes &= ~(sizeof(uintptr_t) - 1);
162 }
163
Christopher Ferris7937a362018-01-18 11:15:49 -0800164 uint64_t start = 0;
Christopher Ferris456abba2015-07-09 15:35:47 -0700165 bool skip_2nd_read = false;
166 if (bytes == 0) {
167 // In this case, we might want to try another read at the beginning of
168 // the next page only if it's within the amount of memory we would have
169 // read.
170 size_t page_size = sysconf(_SC_PAGE_SIZE);
171 start = ((addr + (page_size - 1)) & ~(page_size - 1)) - addr;
172 if (start == 0 || start >= MEMORY_BYTES_TO_DUMP) {
173 skip_2nd_read = true;
174 }
175 }
176
177 if (bytes < MEMORY_BYTES_TO_DUMP && !skip_2nd_read) {
178 // Try to do one more read. This could happen if a read crosses a map,
179 // but the maps do not have any break between them. Or it could happen
180 // if reading from an unreadable map, but the read would cross back
181 // into a readable map. Only requires one extra read because a map has
182 // to contain at least one page, and the total number of bytes to dump
183 // is smaller than a page.
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700184 size_t bytes2 = memory->Read(addr + start + bytes, reinterpret_cast<uint8_t*>(data) + bytes,
185 sizeof(data) - bytes - start);
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700186 bytes += bytes2;
187 if (bytes2 > 0 && bytes % sizeof(uintptr_t) != 0) {
188 // This should never happen, but we'll try and continue any way.
189 ALOGE("Bytes after second read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t));
190 bytes &= ~(sizeof(uintptr_t) - 1);
Kévin PETIT4bb47722013-12-18 16:44:24 +0000191 }
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700192 }
193
194 // Dump the code around memory as:
195 // addr contents ascii
196 // 0000000000008d34 ef000000e8bd0090 e1b00000512fff1e ............../Q
197 // 0000000000008d44 ea00b1f9e92d0090 e3a070fcef000000 ......-..p......
198 // On 32-bit machines, there are still 16 bytes per line but addresses and
199 // words are of course presented differently.
200 uintptr_t* data_ptr = data;
Christopher Ferris456abba2015-07-09 15:35:47 -0700201 size_t current = 0;
202 size_t total_bytes = start + bytes;
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700203 for (size_t line = 0; line < MEMORY_BYTES_TO_DUMP / MEMORY_BYTES_PER_LINE; line++) {
204 std::string logline;
205 android::base::StringAppendF(&logline, " %" PRIPTR, addr);
206
207 addr += MEMORY_BYTES_PER_LINE;
208 std::string ascii;
Christopher Ferris456abba2015-07-09 15:35:47 -0700209 for (size_t i = 0; i < MEMORY_BYTES_PER_LINE / sizeof(uintptr_t); i++) {
210 if (current >= start && current + sizeof(uintptr_t) <= total_bytes) {
Christopher Ferris7937a362018-01-18 11:15:49 -0800211 android::base::StringAppendF(&logline, " %" PRIPTR, static_cast<uint64_t>(*data_ptr));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700212
213 // Fill out the ascii string from the data.
214 uint8_t* ptr = reinterpret_cast<uint8_t*>(data_ptr);
215 for (size_t val = 0; val < sizeof(uintptr_t); val++, ptr++) {
216 if (*ptr >= 0x20 && *ptr < 0x7f) {
217 ascii += *ptr;
218 } else {
219 ascii += '.';
220 }
221 }
Christopher Ferris456abba2015-07-09 15:35:47 -0700222 data_ptr++;
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700223 } else {
224 logline += ' ' + std::string(sizeof(uintptr_t) * 2, '-');
225 ascii += std::string(sizeof(uintptr_t), '.');
226 }
Christopher Ferris456abba2015-07-09 15:35:47 -0700227 current += sizeof(uintptr_t);
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700228 }
229 _LOG(log, logtype::MEMORY, "%s %s\n", logline.c_str(), ascii.c_str());
230 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000231}
Josh Gao57f58f82017-03-15 23:23:22 -0700232
233void read_with_default(const char* path, char* buf, size_t len, const char* default_value) {
Josh Gaobf2dd482017-03-28 13:07:15 -0700234 unique_fd fd(open(path, O_RDONLY | O_CLOEXEC));
Josh Gao57f58f82017-03-15 23:23:22 -0700235 if (fd != -1) {
236 int rc = TEMP_FAILURE_RETRY(read(fd.get(), buf, len - 1));
237 if (rc != -1) {
238 buf[rc] = '\0';
239
240 // Trim trailing newlines.
241 if (rc > 0 && buf[rc - 1] == '\n') {
242 buf[rc - 1] = '\0';
243 }
244 return;
245 }
246 }
247 strcpy(buf, default_value);
248}
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700249
250void drop_capabilities() {
251 __user_cap_header_struct capheader;
252 memset(&capheader, 0, sizeof(capheader));
253 capheader.version = _LINUX_CAPABILITY_VERSION_3;
254 capheader.pid = 0;
255
256 __user_cap_data_struct capdata[2];
257 memset(&capdata, 0, sizeof(capdata));
258
259 if (capset(&capheader, &capdata[0]) == -1) {
260 PLOG(FATAL) << "failed to drop capabilities";
261 }
262
263 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) {
264 PLOG(FATAL) << "failed to set PR_SET_NO_NEW_PRIVS";
265 }
266}
267
Elliott Hughes70d8f282018-04-25 17:00:14 -0700268bool signal_has_si_addr(const siginfo_t* si) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700269 // Manually sent signals won't have si_addr.
Elliott Hughes70d8f282018-04-25 17:00:14 -0700270 if (si->si_code == SI_USER || si->si_code == SI_QUEUE || si->si_code == SI_TKILL) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700271 return false;
272 }
273
Elliott Hughes70d8f282018-04-25 17:00:14 -0700274 switch (si->si_signo) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700275 case SIGBUS:
276 case SIGFPE:
277 case SIGILL:
278 case SIGSEGV:
279 case SIGTRAP:
280 return true;
281 default:
282 return false;
283 }
284}
285
Elliott Hughes70d8f282018-04-25 17:00:14 -0700286bool signal_has_sender(const siginfo_t* si, pid_t caller_pid) {
287 return SI_FROMUSER(si) && (si->si_pid != 0) && (si->si_pid != caller_pid);
288}
289
290void get_signal_sender(char* buf, size_t n, const siginfo_t* si) {
291 snprintf(buf, n, " from pid %d, uid %d", si->si_pid, si->si_uid);
292}
293
294const char* get_signame(const siginfo_t* si) {
295 switch (si->si_signo) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700296 case SIGABRT: return "SIGABRT";
297 case SIGBUS: return "SIGBUS";
298 case SIGFPE: return "SIGFPE";
299 case SIGILL: return "SIGILL";
300 case SIGSEGV: return "SIGSEGV";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700301 case SIGSTKFLT: return "SIGSTKFLT";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700302 case SIGSTOP: return "SIGSTOP";
303 case SIGSYS: return "SIGSYS";
304 case SIGTRAP: return "SIGTRAP";
Josh Gaoa48b41b2019-12-13 14:11:04 -0800305 case BIONIC_SIGNAL_DEBUGGER:
306 return "<debuggerd signal>";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700307 default: return "?";
308 }
309}
310
Elliott Hughes70d8f282018-04-25 17:00:14 -0700311const char* get_sigcode(const siginfo_t* si) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700312 // Try the signal-specific codes...
Elliott Hughes70d8f282018-04-25 17:00:14 -0700313 switch (si->si_signo) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700314 case SIGILL:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700315 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700316 case ILL_ILLOPC: return "ILL_ILLOPC";
317 case ILL_ILLOPN: return "ILL_ILLOPN";
318 case ILL_ILLADR: return "ILL_ILLADR";
319 case ILL_ILLTRP: return "ILL_ILLTRP";
320 case ILL_PRVOPC: return "ILL_PRVOPC";
321 case ILL_PRVREG: return "ILL_PRVREG";
322 case ILL_COPROC: return "ILL_COPROC";
323 case ILL_BADSTK: return "ILL_BADSTK";
Christopher Ferris432791e2018-06-27 15:06:01 -0700324 case ILL_BADIADDR:
325 return "ILL_BADIADDR";
326 case __ILL_BREAK:
327 return "ILL_BREAK";
328 case __ILL_BNDMOD:
329 return "ILL_BNDMOD";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700330 }
Christopher Ferris432791e2018-06-27 15:06:01 -0700331 static_assert(NSIGILL == __ILL_BNDMOD, "missing ILL_* si_code");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700332 break;
333 case SIGBUS:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700334 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700335 case BUS_ADRALN: return "BUS_ADRALN";
336 case BUS_ADRERR: return "BUS_ADRERR";
337 case BUS_OBJERR: return "BUS_OBJERR";
338 case BUS_MCEERR_AR: return "BUS_MCEERR_AR";
339 case BUS_MCEERR_AO: return "BUS_MCEERR_AO";
340 }
341 static_assert(NSIGBUS == BUS_MCEERR_AO, "missing BUS_* si_code");
342 break;
343 case SIGFPE:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700344 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700345 case FPE_INTDIV: return "FPE_INTDIV";
346 case FPE_INTOVF: return "FPE_INTOVF";
347 case FPE_FLTDIV: return "FPE_FLTDIV";
348 case FPE_FLTOVF: return "FPE_FLTOVF";
349 case FPE_FLTUND: return "FPE_FLTUND";
350 case FPE_FLTRES: return "FPE_FLTRES";
351 case FPE_FLTINV: return "FPE_FLTINV";
352 case FPE_FLTSUB: return "FPE_FLTSUB";
Christopher Ferris432791e2018-06-27 15:06:01 -0700353 case __FPE_DECOVF:
354 return "FPE_DECOVF";
355 case __FPE_DECDIV:
356 return "FPE_DECDIV";
357 case __FPE_DECERR:
358 return "FPE_DECERR";
359 case __FPE_INVASC:
360 return "FPE_INVASC";
361 case __FPE_INVDEC:
362 return "FPE_INVDEC";
363 case FPE_FLTUNK:
364 return "FPE_FLTUNK";
365 case FPE_CONDTRAP:
366 return "FPE_CONDTRAP";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700367 }
Christopher Ferris432791e2018-06-27 15:06:01 -0700368 static_assert(NSIGFPE == FPE_CONDTRAP, "missing FPE_* si_code");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700369 break;
370 case SIGSEGV:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700371 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700372 case SEGV_MAPERR: return "SEGV_MAPERR";
373 case SEGV_ACCERR: return "SEGV_ACCERR";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700374 case SEGV_BNDERR: return "SEGV_BNDERR";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700375 case SEGV_PKUERR: return "SEGV_PKUERR";
Christopher Ferris432791e2018-06-27 15:06:01 -0700376 case SEGV_ACCADI:
377 return "SEGV_ACCADI";
378 case SEGV_ADIDERR:
379 return "SEGV_ADIDERR";
380 case SEGV_ADIPERR:
381 return "SEGV_ADIPERR";
Peter Collingbournef8622522020-04-07 14:07:32 -0700382#if defined(ANDROID_EXPERIMENTAL_MTE)
383 case SEGV_MTEAERR:
384 return "SEGV_MTEAERR";
385 case SEGV_MTESERR:
386 return "SEGV_MTESERR";
387#endif
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700388 }
Christopher Ferris432791e2018-06-27 15:06:01 -0700389 static_assert(NSIGSEGV == SEGV_ADIPERR, "missing SEGV_* si_code");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700390 break;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700391 case SIGSYS:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700392 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700393 case SYS_SECCOMP: return "SYS_SECCOMP";
394 }
395 static_assert(NSIGSYS == SYS_SECCOMP, "missing SYS_* si_code");
396 break;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700397 case SIGTRAP:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700398 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700399 case TRAP_BRKPT: return "TRAP_BRKPT";
400 case TRAP_TRACE: return "TRAP_TRACE";
401 case TRAP_BRANCH: return "TRAP_BRANCH";
402 case TRAP_HWBKPT: return "TRAP_HWBKPT";
Christopher Ferris461baeb2018-10-26 11:22:40 -0700403 case TRAP_UNK:
404 return "TRAP_UNDIAGNOSED";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700405 }
Elliott Hughes70d8f282018-04-25 17:00:14 -0700406 if ((si->si_code & 0xff) == SIGTRAP) {
407 switch ((si->si_code >> 8) & 0xff) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700408 case PTRACE_EVENT_FORK:
409 return "PTRACE_EVENT_FORK";
410 case PTRACE_EVENT_VFORK:
411 return "PTRACE_EVENT_VFORK";
412 case PTRACE_EVENT_CLONE:
413 return "PTRACE_EVENT_CLONE";
414 case PTRACE_EVENT_EXEC:
415 return "PTRACE_EVENT_EXEC";
416 case PTRACE_EVENT_VFORK_DONE:
417 return "PTRACE_EVENT_VFORK_DONE";
418 case PTRACE_EVENT_EXIT:
419 return "PTRACE_EVENT_EXIT";
420 case PTRACE_EVENT_SECCOMP:
421 return "PTRACE_EVENT_SECCOMP";
422 case PTRACE_EVENT_STOP:
423 return "PTRACE_EVENT_STOP";
424 }
425 }
Christopher Ferris461baeb2018-10-26 11:22:40 -0700426 static_assert(NSIGTRAP == TRAP_UNK, "missing TRAP_* si_code");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700427 break;
428 }
429 // Then the other codes...
Elliott Hughes70d8f282018-04-25 17:00:14 -0700430 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700431 case SI_USER: return "SI_USER";
432 case SI_KERNEL: return "SI_KERNEL";
433 case SI_QUEUE: return "SI_QUEUE";
434 case SI_TIMER: return "SI_TIMER";
435 case SI_MESGQ: return "SI_MESGQ";
436 case SI_ASYNCIO: return "SI_ASYNCIO";
437 case SI_SIGIO: return "SI_SIGIO";
438 case SI_TKILL: return "SI_TKILL";
439 case SI_DETHREAD: return "SI_DETHREAD";
440 }
441 // Then give up...
442 return "?";
443}
Christopher Ferris4ae266c2019-04-03 09:27:12 -0700444
445void log_backtrace(log_t* log, unwindstack::Unwinder* unwinder, const char* prefix) {
446 if (unwinder->elf_from_memory_not_file()) {
447 _LOG(log, logtype::BACKTRACE,
448 "%sNOTE: Function names and BuildId information is missing for some frames due\n", prefix);
449 _LOG(log, logtype::BACKTRACE,
450 "%sNOTE: to unreadable libraries. For unwinds of apps, only shared libraries\n", prefix);
451 _LOG(log, logtype::BACKTRACE, "%sNOTE: found under the lib/ directory are readable.\n", prefix);
452#if defined(ROOT_POSSIBLE)
453 _LOG(log, logtype::BACKTRACE,
454 "%sNOTE: On this device, run setenforce 0 to make the libraries readable.\n", prefix);
455#endif
456 }
457
458 unwinder->SetDisplayBuildID(true);
459 for (size_t i = 0; i < unwinder->NumFrames(); i++) {
460 _LOG(log, logtype::BACKTRACE, "%s%s\n", prefix, unwinder->FormatFrame(i).c_str());
461 }
462}
Peter Collingbournef03af882020-03-20 18:09:00 -0700463
464#if defined(__aarch64__)
465#define FAR_MAGIC 0x46415201
466
467struct far_context {
468 struct _aarch64_ctx head;
469 __u64 far;
470};
471#endif
472
473uintptr_t get_fault_address(const siginfo_t* siginfo, const ucontext_t* ucontext) {
474 (void)ucontext;
475#if defined(__aarch64__)
476 // This relies on a kernel patch:
477 // https://patchwork.kernel.org/patch/11435077/
478 // that hasn't been accepted into the kernel yet. TODO(pcc): Update this to
479 // use the official interface once it lands.
480 auto* begin = reinterpret_cast<const char*>(ucontext->uc_mcontext.__reserved);
481 auto* end = begin + sizeof(ucontext->uc_mcontext.__reserved);
482 auto* ptr = begin;
483 while (1) {
484 auto* ctx = reinterpret_cast<const _aarch64_ctx*>(ptr);
485 if (ctx->magic == 0) {
486 break;
487 }
488 if (ctx->magic == FAR_MAGIC) {
489 auto* far_ctx = reinterpret_cast<const far_context*>(ctx);
490 return far_ctx->far;
491 }
492 ptr += ctx->size;
493 if (ctx->size % sizeof(void*) != 0 || ptr < begin || ptr >= end) {
494 break;
495 }
496 }
497#endif
498 return reinterpret_cast<uintptr_t>(siginfo->si_addr);
499}