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 18b40c50351ebd9fb40b76a3169ad5cc6a25c453f1Elliott Hughes#include <elf.h> 19abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT#include <errno.h> 200c3f1ae66b693ef0f37066b697cc10a07ef56accChristopher Ferris#include <stdint.h> 21b40c50351ebd9fb40b76a3169ad5cc6a25c453f1Elliott Hughes#include <string.h> 22abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT#include <sys/ptrace.h> 23abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT#include <sys/uio.h> 24abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT 250c3f1ae66b693ef0f37066b697cc10a07ef56accChristopher Ferris#include <backtrace/Backtrace.h> 26abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT 270c3f1ae66b693ef0f37066b697cc10a07ef56accChristopher Ferris#include "machine.h" 280c3f1ae66b693ef0f37066b697cc10a07ef56accChristopher Ferris#include "utility.h" 29abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT 300c3f1ae66b693ef0f37066b697cc10a07ef56accChristopher Ferrisvoid dump_memory_and_code(log_t* log, Backtrace* backtrace) { 310c3f1ae66b693ef0f37066b697cc10a07ef56accChristopher Ferris struct user_pt_regs regs; 320c3f1ae66b693ef0f37066b697cc10a07ef56accChristopher Ferris struct iovec io; 330c3f1ae66b693ef0f37066b697cc10a07ef56accChristopher Ferris io.iov_base = ®s; 340c3f1ae66b693ef0f37066b697cc10a07ef56accChristopher Ferris io.iov_len = sizeof(regs); 35abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT 360c3f1ae66b693ef0f37066b697cc10a07ef56accChristopher Ferris if (ptrace(PTRACE_GETREGSET, backtrace->Tid(), reinterpret_cast<void*>(NT_PRSTATUS), &io) == -1) { 370c3f1ae66b693ef0f37066b697cc10a07ef56accChristopher Ferris _LOG(log, logtype::ERROR, "%s: ptrace failed to get registers: %s", 380c3f1ae66b693ef0f37066b697cc10a07ef56accChristopher Ferris __func__, strerror(errno)); 390c3f1ae66b693ef0f37066b697cc10a07ef56accChristopher Ferris return; 400c3f1ae66b693ef0f37066b697cc10a07ef56accChristopher Ferris } 4162ba489ba00a2689d4e257bc178cff87495f99d7Brigid Smith 420c3f1ae66b693ef0f37066b697cc10a07ef56accChristopher Ferris for (int reg = 0; reg < 31; reg++) { 430c3f1ae66b693ef0f37066b697cc10a07ef56accChristopher Ferris dump_memory(log, backtrace, regs.regs[reg], "memory near x%d:", reg); 440c3f1ae66b693ef0f37066b697cc10a07ef56accChristopher Ferris } 45abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT 460c3f1ae66b693ef0f37066b697cc10a07ef56accChristopher Ferris dump_memory(log, backtrace, static_cast<uintptr_t>(regs.pc), "code around pc:"); 47abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT 480c3f1ae66b693ef0f37066b697cc10a07ef56accChristopher Ferris if (regs.pc != regs.sp) { 490c3f1ae66b693ef0f37066b697cc10a07ef56accChristopher Ferris dump_memory(log, backtrace, static_cast<uintptr_t>(regs.sp), "code around sp:"); 500c3f1ae66b693ef0f37066b697cc10a07ef56accChristopher Ferris } 51abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT} 52abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT 5362ba489ba00a2689d4e257bc178cff87495f99d7Brigid Smithvoid dump_registers(log_t* log, pid_t tid) { 54abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT struct user_pt_regs r; 55abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT struct iovec io; 56abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT io.iov_base = &r; 57abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT io.iov_len = sizeof(r); 58abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT 59abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT if (ptrace(PTRACE_GETREGSET, tid, (void*) NT_PRSTATUS, (void*) &io) == -1) { 60e17f267b2a54d4b1a2b440a94ce06818ece49c7bBrigid Smith _LOG(log, logtype::ERROR, "ptrace error: %s\n", strerror(errno)); 61abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT return; 62abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT } 63abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT 64abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT for (int i = 0; i < 28; i += 4) { 6562ba489ba00a2689d4e257bc178cff87495f99d7Brigid Smith _LOG(log, logtype::REGISTERS, 66b40c50351ebd9fb40b76a3169ad5cc6a25c453f1Elliott Hughes " x%-2d %016llx x%-2d %016llx x%-2d %016llx x%-2d %016llx\n", 67b40c50351ebd9fb40b76a3169ad5cc6a25c453f1Elliott Hughes i, r.regs[i], 68b40c50351ebd9fb40b76a3169ad5cc6a25c453f1Elliott Hughes i+1, r.regs[i+1], 69b40c50351ebd9fb40b76a3169ad5cc6a25c453f1Elliott Hughes i+2, r.regs[i+2], 70b40c50351ebd9fb40b76a3169ad5cc6a25c453f1Elliott Hughes i+3, r.regs[i+3]); 71abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT } 72abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT 73b40c50351ebd9fb40b76a3169ad5cc6a25c453f1Elliott Hughes _LOG(log, logtype::REGISTERS, " x28 %016llx x29 %016llx x30 %016llx\n", 74b40c50351ebd9fb40b76a3169ad5cc6a25c453f1Elliott Hughes r.regs[28], r.regs[29], r.regs[30]); 75abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT 76b40c50351ebd9fb40b76a3169ad5cc6a25c453f1Elliott Hughes _LOG(log, logtype::REGISTERS, " sp %016llx pc %016llx pstate %016llx\n", 77b40c50351ebd9fb40b76a3169ad5cc6a25c453f1Elliott Hughes r.sp, r.pc, r.pstate); 78abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT 79abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT struct user_fpsimd_state f; 80abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT io.iov_base = &f; 81abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT io.iov_len = sizeof(f); 82abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT 83abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT if (ptrace(PTRACE_GETREGSET, tid, (void*) NT_PRFPREG, (void*) &io) == -1) { 84e17f267b2a54d4b1a2b440a94ce06818ece49c7bBrigid Smith _LOG(log, logtype::ERROR, "ptrace error: %s\n", strerror(errno)); 85abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT return; 86abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT } 87abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT 88b40c50351ebd9fb40b76a3169ad5cc6a25c453f1Elliott Hughes for (int i = 0; i < 32; i += 2) { 89b40c50351ebd9fb40b76a3169ad5cc6a25c453f1Elliott Hughes _LOG(log, logtype::FP_REGISTERS, 90b40c50351ebd9fb40b76a3169ad5cc6a25c453f1Elliott Hughes " v%-2d %016" PRIx64 "%016" PRIx64 " v%-2d %016" PRIx64 "%016" PRIx64 "\n", 91b40c50351ebd9fb40b76a3169ad5cc6a25c453f1Elliott Hughes i, 92b40c50351ebd9fb40b76a3169ad5cc6a25c453f1Elliott Hughes static_cast<uint64_t>(f.vregs[i] >> 64), 93b40c50351ebd9fb40b76a3169ad5cc6a25c453f1Elliott Hughes static_cast<uint64_t>(f.vregs[i]), 94b40c50351ebd9fb40b76a3169ad5cc6a25c453f1Elliott Hughes i+1, 95b40c50351ebd9fb40b76a3169ad5cc6a25c453f1Elliott Hughes static_cast<uint64_t>(f.vregs[i+1] >> 64), 96b40c50351ebd9fb40b76a3169ad5cc6a25c453f1Elliott Hughes static_cast<uint64_t>(f.vregs[i+1])); 97abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT } 98b40c50351ebd9fb40b76a3169ad5cc6a25c453f1Elliott Hughes _LOG(log, logtype::FP_REGISTERS, " fpsr %08x fpcr %08x\n", f.fpsr, f.fpcr); 99abc60c26b7448e6b2842351688a7a823b8b787d6Kévin PETIT} 100