blob: 11459633680b62469c080210c9e1c2c7c02b6c5d [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
Elliott Hughes76e7f5e2013-11-20 11:31:29 -080025#include <sys/user.h>
Chris Dearman231e3c82012-08-10 17:06:20 -070026
27#include "../utility.h"
28#include "../machine.h"
29
Christopher Ferris20303f82014-01-10 16:33:16 -080030#define R(x) (static_cast<unsigned int>(x))
Chris Dearman231e3c82012-08-10 17:06:20 -070031
Christopher Ferris20303f82014-01-10 16:33:16 -080032// If configured to do so, dump memory around *all* registers
33// for the crashing thread.
Brigid Smith62ba4892014-06-10 11:53:08 -070034void dump_memory_and_code(log_t* log, pid_t tid) {
Christopher Ferrisb8170942015-01-26 13:31:52 -080035 pt_regs r;
Christopher Ferris20303f82014-01-10 16:33:16 -080036 if (ptrace(PTRACE_GETREGS, tid, 0, &r)) {
37 return;
38 }
39
Brigid Smith62ba4892014-06-10 11:53:08 -070040 static const char REG_NAMES[] = "$0atv0v1a0a1a2a3t0t1t2t3t4t5t6t7s0s1s2s3s4s5s6s7t8t9k0k1gpsps8ra";
Christopher Ferris20303f82014-01-10 16:33:16 -080041
Brigid Smith62ba4892014-06-10 11:53:08 -070042 for (int reg = 0; reg < 32; reg++) {
43 // skip uninteresting registers
44 if (reg == 0 // $0
45 || reg == 26 // $k0
46 || reg == 27 // $k1
47 || reg == 31 // $ra (done below)
48 )
49 continue;
Christopher Ferris20303f82014-01-10 16:33:16 -080050
Brigid Smith62ba4892014-06-10 11:53:08 -070051 uintptr_t addr = R(r.regs[reg]);
Christopher Ferris20303f82014-01-10 16:33:16 -080052
Brigid Smith62ba4892014-06-10 11:53:08 -070053 // Don't bother if it looks like a small int or ~= null, or if
54 // it's in the kernel area.
55 if (addr < 4096 || addr >= 0x80000000) {
56 continue;
Chris Dearman231e3c82012-08-10 17:06:20 -070057 }
Brigid Smith62ba4892014-06-10 11:53:08 -070058
59 _LOG(log, logtype::MEMORY, "\nmemory near %.2s:\n", &REG_NAMES[reg * 2]);
60 dump_memory(log, tid, addr);
Christopher Ferris20303f82014-01-10 16:33:16 -080061 }
Chris Dearman231e3c82012-08-10 17:06:20 -070062
Christopher Ferris20303f82014-01-10 16:33:16 -080063 unsigned int pc = R(r.cp0_epc);
64 unsigned int ra = R(r.regs[31]);
Chris Dearman231e3c82012-08-10 17:06:20 -070065
Brigid Smith62ba4892014-06-10 11:53:08 -070066 _LOG(log, logtype::MEMORY, "\ncode around pc:\n");
67 dump_memory(log, tid, (uintptr_t)pc);
Chris Dearman231e3c82012-08-10 17:06:20 -070068
Christopher Ferris20303f82014-01-10 16:33:16 -080069 if (pc != ra) {
Brigid Smith62ba4892014-06-10 11:53:08 -070070 _LOG(log, logtype::MEMORY, "\ncode around ra:\n");
71 dump_memory(log, tid, (uintptr_t)ra);
Christopher Ferris20303f82014-01-10 16:33:16 -080072 }
Chris Dearman231e3c82012-08-10 17:06:20 -070073}
74
Brigid Smith62ba4892014-06-10 11:53:08 -070075void dump_registers(log_t* log, pid_t tid) {
Christopher Ferrisb8170942015-01-26 13:31:52 -080076 pt_regs r;
Christopher Ferris20303f82014-01-10 16:33:16 -080077 if(ptrace(PTRACE_GETREGS, tid, 0, &r)) {
Brigid Smithe17f2672014-06-19 11:33:38 -070078 _LOG(log, logtype::ERROR, "cannot get registers: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -080079 return;
80 }
Chris Dearman231e3c82012-08-10 17:06:20 -070081
Brigid Smith62ba4892014-06-10 11:53:08 -070082 _LOG(log, logtype::REGISTERS, " zr %08x at %08x v0 %08x v1 %08x\n",
Christopher Ferris20303f82014-01-10 16:33:16 -080083 R(r.regs[0]), R(r.regs[1]), R(r.regs[2]), R(r.regs[3]));
Brigid Smith62ba4892014-06-10 11:53:08 -070084 _LOG(log, logtype::REGISTERS, " a0 %08x a1 %08x a2 %08x a3 %08x\n",
Christopher Ferris20303f82014-01-10 16:33:16 -080085 R(r.regs[4]), R(r.regs[5]), R(r.regs[6]), R(r.regs[7]));
Brigid Smith62ba4892014-06-10 11:53:08 -070086 _LOG(log, logtype::REGISTERS, " t0 %08x t1 %08x t2 %08x t3 %08x\n",
Christopher Ferris20303f82014-01-10 16:33:16 -080087 R(r.regs[8]), R(r.regs[9]), R(r.regs[10]), R(r.regs[11]));
Brigid Smith62ba4892014-06-10 11:53:08 -070088 _LOG(log, logtype::REGISTERS, " t4 %08x t5 %08x t6 %08x t7 %08x\n",
Christopher Ferris20303f82014-01-10 16:33:16 -080089 R(r.regs[12]), R(r.regs[13]), R(r.regs[14]), R(r.regs[15]));
Brigid Smith62ba4892014-06-10 11:53:08 -070090 _LOG(log, logtype::REGISTERS, " s0 %08x s1 %08x s2 %08x s3 %08x\n",
Christopher Ferris20303f82014-01-10 16:33:16 -080091 R(r.regs[16]), R(r.regs[17]), R(r.regs[18]), R(r.regs[19]));
Brigid Smith62ba4892014-06-10 11:53:08 -070092 _LOG(log, logtype::REGISTERS, " s4 %08x s5 %08x s6 %08x s7 %08x\n",
Christopher Ferris20303f82014-01-10 16:33:16 -080093 R(r.regs[20]), R(r.regs[21]), R(r.regs[22]), R(r.regs[23]));
Brigid Smith62ba4892014-06-10 11:53:08 -070094 _LOG(log, logtype::REGISTERS, " t8 %08x t9 %08x k0 %08x k1 %08x\n",
Christopher Ferris20303f82014-01-10 16:33:16 -080095 R(r.regs[24]), R(r.regs[25]), R(r.regs[26]), R(r.regs[27]));
Brigid Smith62ba4892014-06-10 11:53:08 -070096 _LOG(log, logtype::REGISTERS, " gp %08x sp %08x s8 %08x ra %08x\n",
Christopher Ferris20303f82014-01-10 16:33:16 -080097 R(r.regs[28]), R(r.regs[29]), R(r.regs[30]), R(r.regs[31]));
Brigid Smith62ba4892014-06-10 11:53:08 -070098 _LOG(log, logtype::REGISTERS, " hi %08x lo %08x bva %08x epc %08x\n",
Christopher Ferris20303f82014-01-10 16:33:16 -080099 R(r.hi), R(r.lo), R(r.cp0_badvaddr), R(r.cp0_epc));
Chris Dearman231e3c82012-08-10 17:06:20 -0700100}