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#define LOG_TAG "DEBUG"
19
20#include <elf.h>
21#include <errno.h>
22#include <stdint.h>
23#include <string.h>
24#include <sys/ptrace.h>
25#include <sys/uio.h>
26
27#include <backtrace/Backtrace.h>
28#include <log/log.h>
29
30#include "machine.h"
31#include "utility.h"
32
33void dump_memory_and_code(log_t* log, Backtrace* backtrace) {
34  struct user_pt_regs regs;
35  struct iovec io;
36  io.iov_base = &regs;
37  io.iov_len = sizeof(regs);
38
39  if (ptrace(PTRACE_GETREGSET, backtrace->Tid(), reinterpret_cast<void*>(NT_PRSTATUS), &io) == -1) {
40    ALOGE("ptrace failed to get registers: %s", strerror(errno));
41    return;
42  }
43
44  for (int reg = 0; reg < 31; reg++) {
45    dump_memory(log, backtrace, regs.regs[reg], "memory near x%d:", reg);
46  }
47
48  dump_memory(log, backtrace, static_cast<uintptr_t>(regs.pc), "code around pc:");
49
50  if (regs.pc != regs.sp) {
51    dump_memory(log, backtrace, static_cast<uintptr_t>(regs.sp), "code around sp:");
52  }
53}
54
55void dump_registers(log_t* log, pid_t tid) {
56  struct user_pt_regs r;
57  struct iovec io;
58  io.iov_base = &r;
59  io.iov_len = sizeof(r);
60
61  if (ptrace(PTRACE_GETREGSET, tid, (void*) NT_PRSTATUS, (void*) &io) == -1) {
62    ALOGE("ptrace error: %s\n", strerror(errno));
63    return;
64  }
65
66  for (int i = 0; i < 28; i += 4) {
67    _LOG(log, logtype::REGISTERS,
68         "    x%-2d  %016llx  x%-2d  %016llx  x%-2d  %016llx  x%-2d  %016llx\n",
69         i, r.regs[i],
70         i+1, r.regs[i+1],
71         i+2, r.regs[i+2],
72         i+3, r.regs[i+3]);
73  }
74
75  _LOG(log, logtype::REGISTERS, "    x28  %016llx  x29  %016llx  x30  %016llx\n",
76       r.regs[28], r.regs[29], r.regs[30]);
77
78  _LOG(log, logtype::REGISTERS, "    sp   %016llx  pc   %016llx  pstate %016llx\n",
79       r.sp, r.pc, r.pstate);
80
81  struct user_fpsimd_state f;
82  io.iov_base = &f;
83  io.iov_len = sizeof(f);
84
85  if (ptrace(PTRACE_GETREGSET, tid, (void*) NT_PRFPREG, (void*) &io) == -1) {
86    ALOGE("ptrace error: %s\n", strerror(errno));
87    return;
88  }
89
90  for (int i = 0; i < 32; i += 2) {
91    _LOG(log, logtype::FP_REGISTERS,
92         "    v%-2d  %016" PRIx64 "%016" PRIx64 "  v%-2d  %016" PRIx64 "%016" PRIx64 "\n",
93         i,
94         static_cast<uint64_t>(f.vregs[i] >> 64),
95         static_cast<uint64_t>(f.vregs[i]),
96         i+1,
97         static_cast<uint64_t>(f.vregs[i+1] >> 64),
98         static_cast<uint64_t>(f.vregs[i+1]));
99  }
100  _LOG(log, logtype::FP_REGISTERS, "    fpsr %08x  fpcr %08x\n", f.fpsr, f.fpcr);
101}
102