1/*
2** Copyright 2013, The Android Open Source Project
3**
4** Licensed under the Apache License, Version 2.0 (the "License");
5** you may not use this file except in compliance with the License.
6** You may obtain a copy of the License at
7**
8**     http://www.apache.org/licenses/LICENSE-2.0
9**
10** Unless required by applicable law or agreed to in writing, software
11** distributed under the License is distributed on an "AS IS" BASIS,
12** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13** See the License for the specific language governing permissions and
14** limitations under the License.
15*/
16
17#define LOG_TAG "DEBUG"
18
19#include <errno.h>
20#include <stdint.h>
21#include <string.h>
22#include <sys/ptrace.h>
23#include <sys/user.h>
24
25#include <backtrace/Backtrace.h>
26#include <log/log.h>
27
28#include "machine.h"
29#include "utility.h"
30
31void dump_memory_and_code(log_t* log, Backtrace* backtrace) {
32  struct user_regs_struct r;
33  if (ptrace(PTRACE_GETREGS, backtrace->Tid(), 0, &r) == -1) {
34    ALOGE("cannot get registers: %s\n", strerror(errno));
35    return;
36  }
37
38  dump_memory(log, backtrace, static_cast<uintptr_t>(r.rax), "memory near rax:");
39  dump_memory(log, backtrace, static_cast<uintptr_t>(r.rbx), "memory near rbx:");
40  dump_memory(log, backtrace, static_cast<uintptr_t>(r.rcx), "memory near rcx:");
41  dump_memory(log, backtrace, static_cast<uintptr_t>(r.rdx), "memory near rdx:");
42  dump_memory(log, backtrace, static_cast<uintptr_t>(r.rsi), "memory near rsi:");
43  dump_memory(log, backtrace, static_cast<uintptr_t>(r.rdi), "memory near rdi:");
44
45  dump_memory(log, backtrace, static_cast<uintptr_t>(r.rip), "code around rip:");
46}
47
48void dump_registers(log_t* log, pid_t tid) {
49  struct user_regs_struct r;
50  if (ptrace(PTRACE_GETREGS, tid, 0, &r) == -1) {
51    ALOGE("cannot get registers: %s\n", strerror(errno));
52    return;
53  }
54
55  _LOG(log, logtype::REGISTERS, "    rax %016lx  rbx %016lx  rcx %016lx  rdx %016lx\n",
56       r.rax, r.rbx, r.rcx, r.rdx);
57  _LOG(log, logtype::REGISTERS, "    rsi %016lx  rdi %016lx\n",
58       r.rsi, r.rdi);
59  _LOG(log, logtype::REGISTERS, "    r8  %016lx  r9  %016lx  r10 %016lx  r11 %016lx\n",
60       r.r8, r.r9, r.r10, r.r11);
61  _LOG(log, logtype::REGISTERS, "    r12 %016lx  r13 %016lx  r14 %016lx  r15 %016lx\n",
62       r.r12, r.r13, r.r14, r.r15);
63  _LOG(log, logtype::REGISTERS, "    cs  %016lx  ss  %016lx\n",
64       r.cs, r.ss);
65  _LOG(log, logtype::REGISTERS, "    rip %016lx  rbp %016lx  rsp %016lx  eflags %016lx\n",
66       r.rip, r.rbp, r.rsp, r.eflags);
67}
68