blob: 293dcf61035359cc65a2f445d41304dc704f7c2c [file] [log] [blame]
Douglas Leung2ea9a322015-03-09 18:41:32 -07001/*
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
Douglas Leung2ea9a322015-03-09 18:41:32 -070017#include <errno.h>
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070018#include <inttypes.h>
19#include <stdint.h>
20#include <string.h>
Douglas Leung2ea9a322015-03-09 18:41:32 -070021#include <sys/ptrace.h>
22
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070023#include <backtrace/Backtrace.h>
Douglas Leung2ea9a322015-03-09 18:41:32 -070024
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070025#include "machine.h"
26#include "utility.h"
Douglas Leung2ea9a322015-03-09 18:41:32 -070027
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070028#define R(x) (static_cast<uintptr_t>(x))
Douglas Leung2ea9a322015-03-09 18:41:32 -070029
30// If configured to do so, dump memory around *all* registers
31// for the crashing thread.
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070032void dump_memory_and_code(log_t* log, Backtrace* backtrace) {
Douglas Leung2ea9a322015-03-09 18:41:32 -070033 pt_regs r;
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070034 if (ptrace(PTRACE_GETREGS, backtrace->Tid(), 0, &r)) {
35 _LOG(log, logtype::ERROR, "cannot get registers: %s\n", strerror(errno));
Douglas Leung2ea9a322015-03-09 18:41:32 -070036 return;
37 }
38
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070039 static const char reg_names[] = "$0atv0v1a0a1a2a3a4a5a6a7t0t1t2t3s0s1s2s3s4s5s6s7t8t9k0k1gpsps8ra";
Douglas Leung2ea9a322015-03-09 18:41:32 -070040
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;
49
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070050 dump_memory(log, backtrace, R(r.regs[reg]), "memory near %.2s:", &reg_names[reg * 2]);
Douglas Leung2ea9a322015-03-09 18:41:32 -070051 }
52
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070053 uintptr_t pc = R(r.cp0_epc);
54 uintptr_t ra = R(r.regs[31]);
55 dump_memory(log, backtrace, pc, "code around pc:");
Douglas Leung2ea9a322015-03-09 18:41:32 -070056 if (pc != ra) {
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070057 dump_memory(log, backtrace, ra, "code around ra:");
Douglas Leung2ea9a322015-03-09 18:41:32 -070058 }
59}
60
61void dump_registers(log_t* log, pid_t tid) {
62 pt_regs r;
63 if(ptrace(PTRACE_GETREGS, tid, 0, &r)) {
64 _LOG(log, logtype::ERROR, "cannot get registers: %s\n", strerror(errno));
65 return;
66 }
67
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070068 _LOG(log, logtype::REGISTERS, " zr %016" PRIxPTR " at %016" PRIxPTR
69 " v0 %016" PRIxPTR " v1 %016" PRIxPTR "\n",
Douglas Leung2ea9a322015-03-09 18:41:32 -070070 R(r.regs[0]), R(r.regs[1]), R(r.regs[2]), R(r.regs[3]));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070071 _LOG(log, logtype::REGISTERS, " a0 %016" PRIxPTR " a1 %016" PRIxPTR
72 " a2 %016" PRIxPTR " a3 %016" PRIxPTR "\n",
Douglas Leung2ea9a322015-03-09 18:41:32 -070073 R(r.regs[4]), R(r.regs[5]), R(r.regs[6]), R(r.regs[7]));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070074 _LOG(log, logtype::REGISTERS, " a4 %016" PRIxPTR " a5 %016" PRIxPTR
75 " a6 %016" PRIxPTR " a7 %016" PRIxPTR "\n",
Douglas Leung2ea9a322015-03-09 18:41:32 -070076 R(r.regs[8]), R(r.regs[9]), R(r.regs[10]), R(r.regs[11]));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070077 _LOG(log, logtype::REGISTERS, " t0 %016" PRIxPTR " t1 %016" PRIxPTR
78 " t2 %016" PRIxPTR " t3 %016" PRIxPTR "\n",
Douglas Leung2ea9a322015-03-09 18:41:32 -070079 R(r.regs[12]), R(r.regs[13]), R(r.regs[14]), R(r.regs[15]));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070080 _LOG(log, logtype::REGISTERS, " s0 %016" PRIxPTR " s1 %016" PRIxPTR
81 " s2 %016" PRIxPTR " s3 %016" PRIxPTR "\n",
Douglas Leung2ea9a322015-03-09 18:41:32 -070082 R(r.regs[16]), R(r.regs[17]), R(r.regs[18]), R(r.regs[19]));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070083 _LOG(log, logtype::REGISTERS, " s4 %016" PRIxPTR " s5 %016" PRIxPTR
84 " s6 %016" PRIxPTR " s7 %016" PRIxPTR "\n",
Douglas Leung2ea9a322015-03-09 18:41:32 -070085 R(r.regs[20]), R(r.regs[21]), R(r.regs[22]), R(r.regs[23]));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070086 _LOG(log, logtype::REGISTERS, " t8 %016" PRIxPTR " t9 %016" PRIxPTR
87 " k0 %016" PRIxPTR " k1 %016" PRIxPTR "\n",
Douglas Leung2ea9a322015-03-09 18:41:32 -070088 R(r.regs[24]), R(r.regs[25]), R(r.regs[26]), R(r.regs[27]));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070089 _LOG(log, logtype::REGISTERS, " gp %016" PRIxPTR " sp %016" PRIxPTR
90 " s8 %016" PRIxPTR " ra %016" PRIxPTR "\n",
Douglas Leung2ea9a322015-03-09 18:41:32 -070091 R(r.regs[28]), R(r.regs[29]), R(r.regs[30]), R(r.regs[31]));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070092 _LOG(log, logtype::REGISTERS, " hi %016" PRIxPTR " lo %016" PRIxPTR
93 " bva %016" PRIxPTR " epc %016" PRIxPTR "\n",
Douglas Leung2ea9a322015-03-09 18:41:32 -070094 R(r.hi), R(r.lo), R(r.cp0_badvaddr), R(r.cp0_epc));
95}