2006-09-17 09:46:30 +02:00
|
|
|
/*
|
2007-03-06 02:54:20 +01:00
|
|
|
* Copyright (c) 2006-2007 The Regents of The University of Michigan
|
2006-09-17 09:46:30 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met: redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer;
|
|
|
|
* redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution;
|
|
|
|
* neither the name of the copyright holders nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived from
|
|
|
|
* this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* Authors: Gabe Black
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/ptrace.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2011-04-15 19:44:06 +02:00
|
|
|
#include <cerrno>
|
|
|
|
#include <iostream>
|
|
|
|
|
2011-03-03 07:53:11 +01:00
|
|
|
#include "arch/sparc/tracechild.hh"
|
2006-09-17 09:46:30 +02:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2011-03-03 07:53:10 +01:00
|
|
|
bool
|
|
|
|
SparcTraceChild::sendState(int socket)
|
2007-03-15 11:10:50 +01:00
|
|
|
{
|
|
|
|
uint64_t regVal = 0;
|
2011-03-03 07:53:10 +01:00
|
|
|
for (int x = 0; x <= I7; x++) {
|
2007-03-15 11:10:50 +01:00
|
|
|
regVal = getRegVal(x);
|
2011-03-03 07:53:10 +01:00
|
|
|
if (write(socket, ®Val, sizeof(regVal)) == -1) {
|
2007-03-15 11:10:50 +01:00
|
|
|
cerr << "Write failed! " << strerror(errno) << endl;
|
|
|
|
tracing = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
regVal = getRegVal(PC);
|
2011-03-03 07:53:10 +01:00
|
|
|
if (write(socket, ®Val, sizeof(regVal)) == -1) {
|
2007-03-15 11:10:50 +01:00
|
|
|
cerr << "Write failed! " << strerror(errno) << endl;
|
|
|
|
tracing = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
regVal = getRegVal(NPC);
|
2011-03-03 07:53:10 +01:00
|
|
|
if (write(socket, ®Val, sizeof(regVal)) == -1) {
|
2007-03-15 11:10:50 +01:00
|
|
|
cerr << "Write failed! " << strerror(errno) << endl;
|
|
|
|
tracing = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
regVal = getRegVal(CCR);
|
2011-03-03 07:53:10 +01:00
|
|
|
if (write(socket, ®Val, sizeof(regVal)) == -1) {
|
2007-03-15 11:10:50 +01:00
|
|
|
cerr << "Write failed! " << strerror(errno) << endl;
|
|
|
|
tracing = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-03-03 07:53:10 +01:00
|
|
|
int64_t
|
|
|
|
getRegs(regs & myregs, fpu & myfpu, uint64_t * locals,
|
|
|
|
uint64_t * inputs, int num)
|
2006-09-17 09:46:30 +02:00
|
|
|
{
|
|
|
|
assert(num < SparcTraceChild::numregs && num >= 0);
|
2011-03-03 07:53:10 +01:00
|
|
|
switch (num) {
|
|
|
|
//Global registers
|
|
|
|
case SparcTraceChild::G0: return 0;
|
|
|
|
case SparcTraceChild::G1: return myregs.r_g1;
|
|
|
|
case SparcTraceChild::G2: return myregs.r_g2;
|
|
|
|
case SparcTraceChild::G3: return myregs.r_g3;
|
|
|
|
case SparcTraceChild::G4: return myregs.r_g4;
|
|
|
|
case SparcTraceChild::G5: return myregs.r_g5;
|
|
|
|
case SparcTraceChild::G6: return myregs.r_g6;
|
|
|
|
case SparcTraceChild::G7: return myregs.r_g7;
|
|
|
|
//Output registers
|
|
|
|
case SparcTraceChild::O0: return myregs.r_o0;
|
|
|
|
case SparcTraceChild::O1: return myregs.r_o1;
|
|
|
|
case SparcTraceChild::O2: return myregs.r_o2;
|
|
|
|
case SparcTraceChild::O3: return myregs.r_o3;
|
|
|
|
case SparcTraceChild::O4: return myregs.r_o4;
|
|
|
|
case SparcTraceChild::O5: return myregs.r_o5;
|
|
|
|
case SparcTraceChild::O6: return myregs.r_o6;
|
|
|
|
case SparcTraceChild::O7: return myregs.r_o7;
|
|
|
|
//Local registers
|
|
|
|
case SparcTraceChild::L0: return locals[0];
|
|
|
|
case SparcTraceChild::L1: return locals[1];
|
|
|
|
case SparcTraceChild::L2: return locals[2];
|
|
|
|
case SparcTraceChild::L3: return locals[3];
|
|
|
|
case SparcTraceChild::L4: return locals[4];
|
|
|
|
case SparcTraceChild::L5: return locals[5];
|
|
|
|
case SparcTraceChild::L6: return locals[6];
|
|
|
|
case SparcTraceChild::L7: return locals[7];
|
|
|
|
//Input registers
|
|
|
|
case SparcTraceChild::I0: return inputs[0];
|
|
|
|
case SparcTraceChild::I1: return inputs[1];
|
|
|
|
case SparcTraceChild::I2: return inputs[2];
|
|
|
|
case SparcTraceChild::I3: return inputs[3];
|
|
|
|
case SparcTraceChild::I4: return inputs[4];
|
|
|
|
case SparcTraceChild::I5: return inputs[5];
|
|
|
|
case SparcTraceChild::I6: return inputs[6];
|
|
|
|
case SparcTraceChild::I7: return inputs[7];
|
|
|
|
//Floating point
|
|
|
|
case SparcTraceChild::F0: return myfpu.f_fpstatus.fpu_fr[0];
|
|
|
|
case SparcTraceChild::F2: return myfpu.f_fpstatus.fpu_fr[1];
|
|
|
|
case SparcTraceChild::F4: return myfpu.f_fpstatus.fpu_fr[2];
|
|
|
|
case SparcTraceChild::F6: return myfpu.f_fpstatus.fpu_fr[3];
|
|
|
|
case SparcTraceChild::F8: return myfpu.f_fpstatus.fpu_fr[4];
|
|
|
|
case SparcTraceChild::F10: return myfpu.f_fpstatus.fpu_fr[5];
|
|
|
|
case SparcTraceChild::F12: return myfpu.f_fpstatus.fpu_fr[6];
|
|
|
|
case SparcTraceChild::F14: return myfpu.f_fpstatus.fpu_fr[7];
|
|
|
|
case SparcTraceChild::F16: return myfpu.f_fpstatus.fpu_fr[8];
|
|
|
|
case SparcTraceChild::F18: return myfpu.f_fpstatus.fpu_fr[9];
|
|
|
|
case SparcTraceChild::F20: return myfpu.f_fpstatus.fpu_fr[10];
|
|
|
|
case SparcTraceChild::F22: return myfpu.f_fpstatus.fpu_fr[11];
|
|
|
|
case SparcTraceChild::F24: return myfpu.f_fpstatus.fpu_fr[12];
|
|
|
|
case SparcTraceChild::F26: return myfpu.f_fpstatus.fpu_fr[13];
|
|
|
|
case SparcTraceChild::F28: return myfpu.f_fpstatus.fpu_fr[14];
|
|
|
|
case SparcTraceChild::F30: return myfpu.f_fpstatus.fpu_fr[15];
|
|
|
|
case SparcTraceChild::F32: return myfpu.f_fpstatus.fpu_fr[16];
|
|
|
|
case SparcTraceChild::F34: return myfpu.f_fpstatus.fpu_fr[17];
|
|
|
|
case SparcTraceChild::F36: return myfpu.f_fpstatus.fpu_fr[18];
|
|
|
|
case SparcTraceChild::F38: return myfpu.f_fpstatus.fpu_fr[19];
|
|
|
|
case SparcTraceChild::F40: return myfpu.f_fpstatus.fpu_fr[20];
|
|
|
|
case SparcTraceChild::F42: return myfpu.f_fpstatus.fpu_fr[21];
|
|
|
|
case SparcTraceChild::F44: return myfpu.f_fpstatus.fpu_fr[22];
|
|
|
|
case SparcTraceChild::F46: return myfpu.f_fpstatus.fpu_fr[23];
|
|
|
|
case SparcTraceChild::F48: return myfpu.f_fpstatus.fpu_fr[24];
|
|
|
|
case SparcTraceChild::F50: return myfpu.f_fpstatus.fpu_fr[25];
|
|
|
|
case SparcTraceChild::F52: return myfpu.f_fpstatus.fpu_fr[26];
|
|
|
|
case SparcTraceChild::F54: return myfpu.f_fpstatus.fpu_fr[27];
|
|
|
|
case SparcTraceChild::F56: return myfpu.f_fpstatus.fpu_fr[28];
|
|
|
|
case SparcTraceChild::F58: return myfpu.f_fpstatus.fpu_fr[29];
|
|
|
|
case SparcTraceChild::F60: return myfpu.f_fpstatus.fpu_fr[30];
|
|
|
|
case SparcTraceChild::F62: return myfpu.f_fpstatus.fpu_fr[31];
|
|
|
|
//Miscelaneous
|
|
|
|
case SparcTraceChild::FSR: return myfpu.f_fpstatus.Fpu_fsr;
|
|
|
|
case SparcTraceChild::FPRS: return myregs.r_fprs;
|
|
|
|
case SparcTraceChild::PC: return myregs.r_tpc;
|
|
|
|
case SparcTraceChild::NPC: return myregs.r_tnpc;
|
|
|
|
case SparcTraceChild::Y: return myregs.r_y;
|
|
|
|
case SparcTraceChild::CWP:
|
|
|
|
return (myregs.r_tstate >> 0) & ((1 << 5) - 1);
|
|
|
|
case SparcTraceChild::PSTATE:
|
|
|
|
return (myregs.r_tstate >> 8) & ((1 << 13) - 1);
|
|
|
|
case SparcTraceChild::ASI:
|
|
|
|
return (myregs.r_tstate >> 24) & ((1 << 8) - 1);
|
|
|
|
case SparcTraceChild::CCR:
|
|
|
|
return (myregs.r_tstate >> 32) & ((1 << 8) - 1);
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
2006-09-17 09:46:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-03 07:53:10 +01:00
|
|
|
bool
|
|
|
|
SparcTraceChild::update(int pid)
|
2006-09-17 09:46:30 +02:00
|
|
|
{
|
|
|
|
memcpy(&oldregs, &theregs, sizeof(regs));
|
|
|
|
memcpy(&oldfpregs, &thefpregs, sizeof(fpu));
|
|
|
|
memcpy(oldLocals, locals, 8 * sizeof(uint64_t));
|
|
|
|
memcpy(oldInputs, inputs, 8 * sizeof(uint64_t));
|
2011-03-03 07:53:10 +01:00
|
|
|
if (ptrace(PTRACE_GETREGS, pid, &theregs, 0) != 0) {
|
2006-09-17 09:46:30 +02:00
|
|
|
cerr << "Update failed" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-15 11:10:50 +01:00
|
|
|
uint64_t stackPointer = getSP();
|
|
|
|
uint64_t stackBias = 2047;
|
|
|
|
bool v9 = stackPointer % 2;
|
2011-03-03 07:53:10 +01:00
|
|
|
for (unsigned int x = 0; x < 8; x++) {
|
2007-03-15 11:10:50 +01:00
|
|
|
uint64_t localAddr = stackPointer +
|
|
|
|
(v9 ? (stackBias + x * 8) : (x * 4));
|
|
|
|
locals[x] = ptrace(PTRACE_PEEKTEXT, pid, localAddr, 0);
|
2011-03-03 07:53:10 +01:00
|
|
|
if (!v9) locals[x] >>= 32;
|
2007-03-15 11:10:50 +01:00
|
|
|
uint64_t inputAddr = stackPointer +
|
|
|
|
(v9 ? (stackBias + x * 8 + (8 * 8)) : (x * 4 + 8 * 4));
|
|
|
|
inputs[x] = ptrace(PTRACE_PEEKTEXT, pid, inputAddr, 0);
|
2011-03-03 07:53:10 +01:00
|
|
|
if (!v9) inputs[x] >>= 32;
|
2006-09-17 09:46:30 +02:00
|
|
|
}
|
2011-03-03 07:53:10 +01:00
|
|
|
if (ptrace(PTRACE_GETFPREGS, pid, &thefpregs, 0) != 0)
|
2006-09-17 09:46:30 +02:00
|
|
|
return false;
|
2011-03-03 07:53:10 +01:00
|
|
|
for (unsigned int x = 0; x < numregs; x++)
|
2006-09-17 09:46:30 +02:00
|
|
|
regDiffSinceUpdate[x] = (getRegVal(x) != getOldRegVal(x));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
SparcTraceChild::SparcTraceChild()
|
|
|
|
{
|
2011-03-03 07:53:10 +01:00
|
|
|
for (unsigned int x = 0; x < numregs; x++)
|
2006-09-17 09:46:30 +02:00
|
|
|
regDiffSinceUpdate[x] = false;
|
|
|
|
}
|
|
|
|
|
2011-03-03 07:53:10 +01:00
|
|
|
int
|
|
|
|
SparcTraceChild::getTargets(uint32_t inst, uint64_t pc, uint64_t npc,
|
|
|
|
uint64_t &target1, uint64_t &target2)
|
2007-03-06 02:54:20 +01:00
|
|
|
{
|
|
|
|
//We can identify the instruction categories we care about using the top
|
|
|
|
//10 bits of the instruction, excluding the annul bit in the 3rd most
|
|
|
|
//significant bit position and the condition field. We'll call these
|
|
|
|
//bits the "sig" for signature.
|
|
|
|
uint32_t sig = (inst >> 22) & 0x307;
|
|
|
|
uint32_t cond = (inst >> 25) & 0xf;
|
|
|
|
bool annul = (inst & (1 << 29));
|
|
|
|
|
|
|
|
//Check if it's a ba...
|
|
|
|
bool ba = (cond == 0x8) &&
|
|
|
|
(sig == 0x1 || sig == 0x2 || sig == 0x5 || sig == 0x6);
|
|
|
|
//or a bn...
|
|
|
|
bool bn = (cond == 0x0) &&
|
|
|
|
(sig == 0x1 || sig == 0x2 || sig == 0x5 || sig == 0x6);
|
|
|
|
//or a bcc
|
|
|
|
bool bcc = (cond & 0x7) &&
|
|
|
|
(sig == 0x1 || sig == 0x2 || sig == 0x3 || sig == 0x5 || sig == 0x6);
|
|
|
|
|
2011-03-03 07:53:10 +01:00
|
|
|
if (annul) {
|
|
|
|
if (bcc) {
|
2007-03-06 02:54:20 +01:00
|
|
|
target1 = npc;
|
|
|
|
target2 = npc + 4;
|
|
|
|
return 2;
|
2016-02-07 02:21:19 +01:00
|
|
|
} else if (ba) {
|
2007-03-06 02:54:20 +01:00
|
|
|
//This branches immediately to the effective address of the branch
|
|
|
|
//which we'll have to calculate.
|
|
|
|
uint64_t disp = 0;
|
|
|
|
int64_t extender = 0;
|
|
|
|
//Figure out how big the displacement field is, and grab the bits
|
2011-03-03 07:53:10 +01:00
|
|
|
if (sig == 0x1 || sig == 0x5) {
|
2007-03-06 02:54:20 +01:00
|
|
|
disp = inst & ((1 << 19) - 1);
|
|
|
|
extender = 1 << 18;
|
2011-03-03 07:53:10 +01:00
|
|
|
} else {
|
2007-03-06 02:54:20 +01:00
|
|
|
disp = inst & ((1 << 22) - 1);
|
|
|
|
extender = 1 << 21;
|
|
|
|
}
|
|
|
|
//This does sign extension, believe it or not.
|
|
|
|
disp = (disp ^ extender) - extender;
|
|
|
|
//Multiply the displacement by 4. I'm assuming the compiler is
|
|
|
|
//smart enough to turn this into a shift.
|
|
|
|
disp *= 4;
|
|
|
|
target1 = pc + disp;
|
2016-02-07 02:21:19 +01:00
|
|
|
} else if (bn)
|
2007-03-06 02:54:20 +01:00
|
|
|
target1 = npc + 4;
|
|
|
|
else
|
|
|
|
target1 = npc;
|
|
|
|
return 1;
|
2011-03-03 07:53:10 +01:00
|
|
|
} else {
|
2007-03-06 02:54:20 +01:00
|
|
|
target1 = npc;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-03 07:53:10 +01:00
|
|
|
bool
|
|
|
|
SparcTraceChild::step()
|
2006-09-17 09:46:30 +02:00
|
|
|
{
|
2007-03-06 02:54:20 +01:00
|
|
|
//Increment the count of the number of instructions executed
|
|
|
|
instructions++;
|
2006-09-17 09:46:30 +02:00
|
|
|
//Two important considerations are that the address of the instruction
|
|
|
|
//being breakpointed should be word (64bit) aligned, and that both the
|
|
|
|
//next instruction and the instruction after that need to be breakpointed
|
|
|
|
//so that annulled branches will still stop as well.
|
2006-10-19 20:57:06 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Useful constants
|
|
|
|
*/
|
2006-09-17 09:46:30 +02:00
|
|
|
const static uint64_t breakInst = 0x91d02001;
|
2007-03-06 02:54:20 +01:00
|
|
|
const static uint64_t lowBreakInst = breakInst;
|
|
|
|
const static uint64_t highBreakInst = breakInst << 32;
|
2006-09-17 09:46:30 +02:00
|
|
|
const static uint64_t breakWord = breakInst | (breakInst << 32);
|
2006-10-19 20:57:06 +02:00
|
|
|
const static uint64_t lowMask = 0xFFFFFFFFULL;
|
2006-09-17 09:46:30 +02:00
|
|
|
const static uint64_t highMask = lowMask << 32;
|
2006-10-19 20:57:06 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* storage for the original contents of the child process's memory
|
|
|
|
*/
|
2006-09-17 09:46:30 +02:00
|
|
|
uint64_t originalInst, originalAnnulInst;
|
2006-10-19 20:57:06 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get information about where the process is and is headed next.
|
|
|
|
*/
|
|
|
|
uint64_t currentPC = getRegVal(PC);
|
|
|
|
bool unalignedPC = currentPC & 7;
|
|
|
|
uint64_t alignedPC = currentPC & (~7);
|
2006-09-17 09:46:30 +02:00
|
|
|
uint64_t nextPC = getRegVal(NPC);
|
2006-10-19 20:57:06 +02:00
|
|
|
bool unalignedNPC = nextPC & 7;
|
|
|
|
uint64_t alignedNPC = nextPC & (~7);
|
|
|
|
|
2007-03-06 02:54:20 +01:00
|
|
|
//Get the current instruction
|
|
|
|
uint64_t curInst = ptrace(PTRACE_PEEKTEXT, pid, alignedPC);
|
|
|
|
curInst = unalignedPC ? (curInst & 0xffffffffULL) : (curInst >> 32);
|
2006-10-19 20:57:06 +02:00
|
|
|
|
2007-03-06 02:54:20 +01:00
|
|
|
uint64_t bp1, bp2;
|
|
|
|
int numTargets = getTargets(curInst, currentPC, nextPC, bp1, bp2);
|
|
|
|
assert(numTargets == 1 || numTargets == 2);
|
|
|
|
|
|
|
|
bool unalignedBp1 = bp1 & 7;
|
|
|
|
uint64_t alignedBp1 = bp1 & (~7);
|
|
|
|
bool unalignedBp2 = bp2 & 7;
|
|
|
|
uint64_t alignedBp2 = bp2 & (~7);
|
|
|
|
uint64_t origBp1, origBp2;
|
2006-10-19 20:57:06 +02:00
|
|
|
|
|
|
|
/*
|
2007-03-06 02:54:20 +01:00
|
|
|
* Set the first breakpoint
|
2006-10-19 20:57:06 +02:00
|
|
|
*/
|
2007-03-06 02:54:20 +01:00
|
|
|
origBp1 = ptrace(PTRACE_PEEKTEXT, pid, alignedBp1, 0);
|
|
|
|
uint64_t newBp1 = origBp1;
|
|
|
|
newBp1 &= unalignedBp1 ? highMask : lowMask;
|
|
|
|
newBp1 |= unalignedBp1 ? lowBreakInst : highBreakInst;
|
2011-03-03 07:53:10 +01:00
|
|
|
if (ptrace(PTRACE_POKETEXT, pid, alignedBp1, newBp1) != 0)
|
2006-10-19 20:57:06 +02:00
|
|
|
cerr << "Poke failed" << endl;
|
2007-03-06 02:54:20 +01:00
|
|
|
/*
|
|
|
|
* Set the second breakpoint if necessary
|
|
|
|
*/
|
2011-03-03 07:53:10 +01:00
|
|
|
if (numTargets == 2) {
|
2007-03-06 02:54:20 +01:00
|
|
|
origBp2 = ptrace(PTRACE_PEEKTEXT, pid, alignedBp2, 0);
|
|
|
|
uint64_t newBp2 = origBp2;
|
|
|
|
newBp2 &= unalignedBp2 ? highMask : lowMask;
|
|
|
|
newBp2 |= unalignedBp2 ? lowBreakInst : highBreakInst;
|
2011-03-03 07:53:10 +01:00
|
|
|
if (ptrace(PTRACE_POKETEXT, pid, alignedBp2, newBp2) != 0)
|
2006-09-17 09:46:30 +02:00
|
|
|
cerr << "Poke failed" << endl;
|
|
|
|
}
|
2006-10-19 20:57:06 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Restart the child process
|
|
|
|
*/
|
2006-09-17 09:46:30 +02:00
|
|
|
//Note that the "addr" parameter is supposed to be ignored, but in at
|
|
|
|
//least one version of the kernel, it must be 1 or it will set what
|
|
|
|
//pc to continue from
|
2011-03-03 07:53:10 +01:00
|
|
|
if (ptrace(PTRACE_CONT, pid, 1, 0) != 0)
|
2006-09-17 09:46:30 +02:00
|
|
|
cerr << "Cont failed" << endl;
|
|
|
|
doWait();
|
2006-10-19 20:57:06 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Update our record of the child's state
|
|
|
|
*/
|
2006-09-17 09:46:30 +02:00
|
|
|
update(pid);
|
2006-10-19 20:57:06 +02:00
|
|
|
|
|
|
|
/*
|
2007-03-06 02:54:20 +01:00
|
|
|
* Put back the original contents of the childs address space in the
|
|
|
|
* reverse order.
|
2006-10-19 20:57:06 +02:00
|
|
|
*/
|
2011-03-03 07:53:10 +01:00
|
|
|
if (numTargets == 2) {
|
|
|
|
if (ptrace(PTRACE_POKETEXT, pid, alignedBp2, origBp2) != 0)
|
2007-03-06 02:54:20 +01:00
|
|
|
cerr << "Poke failed" << endl;
|
2006-09-17 09:46:30 +02:00
|
|
|
}
|
2011-03-03 07:53:10 +01:00
|
|
|
if (ptrace(PTRACE_POKETEXT, pid, alignedBp1, origBp1) != 0)
|
2007-03-06 02:54:20 +01:00
|
|
|
cerr << "Poke failed" << endl;
|
2006-09-17 09:46:30 +02:00
|
|
|
}
|
|
|
|
|
2011-03-03 07:53:10 +01:00
|
|
|
int64_t
|
|
|
|
SparcTraceChild::getRegVal(int num)
|
2006-09-17 09:46:30 +02:00
|
|
|
{
|
|
|
|
return getRegs(theregs, thefpregs, locals, inputs, num);
|
|
|
|
}
|
|
|
|
|
2011-03-03 07:53:10 +01:00
|
|
|
int64_t
|
|
|
|
SparcTraceChild::getOldRegVal(int num)
|
2006-09-17 09:46:30 +02:00
|
|
|
{
|
|
|
|
return getRegs(oldregs, oldfpregs, oldLocals, oldInputs, num);
|
|
|
|
}
|
|
|
|
|
2011-03-03 07:53:10 +01:00
|
|
|
ostream &
|
|
|
|
SparcTraceChild::outputStartState(ostream & os)
|
2006-09-17 09:46:30 +02:00
|
|
|
{
|
2007-03-06 02:54:20 +01:00
|
|
|
bool v8 = false;
|
2006-09-17 09:46:30 +02:00
|
|
|
uint64_t sp = getSP();
|
2011-03-03 07:53:10 +01:00
|
|
|
if (sp % 2) {
|
2007-03-06 02:54:20 +01:00
|
|
|
os << "Detected a 64 bit executable.\n";
|
|
|
|
v8 = false;
|
2011-03-03 07:53:10 +01:00
|
|
|
} else {
|
2007-03-06 02:54:20 +01:00
|
|
|
os << "Detected a 32 bit executable.\n";
|
|
|
|
v8 = true;
|
|
|
|
}
|
2006-09-17 09:46:30 +02:00
|
|
|
uint64_t pc = getPC();
|
|
|
|
char obuf[1024];
|
|
|
|
sprintf(obuf, "Initial stack pointer = 0x%016llx\n", sp);
|
|
|
|
os << obuf;
|
|
|
|
sprintf(obuf, "Initial program counter = 0x%016llx\n", pc);
|
|
|
|
os << obuf;
|
2011-03-03 07:53:10 +01:00
|
|
|
if (!v8) {
|
2007-03-06 02:54:20 +01:00
|
|
|
//Take out the stack bias
|
|
|
|
sp += 2047;
|
|
|
|
}
|
2006-09-17 09:46:30 +02:00
|
|
|
//Output the window save area
|
2011-03-03 07:53:10 +01:00
|
|
|
for (unsigned int x = 0; x < 16; x++) {
|
2006-09-17 09:46:30 +02:00
|
|
|
uint64_t regspot = ptrace(PTRACE_PEEKDATA, pid, sp, 0);
|
2011-03-03 07:53:10 +01:00
|
|
|
if (v8) regspot = regspot >> 32;
|
2006-09-17 09:46:30 +02:00
|
|
|
sprintf(obuf, "0x%016llx: Window save %d = 0x%016llx\n",
|
|
|
|
sp, x+1, regspot);
|
|
|
|
os << obuf;
|
2007-03-06 02:54:20 +01:00
|
|
|
sp += v8 ? 4 : 8;
|
2006-09-17 09:46:30 +02:00
|
|
|
}
|
|
|
|
//Output the argument count
|
|
|
|
uint64_t cargc = ptrace(PTRACE_PEEKDATA, pid, sp, 0);
|
2011-03-03 07:53:10 +01:00
|
|
|
if (v8) cargc = cargc >> 32;
|
2006-09-17 09:46:30 +02:00
|
|
|
sprintf(obuf, "0x%016llx: Argc = 0x%016llx\n", sp, cargc);
|
|
|
|
os << obuf;
|
2007-03-06 02:54:20 +01:00
|
|
|
sp += v8 ? 4 : 8;
|
2006-09-17 09:46:30 +02:00
|
|
|
//Output argv pointers
|
|
|
|
int argCount = 0;
|
|
|
|
uint64_t cargv;
|
2011-03-03 07:53:10 +01:00
|
|
|
do {
|
2006-09-17 09:46:30 +02:00
|
|
|
cargv = ptrace(PTRACE_PEEKDATA, pid, sp, 0);
|
2011-03-03 07:53:10 +01:00
|
|
|
if (v8) cargv = cargv >> 32;
|
2006-09-17 09:46:30 +02:00
|
|
|
sprintf(obuf, "0x%016llx: argv[%d] = 0x%016llx\n",
|
|
|
|
sp, argCount++, cargv);
|
|
|
|
os << obuf;
|
2007-03-06 02:54:20 +01:00
|
|
|
sp += v8 ? 4 : 8;
|
2016-02-07 02:21:19 +01:00
|
|
|
} while (cargv);
|
2006-09-17 09:46:30 +02:00
|
|
|
//Output the envp pointers
|
|
|
|
int envCount = 0;
|
|
|
|
uint64_t cenvp;
|
2011-03-03 07:53:10 +01:00
|
|
|
do {
|
2006-09-17 09:46:30 +02:00
|
|
|
cenvp = ptrace(PTRACE_PEEKDATA, pid, sp, 0);
|
2011-03-03 07:53:10 +01:00
|
|
|
if (v8) cenvp = cenvp >> 32;
|
2006-09-17 09:46:30 +02:00
|
|
|
sprintf(obuf, "0x%016llx: envp[%d] = 0x%016llx\n",
|
|
|
|
sp, envCount++, cenvp);
|
|
|
|
os << obuf;
|
2007-03-06 02:54:20 +01:00
|
|
|
sp += v8 ? 4 : 8;
|
2011-03-03 07:53:10 +01:00
|
|
|
} while (cenvp);
|
2006-09-17 09:46:30 +02:00
|
|
|
uint64_t auxType, auxVal;
|
2011-03-03 07:53:10 +01:00
|
|
|
do {
|
2006-09-17 09:46:30 +02:00
|
|
|
auxType = ptrace(PTRACE_PEEKDATA, pid, sp, 0);
|
2011-03-03 07:53:10 +01:00
|
|
|
if (v8) auxType = auxType >> 32;
|
2007-03-06 02:54:20 +01:00
|
|
|
sp += (v8 ? 4 : 8);
|
2006-09-17 09:46:30 +02:00
|
|
|
auxVal = ptrace(PTRACE_PEEKDATA, pid, sp, 0);
|
2011-03-03 07:53:10 +01:00
|
|
|
if (v8) auxVal = auxVal >> 32;
|
2007-03-06 02:54:20 +01:00
|
|
|
sp += (v8 ? 4 : 8);
|
2006-09-17 09:46:30 +02:00
|
|
|
sprintf(obuf, "0x%016llx: Auxiliary vector = {0x%016llx, 0x%016llx}\n",
|
2007-03-06 02:54:20 +01:00
|
|
|
sp - 8, auxType, auxVal);
|
2006-09-17 09:46:30 +02:00
|
|
|
os << obuf;
|
2011-03-03 07:53:10 +01:00
|
|
|
} while (auxType != 0 || auxVal != 0);
|
2006-09-17 09:46:30 +02:00
|
|
|
//Print out the argument strings, environment strings, and file name.
|
|
|
|
string current;
|
|
|
|
uint64_t buf;
|
|
|
|
uint64_t currentStart = sp;
|
|
|
|
bool clearedInitialPadding = false;
|
2011-03-03 07:53:10 +01:00
|
|
|
do {
|
2006-09-17 09:46:30 +02:00
|
|
|
buf = ptrace(PTRACE_PEEKDATA, pid, sp, 0);
|
|
|
|
char * cbuf = (char *)&buf;
|
2011-03-03 07:53:10 +01:00
|
|
|
for (int x = 0; x < sizeof(uint32_t); x++) {
|
|
|
|
if (cbuf[x])
|
2006-09-17 09:46:30 +02:00
|
|
|
current += cbuf[x];
|
2011-03-03 07:53:10 +01:00
|
|
|
else {
|
2006-09-17 09:46:30 +02:00
|
|
|
sprintf(obuf, "0x%016llx: \"%s\"\n",
|
|
|
|
currentStart, current.c_str());
|
|
|
|
os << obuf;
|
|
|
|
current = "";
|
|
|
|
currentStart = sp + x + 1;
|
|
|
|
}
|
|
|
|
}
|
2007-03-06 02:54:20 +01:00
|
|
|
sp += (v8 ? 4 : 8);
|
2006-09-17 09:46:30 +02:00
|
|
|
clearedInitialPadding = clearedInitialPadding || buf != 0;
|
2011-03-03 07:53:10 +01:00
|
|
|
} while (!clearedInitialPadding || buf != 0);
|
2006-09-17 09:46:30 +02:00
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2011-03-03 07:53:10 +01:00
|
|
|
TraceChild *
|
|
|
|
genTraceChild()
|
2006-09-17 09:46:30 +02:00
|
|
|
{
|
|
|
|
return new SparcTraceChild;
|
|
|
|
}
|
|
|
|
|