blob: cbf272a366c3cbb3bf21de8af2241007541bf87b [file] [log] [blame]
Christopher Ferris20303f82014-01-10 16:33:16 -08001/*
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 Dearman231e3c82012-08-10 17:06:20 -070016
Christopher Ferrisb36b5922015-06-17 18:35:59 -070017#define LOG_TAG "DEBUG"
18
Chris Dearman231e3c82012-08-10 17:06:20 -070019#include <errno.h>
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070020#include <inttypes.h>
21#include <stdint.h>
22#include <string.h>
Chris Dearman231e3c82012-08-10 17:06:20 -070023#include <sys/ptrace.h>
24
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070025#include <backtrace/Backtrace.h>
Mark Salyzyn30f991f2017-01-10 13:19:54 -080026#include <log/log.h>
Chris Dearman231e3c82012-08-10 17:06:20 -070027
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070028#include "machine.h"
29#include "utility.h"
Chris Dearman231e3c82012-08-10 17:06:20 -070030
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070031#define R(x) (static_cast<uintptr_t>(x))
Chris Dearman231e3c82012-08-10 17:06:20 -070032
Christopher Ferris20303f82014-01-10 16:33:16 -080033// If configured to do so, dump memory around *all* registers
34// for the crashing thread.
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070035void dump_memory_and_code(log_t* log, Backtrace* backtrace) {
Christopher Ferrisb8170942015-01-26 13:31:52 -080036 pt_regs r;
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070037 if (ptrace(PTRACE_GETREGS, backtrace->Tid(), 0, &r)) {
Christopher Ferrisb36b5922015-06-17 18:35:59 -070038 ALOGE("cannot get registers: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -080039 return;
40 }
41
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070042 static const char reg_names[] = "$0atv0v1a0a1a2a3t0t1t2t3t4t5t6t7s0s1s2s3s4s5s6s7t8t9k0k1gpsps8ra";
Christopher Ferris20303f82014-01-10 16:33:16 -080043
Brigid Smith62ba4892014-06-10 11:53:08 -070044 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;
Christopher Ferris20303f82014-01-10 16:33:16 -080052
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070053 dump_memory(log, backtrace, R(r.regs[reg]), "memory near %.2s:", &reg_names[reg * 2]);
Christopher Ferris20303f82014-01-10 16:33:16 -080054 }
Chris Dearman231e3c82012-08-10 17:06:20 -070055
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070056 uintptr_t pc = R(r.cp0_epc);
57 uintptr_t ra = R(r.regs[31]);
58 dump_memory(log, backtrace, pc, "code around pc:");
Christopher Ferris20303f82014-01-10 16:33:16 -080059 if (pc != ra) {
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070060 dump_memory(log, backtrace, ra, "code around ra:");
Christopher Ferris20303f82014-01-10 16:33:16 -080061 }
Chris Dearman231e3c82012-08-10 17:06:20 -070062}
63
Brigid Smith62ba4892014-06-10 11:53:08 -070064void dump_registers(log_t* log, pid_t tid) {
Christopher Ferrisb8170942015-01-26 13:31:52 -080065 pt_regs r;
Christopher Ferris20303f82014-01-10 16:33:16 -080066 if(ptrace(PTRACE_GETREGS, tid, 0, &r)) {
Christopher Ferrisb36b5922015-06-17 18:35:59 -070067 ALOGE("cannot get registers: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -080068 return;
69 }
Chris Dearman231e3c82012-08-10 17:06:20 -070070
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070071 _LOG(log, logtype::REGISTERS, " zr %08" PRIxPTR " at %08" PRIxPTR
72 " v0 %08" PRIxPTR " v1 %08" PRIxPTR "\n",
Christopher Ferris20303f82014-01-10 16:33:16 -080073 R(r.regs[0]), R(r.regs[1]), R(r.regs[2]), R(r.regs[3]));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070074 _LOG(log, logtype::REGISTERS, " a0 %08" PRIxPTR " a1 %08" PRIxPTR
75 " a2 %08" PRIxPTR " a3 %08" PRIxPTR "\n",
Christopher Ferris20303f82014-01-10 16:33:16 -080076 R(r.regs[4]), R(r.regs[5]), R(r.regs[6]), R(r.regs[7]));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070077 _LOG(log, logtype::REGISTERS, " t0 %08" PRIxPTR " t1 %08" PRIxPTR
78 " t2 %08" PRIxPTR " t3 %08" PRIxPTR "\n",
Christopher Ferris20303f82014-01-10 16:33:16 -080079 R(r.regs[8]), R(r.regs[9]), R(r.regs[10]), R(r.regs[11]));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070080 _LOG(log, logtype::REGISTERS, " t4 %08" PRIxPTR " t5 %08" PRIxPTR
81 " t6 %08" PRIxPTR " t7 %08" PRIxPTR "\n",
Christopher Ferris20303f82014-01-10 16:33:16 -080082 R(r.regs[12]), R(r.regs[13]), R(r.regs[14]), R(r.regs[15]));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070083 _LOG(log, logtype::REGISTERS, " s0 %08" PRIxPTR " s1 %08" PRIxPTR
84 " s2 %08" PRIxPTR " s3 %08" PRIxPTR "\n",
Christopher Ferris20303f82014-01-10 16:33:16 -080085 R(r.regs[16]), R(r.regs[17]), R(r.regs[18]), R(r.regs[19]));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070086 _LOG(log, logtype::REGISTERS, " s4 %08" PRIxPTR " s5 %08" PRIxPTR
87 " s6 %08" PRIxPTR " s7 %08" PRIxPTR "\n",
Christopher Ferris20303f82014-01-10 16:33:16 -080088 R(r.regs[20]), R(r.regs[21]), R(r.regs[22]), R(r.regs[23]));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070089 _LOG(log, logtype::REGISTERS, " t8 %08" PRIxPTR " t9 %08" PRIxPTR
90 " k0 %08" PRIxPTR " k1 %08" PRIxPTR "\n",
Christopher Ferris20303f82014-01-10 16:33:16 -080091 R(r.regs[24]), R(r.regs[25]), R(r.regs[26]), R(r.regs[27]));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070092 _LOG(log, logtype::REGISTERS, " gp %08" PRIxPTR " sp %08" PRIxPTR
93 " s8 %08" PRIxPTR " ra %08" PRIxPTR "\n",
Christopher Ferris20303f82014-01-10 16:33:16 -080094 R(r.regs[28]), R(r.regs[29]), R(r.regs[30]), R(r.regs[31]));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070095 _LOG(log, logtype::REGISTERS, " hi %08" PRIxPTR " lo %08" PRIxPTR
96 " bva %08" PRIxPTR " epc %08" PRIxPTR "\n",
Christopher Ferris20303f82014-01-10 16:33:16 -080097 R(r.hi), R(r.lo), R(r.cp0_badvaddr), R(r.cp0_epc));
Chris Dearman231e3c82012-08-10 17:06:20 -070098}