blob: 2413d5e5a7bb8b5bb41106b6e2debd524b20cd0a [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 */
40void dump_memory_and_code(log_t* log, pid_t tid, int scope_flags) {
41 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) {
47 _LOG(log, scope_flags, "%s: ptrace failed to get registers: %s\n",
48 __func__, strerror(errno));
49 return;
50 }
51
52 if (IS_AT_FAULT(scope_flags) && DUMP_MEMORY_FOR_ALL_REGISTERS) {
53 for (int reg = 0; reg < 31; reg++) {
54 uintptr_t addr = regs.regs[reg];
55
56 /*
57 * Don't bother if it looks like a small int or ~= null, or if
58 * it's in the kernel area.
59 */
60 if (addr < 4096 || addr >= (1UL<<63)) {
61 continue;
62 }
63
64 _LOG(log, scope_flags | SCOPE_SENSITIVE, "\nmemory near x%d:\n", reg);
65 dump_memory(log, tid, addr, scope_flags | SCOPE_SENSITIVE);
66 }
67 }
68
69 _LOG(log, scope_flags, "\ncode around pc:\n");
70 dump_memory(log, tid, (uintptr_t)regs.pc, scope_flags);
71
72 if (regs.pc != regs.sp) {
73 _LOG(log, scope_flags, "\ncode around sp:\n");
74 dump_memory(log, tid, (uintptr_t)regs.sp, scope_flags);
75 }
76}
77
78void dump_registers(log_t* log, pid_t tid, int scope_flags)
79{
80 struct user_pt_regs r;
81 struct iovec io;
82 io.iov_base = &r;
83 io.iov_len = sizeof(r);
84
Kévin PETITabc60c22013-12-19 12:36:59 +000085 if (ptrace(PTRACE_GETREGSET, tid, (void*) NT_PRSTATUS, (void*) &io) == -1) {
86 _LOG(log, scope_flags, "ptrace error: %s\n", strerror(errno));
87 return;
88 }
89
90 for (int i = 0; i < 28; i += 4) {
91 _LOG(log, scope_flags, " x%-2d %016lx x%-2d %016lx x%-2d %016lx x%-2d %016lx\n",
92 i, (uint64_t)r.regs[i],
93 i+1, (uint64_t)r.regs[i+1],
94 i+2, (uint64_t)r.regs[i+2],
95 i+3, (uint64_t)r.regs[i+3]);
96 }
97
98 _LOG(log, scope_flags, " x28 %016lx x29 %016lx x30 %016lx\n",
99 (uint64_t)r.regs[28], (uint64_t)r.regs[29], (uint64_t)r.regs[30]);
100
101 _LOG(log, scope_flags, " sp %016lx pc %016lx\n",
102 (uint64_t)r.sp, (uint64_t)r.pc);
103
104
105 struct user_fpsimd_state f;
106 io.iov_base = &f;
107 io.iov_len = sizeof(f);
108
109 if (ptrace(PTRACE_GETREGSET, tid, (void*) NT_PRFPREG, (void*) &io) == -1) {
110 _LOG(log, scope_flags, "ptrace error: %s\n", strerror(errno));
111 return;
112 }
113
114 for (int i = 0; i < 32; i += 4) {
115 _LOG(log, scope_flags, " v%-2d %016lx v%-2d %016lx v%-2d %016lx v%-2d %016lx\n",
116 i, (uint64_t)f.vregs[i],
117 i+1, (uint64_t)f.vregs[i+1],
118 i+2, (uint64_t)f.vregs[i+2],
119 i+3, (uint64_t)f.vregs[i+3]);
120 }
121}