nativetrace.hh revision 4830:aad1410a2b79
1/* 2 * Copyright (c) 2001-2005 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * Authors: Steve Reinhardt 29 * Nathan Binkert 30 */ 31 32#ifndef __NATIVETRACE_HH__ 33#define __NATIVETRACE_HH__ 34 35#include "base/trace.hh" 36#include "cpu/static_inst.hh" 37#include "sim/host.hh" 38#include "sim/insttracer.hh" 39#include "arch/x86/intregs.hh" 40 41class ThreadContext; 42 43 44namespace Trace { 45 46class NativeTrace; 47 48class NativeTraceRecord : public InstRecord 49{ 50 protected: 51 NativeTrace * parent; 52 53 public: 54 NativeTraceRecord(NativeTrace * _parent, 55 Tick _when, ThreadContext *_thread, 56 const StaticInstPtr &_staticInst, Addr _pc, bool spec) 57 : InstRecord(_when, _thread, _staticInst, _pc, spec), parent(_parent) 58 { 59 } 60 61 void dump(); 62}; 63 64class NativeTrace : public InstTracer 65{ 66 protected: 67 int fd; 68 69 ListenSocket native_listener; 70 71 bool checkRcx; 72 bool checkR11; 73 uint64_t oldRcxVal, oldR11Val; 74 uint64_t oldRealRcxVal, oldRealR11Val; 75 76 struct ThreadState { 77 uint64_t rax; 78 uint64_t rcx; 79 uint64_t rdx; 80 uint64_t rbx; 81 uint64_t rsp; 82 uint64_t rbp; 83 uint64_t rsi; 84 uint64_t rdi; 85 uint64_t r8; 86 uint64_t r9; 87 uint64_t r10; 88 uint64_t r11; 89 uint64_t r12; 90 uint64_t r13; 91 uint64_t r14; 92 uint64_t r15; 93 uint64_t rip; 94 95 void update(int fd) 96 { 97 int bytesLeft = sizeof(ThreadState); 98 int bytesRead = 0; 99 do 100 { 101 int res = read(fd, ((char *)this) + bytesRead, bytesLeft); 102 if(res < 0) 103 panic("Read call failed! %s\n", strerror(errno)); 104 bytesLeft -= res; 105 bytesRead += res; 106 } while(bytesLeft); 107 rax = TheISA::gtoh(rax); 108 rcx = TheISA::gtoh(rcx); 109 rdx = TheISA::gtoh(rdx); 110 rbx = TheISA::gtoh(rbx); 111 rsp = TheISA::gtoh(rsp); 112 rbp = TheISA::gtoh(rbp); 113 rsi = TheISA::gtoh(rsi); 114 rdi = TheISA::gtoh(rdi); 115 r8 = TheISA::gtoh(r8); 116 r9 = TheISA::gtoh(r9); 117 r10 = TheISA::gtoh(r10); 118 r11 = TheISA::gtoh(r11); 119 r12 = TheISA::gtoh(r12); 120 r13 = TheISA::gtoh(r13); 121 r14 = TheISA::gtoh(r14); 122 r15 = TheISA::gtoh(r15); 123 rip = TheISA::gtoh(rip); 124 } 125 126 void update(ThreadContext * tc) 127 { 128 rax = tc->readIntReg(X86ISA::INTREG_RAX); 129 rcx = tc->readIntReg(X86ISA::INTREG_RCX); 130 rdx = tc->readIntReg(X86ISA::INTREG_RDX); 131 rbx = tc->readIntReg(X86ISA::INTREG_RBX); 132 rsp = tc->readIntReg(X86ISA::INTREG_RSP); 133 rbp = tc->readIntReg(X86ISA::INTREG_RBP); 134 rsi = tc->readIntReg(X86ISA::INTREG_RSI); 135 rdi = tc->readIntReg(X86ISA::INTREG_RDI); 136 r8 = tc->readIntReg(X86ISA::INTREG_R8); 137 r9 = tc->readIntReg(X86ISA::INTREG_R9); 138 r10 = tc->readIntReg(X86ISA::INTREG_R10); 139 r11 = tc->readIntReg(X86ISA::INTREG_R11); 140 r12 = tc->readIntReg(X86ISA::INTREG_R12); 141 r13 = tc->readIntReg(X86ISA::INTREG_R13); 142 r14 = tc->readIntReg(X86ISA::INTREG_R14); 143 r15 = tc->readIntReg(X86ISA::INTREG_R15); 144 rip = tc->readNextPC(); 145 } 146 147 }; 148 149 ThreadState nState; 150 ThreadState mState; 151 152 153 public: 154 155 template<class T> 156 bool 157 checkReg(const char * regName, T &val, T &realVal) 158 { 159 if(val != realVal) 160 { 161 DPRINTFN("Register %s should be %#x but is %#x.\n", 162 regName, realVal, val); 163 return false; 164 } 165 return true; 166 } 167 168 bool 169 checkRcxReg(const char * regName, uint64_t &, uint64_t &); 170 171 bool 172 checkR11Reg(const char * regName, uint64_t &, uint64_t &); 173 174 NativeTrace(const std::string & name); 175 176 NativeTraceRecord * 177 getInstRecord(Tick when, ThreadContext *tc, 178 const StaticInstPtr staticInst, Addr pc) 179 { 180 if (tc->misspeculating()) 181 return NULL; 182 183 return new NativeTraceRecord(this, when, tc, 184 staticInst, pc, tc->misspeculating()); 185 } 186 187 void 188 check(ThreadContext *, bool syscall); 189 190 friend class NativeTraceRecord; 191}; 192 193/* namespace Trace */ } 194 195#endif // __EXETRACE_HH__ 196