13115SN/A/*
24125SN/A * Copyright (c) 2006-2007 The Regents of The University of Michigan
33115SN/A * All rights reserved.
43115SN/A *
53115SN/A * Redistribution and use in source and binary forms, with or without
63115SN/A * modification, are permitted provided that the following conditions are
73115SN/A * met: redistributions of source code must retain the above copyright
83115SN/A * notice, this list of conditions and the following disclaimer;
93115SN/A * redistributions in binary form must reproduce the above copyright
103115SN/A * notice, this list of conditions and the following disclaimer in the
113115SN/A * documentation and/or other materials provided with the distribution;
123115SN/A * neither the name of the copyright holders nor the names of its
133115SN/A * contributors may be used to endorse or promote products derived from
143115SN/A * this software without specific prior written permission.
153115SN/A *
163115SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
173115SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
183115SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
193115SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
203115SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
213115SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
223115SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
233115SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
243115SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
253115SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
263115SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
273115SN/A *
283115SN/A * Authors: Gabe Black
293115SN/A */
303115SN/A
316406SN/A#include <sys/ptrace.h>
323115SN/A#include <sys/wait.h>
333115SN/A
348229Snate@binkert.org#include <cerrno>
358229Snate@binkert.org#include <cstring>
368229Snate@binkert.org#include <iostream>
378229Snate@binkert.org
388229Snate@binkert.org#include "tracechild.hh"
398229Snate@binkert.org
403115SN/Ausing namespace std;
413115SN/A
428108SN/Abool
438108SN/ATraceChild::startTracing(const char * pathToFile, char * const argv[])
443115SN/A{
458108SN/A    instructions = 0;
468108SN/A    pid = fork();
478108SN/A    if (pid == -1) {
488108SN/A        cout << "fork failed" << endl;
498108SN/A        return false;
508108SN/A    } else if (pid == 0) {
518108SN/A        //We're the child. Get things ready and then exec the program to trace.
528108SN/A        //Let our parent trace us
538108SN/A        if (ptrace(PTRACE_TRACEME, 0, 0, 0) == -1) {
548108SN/A            cout << "Failure calling TRACEME\n" << strerror(errno) << endl;
558108SN/A            return false;
563115SN/A        }
573115SN/A
588108SN/A        //Set up an empty environment for the child... We would want to
598108SN/A        //specify this somehow at some point
608108SN/A        char * env[] = {NULL};
618108SN/A
628108SN/A        //Start the program to trace
638108SN/A        execve(pathToFile, argv, env);
648108SN/A
658108SN/A        //We should never get here, so this is an error!
668108SN/A        cout << "Exec failed\n" <<  strerror(errno) << endl;
678108SN/A        return false;
688108SN/A    }
698108SN/A
708108SN/A    //From this point forward, we know we're in the parent process.
718108SN/A    if (!doWait()) {
728108SN/A        cout << "Didn't wait successfully" << endl;
738108SN/A        return false;
748108SN/A    }
758108SN/A    tracing = true;
768108SN/A    return true;
773115SN/A}
783115SN/A
798108SN/Abool
808108SN/ATraceChild::stopTracing()
813115SN/A{
828108SN/A    if (ptrace(PTRACE_KILL, pid, 0, 0) != 0)
838108SN/A        return false;
848108SN/A    tracing = false;
858108SN/A    return true;
863115SN/A}
873115SN/A
888108SN/Abool
898108SN/ATraceChild::step()
903115SN/A{
918108SN/A    ptraceSingleStep();
923115SN/A}
933115SN/A
948108SN/Abool
958108SN/ATraceChild::ptraceSingleStep()
963115SN/A{
978108SN/A    if (!tracing) {
988108SN/A        cout << "Not tracing!" << endl;
998108SN/A        return false;
1008108SN/A    }
1018108SN/A    if (ptrace(PTRACE_SINGLESTEP, pid, 0, 0) != 0) {
1028108SN/A        switch (errno) {
1038108SN/A          case EBUSY: cout << "EBUSY" << endl; break;
1048108SN/A          case EFAULT: cout << "EFAULT" << endl; break;
1058108SN/A          case EIO: cout << "EIO" << endl; break;
1068108SN/A          case EPERM: cout << "EPERM" << endl; break;
1078108SN/A          case ESRCH: cout << "ESRCH" << endl; break;
1088108SN/A          default: cout << "Unknown error" << endl; break;
1093115SN/A        }
1108108SN/A        cout << "Not able to single step!" << endl;
11111323Ssteve.reinhardt@amd.com        tracing = false;
1128108SN/A        return false;
1138108SN/A    }
1148108SN/A    doWait();
1158108SN/A    update(pid);
1163115SN/A}
1173115SN/A
1188108SN/Abool
1198108SN/ATraceChild::doWait()
1203115SN/A{
1218108SN/A    int wait_val;
1228108SN/A    wait(&wait_val);
1238108SN/A    if (WIFEXITED(wait_val)) {
1248108SN/A        cerr << "Program exited! Exit status is "
1258108SN/A             << WEXITSTATUS(wait_val) << endl;
1268108SN/A        cerr << "Executed " << instructions
1278108SN/A             << " instructions." << endl;
1288108SN/A        tracing = false;
1298108SN/A        return false;
1308108SN/A    }
1318108SN/A    if (WIFSIGNALED(wait_val)) {
1328108SN/A        if (WTERMSIG(wait_val))
1338108SN/A            cerr << "Program terminated by signal "
1348108SN/A                 << WTERMSIG(wait_val) << endl;
1358108SN/A        if (WCOREDUMP(wait_val))
1368108SN/A            cerr << "Program core dumped!" << endl;
1378108SN/A        tracing = false;
1388108SN/A        cerr << "Executed " << instructions
1398108SN/A             << " instructions." << endl;
1408108SN/A        return false;
1418108SN/A    }
1428108SN/A    if (WIFSTOPPED(wait_val) && WSTOPSIG(wait_val) != SIGTRAP) {
1438108SN/A        cerr << "Program stopped by signal " << WSTOPSIG(wait_val) << endl;
1448108SN/A        tracing = false;
1458108SN/A        cerr << "Executed " << instructions << " instructions." << endl;
1468108SN/A            return false;
1478108SN/A    }
1488108SN/A    return true;
1493115SN/A}
150