2009-07-27 09:53:39 +02:00
|
|
|
/*
|
2010-06-02 19:58:17 +02:00
|
|
|
* Copyright (c) 2010 ARM Limited
|
|
|
|
* All rights reserved
|
|
|
|
*
|
|
|
|
* The license below extends only to copyright in the software and shall
|
|
|
|
* not be construed as granting a license to any other intellectual
|
|
|
|
* property including but not limited to intellectual property relating
|
|
|
|
* to a hardware implementation of the functionality of the software
|
|
|
|
* licensed hereunder. You may use the software subject to the license
|
|
|
|
* terms below provided that you ensure that this notice is replicated
|
|
|
|
* unmodified and in its entirety in all distributions of the software,
|
|
|
|
* modified or unmodified, in source code or in binary form.
|
|
|
|
*
|
2009-07-27 09:53:39 +02:00
|
|
|
* Copyright (c) 2006-2009 The Regents of The University of Michigan
|
|
|
|
* 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: Ali Saidi
|
|
|
|
* Gabe Black
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2011-04-15 19:44:06 +02:00
|
|
|
|
|
|
|
#include <cerrno>
|
2010-06-02 19:58:17 +02:00
|
|
|
#include <cstdio>
|
2011-04-15 19:44:06 +02:00
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
2009-07-27 09:53:39 +02:00
|
|
|
|
2011-03-03 07:53:11 +01:00
|
|
|
#include "arch/arm/tracechild.hh"
|
2009-07-27 09:53:39 +02:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
ARMTraceChild::ARMTraceChild()
|
|
|
|
{
|
2010-06-02 19:58:17 +02:00
|
|
|
foundMvn = false;
|
|
|
|
|
2011-05-05 03:38:26 +02:00
|
|
|
memset(®s, 0, sizeof(regs));
|
|
|
|
memset(&oldregs, 0, sizeof(regs));
|
|
|
|
memset(&fpregs, 0, sizeof(vfp_regs));
|
|
|
|
memset(&oldfpregs, 0, sizeof(vfp_regs));
|
|
|
|
|
2009-07-27 09:54:30 +02:00
|
|
|
for (int x = 0; x < numregs; x++) {
|
2009-07-27 09:53:39 +02:00
|
|
|
regDiffSinceUpdate[x] = false;
|
2009-07-27 09:54:30 +02:00
|
|
|
}
|
2011-05-05 03:38:26 +02:00
|
|
|
|
|
|
|
assert(sizeof(regs.uregs)/sizeof(regs.uregs[0]) > CPSR);
|
2009-07-27 09:53:39 +02:00
|
|
|
}
|
|
|
|
|
2011-03-03 07:53:10 +01:00
|
|
|
bool
|
|
|
|
ARMTraceChild::sendState(int socket)
|
2009-07-27 09:53:39 +02:00
|
|
|
{
|
|
|
|
uint32_t regVal = 0;
|
2011-05-05 03:38:26 +02:00
|
|
|
uint64_t message[numregs + 1];
|
2009-07-27 09:54:30 +02:00
|
|
|
int pos = 1;
|
|
|
|
message[0] = 0;
|
|
|
|
for (int x = 0; x < numregs; x++) {
|
|
|
|
if (regDiffSinceUpdate[x]) {
|
2011-05-05 03:38:26 +02:00
|
|
|
message[0] = message[0] | (1ULL << x);
|
2009-07-27 09:54:30 +02:00
|
|
|
message[pos++] = getRegVal(x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t sent = 0;
|
|
|
|
size_t toSend = pos * sizeof(message[0]);
|
|
|
|
uint8_t *messagePtr = (uint8_t *)message;
|
|
|
|
while (toSend != 0) {
|
|
|
|
sent = write(socket, messagePtr, toSend);
|
|
|
|
if (sent == -1) {
|
2009-07-27 09:53:39 +02:00
|
|
|
cerr << "Write failed! " << strerror(errno) << endl;
|
|
|
|
tracing = false;
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-27 09:54:30 +02:00
|
|
|
toSend -= sent;
|
|
|
|
messagePtr += sent;
|
2009-07-27 09:53:39 +02:00
|
|
|
}
|
2010-06-02 19:58:17 +02:00
|
|
|
|
2009-07-27 09:53:39 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-03-03 07:53:10 +01:00
|
|
|
uint32_t
|
|
|
|
ARMTraceChild::getRegs(user_regs &myregs, int num)
|
2009-07-27 09:53:39 +02:00
|
|
|
{
|
2011-05-05 03:38:26 +02:00
|
|
|
assert(num <= CPSR && num >= 0);
|
2009-07-27 09:53:39 +02:00
|
|
|
return myregs.uregs[num];
|
|
|
|
}
|
|
|
|
|
2011-05-05 03:38:26 +02:00
|
|
|
uint64_t
|
|
|
|
ARMTraceChild::getFpRegs(vfp_regs &my_fp_regs, int num)
|
|
|
|
{
|
|
|
|
assert(num >= F0 && num < numregs);
|
|
|
|
if (num == FPSCR)
|
|
|
|
return my_fp_regs.fpscr;
|
|
|
|
|
|
|
|
num -= F0;
|
|
|
|
return my_fp_regs.fpregs[num];
|
|
|
|
}
|
|
|
|
|
2011-03-03 07:53:10 +01:00
|
|
|
bool
|
|
|
|
ARMTraceChild::update(int pid)
|
2009-07-27 09:53:39 +02:00
|
|
|
{
|
|
|
|
oldregs = regs;
|
2011-03-03 07:53:10 +01:00
|
|
|
if (ptrace(PTRACE_GETREGS, pid, 0, ®s) != 0) {
|
2009-07-27 09:53:39 +02:00
|
|
|
cerr << "update: " << strerror(errno) << endl;
|
|
|
|
return false;
|
|
|
|
}
|
2010-06-02 19:58:17 +02:00
|
|
|
|
2011-05-05 03:38:26 +02:00
|
|
|
const uint32_t get_vfp_regs = 32;
|
|
|
|
|
|
|
|
oldfpregs = fpregs;
|
|
|
|
if (ptrace((__ptrace_request)get_vfp_regs, pid, 0, &fpregs) != 0) {
|
|
|
|
cerr << "update: " << strerror(errno) << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-03-03 07:53:10 +01:00
|
|
|
for (unsigned int x = 0; x < numregs; x++)
|
2009-07-27 09:53:39 +02:00
|
|
|
regDiffSinceUpdate[x] = (getRegVal(x) != getOldRegVal(x));
|
2011-05-05 03:38:26 +02:00
|
|
|
|
2009-07-27 09:53:39 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-03-03 07:53:10 +01:00
|
|
|
int64_t
|
|
|
|
ARMTraceChild::getRegVal(int num)
|
2009-07-27 09:53:39 +02:00
|
|
|
{
|
2011-05-05 03:38:26 +02:00
|
|
|
if (num <= CPSR)
|
|
|
|
return getRegs(regs, num);
|
|
|
|
else
|
|
|
|
return (int64_t)getFpRegs(fpregs, num);
|
2009-07-27 09:53:39 +02:00
|
|
|
}
|
|
|
|
|
2011-03-03 07:53:10 +01:00
|
|
|
int64_t
|
|
|
|
ARMTraceChild::getOldRegVal(int num)
|
2009-07-27 09:53:39 +02:00
|
|
|
{
|
2011-05-05 03:38:26 +02:00
|
|
|
if (num <= CPSR)
|
|
|
|
return getRegs(oldregs, num);
|
|
|
|
else
|
|
|
|
return (int64_t)getFpRegs(oldfpregs, num);
|
2009-07-27 09:53:39 +02:00
|
|
|
}
|
|
|
|
|
2011-03-03 07:53:10 +01:00
|
|
|
ostream &
|
|
|
|
ARMTraceChild::outputStartState(ostream & os)
|
2009-07-27 09:53:39 +02:00
|
|
|
{
|
|
|
|
uint32_t sp = getSP();
|
|
|
|
uint32_t pc = getPC();
|
|
|
|
uint32_t highestInfo = 0;
|
|
|
|
char obuf[1024];
|
|
|
|
sprintf(obuf, "Initial stack pointer = 0x%08x\n", sp);
|
|
|
|
os << obuf;
|
|
|
|
sprintf(obuf, "Initial program counter = 0x%08x\n", pc);
|
|
|
|
os << obuf;
|
|
|
|
|
|
|
|
//Output the argument count
|
|
|
|
int32_t cargc = ptrace(PTRACE_PEEKDATA, pid, sp, 0);
|
|
|
|
sprintf(obuf, "0x%08x: Argc = 0x%08x\n", sp, cargc);
|
|
|
|
os << obuf;
|
|
|
|
sp += 4;
|
|
|
|
|
|
|
|
//Output argv pointers
|
|
|
|
int argCount = 0;
|
|
|
|
int32_t cargv;
|
2011-03-03 07:53:10 +01:00
|
|
|
do {
|
2009-07-27 09:53:39 +02:00
|
|
|
cargv = ptrace(PTRACE_PEEKDATA, pid, sp, 0);
|
|
|
|
sprintf(obuf, "0x%08x: argv[%d] = 0x%08x\n",
|
|
|
|
sp, argCount++, cargv);
|
|
|
|
if(cargv)
|
|
|
|
if(highestInfo < cargv)
|
|
|
|
highestInfo = cargv;
|
|
|
|
os << obuf;
|
|
|
|
sp += 4;
|
|
|
|
} while(cargv);
|
|
|
|
|
|
|
|
//Output the envp pointers
|
|
|
|
int envCount = 0;
|
|
|
|
uint32_t cenvp;
|
2011-03-03 07:53:10 +01:00
|
|
|
do {
|
2009-07-27 09:53:39 +02:00
|
|
|
cenvp = ptrace(PTRACE_PEEKDATA, pid, sp, 0);
|
|
|
|
sprintf(obuf, "0x%08x: envp[%d] = 0x%08x\n",
|
|
|
|
sp, envCount++, cenvp);
|
|
|
|
os << obuf;
|
|
|
|
sp += 4;
|
|
|
|
} while(cenvp);
|
|
|
|
uint32_t auxType, auxVal;
|
2011-03-03 07:53:10 +01:00
|
|
|
do {
|
2009-07-27 09:53:39 +02:00
|
|
|
auxType = ptrace(PTRACE_PEEKDATA, pid, sp, 0);
|
|
|
|
sp += 4;
|
|
|
|
auxVal = ptrace(PTRACE_PEEKDATA, pid, sp, 0);
|
|
|
|
sp += 4;
|
|
|
|
sprintf(obuf, "0x%08x: Auxiliary vector = {0x%08x, 0x%08x}\n",
|
|
|
|
sp - 8, auxType, auxVal);
|
|
|
|
os << obuf;
|
|
|
|
} while(auxType != 0 || auxVal != 0);
|
|
|
|
//Print out the argument strings, environment strings, and file name.
|
|
|
|
string current;
|
|
|
|
uint32_t buf;
|
|
|
|
uint32_t currentStart = sp;
|
|
|
|
bool clearedInitialPadding = false;
|
2011-03-03 07:53:10 +01:00
|
|
|
do {
|
2009-07-27 09:53:39 +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])
|
2009-07-27 09:53:39 +02:00
|
|
|
current += cbuf[x];
|
2011-03-03 07:53:10 +01:00
|
|
|
else {
|
2009-07-27 09:53:39 +02:00
|
|
|
sprintf(obuf, "0x%08x: \"%s\"\n",
|
|
|
|
currentStart, current.c_str());
|
|
|
|
os << obuf;
|
|
|
|
current = "";
|
|
|
|
currentStart = sp + x + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sp += 4;
|
|
|
|
clearedInitialPadding = clearedInitialPadding || buf != 0;
|
|
|
|
} while(!clearedInitialPadding || buf != 0 || sp <= highestInfo);
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2011-03-03 07:53:10 +01:00
|
|
|
bool
|
|
|
|
ARMTraceChild::step()
|
2009-07-27 09:53:39 +02:00
|
|
|
{
|
2009-07-29 09:14:43 +02:00
|
|
|
const uint32_t bkpt_inst = 0xe7f001f0;
|
2009-07-27 09:53:39 +02:00
|
|
|
|
2009-07-29 09:14:43 +02:00
|
|
|
uint32_t lr = getRegVal(14);
|
|
|
|
uint32_t pc = getPC();
|
2010-06-02 19:58:17 +02:00
|
|
|
uint32_t lrOp, subsOp;
|
|
|
|
char obuf[128];
|
|
|
|
bool patch = false;
|
2009-07-29 09:14:43 +02:00
|
|
|
|
|
|
|
// Since ARM uses software breakpoints behind the scenes, they don't work
|
|
|
|
// in read only areas like the page of routines provided by the kernel. The
|
|
|
|
// link register generally holds the address the process wants to the
|
|
|
|
// kernel to return to after it's done, so we'll install a software
|
2010-06-02 19:58:17 +02:00
|
|
|
// breakpoint there.
|
|
|
|
//
|
|
|
|
// Calls into the kernel user page always follow the form:
|
|
|
|
// MVN ...
|
|
|
|
// <possible MOV lr,...>
|
|
|
|
// SUB PC, ...
|
|
|
|
//
|
|
|
|
// So we look for this pattern and set a breakpoint on the LR at the SUB
|
|
|
|
// instruction.
|
2009-07-29 09:14:43 +02:00
|
|
|
|
2010-06-02 19:58:17 +02:00
|
|
|
|
|
|
|
subsOp = ptrace(PTRACE_PEEKDATA, pid, pc, 0);
|
|
|
|
if ((subsOp & 0xFFFF0FFF) == 0xe3e00a0f)
|
|
|
|
foundMvn = true;
|
|
|
|
|
|
|
|
if (foundMvn && ((subsOp & 0xFFF0F000) == 0xe240f000)) {
|
|
|
|
foundMvn = false;
|
2009-07-29 09:14:43 +02:00
|
|
|
lrOp = ptrace(PTRACE_PEEKDATA, pid, lr, 0);
|
|
|
|
ptrace(PTRACE_POKEDATA, pid, lr, bkpt_inst);
|
2010-06-02 19:58:17 +02:00
|
|
|
patch = true;
|
2009-07-29 09:14:43 +02:00
|
|
|
}
|
|
|
|
ptraceSingleStep();
|
2010-06-02 19:58:17 +02:00
|
|
|
|
|
|
|
if (patch)
|
2009-07-29 09:14:43 +02:00
|
|
|
ptrace(PTRACE_POKEDATA, pid, lr, lrOp);
|
2009-07-27 09:53:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-03 07:53:10 +01:00
|
|
|
TraceChild *
|
|
|
|
genTraceChild()
|
2009-07-27 09:53:39 +02:00
|
|
|
{
|
|
|
|
return new ARMTraceChild;
|
|
|
|
}
|
|
|
|
|