blob: 7b4e29e466d392e1204a3acc89535242fef13b96 [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
17#include <stddef.h>
Chris Dearman231e3c82012-08-10 17:06:20 -070018#include <stdlib.h>
19#include <string.h>
20#include <stdio.h>
21#include <errno.h>
22#include <sys/types.h>
23#include <sys/ptrace.h>
24
25#include <corkscrew/ptrace.h>
26
Elliott Hughes76e7f5e2013-11-20 11:31:29 -080027#include <sys/user.h>
Chris Dearman231e3c82012-08-10 17:06:20 -070028
29#include "../utility.h"
30#include "../machine.h"
31
Christopher Ferris20303f82014-01-10 16:33:16 -080032// enable to dump memory pointed to by every register
Chris Dearman231e3c82012-08-10 17:06:20 -070033#define DUMP_MEMORY_FOR_ALL_REGISTERS 1
34
Christopher Ferris20303f82014-01-10 16:33:16 -080035#define R(x) (static_cast<unsigned int>(x))
Chris Dearman231e3c82012-08-10 17:06:20 -070036
Christopher Ferris20303f82014-01-10 16:33:16 -080037// If configured to do so, dump memory around *all* registers
38// for the crashing thread.
Christopher Ferris365e4ae2013-10-02 12:26:48 -070039void dump_memory_and_code(log_t* log, pid_t tid, int scope_flags) {
Christopher Ferris20303f82014-01-10 16:33:16 -080040 pt_regs_mips_t r;
41 if (ptrace(PTRACE_GETREGS, tid, 0, &r)) {
42 return;
43 }
44
45 if (IS_AT_FAULT(scope_flags) && DUMP_MEMORY_FOR_ALL_REGISTERS) {
46 static const char REG_NAMES[] = "$0atv0v1a0a1a2a3t0t1t2t3t4t5t6t7s0s1s2s3s4s5s6s7t8t9k0k1gpsps8ra";
47
48 for (int reg = 0; reg < 32; reg++) {
49 // skip uninteresting registers
50 if (reg == 0 // $0
51 || reg == 26 // $k0
52 || reg == 27 // $k1
53 || reg == 31 // $ra (done below)
54 )
55 continue;
56
57 uintptr_t addr = R(r.regs[reg]);
58
59 // Don't bother if it looks like a small int or ~= null, or if
60 // it's in the kernel area.
61 if (addr < 4096 || addr >= 0x80000000) {
62 continue;
63 }
64
65 _LOG(log, scope_flags | SCOPE_SENSITIVE, "\nmemory near %.2s:\n", &REG_NAMES[reg * 2]);
66 dump_memory(log, tid, addr, scope_flags | SCOPE_SENSITIVE);
Chris Dearman231e3c82012-08-10 17:06:20 -070067 }
Christopher Ferris20303f82014-01-10 16:33:16 -080068 }
Chris Dearman231e3c82012-08-10 17:06:20 -070069
Christopher Ferris20303f82014-01-10 16:33:16 -080070 unsigned int pc = R(r.cp0_epc);
71 unsigned int ra = R(r.regs[31]);
Chris Dearman231e3c82012-08-10 17:06:20 -070072
Christopher Ferris20303f82014-01-10 16:33:16 -080073 _LOG(log, scope_flags, "\ncode around pc:\n");
74 dump_memory(log, tid, (uintptr_t)pc, scope_flags);
Chris Dearman231e3c82012-08-10 17:06:20 -070075
Christopher Ferris20303f82014-01-10 16:33:16 -080076 if (pc != ra) {
77 _LOG(log, scope_flags, "\ncode around ra:\n");
78 dump_memory(log, tid, (uintptr_t)ra, scope_flags);
79 }
Chris Dearman231e3c82012-08-10 17:06:20 -070080}
81
Christopher Ferris20303f82014-01-10 16:33:16 -080082void dump_registers(log_t* log, pid_t tid, int scope_flags) {
83 pt_regs_mips_t r;
84 if(ptrace(PTRACE_GETREGS, tid, 0, &r)) {
85 _LOG(log, scope_flags, "cannot get registers: %s\n", strerror(errno));
86 return;
87 }
Chris Dearman231e3c82012-08-10 17:06:20 -070088
Christopher Ferris20303f82014-01-10 16:33:16 -080089 _LOG(log, scope_flags, " zr %08x at %08x v0 %08x v1 %08x\n",
90 R(r.regs[0]), R(r.regs[1]), R(r.regs[2]), R(r.regs[3]));
91 _LOG(log, scope_flags, " a0 %08x a1 %08x a2 %08x a3 %08x\n",
92 R(r.regs[4]), R(r.regs[5]), R(r.regs[6]), R(r.regs[7]));
93 _LOG(log, scope_flags, " t0 %08x t1 %08x t2 %08x t3 %08x\n",
94 R(r.regs[8]), R(r.regs[9]), R(r.regs[10]), R(r.regs[11]));
95 _LOG(log, scope_flags, " t4 %08x t5 %08x t6 %08x t7 %08x\n",
96 R(r.regs[12]), R(r.regs[13]), R(r.regs[14]), R(r.regs[15]));
97 _LOG(log, scope_flags, " s0 %08x s1 %08x s2 %08x s3 %08x\n",
98 R(r.regs[16]), R(r.regs[17]), R(r.regs[18]), R(r.regs[19]));
99 _LOG(log, scope_flags, " s4 %08x s5 %08x s6 %08x s7 %08x\n",
100 R(r.regs[20]), R(r.regs[21]), R(r.regs[22]), R(r.regs[23]));
101 _LOG(log, scope_flags, " t8 %08x t9 %08x k0 %08x k1 %08x\n",
102 R(r.regs[24]), R(r.regs[25]), R(r.regs[26]), R(r.regs[27]));
103 _LOG(log, scope_flags, " gp %08x sp %08x s8 %08x ra %08x\n",
104 R(r.regs[28]), R(r.regs[29]), R(r.regs[30]), R(r.regs[31]));
105 _LOG(log, scope_flags, " hi %08x lo %08x bva %08x epc %08x\n",
106 R(r.hi), R(r.lo), R(r.cp0_badvaddr), R(r.cp0_epc));
Chris Dearman231e3c82012-08-10 17:06:20 -0700107}