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