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 <netinet/in.h> 32#include <sys/ptrace.h> 33#include <sys/socket.h> 34#include <sys/types.h> 35#include <sys/wait.h> 36#include <netdb.h> 37#include <unistd.h> 38 39#include <cerrno> 40#include <cstdio> 41#include <cstring> 42#include <fstream> 43#include <iostream> 44#include <string> 45 46#include "base/arch_check.h" 47#include "tracechild.hh" 48 49using namespace std; 50 51void 52printUsage(const char * execName) 53{ 54 cout << execName << " <options> -- <command> <arguments>" << endl; 55 cout << "options:" << endl; 56 cout << " -h print this help" << endl; 57 cout << " --host remote m5 host to connect to" << endl; 58 cout << " -i print initial stack state" << endl; 59 cout << " -nt don't trace execution" << 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], "-i")) { 92 printInitial = true; 93 } else if (!strcmp(argv[x], "-nt")) { 94 printTrace = false; 95 } else if (!strcmp(argv[x], "--")) { 96 x++; 97 if (x >= argc) { 98 cerr << "Incorrect usage.\n" << endl; 99 printUsage(argv[0]); 100 return 1; 101 } 102 startProgramArgs = x; 103 break; 104 } else { 105 cerr << "Incorrect usage.\n" << endl; 106 printUsage(argv[0]); 107 return 1; 108 } 109 } 110 if (!child->startTracing(argv[startProgramArgs], 111 argv + startProgramArgs)) { 112 cerr << "Couldn't start target program" << endl; 113 return 1; 114 } 115 child->step(); 116 if (printInitial) 117 child->outputStartState(cout); 118 if (printTrace) { 119 // Connect to m5 120 bool portSet = false; 121 int port; 122 int sock = socket(AF_INET, SOCK_STREAM, 0); 123 if (sock < 0) { 124 cerr << "Error opening socket! " << strerror(errno) << endl; 125 return 1; 126 } 127 struct hostent *server; 128 server = gethostbyname(host.c_str()); 129 if (!server) { 130 cerr << "Couldn't get host ip! " << strerror(errno) << endl; 131 return 1; 132 } 133 struct sockaddr_in serv_addr; 134 bzero((char *)&serv_addr, sizeof(serv_addr)); 135 serv_addr.sin_family = AF_INET; 136 bcopy((char *)server->h_addr, 137 (char *)&serv_addr.sin_addr.s_addr, 138 server->h_length); 139 serv_addr.sin_port = htons(8000); 140 if (connect(sock, (sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { 141 cerr << "Couldn't connect to server! " << strerror(errno) << endl; 142 return 1; 143 } 144 while (child->isTracing()) { 145 if (!child->sendState(sock)) 146 break; 147 child->step(); 148 } 149 } 150 if (!child->stopTracing()) { 151 cerr << "Couldn't stop child" << endl; 152 return 1; 153 } 154 return 0; 155} 156 157