blob: f3409d59f89430b692159dff2f2acd07441fbb8c [file] [log] [blame]
Kévin PETITabc60c22013-12-19 12:36:59 +00001/*
2 *
3 * Copyright 2014, The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <stddef.h>
19#include <stdlib.h>
20#include <string.h>
21#include <ctype.h>
22#include <stdio.h>
23#include <errno.h>
24#include <sys/types.h>
25#include <sys/ptrace.h>
26#include <sys/user.h>
27#include <sys/uio.h>
28#include <linux/elf.h>
29
30#include "../utility.h"
31#include "../machine.h"
32
33/* enable to dump memory pointed to by every register */
34#define DUMP_MEMORY_FOR_ALL_REGISTERS 1
35
36/*
37 * If configured to do so, dump memory around *all* registers
38 * for the crashing thread.
39 */
Brigid Smith62ba4892014-06-10 11:53:08 -070040void dump_memory_and_code(log_t* log, pid_t tid) {
Kévin PETITabc60c22013-12-19 12:36:59 +000041 struct user_pt_regs regs;
42 struct iovec io;
43 io.iov_base = &regs;
44 io.iov_len = sizeof(regs);
45
46 if (ptrace(PTRACE_GETREGSET, tid, (void*)NT_PRSTATUS, &io) == -1) {
Brigid Smith62ba4892014-06-10 11:53:08 -070047 LOG_ERROR("%s: ptrace failed to get registers: %s\n",
Kévin PETITabc60c22013-12-19 12:36:59 +000048 __func__, strerror(errno));
49 return;
50 }
51
Brigid Smith62ba4892014-06-10 11:53:08 -070052 for (int reg = 0; reg < 31; reg++) {
53 uintptr_t addr = regs.regs[reg];
Kévin PETITabc60c22013-12-19 12:36:59 +000054
Brigid Smith62ba4892014-06-10 11:53:08 -070055 /*
56 * Don't bother if it looks like a small int or ~= null, or if
57 * it's in the kernel area.
58 */
59 if (addr < 4096 || addr >= (1UL<<63)) {
60 continue;
Kévin PETITabc60c22013-12-19 12:36:59 +000061 }
Brigid Smith62ba4892014-06-10 11:53:08 -070062
63 _LOG(log, logtype::MEMORY, "\nmemory near x%d:\n", reg);
64 dump_memory(log, tid, addr);
Kévin PETITabc60c22013-12-19 12:36:59 +000065 }
66
Brigid Smith62ba4892014-06-10 11:53:08 -070067 _LOG(log, logtype::MEMORY, "\ncode around pc:\n");
68 dump_memory(log, tid, (uintptr_t)regs.pc);
Kévin PETITabc60c22013-12-19 12:36:59 +000069
70 if (regs.pc != regs.sp) {
Brigid Smith62ba4892014-06-10 11:53:08 -070071 _LOG(log, logtype::MEMORY, "\ncode around sp:\n");
72 dump_memory(log, tid, (uintptr_t)regs.sp);
Kévin PETITabc60c22013-12-19 12:36:59 +000073 }
74}
75
Brigid Smith62ba4892014-06-10 11:53:08 -070076void dump_registers(log_t* log, pid_t tid) {
Kévin PETITabc60c22013-12-19 12:36:59 +000077 struct user_pt_regs r;
78 struct iovec io;
79 io.iov_base = &r;
80 io.iov_len = sizeof(r);
81
Kévin PETITabc60c22013-12-19 12:36:59 +000082 if (ptrace(PTRACE_GETREGSET, tid, (void*) NT_PRSTATUS, (void*) &io) == -1) {
Brigid Smith62ba4892014-06-10 11:53:08 -070083 LOG_ERROR("ptrace error: %s\n", strerror(errno));
Kévin PETITabc60c22013-12-19 12:36:59 +000084 return;
85 }
86
87 for (int i = 0; i < 28; i += 4) {
Brigid Smith62ba4892014-06-10 11:53:08 -070088 _LOG(log, logtype::REGISTERS,
89 " x%-2d %016lx x%-2d %016lx x%-2d %016lx x%-2d %016lx\n",
Kévin PETITabc60c22013-12-19 12:36:59 +000090 i, (uint64_t)r.regs[i],
91 i+1, (uint64_t)r.regs[i+1],
92 i+2, (uint64_t)r.regs[i+2],
93 i+3, (uint64_t)r.regs[i+3]);
94 }
95
Brigid Smith62ba4892014-06-10 11:53:08 -070096 _LOG(log, logtype::REGISTERS, " x28 %016lx x29 %016lx x30 %016lx\n",
Kévin PETITabc60c22013-12-19 12:36:59 +000097 (uint64_t)r.regs[28], (uint64_t)r.regs[29], (uint64_t)r.regs[30]);
98
Brigid Smith62ba4892014-06-10 11:53:08 -070099 _LOG(log, logtype::REGISTERS, " sp %016lx pc %016lx\n",
Kévin PETITabc60c22013-12-19 12:36:59 +0000100 (uint64_t)r.sp, (uint64_t)r.pc);
101
102
103 struct user_fpsimd_state f;
104 io.iov_base = &f;
105 io.iov_len = sizeof(f);
106
107 if (ptrace(PTRACE_GETREGSET, tid, (void*) NT_PRFPREG, (void*) &io) == -1) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700108 LOG_ERROR("ptrace error: %s\n", strerror(errno));
Kévin PETITabc60c22013-12-19 12:36:59 +0000109 return;
110 }
111
112 for (int i = 0; i < 32; i += 4) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700113 _LOG(log, logtype::REGISTERS, " v%-2d %016lx v%-2d %016lx v%-2d %016lx v%-2d %016lx\n",
Kévin PETITabc60c22013-12-19 12:36:59 +0000114 i, (uint64_t)f.vregs[i],
115 i+1, (uint64_t)f.vregs[i+1],
116 i+2, (uint64_t)f.vregs[i+2],
117 i+3, (uint64_t)f.vregs[i+3]);
118 }
119}