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