1/* 2 * Copyright (c) 2006-2007 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: Gabe Black 29 */ 30 31#include <sys/ptrace.h> 32#include <sys/wait.h> 33 34#include <cerrno> 35#include <cstring> 36#include <iostream> 37 38#include "tracechild.hh" 39 40using namespace std; 41 42bool 43TraceChild::startTracing(const char * pathToFile, char * const argv[]) 44{ 45 instructions = 0; 46 pid = fork(); 47 if (pid == -1) { 48 cout << "fork failed" << endl; 49 return false; 50 } else if (pid == 0) { 51 //We're the child. Get things ready and then exec the program to trace. 52 //Let our parent trace us 53 if (ptrace(PTRACE_TRACEME, 0, 0, 0) == -1) { 54 cout << "Failure calling TRACEME\n" << strerror(errno) << endl; 55 return false; 56 } 57 58 //Set up an empty environment for the child... We would want to 59 //specify this somehow at some point 60 char * env[] = {NULL}; 61 62 //Start the program to trace 63 execve(pathToFile, argv, env); 64 65 //We should never get here, so this is an error! 66 cout << "Exec failed\n" << strerror(errno) << endl; 67 return false; 68 } 69 70 //From this point forward, we know we're in the parent process. 71 if (!doWait()) { 72 cout << "Didn't wait successfully" << endl; 73 return false; 74 } 75 tracing = true; 76 return true; 77} 78 79bool 80TraceChild::stopTracing() 81{ 82 if (ptrace(PTRACE_KILL, pid, 0, 0) != 0) 83 return false; 84 tracing = false; 85 return true; 86} 87 88bool 89TraceChild::step() 90{ 91 ptraceSingleStep(); 92} 93 94bool 95TraceChild::ptraceSingleStep() 96{ 97 if (!tracing) { 98 cout << "Not tracing!" << endl; 99 return false; 100 } 101 if (ptrace(PTRACE_SINGLESTEP, pid, 0, 0) != 0) { 102 switch (errno) { 103 case EBUSY: cout << "EBUSY" << endl; break; 104 case EFAULT: cout << "EFAULT" << endl; break; 105 case EIO: cout << "EIO" << endl; break; 106 case EPERM: cout << "EPERM" << endl; break; 107 case ESRCH: cout << "ESRCH" << endl; break; 108 default: cout << "Unknown error" << endl; break; 109 } 110 cout << "Not able to single step!" << endl; 111 tracing = false; 112 return false; 113 } 114 doWait(); 115 update(pid); 116} 117 118bool 119TraceChild::doWait() 120{ 121 int wait_val; 122 wait(&wait_val); 123 if (WIFEXITED(wait_val)) { 124 cerr << "Program exited! Exit status is " 125 << WEXITSTATUS(wait_val) << endl; 126 cerr << "Executed " << instructions 127 << " instructions." << endl; 128 tracing = false; 129 return false; 130 } 131 if (WIFSIGNALED(wait_val)) { 132 if (WTERMSIG(wait_val)) 133 cerr << "Program terminated by signal " 134 << WTERMSIG(wait_val) << endl; 135 if (WCOREDUMP(wait_val)) 136 cerr << "Program core dumped!" << endl; 137 tracing = false; 138 cerr << "Executed " << instructions 139 << " instructions." << endl; 140 return false; 141 } 142 if (WIFSTOPPED(wait_val) && WSTOPSIG(wait_val) != SIGTRAP) { 143 cerr << "Program stopped by signal " << WSTOPSIG(wait_val) << endl; 144 tracing = false; 145 cerr << "Executed " << instructions << " instructions." << endl; 146 return false; 147 } 148 return true; 149} 150