blob: d7067ca6e5d22d0c8a9c699586249e4df1d3832c [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 -070047bool is_allowed_in_logcat(enum logtype ltype) {
Christopher Ferrisb36b5922015-06-17 18:35:59 -070048 if ((ltype == HEADER)
Brigid Smith62ba4892014-06-10 11:53:08 -070049 || (ltype == REGISTERS)
50 || (ltype == BACKTRACE)) {
51 return true;
52 }
53 return false;
54}
55
Josh Gaobf2dd482017-03-28 13:07:15 -070056static bool should_write_to_kmsg() {
57 // Write to kmsg if tombstoned isn't up, and we're able to do so.
58 if (!android::base::GetBoolProperty("ro.debuggable", false)) {
59 return false;
60 }
61
62 if (android::base::GetProperty("init.svc.tombstoned", "") == "running") {
63 return false;
64 }
65
66 return true;
67}
68
Chenjie Luo97258aa2017-03-06 12:12:07 -080069__attribute__((__weak__, visibility("default")))
Brigid Smith62ba4892014-06-10 11:53:08 -070070void _LOG(log_t* log, enum logtype ltype, const char* fmt, ...) {
Mitch Phillipsaadebd82019-11-18 15:17:18 -080071 va_list ap;
72 va_start(ap, fmt);
73 _VLOG(log, ltype, fmt, ap);
74 va_end(ap);
75}
76
77__attribute__((__weak__, visibility("default")))
78void _VLOG(log_t* log, enum logtype ltype, const char* fmt, va_list ap) {
Brigid Smith50eb5462014-06-18 14:17:57 -070079 bool write_to_tombstone = (log->tfd != -1);
80 bool write_to_logcat = is_allowed_in_logcat(ltype)
Brigid Smithc75a02f2014-07-17 14:52:33 -070081 && log->crashed_tid != -1
82 && log->current_tid != -1
Brigid Smith50eb5462014-06-18 14:17:57 -070083 && (log->crashed_tid == log->current_tid);
Josh Gaobf2dd482017-03-28 13:07:15 -070084 static bool write_to_kmsg = should_write_to_kmsg();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085
Josh Gao1cc7bd82018-02-13 13:16:17 -080086 std::string msg;
Josh Gao1cc7bd82018-02-13 13:16:17 -080087 android::base::StringAppendV(&msg, fmt, ap);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088
Josh Gao1cc7bd82018-02-13 13:16:17 -080089 if (msg.empty()) return;
Christopher Ferris20303f82014-01-10 16:33:16 -080090
Brigid Smith62ba4892014-06-10 11:53:08 -070091 if (write_to_tombstone) {
Josh Gao1cc7bd82018-02-13 13:16:17 -080092 TEMP_FAILURE_RETRY(write(log->tfd, msg.c_str(), msg.size()));
Christopher Ferris20303f82014-01-10 16:33:16 -080093 }
94
Brigid Smith62ba4892014-06-10 11:53:08 -070095 if (write_to_logcat) {
Josh Gao1cc7bd82018-02-13 13:16:17 -080096 __android_log_buf_write(LOG_ID_CRASH, ANDROID_LOG_FATAL, LOG_TAG, msg.c_str());
Christopher Ferris99235e92016-05-03 16:32:13 -070097 if (log->amfd_data != nullptr) {
Josh Gao1cc7bd82018-02-13 13:16:17 -080098 *log->amfd_data += msg;
Christopher Tateded2e5a2013-03-19 13:12:23 -070099 }
Josh Gaobf2dd482017-03-28 13:07:15 -0700100
101 if (write_to_kmsg) {
102 unique_fd kmsg_fd(open("/dev/kmsg_debug", O_WRONLY | O_APPEND | O_CLOEXEC));
103 if (kmsg_fd.get() >= 0) {
104 // Our output might contain newlines which would otherwise be handled by the android logger.
105 // Split the lines up ourselves before sending to the kernel logger.
Josh Gao1cc7bd82018-02-13 13:16:17 -0800106 if (msg.back() == '\n') {
107 msg.back() = '\0';
Josh Gaobf2dd482017-03-28 13:07:15 -0700108 }
109
Josh Gao1cc7bd82018-02-13 13:16:17 -0800110 std::vector<std::string> fragments = android::base::Split(msg, "\n");
Josh Gaobf2dd482017-03-28 13:07:15 -0700111 for (const std::string& fragment : fragments) {
112 static constexpr char prefix[] = "<3>DEBUG: ";
113 struct iovec iov[3];
114 iov[0].iov_base = const_cast<char*>(prefix);
115 iov[0].iov_len = strlen(prefix);
116 iov[1].iov_base = const_cast<char*>(fragment.c_str());
117 iov[1].iov_len = fragment.length();
118 iov[2].iov_base = const_cast<char*>("\n");
119 iov[2].iov_len = 1;
120 TEMP_FAILURE_RETRY(writev(kmsg_fd.get(), iov, 3));
121 }
122 }
123 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800124 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125}
126
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700127#define MEMORY_BYTES_TO_DUMP 256
128#define MEMORY_BYTES_PER_LINE 16
Kévin PETIT4bb47722013-12-18 16:44:24 +0000129
Elliott Hughese1415a52018-02-15 09:18:21 -0800130void dump_memory(log_t* log, unwindstack::Memory* memory, uint64_t addr, const std::string& label) {
Peter Collingbournefe8997a2020-07-20 15:08:52 -0700131 // Align the address to the number of bytes per line to avoid confusing memory tag output if
132 // memory is tagged and we start from a misaligned address. Start 32 bytes before the address.
133 addr &= ~(MEMORY_BYTES_PER_LINE - 1);
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700134 if (addr >= 4128) {
135 addr -= 32;
136 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000137
Peter Collingbourneb1fcedb2020-07-21 16:29:02 -0700138 // We don't want the address tag to appear in the addresses in the memory dump.
Peter Collingbourne10e428d2020-07-17 14:49:31 -0700139 addr = untag_address(addr);
140
Peter Collingbourneb1fcedb2020-07-21 16:29:02 -0700141 // Don't bother if the address would overflow, taking tag bits into account. Note that
142 // untag_address truncates to 32 bits on 32-bit platforms as a side effect of returning a
143 // uintptr_t, so this also checks for 32-bit overflow.
144 if (untag_address(addr + MEMORY_BYTES_TO_DUMP - 1) < addr) {
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700145 return;
146 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000147
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700148 // Dump 256 bytes
149 uintptr_t data[MEMORY_BYTES_TO_DUMP/sizeof(uintptr_t)];
150 memset(data, 0, MEMORY_BYTES_TO_DUMP);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700151 size_t bytes = memory->Read(addr, reinterpret_cast<uint8_t*>(data), sizeof(data));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700152 if (bytes % sizeof(uintptr_t) != 0) {
153 // This should never happen, but just in case.
154 ALOGE("Bytes read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t));
155 bytes &= ~(sizeof(uintptr_t) - 1);
156 }
157
Christopher Ferris7937a362018-01-18 11:15:49 -0800158 uint64_t start = 0;
Christopher Ferris456abba2015-07-09 15:35:47 -0700159 bool skip_2nd_read = false;
160 if (bytes == 0) {
161 // In this case, we might want to try another read at the beginning of
162 // the next page only if it's within the amount of memory we would have
163 // read.
164 size_t page_size = sysconf(_SC_PAGE_SIZE);
165 start = ((addr + (page_size - 1)) & ~(page_size - 1)) - addr;
166 if (start == 0 || start >= MEMORY_BYTES_TO_DUMP) {
167 skip_2nd_read = true;
168 }
169 }
170
171 if (bytes < MEMORY_BYTES_TO_DUMP && !skip_2nd_read) {
172 // Try to do one more read. This could happen if a read crosses a map,
173 // but the maps do not have any break between them. Or it could happen
174 // if reading from an unreadable map, but the read would cross back
175 // into a readable map. Only requires one extra read because a map has
176 // to contain at least one page, and the total number of bytes to dump
177 // is smaller than a page.
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700178 size_t bytes2 = memory->Read(addr + start + bytes, reinterpret_cast<uint8_t*>(data) + bytes,
179 sizeof(data) - bytes - start);
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700180 bytes += bytes2;
181 if (bytes2 > 0 && bytes % sizeof(uintptr_t) != 0) {
182 // This should never happen, but we'll try and continue any way.
183 ALOGE("Bytes after second read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t));
184 bytes &= ~(sizeof(uintptr_t) - 1);
Kévin PETIT4bb47722013-12-18 16:44:24 +0000185 }
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700186 }
187
Peter Collingbourneb1fcedb2020-07-21 16:29:02 -0700188 // If we were unable to read anything, it probably means that the register doesn't contain a
189 // valid pointer. In that case, skip the output for this register entirely rather than emitting 16
190 // lines of dashes.
191 if (bytes == 0) {
192 return;
193 }
194
195 _LOG(log, logtype::MEMORY, "\n%s:\n", label.c_str());
196
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700197 // Dump the code around memory as:
198 // addr contents ascii
199 // 0000000000008d34 ef000000e8bd0090 e1b00000512fff1e ............../Q
200 // 0000000000008d44 ea00b1f9e92d0090 e3a070fcef000000 ......-..p......
201 // On 32-bit machines, there are still 16 bytes per line but addresses and
202 // words are of course presented differently.
203 uintptr_t* data_ptr = data;
Christopher Ferris456abba2015-07-09 15:35:47 -0700204 size_t current = 0;
205 size_t total_bytes = start + bytes;
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700206 for (size_t line = 0; line < MEMORY_BYTES_TO_DUMP / MEMORY_BYTES_PER_LINE; line++) {
Peter Collingbournefe8997a2020-07-20 15:08:52 -0700207 uint64_t tagged_addr = addr;
208 long tag = memory->ReadTag(addr);
209 if (tag >= 0) {
210 tagged_addr |= static_cast<uint64_t>(tag) << 56;
211 }
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700212 std::string logline;
Peter Collingbournefe8997a2020-07-20 15:08:52 -0700213 android::base::StringAppendF(&logline, " %" PRIPTR, tagged_addr);
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700214
215 addr += MEMORY_BYTES_PER_LINE;
216 std::string ascii;
Christopher Ferris456abba2015-07-09 15:35:47 -0700217 for (size_t i = 0; i < MEMORY_BYTES_PER_LINE / sizeof(uintptr_t); i++) {
218 if (current >= start && current + sizeof(uintptr_t) <= total_bytes) {
Christopher Ferris7937a362018-01-18 11:15:49 -0800219 android::base::StringAppendF(&logline, " %" PRIPTR, static_cast<uint64_t>(*data_ptr));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700220
221 // Fill out the ascii string from the data.
222 uint8_t* ptr = reinterpret_cast<uint8_t*>(data_ptr);
223 for (size_t val = 0; val < sizeof(uintptr_t); val++, ptr++) {
224 if (*ptr >= 0x20 && *ptr < 0x7f) {
225 ascii += *ptr;
226 } else {
227 ascii += '.';
228 }
229 }
Christopher Ferris456abba2015-07-09 15:35:47 -0700230 data_ptr++;
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700231 } else {
232 logline += ' ' + std::string(sizeof(uintptr_t) * 2, '-');
233 ascii += std::string(sizeof(uintptr_t), '.');
234 }
Christopher Ferris456abba2015-07-09 15:35:47 -0700235 current += sizeof(uintptr_t);
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700236 }
237 _LOG(log, logtype::MEMORY, "%s %s\n", logline.c_str(), ascii.c_str());
238 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000239}
Josh Gao57f58f82017-03-15 23:23:22 -0700240
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700241void drop_capabilities() {
242 __user_cap_header_struct capheader;
243 memset(&capheader, 0, sizeof(capheader));
244 capheader.version = _LINUX_CAPABILITY_VERSION_3;
245 capheader.pid = 0;
246
247 __user_cap_data_struct capdata[2];
248 memset(&capdata, 0, sizeof(capdata));
249
250 if (capset(&capheader, &capdata[0]) == -1) {
251 PLOG(FATAL) << "failed to drop capabilities";
252 }
253
254 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) {
255 PLOG(FATAL) << "failed to set PR_SET_NO_NEW_PRIVS";
256 }
257}
258
Elliott Hughes70d8f282018-04-25 17:00:14 -0700259bool signal_has_si_addr(const siginfo_t* si) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700260 // Manually sent signals won't have si_addr.
Elliott Hughes70d8f282018-04-25 17:00:14 -0700261 if (si->si_code == SI_USER || si->si_code == SI_QUEUE || si->si_code == SI_TKILL) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700262 return false;
263 }
264
Elliott Hughes70d8f282018-04-25 17:00:14 -0700265 switch (si->si_signo) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700266 case SIGBUS:
267 case SIGFPE:
268 case SIGILL:
269 case SIGSEGV:
270 case SIGTRAP:
271 return true;
272 default:
273 return false;
274 }
275}
276
Elliott Hughes70d8f282018-04-25 17:00:14 -0700277bool signal_has_sender(const siginfo_t* si, pid_t caller_pid) {
278 return SI_FROMUSER(si) && (si->si_pid != 0) && (si->si_pid != caller_pid);
279}
280
281void get_signal_sender(char* buf, size_t n, const siginfo_t* si) {
282 snprintf(buf, n, " from pid %d, uid %d", si->si_pid, si->si_uid);
283}
284
285const char* get_signame(const siginfo_t* si) {
286 switch (si->si_signo) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700287 case SIGABRT: return "SIGABRT";
288 case SIGBUS: return "SIGBUS";
289 case SIGFPE: return "SIGFPE";
290 case SIGILL: return "SIGILL";
291 case SIGSEGV: return "SIGSEGV";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700292 case SIGSTKFLT: return "SIGSTKFLT";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700293 case SIGSTOP: return "SIGSTOP";
294 case SIGSYS: return "SIGSYS";
295 case SIGTRAP: return "SIGTRAP";
Josh Gaoa48b41b2019-12-13 14:11:04 -0800296 case BIONIC_SIGNAL_DEBUGGER:
297 return "<debuggerd signal>";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700298 default: return "?";
299 }
300}
301
Elliott Hughes70d8f282018-04-25 17:00:14 -0700302const char* get_sigcode(const siginfo_t* si) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700303 // Try the signal-specific codes...
Elliott Hughes70d8f282018-04-25 17:00:14 -0700304 switch (si->si_signo) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700305 case SIGILL:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700306 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700307 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";
Christopher Ferris432791e2018-06-27 15:06:01 -0700315 case ILL_BADIADDR:
316 return "ILL_BADIADDR";
317 case __ILL_BREAK:
318 return "ILL_BREAK";
319 case __ILL_BNDMOD:
320 return "ILL_BNDMOD";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700321 }
Christopher Ferris432791e2018-06-27 15:06:01 -0700322 static_assert(NSIGILL == __ILL_BNDMOD, "missing ILL_* si_code");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700323 break;
324 case SIGBUS:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700325 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700326 case BUS_ADRALN: return "BUS_ADRALN";
327 case BUS_ADRERR: return "BUS_ADRERR";
328 case BUS_OBJERR: return "BUS_OBJERR";
329 case BUS_MCEERR_AR: return "BUS_MCEERR_AR";
330 case BUS_MCEERR_AO: return "BUS_MCEERR_AO";
331 }
332 static_assert(NSIGBUS == BUS_MCEERR_AO, "missing BUS_* si_code");
333 break;
334 case SIGFPE:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700335 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700336 case FPE_INTDIV: return "FPE_INTDIV";
337 case FPE_INTOVF: return "FPE_INTOVF";
338 case FPE_FLTDIV: return "FPE_FLTDIV";
339 case FPE_FLTOVF: return "FPE_FLTOVF";
340 case FPE_FLTUND: return "FPE_FLTUND";
341 case FPE_FLTRES: return "FPE_FLTRES";
342 case FPE_FLTINV: return "FPE_FLTINV";
343 case FPE_FLTSUB: return "FPE_FLTSUB";
Christopher Ferris432791e2018-06-27 15:06:01 -0700344 case __FPE_DECOVF:
345 return "FPE_DECOVF";
346 case __FPE_DECDIV:
347 return "FPE_DECDIV";
348 case __FPE_DECERR:
349 return "FPE_DECERR";
350 case __FPE_INVASC:
351 return "FPE_INVASC";
352 case __FPE_INVDEC:
353 return "FPE_INVDEC";
354 case FPE_FLTUNK:
355 return "FPE_FLTUNK";
356 case FPE_CONDTRAP:
357 return "FPE_CONDTRAP";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700358 }
Christopher Ferris432791e2018-06-27 15:06:01 -0700359 static_assert(NSIGFPE == FPE_CONDTRAP, "missing FPE_* si_code");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700360 break;
361 case SIGSEGV:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700362 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700363 case SEGV_MAPERR: return "SEGV_MAPERR";
364 case SEGV_ACCERR: return "SEGV_ACCERR";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700365 case SEGV_BNDERR: return "SEGV_BNDERR";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700366 case SEGV_PKUERR: return "SEGV_PKUERR";
Christopher Ferris432791e2018-06-27 15:06:01 -0700367 case SEGV_ACCADI:
368 return "SEGV_ACCADI";
369 case SEGV_ADIDERR:
370 return "SEGV_ADIDERR";
371 case SEGV_ADIPERR:
372 return "SEGV_ADIPERR";
Peter Collingbournef8622522020-04-07 14:07:32 -0700373#if defined(ANDROID_EXPERIMENTAL_MTE)
374 case SEGV_MTEAERR:
375 return "SEGV_MTEAERR";
376 case SEGV_MTESERR:
377 return "SEGV_MTESERR";
378#endif
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700379 }
Christopher Ferris432791e2018-06-27 15:06:01 -0700380 static_assert(NSIGSEGV == SEGV_ADIPERR, "missing SEGV_* si_code");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700381 break;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700382 case SIGSYS:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700383 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700384 case SYS_SECCOMP: return "SYS_SECCOMP";
385 }
386 static_assert(NSIGSYS == SYS_SECCOMP, "missing SYS_* si_code");
387 break;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700388 case SIGTRAP:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700389 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700390 case TRAP_BRKPT: return "TRAP_BRKPT";
391 case TRAP_TRACE: return "TRAP_TRACE";
392 case TRAP_BRANCH: return "TRAP_BRANCH";
393 case TRAP_HWBKPT: return "TRAP_HWBKPT";
Christopher Ferris461baeb2018-10-26 11:22:40 -0700394 case TRAP_UNK:
395 return "TRAP_UNDIAGNOSED";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700396 }
Elliott Hughes70d8f282018-04-25 17:00:14 -0700397 if ((si->si_code & 0xff) == SIGTRAP) {
398 switch ((si->si_code >> 8) & 0xff) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700399 case PTRACE_EVENT_FORK:
400 return "PTRACE_EVENT_FORK";
401 case PTRACE_EVENT_VFORK:
402 return "PTRACE_EVENT_VFORK";
403 case PTRACE_EVENT_CLONE:
404 return "PTRACE_EVENT_CLONE";
405 case PTRACE_EVENT_EXEC:
406 return "PTRACE_EVENT_EXEC";
407 case PTRACE_EVENT_VFORK_DONE:
408 return "PTRACE_EVENT_VFORK_DONE";
409 case PTRACE_EVENT_EXIT:
410 return "PTRACE_EVENT_EXIT";
411 case PTRACE_EVENT_SECCOMP:
412 return "PTRACE_EVENT_SECCOMP";
413 case PTRACE_EVENT_STOP:
414 return "PTRACE_EVENT_STOP";
415 }
416 }
Christopher Ferris461baeb2018-10-26 11:22:40 -0700417 static_assert(NSIGTRAP == TRAP_UNK, "missing TRAP_* si_code");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700418 break;
419 }
420 // Then the other codes...
Elliott Hughes70d8f282018-04-25 17:00:14 -0700421 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700422 case SI_USER: return "SI_USER";
423 case SI_KERNEL: return "SI_KERNEL";
424 case SI_QUEUE: return "SI_QUEUE";
425 case SI_TIMER: return "SI_TIMER";
426 case SI_MESGQ: return "SI_MESGQ";
427 case SI_ASYNCIO: return "SI_ASYNCIO";
428 case SI_SIGIO: return "SI_SIGIO";
429 case SI_TKILL: return "SI_TKILL";
430 case SI_DETHREAD: return "SI_DETHREAD";
431 }
432 // Then give up...
433 return "?";
434}
Christopher Ferris4ae266c2019-04-03 09:27:12 -0700435
436void log_backtrace(log_t* log, unwindstack::Unwinder* unwinder, const char* prefix) {
437 if (unwinder->elf_from_memory_not_file()) {
438 _LOG(log, logtype::BACKTRACE,
439 "%sNOTE: Function names and BuildId information is missing for some frames due\n", prefix);
440 _LOG(log, logtype::BACKTRACE,
441 "%sNOTE: to unreadable libraries. For unwinds of apps, only shared libraries\n", prefix);
442 _LOG(log, logtype::BACKTRACE, "%sNOTE: found under the lib/ directory are readable.\n", prefix);
443#if defined(ROOT_POSSIBLE)
444 _LOG(log, logtype::BACKTRACE,
445 "%sNOTE: On this device, run setenforce 0 to make the libraries readable.\n", prefix);
446#endif
447 }
448
449 unwinder->SetDisplayBuildID(true);
450 for (size_t i = 0; i < unwinder->NumFrames(); i++) {
451 _LOG(log, logtype::BACKTRACE, "%s%s\n", prefix, unwinder->FormatFrame(i).c_str());
452 }
453}