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