2 * Copyright (c) 2006 The Regents of The University of Michigan
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.
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.
31 #include "tracechild.hh"
33 #include <sys/ptrace.h>
39 bool TraceChild::startTracing(const char * pathToFile
, const char * arg
)
44 cout
<< "fork failed" << endl
;
49 //We're the child. Get things ready and then exec the
52 //Let our parent trace us
53 ptrace(PTRACE_TRACEME
, 0, 0, 0);
55 //Start the program to trace
56 execl(pathToFile
, arg
);
58 //We should never get here, so this is an error!
62 //From this point forward, we know we're in the parent process.
65 cout
<< "Didn't wait successfully" << endl
;
71 cout
<< "Didn't update successfully!" << endl
;
77 bool TraceChild::stopTracing()
79 if(ptrace(PTRACE_KILL
, pid
, 0, 0) != 0)
85 bool TraceChild::step()
90 bool TraceChild::ptraceSingleStep()
94 cout
<< "Not tracing!" << endl
;
97 if(ptrace(PTRACE_SINGLESTEP
, pid
, 0, 0) != 0)
101 case EBUSY
: cout
<< "EBUSY" << endl
; break;
102 case EFAULT
: cout
<< "EFAULT" << endl
; break;
103 case EIO
: cout
<< "EIO" << endl
; break;
104 case EPERM
: cout
<< "EPERM" << endl
; break;
105 case ESRCH
: cout
<< "ESRCH" << endl
; break;
106 default: cout
<< "Unknown error" << endl
; break;
108 cout
<< "Not able to single step!" << endl
;
116 bool TraceChild::doWait()
120 if(WIFEXITED(wait_val
))
122 cerr
<< "Program exited! Exit status is "
123 << WEXITSTATUS(wait_val
) << endl
;
127 if(WIFSIGNALED(wait_val
))
129 if(WTERMSIG(wait_val
))
130 cerr
<< "Program terminated by signal "
131 << WTERMSIG(wait_val
) << endl
;
132 if(WCOREDUMP(wait_val
))
133 cerr
<< "Program core dumped!" << endl
;
137 if(WIFSTOPPED(wait_val
) && WSTOPSIG(wait_val
) != SIGTRAP
)
139 cerr
<< "Program stopped by signal "
140 << WSTOPSIG(wait_val
) << endl
;