001    package edu.rice.cs.cunit;
002    
003    import edu.rice.cs.cunit.instrumentors.threadCheck.CompoundThreadCheckStrategy;
004    import edu.rice.cs.cunit.instrumentors.threadCheck.CompoundReflectionThreadCheckStrategy;
005    import edu.rice.cs.cunit.util.Debug;
006    
007    import java.io.FileOutputStream;
008    import java.io.PrintStream;
009    import java.io.FileNotFoundException;
010    
011    /**
012     * Execute a Java program by loading it with the InstrumentingClassLoader and running the Thread Checker.
013     *
014     * @author Mathias Ricken
015     */
016    public class TCRun extends ACUnitRun {
017        /**
018         * String with syntax description for TCRun.
019         */
020        protected static final String OPTIONS_FORMAT_TCRUN = "[-h] [-d] [-output <txt>] [-r] [-ns] [-backup] [-X <s>] <class> [args...]";
021    
022        /**
023         * Static main method.
024         *
025         * @param args command line arguments
026         */
027        public static void main(String args[]) {
028            (new TCRun()).run(args);
029        }
030    
031        /**
032         * Print a help message to the specified stream.
033         * @param out output stream
034         */
035        protected static void help(PrintStream out) {
036            //           ---------1---------2---------3---------4---------5---------6---------7---------8
037            out.println("Flags : "+ OPTIONS_FORMAT_TCRUN);
038            out.println("-h                Show this help");
039            out.println("-d                Debug output");
040            out.println("-output <txt>     Debug output to file <txt>");
041            out.println("-r                Run the reflection-based instrumentor");
042            out.println("-ns               Do not save instrumented class files");
043            out.println("-backup           Make backups of original class files (only if no -nw)");
044            out.println("-X <s>            Pass <s> as parameter to instrumentors; may be repeated");
045            out.println("<class> [args...] Class file whose main method to execute, and arguments.");
046        }
047    
048        /**
049         * Read the command line arguments or ask for them in a dialog.
050         *
051         * @param args command line arguments
052         */
053        protected void readArgs(String[] args) {
054            if (args.length == 0) {
055                help(System.err);
056                System.exit(1);
057            }
058    
059            int argIndex = 0;
060            while(args[argIndex].startsWith("-")) {
061                final String a = args[argIndex];
062                if (a.equalsIgnoreCase("-?") || a.equalsIgnoreCase("-h")) {
063                    help(System.out);
064                    System.exit(1);
065                }
066                else if (a.equals("-nw")) {
067                    _noSaveInstrumented = true;
068                }
069                else if (a.equals("-backup")) {
070                    _makeBackups = true;
071                }
072                else if (a.equals("-d")) {
073                    Debug.out.setDebug(true);
074                }
075                else if (a.equals("-output")) {
076                    if (argIndex + 1 > args.length) {
077                        System.err.println("Error: <txt> file parameter missing.");
078                        help(System.err);
079                        System.exit(1);
080                    }
081                    String name = args[++argIndex];
082                    try {
083                        FileOutputStream debugOut = new FileOutputStream(name);
084                        Debug.out.setOutput(new PrintStream(debugOut, true));
085                        Debug.out.setDebug(true);
086                    }
087                    catch(FileNotFoundException e) {
088                        System.err.println("Error: Could not open <txt> file " + name + " for output.");
089                        System.exit(1);
090                    }
091                }
092                else if (a.equals("-r")) {
093                    _instrumentorClassName = CompoundReflectionThreadCheckStrategy.class.getName();
094                }
095                else if (a.equals("-X")) {
096                    if (argIndex + 1 > args.length) {
097                        System.err.println("Error: <s> parameter missing.");
098                        help(System.err);
099                        System.exit(1);
100                    }
101                    _parameters.add(args[++argIndex]);
102                }
103                ++argIndex;
104            }
105    
106            if (_noSaveInstrumented && _makeBackups) {
107                System.err.println("Error: -backup may only be specified without -ns.");
108                System.exit(1);
109            }
110    
111            _className = args[argIndex];
112    
113            _progArgs = new String[args.length - 1 - argIndex];
114            System.arraycopy(args, 1, _progArgs, 0, _progArgs.length);
115        }
116    
117        /**
118         * @return the default name of the instrumentor.
119         */
120        public String getDefaultInstrumentorName() {
121            return CompoundThreadCheckStrategy.class.getName();
122        }
123    }