blob: c5f9259297c9ca7c63d0451f13ea846169601e0c [file] [log] [blame]
Elliott Hughes18ddd422013-11-14 17:06:46 -08001/*
Christopher Ferris20303f82014-01-10 16:33:16 -08002 * Copyright 2006, 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 */
Bruce Beare6cc49232010-10-13 16:11:15 -070016
Bruce Beare6cc49232010-10-13 16:11:15 -070017#include <errno.h>
Christopher Ferris0c3f1ae2015-05-22 14:26:13 -070018#include <stdint.h>
19#include <string.h>
Bruce Beare6cc49232010-10-13 16:11:15 -070020#include <sys/ptrace.h>
Bruce Beare6cc49232010-10-13 16:11:15 -070021
Christopher Ferris0c3f1ae2015-05-22 14:26:13 -070022#include <backtrace/Backtrace.h>
Bruce Beare6cc49232010-10-13 16:11:15 -070023
Christopher Ferris0c3f1ae2015-05-22 14:26:13 -070024#include "machine.h"
25#include "utility.h"
26
27void dump_memory_and_code(log_t* log, Backtrace* backtrace) {
28 struct pt_regs r;
29 if (ptrace(PTRACE_GETREGS, backtrace->Tid(), 0, &r) == -1) {
30 _LOG(log, logtype::ERROR, "cannot get registers: %s\n", strerror(errno));
31 return;
32 }
33
34 dump_memory(log, backtrace, static_cast<uintptr_t>(r.eax), "memory near eax:");
35 dump_memory(log, backtrace, static_cast<uintptr_t>(r.ebx), "memory near ebx:");
36 dump_memory(log, backtrace, static_cast<uintptr_t>(r.ecx), "memory near ecx:");
37 dump_memory(log, backtrace, static_cast<uintptr_t>(r.edx), "memory near edx:");
38 dump_memory(log, backtrace, static_cast<uintptr_t>(r.esi), "memory near esi:");
39 dump_memory(log, backtrace, static_cast<uintptr_t>(r.edi), "memory near edi:");
40
41 dump_memory(log, backtrace, static_cast<uintptr_t>(r.eip), "code around eip:");
Jeff Brown053b8652012-06-06 16:25:03 -070042}
43
Brigid Smith62ba4892014-06-10 11:53:08 -070044void dump_registers(log_t* log, pid_t tid) {
Christopher Ferris20303f82014-01-10 16:33:16 -080045 struct pt_regs r;
46 if (ptrace(PTRACE_GETREGS, tid, 0, &r) == -1) {
Brigid Smithe17f2672014-06-19 11:33:38 -070047 _LOG(log, logtype::ERROR, "cannot get registers: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -080048 return;
49 }
Christopher Ferris0c3f1ae2015-05-22 14:26:13 -070050
Brigid Smith62ba4892014-06-10 11:53:08 -070051 _LOG(log, logtype::REGISTERS, " eax %08lx ebx %08lx ecx %08lx edx %08lx\n",
Christopher Ferris20303f82014-01-10 16:33:16 -080052 r.eax, r.ebx, r.ecx, r.edx);
Brigid Smith62ba4892014-06-10 11:53:08 -070053 _LOG(log, logtype::REGISTERS, " esi %08lx edi %08lx\n",
Christopher Ferris20303f82014-01-10 16:33:16 -080054 r.esi, r.edi);
Brigid Smith62ba4892014-06-10 11:53:08 -070055 _LOG(log, logtype::REGISTERS, " xcs %08x xds %08x xes %08x xfs %08x xss %08x\n",
Christopher Ferris20303f82014-01-10 16:33:16 -080056 r.xcs, r.xds, r.xes, r.xfs, r.xss);
Brigid Smith62ba4892014-06-10 11:53:08 -070057 _LOG(log, logtype::REGISTERS, " eip %08lx ebp %08lx esp %08lx flags %08lx\n",
Christopher Ferris20303f82014-01-10 16:33:16 -080058 r.eip, r.ebp, r.esp, r.eflags);
Bruce Beare6cc49232010-10-13 16:11:15 -070059}