blob: 74a1423b2c1ec29868db456347ec5d7be427fb46 [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 Ferrisc95047d2022-03-14 15:02:11 -070031#include <set>
Christopher Ferris99235e92016-05-03 16:32:13 -070032#include <string>
33
Josh Gaobf2dd482017-03-28 13:07:15 -070034#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>
Josh Gao618cea32021-01-26 17:45:43 -080038#include <async_safe/log.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>
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -070042#include <unwindstack/AndroidUnwinder.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070043#include <unwindstack/Memory.h>
Christopher Ferris4ae266c2019-04-03 09:27:12 -070044#include <unwindstack/Unwinder.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045
Peter Collingbourne47d784e2021-11-05 18:40:52 -070046using android::base::StringPrintf;
Josh Gaobf2dd482017-03-28 13:07:15 -070047using android::base::unique_fd;
48
Brigid Smith62ba4892014-06-10 11:53:08 -070049bool is_allowed_in_logcat(enum logtype ltype) {
Christopher Ferrisb36b5922015-06-17 18:35:59 -070050 if ((ltype == HEADER)
Brigid Smith62ba4892014-06-10 11:53:08 -070051 || (ltype == REGISTERS)
52 || (ltype == BACKTRACE)) {
53 return true;
54 }
55 return false;
56}
57
Josh Gaobf2dd482017-03-28 13:07:15 -070058static bool should_write_to_kmsg() {
59 // Write to kmsg if tombstoned isn't up, and we're able to do so.
60 if (!android::base::GetBoolProperty("ro.debuggable", false)) {
61 return false;
62 }
63
64 if (android::base::GetProperty("init.svc.tombstoned", "") == "running") {
65 return false;
66 }
67
68 return true;
69}
70
Chenjie Luo97258aa2017-03-06 12:12:07 -080071__attribute__((__weak__, visibility("default")))
Brigid Smith62ba4892014-06-10 11:53:08 -070072void _LOG(log_t* log, enum logtype ltype, const char* fmt, ...) {
Mitch Phillipsaadebd82019-11-18 15:17:18 -080073 va_list ap;
74 va_start(ap, fmt);
75 _VLOG(log, ltype, fmt, ap);
76 va_end(ap);
77}
78
79__attribute__((__weak__, visibility("default")))
80void _VLOG(log_t* log, enum logtype ltype, const char* fmt, va_list ap) {
Brigid Smith50eb5462014-06-18 14:17:57 -070081 bool write_to_tombstone = (log->tfd != -1);
82 bool write_to_logcat = is_allowed_in_logcat(ltype)
Brigid Smithc75a02f2014-07-17 14:52:33 -070083 && log->crashed_tid != -1
84 && log->current_tid != -1
Brigid Smith50eb5462014-06-18 14:17:57 -070085 && (log->crashed_tid == log->current_tid);
Josh Gaobf2dd482017-03-28 13:07:15 -070086 static bool write_to_kmsg = should_write_to_kmsg();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087
Josh Gao1cc7bd82018-02-13 13:16:17 -080088 std::string msg;
Josh Gao1cc7bd82018-02-13 13:16:17 -080089 android::base::StringAppendV(&msg, fmt, ap);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090
Josh Gao1cc7bd82018-02-13 13:16:17 -080091 if (msg.empty()) return;
Christopher Ferris20303f82014-01-10 16:33:16 -080092
Brigid Smith62ba4892014-06-10 11:53:08 -070093 if (write_to_tombstone) {
Josh Gao1cc7bd82018-02-13 13:16:17 -080094 TEMP_FAILURE_RETRY(write(log->tfd, msg.c_str(), msg.size()));
Christopher Ferris20303f82014-01-10 16:33:16 -080095 }
96
Brigid Smith62ba4892014-06-10 11:53:08 -070097 if (write_to_logcat) {
Josh Gao1cc7bd82018-02-13 13:16:17 -080098 __android_log_buf_write(LOG_ID_CRASH, ANDROID_LOG_FATAL, LOG_TAG, msg.c_str());
Christopher Ferris99235e92016-05-03 16:32:13 -070099 if (log->amfd_data != nullptr) {
Josh Gao1cc7bd82018-02-13 13:16:17 -0800100 *log->amfd_data += msg;
Christopher Tateded2e5a2013-03-19 13:12:23 -0700101 }
Josh Gaobf2dd482017-03-28 13:07:15 -0700102
103 if (write_to_kmsg) {
104 unique_fd kmsg_fd(open("/dev/kmsg_debug", O_WRONLY | O_APPEND | O_CLOEXEC));
105 if (kmsg_fd.get() >= 0) {
106 // Our output might contain newlines which would otherwise be handled by the android logger.
107 // Split the lines up ourselves before sending to the kernel logger.
Josh Gao1cc7bd82018-02-13 13:16:17 -0800108 if (msg.back() == '\n') {
109 msg.back() = '\0';
Josh Gaobf2dd482017-03-28 13:07:15 -0700110 }
111
Josh Gao1cc7bd82018-02-13 13:16:17 -0800112 std::vector<std::string> fragments = android::base::Split(msg, "\n");
Josh Gaobf2dd482017-03-28 13:07:15 -0700113 for (const std::string& fragment : fragments) {
114 static constexpr char prefix[] = "<3>DEBUG: ";
115 struct iovec iov[3];
116 iov[0].iov_base = const_cast<char*>(prefix);
117 iov[0].iov_len = strlen(prefix);
118 iov[1].iov_base = const_cast<char*>(fragment.c_str());
119 iov[1].iov_len = fragment.length();
120 iov[2].iov_base = const_cast<char*>("\n");
121 iov[2].iov_len = 1;
122 TEMP_FAILURE_RETRY(writev(kmsg_fd.get(), iov, 3));
123 }
124 }
125 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800126 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800127}
128
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700129#define MEMORY_BYTES_TO_DUMP 256
130#define MEMORY_BYTES_PER_LINE 16
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800131static_assert(MEMORY_BYTES_PER_LINE == kTagGranuleSize);
Kévin PETIT4bb47722013-12-18 16:44:24 +0000132
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800133ssize_t dump_memory(void* out, size_t len, uint8_t* tags, size_t tags_len, uint64_t* addr,
Josh Gao76e1e302021-01-26 15:53:11 -0800134 unwindstack::Memory* memory) {
Peter Collingbournefe8997a2020-07-20 15:08:52 -0700135 // Align the address to the number of bytes per line to avoid confusing memory tag output if
136 // memory is tagged and we start from a misaligned address. Start 32 bytes before the address.
Josh Gao76e1e302021-01-26 15:53:11 -0800137 *addr &= ~(MEMORY_BYTES_PER_LINE - 1);
138 if (*addr >= 4128) {
139 *addr -= 32;
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700140 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000141
Peter Collingbourneb1fcedb2020-07-21 16:29:02 -0700142 // We don't want the address tag to appear in the addresses in the memory dump.
Josh Gao76e1e302021-01-26 15:53:11 -0800143 *addr = untag_address(*addr);
Peter Collingbourne10e428d2020-07-17 14:49:31 -0700144
Peter Collingbourneb1fcedb2020-07-21 16:29:02 -0700145 // Don't bother if the address would overflow, taking tag bits into account. Note that
146 // untag_address truncates to 32 bits on 32-bit platforms as a side effect of returning a
147 // uintptr_t, so this also checks for 32-bit overflow.
Josh Gao76e1e302021-01-26 15:53:11 -0800148 if (untag_address(*addr + MEMORY_BYTES_TO_DUMP - 1) < *addr) {
149 return -1;
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700150 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000151
Josh Gao76e1e302021-01-26 15:53:11 -0800152 memset(out, 0, len);
153
154 size_t bytes = memory->Read(*addr, reinterpret_cast<uint8_t*>(out), len);
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700155 if (bytes % sizeof(uintptr_t) != 0) {
156 // This should never happen, but just in case.
157 ALOGE("Bytes read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t));
158 bytes &= ~(sizeof(uintptr_t) - 1);
159 }
160
Christopher Ferris456abba2015-07-09 15:35:47 -0700161 bool skip_2nd_read = false;
162 if (bytes == 0) {
163 // In this case, we might want to try another read at the beginning of
164 // the next page only if it's within the amount of memory we would have
165 // read.
166 size_t page_size = sysconf(_SC_PAGE_SIZE);
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800167 uint64_t next_page = (*addr + (page_size - 1)) & ~(page_size - 1);
168 if (next_page == *addr || next_page >= *addr + len) {
Christopher Ferris456abba2015-07-09 15:35:47 -0700169 skip_2nd_read = true;
170 }
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800171 *addr = next_page;
Christopher Ferris456abba2015-07-09 15:35:47 -0700172 }
173
Josh Gao76e1e302021-01-26 15:53:11 -0800174 if (bytes < len && !skip_2nd_read) {
Christopher Ferris456abba2015-07-09 15:35:47 -0700175 // Try to do one more read. This could happen if a read crosses a map,
176 // but the maps do not have any break between them. Or it could happen
177 // if reading from an unreadable map, but the read would cross back
178 // into a readable map. Only requires one extra read because a map has
179 // to contain at least one page, and the total number of bytes to dump
180 // is smaller than a page.
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800181 size_t bytes2 = memory->Read(*addr + bytes, static_cast<uint8_t*>(out) + bytes, len - bytes);
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700182 bytes += bytes2;
183 if (bytes2 > 0 && bytes % sizeof(uintptr_t) != 0) {
184 // This should never happen, but we'll try and continue any way.
185 ALOGE("Bytes after second read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t));
186 bytes &= ~(sizeof(uintptr_t) - 1);
Kévin PETIT4bb47722013-12-18 16:44:24 +0000187 }
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700188 }
189
Peter Collingbourneb1fcedb2020-07-21 16:29:02 -0700190 // If we were unable to read anything, it probably means that the register doesn't contain a
Josh Gao76e1e302021-01-26 15:53:11 -0800191 // valid pointer.
Peter Collingbourneb1fcedb2020-07-21 16:29:02 -0700192 if (bytes == 0) {
Josh Gao76e1e302021-01-26 15:53:11 -0800193 return -1;
194 }
195
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800196 for (uint64_t tag_granule = 0; tag_granule < bytes / kTagGranuleSize; ++tag_granule) {
197 long tag = memory->ReadTag(*addr + kTagGranuleSize * tag_granule);
198 if (tag_granule < tags_len) {
199 tags[tag_granule] = tag >= 0 ? tag : 0;
200 } else {
201 ALOGE("Insufficient space for tags");
202 }
203 }
204
Josh Gao76e1e302021-01-26 15:53:11 -0800205 return bytes;
206}
207
208void dump_memory(log_t* log, unwindstack::Memory* memory, uint64_t addr, const std::string& label) {
209 // Dump 256 bytes
210 uintptr_t data[MEMORY_BYTES_TO_DUMP / sizeof(uintptr_t)];
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800211 uint8_t tags[MEMORY_BYTES_TO_DUMP / kTagGranuleSize];
Josh Gao76e1e302021-01-26 15:53:11 -0800212
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800213 ssize_t bytes = dump_memory(data, sizeof(data), tags, sizeof(tags), &addr, memory);
Josh Gao76e1e302021-01-26 15:53:11 -0800214 if (bytes == -1) {
Peter Collingbourneb1fcedb2020-07-21 16:29:02 -0700215 return;
216 }
217
218 _LOG(log, logtype::MEMORY, "\n%s:\n", label.c_str());
219
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700220 // Dump the code around memory as:
221 // addr contents ascii
222 // 0000000000008d34 ef000000e8bd0090 e1b00000512fff1e ............../Q
223 // 0000000000008d44 ea00b1f9e92d0090 e3a070fcef000000 ......-..p......
224 // On 32-bit machines, there are still 16 bytes per line but addresses and
225 // words are of course presented differently.
226 uintptr_t* data_ptr = data;
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800227 uint8_t* tags_ptr = tags;
228 for (size_t line = 0; line < static_cast<size_t>(bytes) / MEMORY_BYTES_PER_LINE; line++) {
229 uint64_t tagged_addr = addr | static_cast<uint64_t>(*tags_ptr++) << 56;
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700230 std::string logline;
Peter Collingbournefe8997a2020-07-20 15:08:52 -0700231 android::base::StringAppendF(&logline, " %" PRIPTR, tagged_addr);
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700232
233 addr += MEMORY_BYTES_PER_LINE;
234 std::string ascii;
Christopher Ferris456abba2015-07-09 15:35:47 -0700235 for (size_t i = 0; i < MEMORY_BYTES_PER_LINE / sizeof(uintptr_t); i++) {
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800236 android::base::StringAppendF(&logline, " %" PRIPTR, static_cast<uint64_t>(*data_ptr));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700237
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800238 // Fill out the ascii string from the data.
239 uint8_t* ptr = reinterpret_cast<uint8_t*>(data_ptr);
240 for (size_t val = 0; val < sizeof(uintptr_t); val++, ptr++) {
241 if (*ptr >= 0x20 && *ptr < 0x7f) {
242 ascii += *ptr;
243 } else {
244 ascii += '.';
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700245 }
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700246 }
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800247 data_ptr++;
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700248 }
249 _LOG(log, logtype::MEMORY, "%s %s\n", logline.c_str(), ascii.c_str());
250 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000251}
Josh Gao57f58f82017-03-15 23:23:22 -0700252
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700253void drop_capabilities() {
254 __user_cap_header_struct capheader;
255 memset(&capheader, 0, sizeof(capheader));
256 capheader.version = _LINUX_CAPABILITY_VERSION_3;
257 capheader.pid = 0;
258
259 __user_cap_data_struct capdata[2];
260 memset(&capdata, 0, sizeof(capdata));
261
262 if (capset(&capheader, &capdata[0]) == -1) {
Josh Gao618cea32021-01-26 17:45:43 -0800263 async_safe_fatal("failed to drop capabilities: %s", strerror(errno));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700264 }
265
266 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) {
Josh Gao618cea32021-01-26 17:45:43 -0800267 async_safe_fatal("failed to set PR_SET_NO_NEW_PRIVS: %s", strerror(errno));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700268 }
269}
270
Elliott Hughes70d8f282018-04-25 17:00:14 -0700271bool signal_has_si_addr(const siginfo_t* si) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700272 // Manually sent signals won't have si_addr.
Elliott Hughes70d8f282018-04-25 17:00:14 -0700273 if (si->si_code == SI_USER || si->si_code == SI_QUEUE || si->si_code == SI_TKILL) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700274 return false;
275 }
276
Elliott Hughes70d8f282018-04-25 17:00:14 -0700277 switch (si->si_signo) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700278 case SIGBUS:
279 case SIGFPE:
280 case SIGILL:
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700281 case SIGTRAP:
282 return true;
Peter Collingbourne773acaa2021-11-10 16:29:23 -0800283 case SIGSEGV:
284 return si->si_code != SEGV_MTEAERR;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700285 default:
286 return false;
287 }
288}
289
Elliott Hughes70d8f282018-04-25 17:00:14 -0700290bool signal_has_sender(const siginfo_t* si, pid_t caller_pid) {
291 return SI_FROMUSER(si) && (si->si_pid != 0) && (si->si_pid != caller_pid);
292}
293
294void get_signal_sender(char* buf, size_t n, const siginfo_t* si) {
295 snprintf(buf, n, " from pid %d, uid %d", si->si_pid, si->si_uid);
296}
297
298const char* get_signame(const siginfo_t* si) {
299 switch (si->si_signo) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700300 case SIGABRT: return "SIGABRT";
301 case SIGBUS: return "SIGBUS";
302 case SIGFPE: return "SIGFPE";
303 case SIGILL: return "SIGILL";
304 case SIGSEGV: return "SIGSEGV";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700305 case SIGSTKFLT: return "SIGSTKFLT";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700306 case SIGSTOP: return "SIGSTOP";
307 case SIGSYS: return "SIGSYS";
308 case SIGTRAP: return "SIGTRAP";
Josh Gaoa48b41b2019-12-13 14:11:04 -0800309 case BIONIC_SIGNAL_DEBUGGER:
310 return "<debuggerd signal>";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700311 default: return "?";
312 }
313}
314
Elliott Hughes70d8f282018-04-25 17:00:14 -0700315const char* get_sigcode(const siginfo_t* si) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700316 // Try the signal-specific codes...
Elliott Hughes70d8f282018-04-25 17:00:14 -0700317 switch (si->si_signo) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700318 case SIGILL:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700319 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700320 case ILL_ILLOPC: return "ILL_ILLOPC";
321 case ILL_ILLOPN: return "ILL_ILLOPN";
322 case ILL_ILLADR: return "ILL_ILLADR";
323 case ILL_ILLTRP: return "ILL_ILLTRP";
324 case ILL_PRVOPC: return "ILL_PRVOPC";
325 case ILL_PRVREG: return "ILL_PRVREG";
326 case ILL_COPROC: return "ILL_COPROC";
327 case ILL_BADSTK: return "ILL_BADSTK";
Christopher Ferris432791e2018-06-27 15:06:01 -0700328 case ILL_BADIADDR:
329 return "ILL_BADIADDR";
330 case __ILL_BREAK:
331 return "ILL_BREAK";
332 case __ILL_BNDMOD:
333 return "ILL_BNDMOD";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700334 }
Christopher Ferris432791e2018-06-27 15:06:01 -0700335 static_assert(NSIGILL == __ILL_BNDMOD, "missing ILL_* si_code");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700336 break;
337 case SIGBUS:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700338 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700339 case BUS_ADRALN: return "BUS_ADRALN";
340 case BUS_ADRERR: return "BUS_ADRERR";
341 case BUS_OBJERR: return "BUS_OBJERR";
342 case BUS_MCEERR_AR: return "BUS_MCEERR_AR";
343 case BUS_MCEERR_AO: return "BUS_MCEERR_AO";
344 }
345 static_assert(NSIGBUS == BUS_MCEERR_AO, "missing BUS_* si_code");
346 break;
347 case SIGFPE:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700348 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700349 case FPE_INTDIV: return "FPE_INTDIV";
350 case FPE_INTOVF: return "FPE_INTOVF";
351 case FPE_FLTDIV: return "FPE_FLTDIV";
352 case FPE_FLTOVF: return "FPE_FLTOVF";
353 case FPE_FLTUND: return "FPE_FLTUND";
354 case FPE_FLTRES: return "FPE_FLTRES";
355 case FPE_FLTINV: return "FPE_FLTINV";
356 case FPE_FLTSUB: return "FPE_FLTSUB";
Christopher Ferris432791e2018-06-27 15:06:01 -0700357 case __FPE_DECOVF:
358 return "FPE_DECOVF";
359 case __FPE_DECDIV:
360 return "FPE_DECDIV";
361 case __FPE_DECERR:
362 return "FPE_DECERR";
363 case __FPE_INVASC:
364 return "FPE_INVASC";
365 case __FPE_INVDEC:
366 return "FPE_INVDEC";
367 case FPE_FLTUNK:
368 return "FPE_FLTUNK";
369 case FPE_CONDTRAP:
370 return "FPE_CONDTRAP";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700371 }
Christopher Ferris432791e2018-06-27 15:06:01 -0700372 static_assert(NSIGFPE == FPE_CONDTRAP, "missing FPE_* si_code");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700373 break;
374 case SIGSEGV:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700375 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700376 case SEGV_MAPERR: return "SEGV_MAPERR";
377 case SEGV_ACCERR: return "SEGV_ACCERR";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700378 case SEGV_BNDERR: return "SEGV_BNDERR";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700379 case SEGV_PKUERR: return "SEGV_PKUERR";
Christopher Ferris432791e2018-06-27 15:06:01 -0700380 case SEGV_ACCADI:
381 return "SEGV_ACCADI";
382 case SEGV_ADIDERR:
383 return "SEGV_ADIDERR";
384 case SEGV_ADIPERR:
385 return "SEGV_ADIPERR";
Peter Collingbournef8622522020-04-07 14:07:32 -0700386 case SEGV_MTEAERR:
387 return "SEGV_MTEAERR";
388 case SEGV_MTESERR:
389 return "SEGV_MTESERR";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700390 }
Christopher Ferris6ddc2c52020-12-14 20:53:52 -0800391 static_assert(NSIGSEGV == SEGV_MTESERR, "missing SEGV_* si_code");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700392 break;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700393 case SIGSYS:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700394 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700395 case SYS_SECCOMP: return "SYS_SECCOMP";
Christopher Ferris95b026f2021-02-18 11:44:10 -0800396 case SYS_USER_DISPATCH:
397 return "SYS_USER_DISPATCH";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700398 }
Christopher Ferris95b026f2021-02-18 11:44:10 -0800399 static_assert(NSIGSYS == SYS_USER_DISPATCH, "missing SYS_* si_code");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700400 break;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700401 case SIGTRAP:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700402 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700403 case TRAP_BRKPT: return "TRAP_BRKPT";
404 case TRAP_TRACE: return "TRAP_TRACE";
405 case TRAP_BRANCH: return "TRAP_BRANCH";
406 case TRAP_HWBKPT: return "TRAP_HWBKPT";
Christopher Ferris461baeb2018-10-26 11:22:40 -0700407 case TRAP_UNK:
408 return "TRAP_UNDIAGNOSED";
Christopher Ferrisc31ccb42021-07-01 01:43:21 +0000409 case TRAP_PERF:
410 return "TRAP_PERF";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700411 }
Elliott Hughes70d8f282018-04-25 17:00:14 -0700412 if ((si->si_code & 0xff) == SIGTRAP) {
413 switch ((si->si_code >> 8) & 0xff) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700414 case PTRACE_EVENT_FORK:
415 return "PTRACE_EVENT_FORK";
416 case PTRACE_EVENT_VFORK:
417 return "PTRACE_EVENT_VFORK";
418 case PTRACE_EVENT_CLONE:
419 return "PTRACE_EVENT_CLONE";
420 case PTRACE_EVENT_EXEC:
421 return "PTRACE_EVENT_EXEC";
422 case PTRACE_EVENT_VFORK_DONE:
423 return "PTRACE_EVENT_VFORK_DONE";
424 case PTRACE_EVENT_EXIT:
425 return "PTRACE_EVENT_EXIT";
426 case PTRACE_EVENT_SECCOMP:
427 return "PTRACE_EVENT_SECCOMP";
428 case PTRACE_EVENT_STOP:
429 return "PTRACE_EVENT_STOP";
430 }
431 }
Christopher Ferrisc31ccb42021-07-01 01:43:21 +0000432 static_assert(NSIGTRAP == TRAP_PERF, "missing TRAP_* si_code");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700433 break;
434 }
435 // Then the other codes...
Elliott Hughes70d8f282018-04-25 17:00:14 -0700436 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700437 case SI_USER: return "SI_USER";
438 case SI_KERNEL: return "SI_KERNEL";
439 case SI_QUEUE: return "SI_QUEUE";
440 case SI_TIMER: return "SI_TIMER";
441 case SI_MESGQ: return "SI_MESGQ";
442 case SI_ASYNCIO: return "SI_ASYNCIO";
443 case SI_SIGIO: return "SI_SIGIO";
444 case SI_TKILL: return "SI_TKILL";
445 case SI_DETHREAD: return "SI_DETHREAD";
446 }
447 // Then give up...
448 return "?";
449}
Christopher Ferris4ae266c2019-04-03 09:27:12 -0700450
Elliott Hughesd13ea522022-01-13 09:20:26 -0800451#define DESCRIBE_FLAG(flag) \
452 if (value & flag) { \
453 desc += ", "; \
454 desc += #flag; \
455 value &= ~flag; \
456 }
457
458static std::string describe_end(long value, std::string& desc) {
459 if (value) {
460 desc += StringPrintf(", unknown 0x%lx", value);
461 }
462 return desc.empty() ? "" : " (" + desc.substr(2) + ")";
463}
464
465std::string describe_tagged_addr_ctrl(long value) {
Peter Collingbourne47d784e2021-11-05 18:40:52 -0700466 std::string desc;
Elliott Hughesd13ea522022-01-13 09:20:26 -0800467 DESCRIBE_FLAG(PR_TAGGED_ADDR_ENABLE);
468 DESCRIBE_FLAG(PR_MTE_TCF_SYNC);
469 DESCRIBE_FLAG(PR_MTE_TCF_ASYNC);
470 if (value & PR_MTE_TAG_MASK) {
471 desc += StringPrintf(", mask 0x%04lx", (value & PR_MTE_TAG_MASK) >> PR_MTE_TAG_SHIFT);
472 value &= ~PR_MTE_TAG_MASK;
Peter Collingbourne47d784e2021-11-05 18:40:52 -0700473 }
Elliott Hughesd13ea522022-01-13 09:20:26 -0800474 return describe_end(value, desc);
475}
476
477std::string describe_pac_enabled_keys(long value) {
478 std::string desc;
479 DESCRIBE_FLAG(PR_PAC_APIAKEY);
480 DESCRIBE_FLAG(PR_PAC_APIBKEY);
481 DESCRIBE_FLAG(PR_PAC_APDAKEY);
482 DESCRIBE_FLAG(PR_PAC_APDBKEY);
483 DESCRIBE_FLAG(PR_PAC_APGAKEY);
484 return describe_end(value, desc);
Peter Collingbourne47d784e2021-11-05 18:40:52 -0700485}
486
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700487void log_backtrace(log_t* log, unwindstack::AndroidUnwinder* unwinder,
488 unwindstack::AndroidUnwinderData& data, const char* prefix) {
Christopher Ferrisc95047d2022-03-14 15:02:11 -0700489 std::set<std::string> unreadable_elf_files;
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700490 for (const auto& frame : data.frames) {
Christopher Ferrisc95047d2022-03-14 15:02:11 -0700491 if (frame.map_info != nullptr && frame.map_info->ElfFileNotReadable()) {
492 unreadable_elf_files.emplace(frame.map_info->name());
493 }
494 }
495
496 // Put the preamble ahead of the backtrace.
497 if (!unreadable_elf_files.empty()) {
Christopher Ferris4ae266c2019-04-03 09:27:12 -0700498 _LOG(log, logtype::BACKTRACE,
499 "%sNOTE: Function names and BuildId information is missing for some frames due\n", prefix);
500 _LOG(log, logtype::BACKTRACE,
501 "%sNOTE: to unreadable libraries. For unwinds of apps, only shared libraries\n", prefix);
502 _LOG(log, logtype::BACKTRACE, "%sNOTE: found under the lib/ directory are readable.\n", prefix);
503#if defined(ROOT_POSSIBLE)
504 _LOG(log, logtype::BACKTRACE,
505 "%sNOTE: On this device, run setenforce 0 to make the libraries readable.\n", prefix);
506#endif
Christopher Ferrisc95047d2022-03-14 15:02:11 -0700507 _LOG(log, logtype::BACKTRACE, "%sNOTE: Unreadable libraries:\n", prefix);
508 for (auto& name : unreadable_elf_files) {
509 _LOG(log, logtype::BACKTRACE, "%sNOTE: %s\n", prefix, name.c_str());
510 }
Christopher Ferris4ae266c2019-04-03 09:27:12 -0700511 }
512
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700513 for (const auto& frame : data.frames) {
Christopher Ferrisc95047d2022-03-14 15:02:11 -0700514 _LOG(log, logtype::BACKTRACE, "%s%s\n", prefix, unwinder->FormatFrame(frame).c_str());
Christopher Ferris4ae266c2019-04-03 09:27:12 -0700515 }
516}