blob: 0a1d2a4da2b99d600e91a8164c40951c0c78e347 [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>
Josh Gaoa48b41b2019-12-13 14:11:04 -080038#include <bionic/reserved_signals.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>
Christopher Ferris4ae266c2019-04-03 09:27:12 -070042#include <unwindstack/Unwinder.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043
Josh Gaobf2dd482017-03-28 13:07:15 -070044using android::base::unique_fd;
45
Brigid Smith62ba4892014-06-10 11:53:08 -070046// Whitelist output desired in the logcat output.
47bool 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) {
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700131 // Align the address to sizeof(long) and start 32 bytes before the address.
132 addr &= ~(sizeof(long) - 1);
133 if (addr >= 4128) {
134 addr -= 32;
135 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000136
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700137 // Don't bother if the address looks too low, or looks too high.
138 if (addr < 4096 ||
139#if defined(__LP64__)
140 addr > 0x4000000000000000UL - MEMORY_BYTES_TO_DUMP) {
Kévin PETIT4bb47722013-12-18 16:44:24 +0000141#else
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700142 addr > 0xffff0000 - MEMORY_BYTES_TO_DUMP) {
Kévin PETIT4bb47722013-12-18 16:44:24 +0000143#endif
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700144 return;
145 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000146
Elliott Hughese1415a52018-02-15 09:18:21 -0800147 _LOG(log, logtype::MEMORY, "\n%s:\n", label.c_str());
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700148
149 // Dump 256 bytes
150 uintptr_t data[MEMORY_BYTES_TO_DUMP/sizeof(uintptr_t)];
151 memset(data, 0, MEMORY_BYTES_TO_DUMP);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700152 size_t bytes = memory->Read(addr, reinterpret_cast<uint8_t*>(data), sizeof(data));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700153 if (bytes % sizeof(uintptr_t) != 0) {
154 // This should never happen, but just in case.
155 ALOGE("Bytes read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t));
156 bytes &= ~(sizeof(uintptr_t) - 1);
157 }
158
Christopher Ferris7937a362018-01-18 11:15:49 -0800159 uint64_t start = 0;
Christopher Ferris456abba2015-07-09 15:35:47 -0700160 bool skip_2nd_read = false;
161 if (bytes == 0) {
162 // In this case, we might want to try another read at the beginning of
163 // the next page only if it's within the amount of memory we would have
164 // read.
165 size_t page_size = sysconf(_SC_PAGE_SIZE);
166 start = ((addr + (page_size - 1)) & ~(page_size - 1)) - addr;
167 if (start == 0 || start >= MEMORY_BYTES_TO_DUMP) {
168 skip_2nd_read = true;
169 }
170 }
171
172 if (bytes < MEMORY_BYTES_TO_DUMP && !skip_2nd_read) {
173 // Try to do one more read. This could happen if a read crosses a map,
174 // but the maps do not have any break between them. Or it could happen
175 // if reading from an unreadable map, but the read would cross back
176 // into a readable map. Only requires one extra read because a map has
177 // to contain at least one page, and the total number of bytes to dump
178 // is smaller than a page.
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700179 size_t bytes2 = memory->Read(addr + start + bytes, reinterpret_cast<uint8_t*>(data) + bytes,
180 sizeof(data) - bytes - start);
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700181 bytes += bytes2;
182 if (bytes2 > 0 && bytes % sizeof(uintptr_t) != 0) {
183 // This should never happen, but we'll try and continue any way.
184 ALOGE("Bytes after second read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t));
185 bytes &= ~(sizeof(uintptr_t) - 1);
Kévin PETIT4bb47722013-12-18 16:44:24 +0000186 }
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700187 }
188
189 // Dump the code around memory as:
190 // addr contents ascii
191 // 0000000000008d34 ef000000e8bd0090 e1b00000512fff1e ............../Q
192 // 0000000000008d44 ea00b1f9e92d0090 e3a070fcef000000 ......-..p......
193 // On 32-bit machines, there are still 16 bytes per line but addresses and
194 // words are of course presented differently.
195 uintptr_t* data_ptr = data;
Christopher Ferris456abba2015-07-09 15:35:47 -0700196 size_t current = 0;
197 size_t total_bytes = start + bytes;
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700198 for (size_t line = 0; line < MEMORY_BYTES_TO_DUMP / MEMORY_BYTES_PER_LINE; line++) {
199 std::string logline;
200 android::base::StringAppendF(&logline, " %" PRIPTR, addr);
201
202 addr += MEMORY_BYTES_PER_LINE;
203 std::string ascii;
Christopher Ferris456abba2015-07-09 15:35:47 -0700204 for (size_t i = 0; i < MEMORY_BYTES_PER_LINE / sizeof(uintptr_t); i++) {
205 if (current >= start && current + sizeof(uintptr_t) <= total_bytes) {
Christopher Ferris7937a362018-01-18 11:15:49 -0800206 android::base::StringAppendF(&logline, " %" PRIPTR, static_cast<uint64_t>(*data_ptr));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700207
208 // Fill out the ascii string from the data.
209 uint8_t* ptr = reinterpret_cast<uint8_t*>(data_ptr);
210 for (size_t val = 0; val < sizeof(uintptr_t); val++, ptr++) {
211 if (*ptr >= 0x20 && *ptr < 0x7f) {
212 ascii += *ptr;
213 } else {
214 ascii += '.';
215 }
216 }
Christopher Ferris456abba2015-07-09 15:35:47 -0700217 data_ptr++;
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700218 } else {
219 logline += ' ' + std::string(sizeof(uintptr_t) * 2, '-');
220 ascii += std::string(sizeof(uintptr_t), '.');
221 }
Christopher Ferris456abba2015-07-09 15:35:47 -0700222 current += sizeof(uintptr_t);
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700223 }
224 _LOG(log, logtype::MEMORY, "%s %s\n", logline.c_str(), ascii.c_str());
225 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000226}
Josh Gao57f58f82017-03-15 23:23:22 -0700227
228void read_with_default(const char* path, char* buf, size_t len, const char* default_value) {
Josh Gaobf2dd482017-03-28 13:07:15 -0700229 unique_fd fd(open(path, O_RDONLY | O_CLOEXEC));
Josh Gao57f58f82017-03-15 23:23:22 -0700230 if (fd != -1) {
231 int rc = TEMP_FAILURE_RETRY(read(fd.get(), buf, len - 1));
232 if (rc != -1) {
233 buf[rc] = '\0';
234
235 // Trim trailing newlines.
236 if (rc > 0 && buf[rc - 1] == '\n') {
237 buf[rc - 1] = '\0';
238 }
239 return;
240 }
241 }
242 strcpy(buf, default_value);
243}
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700244
245void drop_capabilities() {
246 __user_cap_header_struct capheader;
247 memset(&capheader, 0, sizeof(capheader));
248 capheader.version = _LINUX_CAPABILITY_VERSION_3;
249 capheader.pid = 0;
250
251 __user_cap_data_struct capdata[2];
252 memset(&capdata, 0, sizeof(capdata));
253
254 if (capset(&capheader, &capdata[0]) == -1) {
255 PLOG(FATAL) << "failed to drop capabilities";
256 }
257
258 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) {
259 PLOG(FATAL) << "failed to set PR_SET_NO_NEW_PRIVS";
260 }
261}
262
Elliott Hughes70d8f282018-04-25 17:00:14 -0700263bool signal_has_si_addr(const siginfo_t* si) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700264 // Manually sent signals won't have si_addr.
Elliott Hughes70d8f282018-04-25 17:00:14 -0700265 if (si->si_code == SI_USER || si->si_code == SI_QUEUE || si->si_code == SI_TKILL) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700266 return false;
267 }
268
Elliott Hughes70d8f282018-04-25 17:00:14 -0700269 switch (si->si_signo) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700270 case SIGBUS:
271 case SIGFPE:
272 case SIGILL:
273 case SIGSEGV:
274 case SIGTRAP:
275 return true;
276 default:
277 return false;
278 }
279}
280
Elliott Hughes70d8f282018-04-25 17:00:14 -0700281bool signal_has_sender(const siginfo_t* si, pid_t caller_pid) {
282 return SI_FROMUSER(si) && (si->si_pid != 0) && (si->si_pid != caller_pid);
283}
284
285void get_signal_sender(char* buf, size_t n, const siginfo_t* si) {
286 snprintf(buf, n, " from pid %d, uid %d", si->si_pid, si->si_uid);
287}
288
289const char* get_signame(const siginfo_t* si) {
290 switch (si->si_signo) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700291 case SIGABRT: return "SIGABRT";
292 case SIGBUS: return "SIGBUS";
293 case SIGFPE: return "SIGFPE";
294 case SIGILL: return "SIGILL";
295 case SIGSEGV: return "SIGSEGV";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700296 case SIGSTKFLT: return "SIGSTKFLT";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700297 case SIGSTOP: return "SIGSTOP";
298 case SIGSYS: return "SIGSYS";
299 case SIGTRAP: return "SIGTRAP";
Josh Gaoa48b41b2019-12-13 14:11:04 -0800300 case BIONIC_SIGNAL_DEBUGGER:
301 return "<debuggerd signal>";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700302 default: return "?";
303 }
304}
305
Elliott Hughes70d8f282018-04-25 17:00:14 -0700306const char* get_sigcode(const siginfo_t* si) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700307 // Try the signal-specific codes...
Elliott Hughes70d8f282018-04-25 17:00:14 -0700308 switch (si->si_signo) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700309 case SIGILL:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700310 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700311 case ILL_ILLOPC: return "ILL_ILLOPC";
312 case ILL_ILLOPN: return "ILL_ILLOPN";
313 case ILL_ILLADR: return "ILL_ILLADR";
314 case ILL_ILLTRP: return "ILL_ILLTRP";
315 case ILL_PRVOPC: return "ILL_PRVOPC";
316 case ILL_PRVREG: return "ILL_PRVREG";
317 case ILL_COPROC: return "ILL_COPROC";
318 case ILL_BADSTK: return "ILL_BADSTK";
Christopher Ferris432791e2018-06-27 15:06:01 -0700319 case ILL_BADIADDR:
320 return "ILL_BADIADDR";
321 case __ILL_BREAK:
322 return "ILL_BREAK";
323 case __ILL_BNDMOD:
324 return "ILL_BNDMOD";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700325 }
Christopher Ferris432791e2018-06-27 15:06:01 -0700326 static_assert(NSIGILL == __ILL_BNDMOD, "missing ILL_* si_code");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700327 break;
328 case SIGBUS:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700329 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700330 case BUS_ADRALN: return "BUS_ADRALN";
331 case BUS_ADRERR: return "BUS_ADRERR";
332 case BUS_OBJERR: return "BUS_OBJERR";
333 case BUS_MCEERR_AR: return "BUS_MCEERR_AR";
334 case BUS_MCEERR_AO: return "BUS_MCEERR_AO";
335 }
336 static_assert(NSIGBUS == BUS_MCEERR_AO, "missing BUS_* si_code");
337 break;
338 case SIGFPE:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700339 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700340 case FPE_INTDIV: return "FPE_INTDIV";
341 case FPE_INTOVF: return "FPE_INTOVF";
342 case FPE_FLTDIV: return "FPE_FLTDIV";
343 case FPE_FLTOVF: return "FPE_FLTOVF";
344 case FPE_FLTUND: return "FPE_FLTUND";
345 case FPE_FLTRES: return "FPE_FLTRES";
346 case FPE_FLTINV: return "FPE_FLTINV";
347 case FPE_FLTSUB: return "FPE_FLTSUB";
Christopher Ferris432791e2018-06-27 15:06:01 -0700348 case __FPE_DECOVF:
349 return "FPE_DECOVF";
350 case __FPE_DECDIV:
351 return "FPE_DECDIV";
352 case __FPE_DECERR:
353 return "FPE_DECERR";
354 case __FPE_INVASC:
355 return "FPE_INVASC";
356 case __FPE_INVDEC:
357 return "FPE_INVDEC";
358 case FPE_FLTUNK:
359 return "FPE_FLTUNK";
360 case FPE_CONDTRAP:
361 return "FPE_CONDTRAP";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700362 }
Christopher Ferris432791e2018-06-27 15:06:01 -0700363 static_assert(NSIGFPE == FPE_CONDTRAP, "missing FPE_* si_code");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700364 break;
365 case SIGSEGV:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700366 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700367 case SEGV_MAPERR: return "SEGV_MAPERR";
368 case SEGV_ACCERR: return "SEGV_ACCERR";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700369 case SEGV_BNDERR: return "SEGV_BNDERR";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700370 case SEGV_PKUERR: return "SEGV_PKUERR";
Christopher Ferris432791e2018-06-27 15:06:01 -0700371 case SEGV_ACCADI:
372 return "SEGV_ACCADI";
373 case SEGV_ADIDERR:
374 return "SEGV_ADIDERR";
375 case SEGV_ADIPERR:
376 return "SEGV_ADIPERR";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700377 }
Christopher Ferris432791e2018-06-27 15:06:01 -0700378 static_assert(NSIGSEGV == SEGV_ADIPERR, "missing SEGV_* si_code");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700379 break;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700380 case SIGSYS:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700381 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700382 case SYS_SECCOMP: return "SYS_SECCOMP";
383 }
384 static_assert(NSIGSYS == SYS_SECCOMP, "missing SYS_* si_code");
385 break;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700386 case SIGTRAP:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700387 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700388 case TRAP_BRKPT: return "TRAP_BRKPT";
389 case TRAP_TRACE: return "TRAP_TRACE";
390 case TRAP_BRANCH: return "TRAP_BRANCH";
391 case TRAP_HWBKPT: return "TRAP_HWBKPT";
Christopher Ferris461baeb2018-10-26 11:22:40 -0700392 case TRAP_UNK:
393 return "TRAP_UNDIAGNOSED";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700394 }
Elliott Hughes70d8f282018-04-25 17:00:14 -0700395 if ((si->si_code & 0xff) == SIGTRAP) {
396 switch ((si->si_code >> 8) & 0xff) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700397 case PTRACE_EVENT_FORK:
398 return "PTRACE_EVENT_FORK";
399 case PTRACE_EVENT_VFORK:
400 return "PTRACE_EVENT_VFORK";
401 case PTRACE_EVENT_CLONE:
402 return "PTRACE_EVENT_CLONE";
403 case PTRACE_EVENT_EXEC:
404 return "PTRACE_EVENT_EXEC";
405 case PTRACE_EVENT_VFORK_DONE:
406 return "PTRACE_EVENT_VFORK_DONE";
407 case PTRACE_EVENT_EXIT:
408 return "PTRACE_EVENT_EXIT";
409 case PTRACE_EVENT_SECCOMP:
410 return "PTRACE_EVENT_SECCOMP";
411 case PTRACE_EVENT_STOP:
412 return "PTRACE_EVENT_STOP";
413 }
414 }
Christopher Ferris461baeb2018-10-26 11:22:40 -0700415 static_assert(NSIGTRAP == TRAP_UNK, "missing TRAP_* si_code");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700416 break;
417 }
418 // Then the other codes...
Elliott Hughes70d8f282018-04-25 17:00:14 -0700419 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700420 case SI_USER: return "SI_USER";
421 case SI_KERNEL: return "SI_KERNEL";
422 case SI_QUEUE: return "SI_QUEUE";
423 case SI_TIMER: return "SI_TIMER";
424 case SI_MESGQ: return "SI_MESGQ";
425 case SI_ASYNCIO: return "SI_ASYNCIO";
426 case SI_SIGIO: return "SI_SIGIO";
427 case SI_TKILL: return "SI_TKILL";
428 case SI_DETHREAD: return "SI_DETHREAD";
429 }
430 // Then give up...
431 return "?";
432}
Christopher Ferris4ae266c2019-04-03 09:27:12 -0700433
434void log_backtrace(log_t* log, unwindstack::Unwinder* unwinder, const char* prefix) {
435 if (unwinder->elf_from_memory_not_file()) {
436 _LOG(log, logtype::BACKTRACE,
437 "%sNOTE: Function names and BuildId information is missing for some frames due\n", prefix);
438 _LOG(log, logtype::BACKTRACE,
439 "%sNOTE: to unreadable libraries. For unwinds of apps, only shared libraries\n", prefix);
440 _LOG(log, logtype::BACKTRACE, "%sNOTE: found under the lib/ directory are readable.\n", prefix);
441#if defined(ROOT_POSSIBLE)
442 _LOG(log, logtype::BACKTRACE,
443 "%sNOTE: On this device, run setenforce 0 to make the libraries readable.\n", prefix);
444#endif
445 }
446
447 unwinder->SetDisplayBuildID(true);
448 for (size_t i = 0; i < unwinder->NumFrames(); i++) {
449 _LOG(log, logtype::BACKTRACE, "%s%s\n", prefix, unwinder->FormatFrame(i).c_str());
450 }
451}