blob: fc86bc2b93c003bc0a4467ccd6682a89f25747c8 [file] [log] [blame]
Pavel Chupinc6c194c2013-11-21 23:17:20 +04001/*
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
Christopher Ferrisb36b5922015-06-17 18:35:59 -070017#define LOG_TAG "DEBUG"
18
Pavel Chupinc6c194c2013-11-21 23:17:20 +040019#include <errno.h>
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070020#include <stdint.h>
Pavel Chupinc6c194c2013-11-21 23:17:20 +040021#include <sys/ptrace.h>
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070022#include <string.h>
Pavel Chupinc6c194c2013-11-21 23:17:20 +040023#include <sys/user.h>
24
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070025#include <backtrace/Backtrace.h>
Christopher Ferrisb36b5922015-06-17 18:35:59 -070026#include <log/log.h>
Pavel Chupinc6c194c2013-11-21 23:17:20 +040027
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070028#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) {
Christopher Ferrisb36b5922015-06-17 18:35:59 -070034 ALOGE("cannot get registers: %s\n", strerror(errno));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070035 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:");
Pavel Chupinc6c194c2013-11-21 23:17:20 +040046}
47
Brigid Smith62ba4892014-06-10 11:53:08 -070048void dump_registers(log_t* log, pid_t tid) {
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070049 struct user_regs_struct r;
50 if (ptrace(PTRACE_GETREGS, tid, 0, &r) == -1) {
Christopher Ferrisb36b5922015-06-17 18:35:59 -070051 ALOGE("cannot get registers: %s\n", strerror(errno));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070052 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);
Pavel Chupinc6c194c2013-11-21 23:17:20 +040067}