tracechild.cc revision 8113
14780SN/A/*
24780SN/A * Copyright (c) 2007 The Regents of The University of Michigan
34780SN/A * All rights reserved.
44780SN/A *
54780SN/A * Redistribution and use in source and binary forms, with or without
64780SN/A * modification, are permitted provided that the following conditions are
74780SN/A * met: redistributions of source code must retain the above copyright
84780SN/A * notice, this list of conditions and the following disclaimer;
94780SN/A * redistributions in binary form must reproduce the above copyright
104780SN/A * notice, this list of conditions and the following disclaimer in the
114780SN/A * documentation and/or other materials provided with the distribution;
124780SN/A * neither the name of the copyright holders nor the names of its
134780SN/A * contributors may be used to endorse or promote products derived from
144780SN/A * this software without specific prior written permission.
154780SN/A *
164780SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
174780SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
184780SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
194780SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
204780SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
214780SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
224780SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
234780SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
244780SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
254780SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
264780SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
274780SN/A *
284780SN/A * Authors: Gabe Black
294780SN/A */
304780SN/A
314780SN/A#include <iostream>
324795SN/A#include <iomanip>
334780SN/A#include <errno.h>
344780SN/A#include <sys/ptrace.h>
354780SN/A#include <stdint.h>
367071SN/A#include <string.h>
374780SN/A
388113Sgblack@eecs.umich.edu#include "arch/amd64/tracechild.hh"
394780SN/A
404780SN/Ausing namespace std;
414780SN/A
428108SN/Abool
438108SN/AAMD64TraceChild::sendState(int socket)
444780SN/A{
455049SN/A    uint64_t regVal64 = 0;
465049SN/A    uint32_t regVal32 = 0;
478108SN/A    for (int x = 0; x <= R15; x++) {
485049SN/A        regVal64 = getRegVal(x);
498108SN/A        if (write(socket, &regVal64, sizeof(regVal64)) == -1) {
504780SN/A            cerr << "Write failed! " << strerror(errno) << endl;
514780SN/A            tracing = false;
524780SN/A            return false;
534780SN/A        }
544780SN/A    }
555049SN/A    regVal64 = getRegVal(RIP);
568108SN/A    if (write(socket, &regVal64, sizeof(regVal64)) == -1) {
574780SN/A        cerr << "Write failed! " << strerror(errno) << endl;
584780SN/A        tracing = false;
594780SN/A        return false;
604780SN/A    }
618108SN/A    for (int x = MMX0_0; x <= MMX7_1; x++) {
625049SN/A        regVal32 = getRegVal(x);
638108SN/A        if (write(socket, &regVal32, sizeof(regVal32)) == -1) {
645049SN/A            cerr << "Write failed! " << strerror(errno) << endl;
655049SN/A            tracing = false;
665049SN/A            return false;
675049SN/A        }
685049SN/A    }
698108SN/A    for (int x = XMM0_0; x <= XMM15_3; x++) {
705049SN/A        regVal32 = getRegVal(x);
718108SN/A        if (write(socket, &regVal32, sizeof(regVal32)) == -1) {
725049SN/A            cerr << "Write failed! " << strerror(errno) << endl;
735049SN/A            tracing = false;
745049SN/A            return false;
755049SN/A        }
765049SN/A    }
774780SN/A    return true;
784780SN/A}
794780SN/A
808108SN/Aint64_t
818108SN/AAMD64TraceChild::getRegs(user_regs_struct & myregs,
825049SN/A        user_fpregs_struct & myfpregs, int num)
834780SN/A{
845049SN/A    assert(num < numregs && num >= 0);
858108SN/A    switch (num) {
868108SN/A      //GPRs
878108SN/A      case RAX: return myregs.rax;
888108SN/A      case RBX: return myregs.rbx;
898108SN/A      case RCX: return myregs.rcx;
908108SN/A      case RDX: return myregs.rdx;
918108SN/A      //Index registers
928108SN/A      case RSI: return myregs.rsi;
938108SN/A      case RDI: return myregs.rdi;
948108SN/A      //Base pointer and stack pointer
958108SN/A      case RBP: return myregs.rbp;
968108SN/A      case RSP: return myregs.rsp;
978108SN/A      //New 64 bit mode registers
988108SN/A      case R8: return myregs.r8;
998108SN/A      case R9: return myregs.r9;
1008108SN/A      case R10: return myregs.r10;
1018108SN/A      case R11: return myregs.r11;
1028108SN/A      case R12: return myregs.r12;
1038108SN/A      case R13: return myregs.r13;
1048108SN/A      case R14: return myregs.r14;
1058108SN/A      case R15: return myregs.r15;
1068108SN/A      //Segmentation registers
1078108SN/A      case CS: return myregs.cs;
1088108SN/A      case DS: return myregs.ds;
1098108SN/A      case ES: return myregs.es;
1108108SN/A      case FS: return myregs.fs;
1118108SN/A      case GS: return myregs.gs;
1128108SN/A      case SS: return myregs.ss;
1138108SN/A      case FS_BASE: return myregs.fs_base;
1148108SN/A      case GS_BASE: return myregs.gs_base;
1158108SN/A      //PC
1168108SN/A      case RIP: return myregs.rip;
1178108SN/A      //Flags
1188108SN/A      case EFLAGS: return myregs.eflags;
1198108SN/A      //MMX
1208108SN/A      case MMX0_0: return myfpregs.st_space[0];
1218108SN/A      case MMX0_1: return myfpregs.st_space[1];
1228108SN/A      case MMX1_0: return myfpregs.st_space[2];
1238108SN/A      case MMX1_1: return myfpregs.st_space[3];
1248108SN/A      case MMX2_0: return myfpregs.st_space[4];
1258108SN/A      case MMX2_1: return myfpregs.st_space[5];
1268108SN/A      case MMX3_0: return myfpregs.st_space[6];
1278108SN/A      case MMX3_1: return myfpregs.st_space[7];
1288108SN/A      case MMX4_0: return myfpregs.st_space[8];
1298108SN/A      case MMX4_1: return myfpregs.st_space[9];
1308108SN/A      case MMX5_0: return myfpregs.st_space[10];
1318108SN/A      case MMX5_1: return myfpregs.st_space[11];
1328108SN/A      case MMX6_0: return myfpregs.st_space[12];
1338108SN/A      case MMX6_1: return myfpregs.st_space[13];
1348108SN/A      case MMX7_0: return myfpregs.st_space[14];
1358108SN/A      case MMX7_1: return myfpregs.st_space[15];
1368108SN/A      //XMM
1378108SN/A      case XMM0_0: return myfpregs.xmm_space[0];
1388108SN/A      case XMM0_1: return myfpregs.xmm_space[1];
1398108SN/A      case XMM0_2: return myfpregs.xmm_space[2];
1408108SN/A      case XMM0_3: return myfpregs.xmm_space[3];
1418108SN/A      case XMM1_0: return myfpregs.xmm_space[4];
1428108SN/A      case XMM1_1: return myfpregs.xmm_space[5];
1438108SN/A      case XMM1_2: return myfpregs.xmm_space[6];
1448108SN/A      case XMM1_3: return myfpregs.xmm_space[7];
1458108SN/A      case XMM2_0: return myfpregs.xmm_space[8];
1468108SN/A      case XMM2_1: return myfpregs.xmm_space[9];
1478108SN/A      case XMM2_2: return myfpregs.xmm_space[10];
1488108SN/A      case XMM2_3: return myfpregs.xmm_space[11];
1498108SN/A      case XMM3_0: return myfpregs.xmm_space[12];
1508108SN/A      case XMM3_1: return myfpregs.xmm_space[13];
1518108SN/A      case XMM3_2: return myfpregs.xmm_space[14];
1528108SN/A      case XMM3_3: return myfpregs.xmm_space[15];
1538108SN/A      case XMM4_0: return myfpregs.xmm_space[16];
1548108SN/A      case XMM4_1: return myfpregs.xmm_space[17];
1558108SN/A      case XMM4_2: return myfpregs.xmm_space[18];
1568108SN/A      case XMM4_3: return myfpregs.xmm_space[19];
1578108SN/A      case XMM5_0: return myfpregs.xmm_space[20];
1588108SN/A      case XMM5_1: return myfpregs.xmm_space[21];
1598108SN/A      case XMM5_2: return myfpregs.xmm_space[22];
1608108SN/A      case XMM5_3: return myfpregs.xmm_space[23];
1618108SN/A      case XMM6_0: return myfpregs.xmm_space[24];
1628108SN/A      case XMM6_1: return myfpregs.xmm_space[25];
1638108SN/A      case XMM6_2: return myfpregs.xmm_space[26];
1648108SN/A      case XMM6_3: return myfpregs.xmm_space[27];
1658108SN/A      case XMM7_0: return myfpregs.xmm_space[28];
1668108SN/A      case XMM7_1: return myfpregs.xmm_space[29];
1678108SN/A      case XMM7_2: return myfpregs.xmm_space[30];
1688108SN/A      case XMM7_3: return myfpregs.xmm_space[31];
1698108SN/A      case XMM8_0: return myfpregs.xmm_space[32];
1708108SN/A      case XMM8_1: return myfpregs.xmm_space[33];
1718108SN/A      case XMM8_2: return myfpregs.xmm_space[34];
1728108SN/A      case XMM8_3: return myfpregs.xmm_space[35];
1738108SN/A      case XMM9_0: return myfpregs.xmm_space[36];
1748108SN/A      case XMM9_1: return myfpregs.xmm_space[37];
1758108SN/A      case XMM9_2: return myfpregs.xmm_space[38];
1768108SN/A      case XMM9_3: return myfpregs.xmm_space[39];
1778108SN/A      case XMM10_0: return myfpregs.xmm_space[40];
1788108SN/A      case XMM10_1: return myfpregs.xmm_space[41];
1798108SN/A      case XMM10_2: return myfpregs.xmm_space[42];
1808108SN/A      case XMM10_3: return myfpregs.xmm_space[43];
1818108SN/A      case XMM11_0: return myfpregs.xmm_space[44];
1828108SN/A      case XMM11_1: return myfpregs.xmm_space[45];
1838108SN/A      case XMM11_2: return myfpregs.xmm_space[46];
1848108SN/A      case XMM11_3: return myfpregs.xmm_space[47];
1858108SN/A      case XMM12_0: return myfpregs.xmm_space[48];
1868108SN/A      case XMM12_1: return myfpregs.xmm_space[49];
1878108SN/A      case XMM12_2: return myfpregs.xmm_space[50];
1888108SN/A      case XMM12_3: return myfpregs.xmm_space[51];
1898108SN/A      case XMM13_0: return myfpregs.xmm_space[52];
1908108SN/A      case XMM13_1: return myfpregs.xmm_space[53];
1918108SN/A      case XMM13_2: return myfpregs.xmm_space[54];
1928108SN/A      case XMM13_3: return myfpregs.xmm_space[55];
1938108SN/A      case XMM14_0: return myfpregs.xmm_space[56];
1948108SN/A      case XMM14_1: return myfpregs.xmm_space[57];
1958108SN/A      case XMM14_2: return myfpregs.xmm_space[58];
1968108SN/A      case XMM14_3: return myfpregs.xmm_space[59];
1978108SN/A      case XMM15_0: return myfpregs.xmm_space[60];
1988108SN/A      case XMM15_1: return myfpregs.xmm_space[61];
1998108SN/A      case XMM15_2: return myfpregs.xmm_space[62];
2008108SN/A      case XMM15_3: return myfpregs.xmm_space[63];
2018108SN/A      default:
2028108SN/A        assert(0);
2038108SN/A        return 0;
2045049SN/A    }
2054780SN/A}
2064780SN/A
2078108SN/Abool
2088108SN/AAMD64TraceChild::update(int pid)
2094780SN/A{
2104780SN/A    oldregs = regs;
2115049SN/A    oldfpregs = fpregs;
2128108SN/A    if (ptrace(PTRACE_GETREGS, pid, 0, &regs) != 0) {
2134780SN/A        cerr << "update: " << strerror(errno) << endl;
2144780SN/A        return false;
2154780SN/A    }
2168108SN/A    if (ptrace(PTRACE_GETFPREGS, pid, 0, &fpregs) != 0) {
2175049SN/A        cerr << "update: " << strerror(errno) << endl;
2185049SN/A        return false;
2195049SN/A    }
2208108SN/A    for (unsigned int x = 0; x < numregs; x++)
2214780SN/A        regDiffSinceUpdate[x] = (getRegVal(x) != getOldRegVal(x));
2224780SN/A    return true;
2234780SN/A}
2244780SN/A
2254780SN/AAMD64TraceChild::AMD64TraceChild()
2264780SN/A{
2278108SN/A    for (unsigned int x = 0; x < numregs; x++)
2284780SN/A        regDiffSinceUpdate[x] = false;
2294780SN/A}
2304780SN/A
2318108SN/Aint64_t
2328108SN/AAMD64TraceChild::getRegVal(int num)
2334780SN/A{
2348108SN/A    return getRegs(regs, fpregs, num);
2354780SN/A}
2364780SN/A
2378108SN/Aint64_t
2388108SN/AAMD64TraceChild::getOldRegVal(int num)
2394780SN/A{
2408108SN/A    return getRegs(oldregs, oldfpregs, num);
2414780SN/A}
2424780SN/A
2438108SN/Aostream &
2448108SN/AAMD64TraceChild::outputStartState(ostream & os)
2454780SN/A{
2464780SN/A    uint64_t sp = getSP();
2474780SN/A    uint64_t pc = getPC();
2484843SN/A    uint64_t highestInfo = 0;
2494780SN/A    char obuf[1024];
2507071SN/A    sprintf(obuf, "Initial stack pointer = 0x%016lx\n", sp);
2514780SN/A    os << obuf;
2527071SN/A    sprintf(obuf, "Initial program counter = 0x%016lx\n", pc);
2534780SN/A    os << obuf;
2544780SN/A
2554780SN/A    //Output the argument count
2564780SN/A    uint64_t cargc = ptrace(PTRACE_PEEKDATA, pid, sp, 0);
2577071SN/A    sprintf(obuf, "0x%016lx: Argc = 0x%016lx\n", sp, cargc);
2584780SN/A    os << obuf;
2594780SN/A    sp += 8;
2604780SN/A
2614780SN/A    //Output argv pointers
2624780SN/A    int argCount = 0;
2634780SN/A    uint64_t cargv;
2648108SN/A    do {
2654780SN/A        cargv = ptrace(PTRACE_PEEKDATA, pid, sp, 0);
2667071SN/A        sprintf(obuf, "0x%016lx: argv[%d] = 0x%016lx\n",
2674780SN/A                sp, argCount++, cargv);
2688108SN/A        if (cargv)
2698108SN/A            if (highestInfo < cargv)
2704843SN/A                highestInfo = cargv;
2714780SN/A        os << obuf;
2724780SN/A        sp += 8;
2734780SN/A    } while(cargv);
2744780SN/A
2754780SN/A    //Output the envp pointers
2764780SN/A    int envCount = 0;
2774780SN/A    uint64_t cenvp;
2788108SN/A    do {
2794780SN/A        cenvp = ptrace(PTRACE_PEEKDATA, pid, sp, 0);
2807071SN/A        sprintf(obuf, "0x%016lx: envp[%d] = 0x%016lx\n",
2814780SN/A                sp, envCount++, cenvp);
2824780SN/A        os << obuf;
2834780SN/A        sp += 8;
2844780SN/A    } while(cenvp);
2854780SN/A    uint64_t auxType, auxVal;
2868108SN/A    do {
2874780SN/A        auxType = ptrace(PTRACE_PEEKDATA, pid, sp, 0);
2884780SN/A        sp += 8;
2894780SN/A        auxVal = ptrace(PTRACE_PEEKDATA, pid, sp, 0);
2904780SN/A        sp += 8;
2917071SN/A        sprintf(obuf, "0x%016lx: Auxiliary vector = {0x%016lx, 0x%016lx}\n",
2924791SN/A                sp - 16, auxType, auxVal);
2934780SN/A        os << obuf;
2944780SN/A    } while(auxType != 0 || auxVal != 0);
2954780SN/A    //Print out the argument strings, environment strings, and file name.
2964780SN/A    string current;
2974780SN/A    uint64_t buf;
2984780SN/A    uint64_t currentStart = sp;
2994780SN/A    bool clearedInitialPadding = false;
3008108SN/A    do {
3014780SN/A        buf = ptrace(PTRACE_PEEKDATA, pid, sp, 0);
3024780SN/A        char * cbuf = (char *)&buf;
3038108SN/A        for (int x = 0; x < sizeof(uint64_t); x++) {
3048108SN/A            if (cbuf[x])
3054780SN/A                current += cbuf[x];
3068108SN/A            else {
3077071SN/A                sprintf(obuf, "0x%016lx: \"%s\"\n",
3084780SN/A                        currentStart, current.c_str());
3094780SN/A                os << obuf;
3104780SN/A                current = "";
3114780SN/A                currentStart = sp + x + 1;
3124780SN/A            }
3134780SN/A        }
3144780SN/A        sp += 8;
3154780SN/A        clearedInitialPadding = clearedInitialPadding || buf != 0;
3168108SN/A    } while (!clearedInitialPadding || buf != 0 || sp <= highestInfo);
3174780SN/A    return os;
3184780SN/A}
3194780SN/A
3208108SN/Auint64_t
3218108SN/AAMD64TraceChild::findSyscall()
3224795SN/A{
3234795SN/A    uint64_t rip = getPC();
3244795SN/A    bool foundOpcode = false;
3254795SN/A    bool twoByteOpcode = false;
3268108SN/A    for (;;) {
3274795SN/A        uint64_t buf = ptrace(PTRACE_PEEKDATA, pid, rip, 0);
3288108SN/A        for (int i = 0; i < sizeof(uint64_t); i++) {
3294795SN/A            unsigned char byte = buf & 0xFF;
3308108SN/A            if (!foundOpcode) {
3314795SN/A                if(!(byte == 0x66 || //operand override
3324795SN/A                     byte == 0x67 || //address override
3334795SN/A                     byte == 0x2E || //cs
3344795SN/A                     byte == 0x3E || //ds
3354795SN/A                     byte == 0x26 || //es
3364795SN/A                     byte == 0x64 || //fs
3374795SN/A                     byte == 0x65 || //gs
3384795SN/A                     byte == 0x36 || //ss
3394795SN/A                     byte == 0xF0 || //lock
3404795SN/A                     byte == 0xF2 || //repe
3414795SN/A                     byte == 0xF3 || //repne
3424795SN/A                     (byte >= 0x40 && byte <= 0x4F) // REX
3438108SN/A                    )) {
3444795SN/A                    foundOpcode = true;
3454795SN/A                }
3464795SN/A            }
3478108SN/A            if (foundOpcode) {
3488108SN/A                if (twoByteOpcode) {
3494795SN/A                    //SYSCALL or SYSENTER
3508108SN/A                    if (byte == 0x05 || byte == 0x34)
3514795SN/A                        return rip + 1;
3524795SN/A                    else
3534795SN/A                        return 0;
3544795SN/A                }
3558108SN/A                if (!twoByteOpcode) {
3568108SN/A                    if (byte == 0xCC) // INT3
3574795SN/A                        return rip + 1;
3588108SN/A                    else if (byte == 0xCD) // INT with byte immediate
3594795SN/A                        return rip + 2;
3608108SN/A                    else if (byte == 0x0F) // two byte opcode prefix
3614795SN/A                        twoByteOpcode = true;
3624795SN/A                    else
3634795SN/A                        return 0;
3644795SN/A                }
3654795SN/A            }
3664795SN/A            buf >>= 8;
3674795SN/A            rip++;
3684795SN/A        }
3694795SN/A    }
3704795SN/A}
3714795SN/A
3728108SN/Abool
3738108SN/AAMD64TraceChild::step()
3744795SN/A{
3754795SN/A    uint64_t ripAfterSyscall = findSyscall();
3768108SN/A    if (ripAfterSyscall) {
3774795SN/A        //Get the original contents of memory
3784795SN/A        uint64_t buf = ptrace(PTRACE_PEEKDATA, pid, ripAfterSyscall, 0);
3794795SN/A        //Patch the first two bytes of the memory immediately after this with
3804795SN/A        //jmp -2. Either single stepping will take over before this
3814795SN/A        //instruction, leaving the rip where it should be, or it will take
3824795SN/A        //over after this instruction, -still- leaving the rip where it should
3834795SN/A        //be.
3844795SN/A        uint64_t newBuf = (buf & ~0xFFFF) | 0xFEEB;
3854795SN/A        //Write the patched memory to the processes address space
3864795SN/A        ptrace(PTRACE_POKEDATA, pid, ripAfterSyscall, newBuf);
3874795SN/A        //Step and hit it
3884795SN/A        ptraceSingleStep();
3894795SN/A        //Put things back to the way they started
3904795SN/A        ptrace(PTRACE_POKEDATA, pid, ripAfterSyscall, buf);
3918108SN/A    } else {
3924955SN/A        //Get all the way past repe and repne string instructions in one shot.
3934955SN/A        uint64_t newPC, origPC = getPC();
3948108SN/A        do {
3954955SN/A            ptraceSingleStep();
3964955SN/A            newPC = getPC();
3974955SN/A        } while(newPC == origPC);
3984955SN/A    }
3994795SN/A}
4004795SN/A
4014780SN/ATraceChild * genTraceChild()
4024780SN/A{
4038108SN/A    return new AMD64TraceChild;
4044780SN/A}
405