ARM: Only send information that changed between statetrace and M5.
[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 <cstring>
33 #include <errno.h>
34 #include <iostream>
35 #include <sys/ptrace.h>
36 #include <sys/wait.h>
37
38 using namespace std;
39
40 bool 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 return true;
83 }
84
85 bool TraceChild::stopTracing()
86 {
87 if(ptrace(PTRACE_KILL, pid, 0, 0) != 0)
88 return false;
89 tracing = false;
90 return true;
91 }
92
93 bool TraceChild::step()
94 {
95 ptraceSingleStep();
96 }
97
98 bool TraceChild::ptraceSingleStep()
99 {
100 if(!tracing)
101 {
102 cout << "Not tracing!" << endl;
103 return false;
104 }
105 if(ptrace(PTRACE_SINGLESTEP, pid, 0, 0) != 0)
106 {
107 switch(errno)
108 {
109 case EBUSY: cout << "EBUSY" << endl; break;
110 case EFAULT: cout << "EFAULT" << endl; break;
111 case EIO: cout << "EIO" << endl; break;
112 case EPERM: cout << "EPERM" << endl; break;
113 case ESRCH: cout << "ESRCH" << endl; break;
114 default: cout << "Unknown error" << endl; break;
115 }
116 cout << "Not able to single step!" << endl;
117 tracing == false;
118 return false;
119 }
120 doWait();
121 update(pid);
122 }
123
124 bool TraceChild::doWait()
125 {
126 int wait_val;
127 wait(&wait_val);
128 if(WIFEXITED(wait_val))
129 {
130 cerr << "Program exited! Exit status is "
131 << WEXITSTATUS(wait_val) << endl;
132 cerr << "Executed " << instructions
133 << " instructions." << endl;
134 tracing = false;
135 return false;
136 }
137 if(WIFSIGNALED(wait_val))
138 {
139 if(WTERMSIG(wait_val))
140 cerr << "Program terminated by signal "
141 << WTERMSIG(wait_val) << endl;
142 if(WCOREDUMP(wait_val))
143 cerr << "Program core dumped!" << endl;
144 tracing = false;
145 cerr << "Executed " << instructions
146 << " instructions." << endl;
147 return false;
148 }
149 if(WIFSTOPPED(wait_val) && WSTOPSIG(wait_val) != SIGTRAP)
150 {
151 cerr << "Program stopped by signal "
152 << WSTOPSIG(wait_val) << endl;
153 tracing = false;
154 cerr << "Executed " << instructions
155 << " instructions." << endl;
156 return false;
157 }
158 return true;
159 }