Statetrace: Allow the user to override CXX.
[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
41 TraceChild::startTracing(const char * pathToFile, char * const argv[])
42 {
43 instructions = 0;
44 pid = fork();
45 if (pid == -1) {
46 cout << "fork failed" << endl;
47 return false;
48 } else if (pid == 0) {
49 //We're the child. Get things ready and then exec the program to trace.
50 //Let our parent trace us
51 if (ptrace(PTRACE_TRACEME, 0, 0, 0) == -1) {
52 cout << "Failure calling TRACEME\n" << strerror(errno) << endl;
53 return false;
54 }
55
56 //Set up an empty environment for the child... We would want to
57 //specify this somehow at some point
58 char * env[] = {NULL};
59
60 //Start the program to trace
61 execve(pathToFile, argv, env);
62
63 //We should never get here, so this is an error!
64 cout << "Exec failed\n" << strerror(errno) << endl;
65 return false;
66 }
67
68 //From this point forward, we know we're in the parent process.
69 if (!doWait()) {
70 cout << "Didn't wait successfully" << endl;
71 return false;
72 }
73 tracing = true;
74 return true;
75 }
76
77 bool
78 TraceChild::stopTracing()
79 {
80 if (ptrace(PTRACE_KILL, pid, 0, 0) != 0)
81 return false;
82 tracing = false;
83 return true;
84 }
85
86 bool
87 TraceChild::step()
88 {
89 ptraceSingleStep();
90 }
91
92 bool
93 TraceChild::ptraceSingleStep()
94 {
95 if (!tracing) {
96 cout << "Not tracing!" << endl;
97 return false;
98 }
99 if (ptrace(PTRACE_SINGLESTEP, pid, 0, 0) != 0) {
100 switch (errno) {
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;
107 }
108 cout << "Not able to single step!" << endl;
109 tracing == false;
110 return false;
111 }
112 doWait();
113 update(pid);
114 }
115
116 bool
117 TraceChild::doWait()
118 {
119 int wait_val;
120 wait(&wait_val);
121 if (WIFEXITED(wait_val)) {
122 cerr << "Program exited! Exit status is "
123 << WEXITSTATUS(wait_val) << endl;
124 cerr << "Executed " << instructions
125 << " instructions." << endl;
126 tracing = false;
127 return false;
128 }
129 if (WIFSIGNALED(wait_val)) {
130 if (WTERMSIG(wait_val))
131 cerr << "Program terminated by signal "
132 << WTERMSIG(wait_val) << endl;
133 if (WCOREDUMP(wait_val))
134 cerr << "Program core dumped!" << endl;
135 tracing = false;
136 cerr << "Executed " << instructions
137 << " instructions." << endl;
138 return false;
139 }
140 if (WIFSTOPPED(wait_val) && WSTOPSIG(wait_val) != SIGTRAP) {
141 cerr << "Program stopped by signal " << WSTOPSIG(wait_val) << endl;
142 tracing = false;
143 cerr << "Executed " << instructions << " instructions." << endl;
144 return false;
145 }
146 return true;
147 }