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