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