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