Automated merge with ssh://hg@m5sim.org/m5
[gem5.git] / util / statetrace / statetrace.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 <cstring>
32 #include <errno.h>
33 #include <fstream>
34 #include <iostream>
35 #include <netdb.h>
36 #include <netinet/in.h>
37 #include <stdio.h>
38 #include <string>
39 #include <sys/ptrace.h>
40 #include <sys/socket.h>
41 #include <sys/types.h>
42 #include <sys/wait.h>
43 #include <unistd.h>
44
45 #include "printer.hh"
46 #include "tracechild.hh"
47
48 using namespace std;
49
50 void printUsage(const char * execName)
51 {
52 cout << execName << " -h | -r -- <command> <arguments>" << endl;
53 }
54
55 int main(int argc, char * argv[], char * envp[])
56 {
57 TraceChild * child = genTraceChild();
58 string args;
59 int startProgramArgs;
60
61 //Parse the command line arguments
62 bool printInitial = false;
63 bool printTrace = true;
64 string host = "localhost";
65 for(int x = 1; x < argc; x++)
66 {
67 if(!strcmp(argv[x], "-h"))
68 {
69 printUsage(argv[0]);
70 return 0;
71 }
72 if(!strcmp(argv[x], "--host"))
73 {
74 x++;
75 if(x >= argc)
76 {
77 cerr << "Incorrect usage.\n" << endl;
78 printUsage(argv[0]);
79 return 1;
80 }
81 host = argv[x];
82 }
83 else if(!strcmp(argv[x], "-r"))
84 {
85 cout << "Legal register names:" << endl;
86 int numRegs = child->getNumRegs();
87 for(unsigned int x = 0; x < numRegs; x++)
88 {
89 cout << "\t" << child->getRegName(x) << endl;
90 }
91 return 0;
92 }
93 else if(!strcmp(argv[x], "-i"))
94 {
95 printInitial = true;
96 }
97 else if(!strcmp(argv[x], "-nt"))
98 {
99 printTrace = false;
100 }
101 else if(!strcmp(argv[x], "--"))
102 {
103 x++;
104 if(x >= argc)
105 {
106 cerr << "Incorrect usage.\n" << endl;
107 printUsage(argv[0]);
108 return 1;
109 }
110 startProgramArgs = x;
111 break;
112 }
113 else
114 {
115 cerr << "Incorrect usage.\n" << endl;
116 printUsage(argv[0]);
117 return 1;
118 }
119 }
120 if(!child->startTracing(argv[startProgramArgs],
121 argv + startProgramArgs))
122 {
123 cerr << "Couldn't start target program" << endl;
124 return 1;
125 }
126 child->step();
127 if(printInitial)
128 {
129 child->outputStartState(cout);
130 }
131 if(printTrace)
132 {
133 // Connect to m5
134 bool portSet = false;
135 int port;
136 int sock = socket(AF_INET, SOCK_STREAM, 0);
137 if(sock < 0)
138 {
139 cerr << "Error opening socket! " << strerror(errno) << endl;
140 return 1;
141 }
142 struct hostent *server;
143 server = gethostbyname(host.c_str());
144 if(!server)
145 {
146 cerr << "Couldn't get host ip! " << strerror(errno) << endl;
147 return 1;
148 }
149 struct sockaddr_in serv_addr;
150 bzero((char *)&serv_addr, sizeof(serv_addr));
151 serv_addr.sin_family = AF_INET;
152 bcopy((char *)server->h_addr,
153 (char *)&serv_addr.sin_addr.s_addr,
154 server->h_length);
155 serv_addr.sin_port = htons(8000);
156 if(connect(sock, (sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
157 {
158 cerr << "Couldn't connect to server! " << strerror(errno) << endl;
159 return 1;
160 }
161 while(child->isTracing())
162 {
163 if(!child->sendState(sock))
164 break;
165 child->step();
166 }
167 }
168 if(!child->stopTracing())
169 {
170 cerr << "Couldn't stop child" << endl;
171 return 1;
172 }
173 return 0;
174 }
175