machine.cpp revision abc60c26b7448e6b2842351688a7a823b8b787d6
1/*
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
85  bool only_in_tombstone = !IS_AT_FAULT(scope_flags);
86
87  if (ptrace(PTRACE_GETREGSET, tid, (void*) NT_PRSTATUS, (void*) &io) == -1) {
88    _LOG(log, scope_flags, "ptrace error: %s\n", strerror(errno));
89    return;
90  }
91
92  for (int i = 0; i < 28; i += 4) {
93    _LOG(log, scope_flags, "    x%-2d  %016lx  x%-2d  %016lx  x%-2d  %016lx  x%-2d  %016lx\n",
94         i, (uint64_t)r.regs[i],
95         i+1, (uint64_t)r.regs[i+1],
96         i+2, (uint64_t)r.regs[i+2],
97         i+3, (uint64_t)r.regs[i+3]);
98  }
99
100  _LOG(log, scope_flags, "    x28  %016lx  x29  %016lx  x30  %016lx\n",
101       (uint64_t)r.regs[28], (uint64_t)r.regs[29], (uint64_t)r.regs[30]);
102
103  _LOG(log, scope_flags, "    sp   %016lx  pc   %016lx\n",
104       (uint64_t)r.sp, (uint64_t)r.pc);
105
106
107  struct user_fpsimd_state f;
108  io.iov_base = &f;
109  io.iov_len = sizeof(f);
110
111  if (ptrace(PTRACE_GETREGSET, tid, (void*) NT_PRFPREG, (void*) &io) == -1) {
112    _LOG(log, scope_flags, "ptrace error: %s\n", strerror(errno));
113    return;
114  }
115
116  for (int i = 0; i < 32; i += 4) {
117    _LOG(log, scope_flags, "    v%-2d  %016lx  v%-2d  %016lx  v%-2d  %016lx  v%-2d  %016lx\n",
118         i, (uint64_t)f.vregs[i],
119         i+1, (uint64_t)f.vregs[i+1],
120         i+2, (uint64_t)f.vregs[i+2],
121         i+3, (uint64_t)f.vregs[i+3]);
122  }
123}
124