ff20625a1f9ca6cd1079278a32d34d09d67f3b04
[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 "tracechild.hh"
46
47 using namespace std;
48
49 void
50 printUsage(const char * execName)
51 {
52 cout << execName << " <options> -- <command> <arguments>" << endl;
53 cout << "options:" << endl;
54 cout << " -h print this help" << endl;
55 cout << " --host remote m5 host to connect to" << endl;
56 cout << " -r print register names" << endl;
57 cout << " -i print initial stack state" << endl;
58 cout << " -nt don't print an instruction trace" << endl;
59 }
60
61 int
62 main(int argc, char * argv[], char * envp[])
63 {
64 TraceChild * child = genTraceChild();
65 string args;
66 int startProgramArgs;
67
68 //Parse the command line arguments
69 bool printInitial = false;
70 bool printTrace = true;
71 string host = "localhost";
72
73 if (argc == 1) {
74 printUsage(argv[0]);
75 return 0;
76 }
77 for (int x = 1; x < argc; x++) {
78 if (!strcmp(argv[x], "-h")) {
79 printUsage(argv[0]);
80 return 0;
81 }
82 if (!strcmp(argv[x], "--host")) {
83 x++;
84 if (x >= argc) {
85 cerr << "Incorrect usage.\n" << endl;
86 printUsage(argv[0]);
87 return 1;
88 }
89 host = argv[x];
90 } else if (!strcmp(argv[x], "-r")) {
91 cout << "Legal register names:" << endl;
92 int numRegs = child->getNumRegs();
93 for (unsigned int x = 0; x < numRegs; x++)
94 cout << "\t" << child->getRegName(x) << endl;
95 return 0;
96 } else if (!strcmp(argv[x], "-i")) {
97 printInitial = true;
98 } else if (!strcmp(argv[x], "-nt")) {
99 printTrace = false;
100 } else if (!strcmp(argv[x], "--")) {
101 x++;
102 if (x >= argc) {
103 cerr << "Incorrect usage.\n" << endl;
104 printUsage(argv[0]);
105 return 1;
106 }
107 startProgramArgs = x;
108 break;
109 } else {
110 cerr << "Incorrect usage.\n" << endl;
111 printUsage(argv[0]);
112 return 1;
113 }
114 }
115 if (!child->startTracing(argv[startProgramArgs],
116 argv + startProgramArgs)) {
117 cerr << "Couldn't start target program" << endl;
118 return 1;
119 }
120 child->step();
121 if (printInitial)
122 child->outputStartState(cout);
123 if (printTrace) {
124 // Connect to m5
125 bool portSet = false;
126 int port;
127 int sock = socket(AF_INET, SOCK_STREAM, 0);
128 if (sock < 0) {
129 cerr << "Error opening socket! " << strerror(errno) << endl;
130 return 1;
131 }
132 struct hostent *server;
133 server = gethostbyname(host.c_str());
134 if (!server) {
135 cerr << "Couldn't get host ip! " << strerror(errno) << endl;
136 return 1;
137 }
138 struct sockaddr_in serv_addr;
139 bzero((char *)&serv_addr, sizeof(serv_addr));
140 serv_addr.sin_family = AF_INET;
141 bcopy((char *)server->h_addr,
142 (char *)&serv_addr.sin_addr.s_addr,
143 server->h_length);
144 serv_addr.sin_port = htons(8000);
145 if (connect(sock, (sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
146 cerr << "Couldn't connect to server! " << strerror(errno) << endl;
147 return 1;
148 }
149 while (child->isTracing()) {
150 if (!child->sendState(sock))
151 break;
152 child->step();
153 }
154 }
155 if (!child->stopTracing()) {
156 cerr << "Couldn't stop child" << endl;
157 return 1;
158 }
159 return 0;
160 }
161