1abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT/*
2abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT *
3abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT * Copyright 2014, The Android Open Source Project
4abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT *
5abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT * Licensed under the Apache License, Version 2.0 (the "License");
6abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT * you may not use this file except in compliance with the License.
7abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT * You may obtain a copy of the License at
8abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT *
9abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT *     http://www.apache.org/licenses/LICENSE-2.0
10abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT *
11abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT * Unless required by applicable law or agreed to in writing, software
12abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT * distributed under the License is distributed on an "AS IS" BASIS,
13abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT * See the License for the specific language governing permissions and
15abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT * limitations under the License.
16abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT */
17abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT
18aae5d437556d877f2a198601394ba8ea7a559a70Elliott Hughes#include <elf.h>
19abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT#include <errno.h>
20aae5d437556d877f2a198601394ba8ea7a559a70Elliott Hughes#include <inttypes.h>
21aae5d437556d877f2a198601394ba8ea7a559a70Elliott Hughes#include <string.h>
22abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT#include <sys/types.h>
23abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT#include <sys/ptrace.h>
24abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT#include <sys/user.h>
25abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT#include <sys/uio.h>
26abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT
27abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT#include "../utility.h"
28abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT#include "../machine.h"
29abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT
3062ba489ba00a2689d4e257bc178cff87495f99d7Brigid Smithvoid dump_memory_and_code(log_t* log, pid_t tid) {
31abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT    struct user_pt_regs regs;
32abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT    struct iovec io;
33abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT    io.iov_base = &regs;
34abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT    io.iov_len = sizeof(regs);
35abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT
36abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT    if (ptrace(PTRACE_GETREGSET, tid, (void*)NT_PRSTATUS, &io) == -1) {
37e17f267b2a54d4b1a2b440a94ce06818ece49c7bBrigid Smith        _LOG(log, logtype::ERROR, "%s: ptrace failed to get registers: %s\n",
38abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT             __func__, strerror(errno));
39abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT        return;
40abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT    }
41abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT
4262ba489ba00a2689d4e257bc178cff87495f99d7Brigid Smith    for (int reg = 0; reg < 31; reg++) {
4362ba489ba00a2689d4e257bc178cff87495f99d7Brigid Smith        uintptr_t addr = regs.regs[reg];
44abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT
4562ba489ba00a2689d4e257bc178cff87495f99d7Brigid Smith        /*
4662ba489ba00a2689d4e257bc178cff87495f99d7Brigid Smith         * Don't bother if it looks like a small int or ~= null, or if
4762ba489ba00a2689d4e257bc178cff87495f99d7Brigid Smith         * it's in the kernel area.
4862ba489ba00a2689d4e257bc178cff87495f99d7Brigid Smith         */
4962ba489ba00a2689d4e257bc178cff87495f99d7Brigid Smith        if (addr < 4096 || addr >= (1UL<<63)) {
5062ba489ba00a2689d4e257bc178cff87495f99d7Brigid Smith            continue;
51abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT        }
5262ba489ba00a2689d4e257bc178cff87495f99d7Brigid Smith
5362ba489ba00a2689d4e257bc178cff87495f99d7Brigid Smith        _LOG(log, logtype::MEMORY, "\nmemory near x%d:\n", reg);
5462ba489ba00a2689d4e257bc178cff87495f99d7Brigid Smith        dump_memory(log, tid, addr);
55abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT    }
56abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT
5762ba489ba00a2689d4e257bc178cff87495f99d7Brigid Smith    _LOG(log, logtype::MEMORY, "\ncode around pc:\n");
5862ba489ba00a2689d4e257bc178cff87495f99d7Brigid Smith    dump_memory(log, tid, (uintptr_t)regs.pc);
59abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT
60abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT    if (regs.pc != regs.sp) {
6162ba489ba00a2689d4e257bc178cff87495f99d7Brigid Smith        _LOG(log, logtype::MEMORY, "\ncode around sp:\n");
6262ba489ba00a2689d4e257bc178cff87495f99d7Brigid Smith        dump_memory(log, tid, (uintptr_t)regs.sp);
63abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT    }
64abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT}
65abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT
6662ba489ba00a2689d4e257bc178cff87495f99d7Brigid Smithvoid dump_registers(log_t* log, pid_t tid) {
67abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT  struct user_pt_regs r;
68abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT  struct iovec io;
69abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT  io.iov_base = &r;
70abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT  io.iov_len = sizeof(r);
71abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT
72abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT  if (ptrace(PTRACE_GETREGSET, tid, (void*) NT_PRSTATUS, (void*) &io) == -1) {
73e17f267b2a54d4b1a2b440a94ce06818ece49c7bBrigid Smith    _LOG(log, logtype::ERROR, "ptrace error: %s\n", strerror(errno));
74abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT    return;
75abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT  }
76abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT
77abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT  for (int i = 0; i < 28; i += 4) {
7862ba489ba00a2689d4e257bc178cff87495f99d7Brigid Smith    _LOG(log, logtype::REGISTERS,
79aae5d437556d877f2a198601394ba8ea7a559a70Elliott Hughes         "    x%-2d  %016llx  x%-2d  %016llx  x%-2d  %016llx  x%-2d  %016llx\n",
80aae5d437556d877f2a198601394ba8ea7a559a70Elliott Hughes         i, r.regs[i],
81aae5d437556d877f2a198601394ba8ea7a559a70Elliott Hughes         i+1, r.regs[i+1],
82aae5d437556d877f2a198601394ba8ea7a559a70Elliott Hughes         i+2, r.regs[i+2],
83aae5d437556d877f2a198601394ba8ea7a559a70Elliott Hughes         i+3, r.regs[i+3]);
84abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT  }
85abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT
86aae5d437556d877f2a198601394ba8ea7a559a70Elliott Hughes  _LOG(log, logtype::REGISTERS, "    x28  %016llx  x29  %016llx  x30  %016llx\n",
87aae5d437556d877f2a198601394ba8ea7a559a70Elliott Hughes       r.regs[28], r.regs[29], r.regs[30]);
88abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT
89aae5d437556d877f2a198601394ba8ea7a559a70Elliott Hughes  _LOG(log, logtype::REGISTERS, "    sp   %016llx  pc   %016llx  pstate %016llx\n",
90aae5d437556d877f2a198601394ba8ea7a559a70Elliott Hughes       r.sp, r.pc, r.pstate);
91abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT
92abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT  struct user_fpsimd_state f;
93abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT  io.iov_base = &f;
94abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT  io.iov_len = sizeof(f);
95abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT
96abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT  if (ptrace(PTRACE_GETREGSET, tid, (void*) NT_PRFPREG, (void*) &io) == -1) {
97e17f267b2a54d4b1a2b440a94ce06818ece49c7bBrigid Smith    _LOG(log, logtype::ERROR, "ptrace error: %s\n", strerror(errno));
98abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT    return;
99abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT  }
100abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT
101aae5d437556d877f2a198601394ba8ea7a559a70Elliott Hughes  for (int i = 0; i < 32; i += 2) {
102aae5d437556d877f2a198601394ba8ea7a559a70Elliott Hughes    _LOG(log, logtype::FP_REGISTERS,
103aae5d437556d877f2a198601394ba8ea7a559a70Elliott Hughes         "    v%-2d  %016" PRIx64 "%016" PRIx64 "  v%-2d  %016" PRIx64 "%016" PRIx64 "\n",
104aae5d437556d877f2a198601394ba8ea7a559a70Elliott Hughes         i,
105aae5d437556d877f2a198601394ba8ea7a559a70Elliott Hughes         static_cast<uint64_t>(f.vregs[i] >> 64),
106aae5d437556d877f2a198601394ba8ea7a559a70Elliott Hughes         static_cast<uint64_t>(f.vregs[i]),
107aae5d437556d877f2a198601394ba8ea7a559a70Elliott Hughes         i+1,
108aae5d437556d877f2a198601394ba8ea7a559a70Elliott Hughes         static_cast<uint64_t>(f.vregs[i+1] >> 64),
109aae5d437556d877f2a198601394ba8ea7a559a70Elliott Hughes         static_cast<uint64_t>(f.vregs[i+1]));
110abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT  }
111aae5d437556d877f2a198601394ba8ea7a559a70Elliott Hughes  _LOG(log, logtype::FP_REGISTERS, "    fpsr %08x  fpcr %08x\n", f.fpsr, f.fpcr);
112abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT}
113