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