Douglas Leung | 2ea9a32 | 2015-03-09 18:41:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014, 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 | */ |
| 16 | |
Christopher Ferris | b36b592 | 2015-06-17 18:35:59 -0700 | [diff] [blame] | 17 | #define LOG_TAG "DEBUG" |
| 18 | |
Douglas Leung | 2ea9a32 | 2015-03-09 18:41:32 -0700 | [diff] [blame] | 19 | #include <errno.h> |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 20 | #include <inttypes.h> |
| 21 | #include <stdint.h> |
| 22 | #include <string.h> |
Douglas Leung | 2ea9a32 | 2015-03-09 18:41:32 -0700 | [diff] [blame] | 23 | #include <sys/ptrace.h> |
| 24 | |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 25 | #include <android/log.h> |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 26 | #include <backtrace/Backtrace.h> |
Douglas Leung | 2ea9a32 | 2015-03-09 18:41:32 -0700 | [diff] [blame] | 27 | |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 28 | #include "machine.h" |
| 29 | #include "utility.h" |
Douglas Leung | 2ea9a32 | 2015-03-09 18:41:32 -0700 | [diff] [blame] | 30 | |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 31 | #define R(x) (static_cast<uintptr_t>(x)) |
Douglas Leung | 2ea9a32 | 2015-03-09 18:41:32 -0700 | [diff] [blame] | 32 | |
| 33 | // If configured to do so, dump memory around *all* registers |
| 34 | // for the crashing thread. |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 35 | void dump_memory_and_code(log_t* log, Backtrace* backtrace) { |
Douglas Leung | 2ea9a32 | 2015-03-09 18:41:32 -0700 | [diff] [blame] | 36 | pt_regs r; |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 37 | if (ptrace(PTRACE_GETREGS, backtrace->Tid(), 0, &r)) { |
Christopher Ferris | b36b592 | 2015-06-17 18:35:59 -0700 | [diff] [blame] | 38 | ALOGE("cannot get registers: %s\n", strerror(errno)); |
Douglas Leung | 2ea9a32 | 2015-03-09 18:41:32 -0700 | [diff] [blame] | 39 | return; |
| 40 | } |
| 41 | |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 42 | static const char reg_names[] = "$0atv0v1a0a1a2a3a4a5a6a7t0t1t2t3s0s1s2s3s4s5s6s7t8t9k0k1gpsps8ra"; |
Douglas Leung | 2ea9a32 | 2015-03-09 18:41:32 -0700 | [diff] [blame] | 43 | |
| 44 | for (int reg = 0; reg < 32; reg++) { |
| 45 | // skip uninteresting registers |
| 46 | if (reg == 0 // $0 |
| 47 | || reg == 26 // $k0 |
| 48 | || reg == 27 // $k1 |
| 49 | || reg == 31 // $ra (done below) |
| 50 | ) |
| 51 | continue; |
| 52 | |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 53 | dump_memory(log, backtrace, R(r.regs[reg]), "memory near %.2s:", ®_names[reg * 2]); |
Douglas Leung | 2ea9a32 | 2015-03-09 18:41:32 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 56 | uintptr_t pc = R(r.cp0_epc); |
| 57 | uintptr_t ra = R(r.regs[31]); |
| 58 | dump_memory(log, backtrace, pc, "code around pc:"); |
Douglas Leung | 2ea9a32 | 2015-03-09 18:41:32 -0700 | [diff] [blame] | 59 | if (pc != ra) { |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 60 | dump_memory(log, backtrace, ra, "code around ra:"); |
Douglas Leung | 2ea9a32 | 2015-03-09 18:41:32 -0700 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | void dump_registers(log_t* log, pid_t tid) { |
| 65 | pt_regs r; |
| 66 | if(ptrace(PTRACE_GETREGS, tid, 0, &r)) { |
Christopher Ferris | b36b592 | 2015-06-17 18:35:59 -0700 | [diff] [blame] | 67 | ALOGE("cannot get registers: %s\n", strerror(errno)); |
Douglas Leung | 2ea9a32 | 2015-03-09 18:41:32 -0700 | [diff] [blame] | 68 | return; |
| 69 | } |
| 70 | |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 71 | _LOG(log, logtype::REGISTERS, " zr %016" PRIxPTR " at %016" PRIxPTR |
| 72 | " v0 %016" PRIxPTR " v1 %016" PRIxPTR "\n", |
Douglas Leung | 2ea9a32 | 2015-03-09 18:41:32 -0700 | [diff] [blame] | 73 | R(r.regs[0]), R(r.regs[1]), R(r.regs[2]), R(r.regs[3])); |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 74 | _LOG(log, logtype::REGISTERS, " a0 %016" PRIxPTR " a1 %016" PRIxPTR |
| 75 | " a2 %016" PRIxPTR " a3 %016" PRIxPTR "\n", |
Douglas Leung | 2ea9a32 | 2015-03-09 18:41:32 -0700 | [diff] [blame] | 76 | R(r.regs[4]), R(r.regs[5]), R(r.regs[6]), R(r.regs[7])); |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 77 | _LOG(log, logtype::REGISTERS, " a4 %016" PRIxPTR " a5 %016" PRIxPTR |
| 78 | " a6 %016" PRIxPTR " a7 %016" PRIxPTR "\n", |
Douglas Leung | 2ea9a32 | 2015-03-09 18:41:32 -0700 | [diff] [blame] | 79 | R(r.regs[8]), R(r.regs[9]), R(r.regs[10]), R(r.regs[11])); |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 80 | _LOG(log, logtype::REGISTERS, " t0 %016" PRIxPTR " t1 %016" PRIxPTR |
| 81 | " t2 %016" PRIxPTR " t3 %016" PRIxPTR "\n", |
Douglas Leung | 2ea9a32 | 2015-03-09 18:41:32 -0700 | [diff] [blame] | 82 | R(r.regs[12]), R(r.regs[13]), R(r.regs[14]), R(r.regs[15])); |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 83 | _LOG(log, logtype::REGISTERS, " s0 %016" PRIxPTR " s1 %016" PRIxPTR |
| 84 | " s2 %016" PRIxPTR " s3 %016" PRIxPTR "\n", |
Douglas Leung | 2ea9a32 | 2015-03-09 18:41:32 -0700 | [diff] [blame] | 85 | R(r.regs[16]), R(r.regs[17]), R(r.regs[18]), R(r.regs[19])); |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 86 | _LOG(log, logtype::REGISTERS, " s4 %016" PRIxPTR " s5 %016" PRIxPTR |
| 87 | " s6 %016" PRIxPTR " s7 %016" PRIxPTR "\n", |
Douglas Leung | 2ea9a32 | 2015-03-09 18:41:32 -0700 | [diff] [blame] | 88 | R(r.regs[20]), R(r.regs[21]), R(r.regs[22]), R(r.regs[23])); |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 89 | _LOG(log, logtype::REGISTERS, " t8 %016" PRIxPTR " t9 %016" PRIxPTR |
| 90 | " k0 %016" PRIxPTR " k1 %016" PRIxPTR "\n", |
Douglas Leung | 2ea9a32 | 2015-03-09 18:41:32 -0700 | [diff] [blame] | 91 | R(r.regs[24]), R(r.regs[25]), R(r.regs[26]), R(r.regs[27])); |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 92 | _LOG(log, logtype::REGISTERS, " gp %016" PRIxPTR " sp %016" PRIxPTR |
| 93 | " s8 %016" PRIxPTR " ra %016" PRIxPTR "\n", |
Douglas Leung | 2ea9a32 | 2015-03-09 18:41:32 -0700 | [diff] [blame] | 94 | R(r.regs[28]), R(r.regs[29]), R(r.regs[30]), R(r.regs[31])); |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 95 | _LOG(log, logtype::REGISTERS, " hi %016" PRIxPTR " lo %016" PRIxPTR |
| 96 | " bva %016" PRIxPTR " epc %016" PRIxPTR "\n", |
Douglas Leung | 2ea9a32 | 2015-03-09 18:41:32 -0700 | [diff] [blame] | 97 | R(r.hi), R(r.lo), R(r.cp0_badvaddr), R(r.cp0_epc)); |
| 98 | } |