001    package edu.rice.cs.cunit;
002    
003    import edu.rice.cs.cunit.instrumentors.IInstrumentationStrategy;
004    import edu.rice.cs.cunit.instrumentors.threadCheck.CompoundThreadCheckStrategy;
005    import edu.rice.cs.cunit.instrumentors.threadCheck.CompoundReflectionThreadCheckStrategy;
006    import edu.rice.cs.cunit.util.Debug;
007    
008    import java.io.FileNotFoundException;
009    import java.io.FileOutputStream;
010    import java.io.PrintStream;
011    
012    /**
013     * Instruments files using the CompoundThreadCheckStrategy.
014     *
015     * @author Mathias Ricken
016     */
017    public class TCFileInstrumentor extends FileInstrumentor {
018        /**
019         * Run the general file instrumentor.
020         * @param args command line arguments.
021         */
022        public static void main(String[] args) {
023            setDefaultCunitRtJarName("tcrt.jar");
024            setDefaultInstrumentorName(CompoundThreadCheckStrategy.class.getName());
025            setDefaultSystemInstrumentorName(CompoundThreadCheckStrategy.class.getName());
026            new TCTool().run(args);
027        }
028    
029        /**
030         * Class to perform the command line utility tasks.
031         */
032        public static class TCTool extends FileInstrumentor.Tool {
033            /**
034             * Check the arguments, set the fields, and return the index of the first file.
035             * @param args command line arguments
036             * @return index of first file
037             */
038            protected int checkArgs(String[] args) {
039                if (args.length == 0) {
040                    help(System.out);
041                    System.exit(1);
042                }
043    
044                int argIndex = 0;
045                while((argIndex < args.length) && (args[argIndex].startsWith("-"))) {
046                    String a = args[argIndex++];
047                    if (a.equals("-h")) {
048                        help(System.out);
049                        System.exit(1);
050                    }
051                    else if (a.equals("-quiet")) {
052                        Debug.out.setDebug(false);
053                    }
054                    else if (a.equals("-nobackup")) {
055                        _createBackups = false;
056                    }
057                    else if (a.equals("-unpackjars")) {
058                        _unpackJars = true;
059                    }
060                    else if (a.equals("-noannot")) {
061                        _addAnnotationAttribute = false;
062                    }
063                    else if (a.equals("-output")) {
064                        if (argIndex + 1 > args.length) {
065                            System.err.println("Error: <txt> file parameter missing.");
066                            help(System.err);
067                            System.exit(1);
068                        }
069                        String name = args[argIndex++];
070                        try {
071                            _debugOut = new FileOutputStream(name);
072                            Debug.out.setOutput(new PrintStream(_debugOut, true));
073                        }
074                        catch(FileNotFoundException e) {
075                            System.err.println("Error: Could not open <txt> file " + name + " for output.");
076                            System.exit(1);
077                        }
078                    }
079                    else if (a.equals("-rt")) {
080                        _instrumentRt = true;
081                    }
082                    else if (a.equals("-rts")) {
083                        if (argIndex + 1 > args.length) {
084                            System.err.println("Error: <s> jar file parameter missing.");
085                            help(System.err);
086                            System.exit(1);
087                        }
088                        _sourceRtJarName = args[argIndex++];
089                    }
090                    else if (a.equals("-rtm")) {
091                        if (argIndex + 1 > args.length) {
092                            System.err.println("Error: <m> jar file parameter missing.");
093                            help(System.err);
094                            System.exit(1);
095                        }
096                        _cunitRtJarName = args[argIndex++];
097                    }
098                    else if (a.equals("-rtd")) {
099                        if (argIndex + 1 > args.length) {
100                            System.err.println("Error: <d> jar file parameter missing.");
101                            help(System.err);
102                            System.exit(1);
103                        }
104                        _destRtJarName = args[argIndex++];
105                    }
106                    else if (a.equals("-r")) {
107                        _instrumentorClassName = CompoundReflectionThreadCheckStrategy.class.getName();
108                        _sysInstrumentorClassName = CompoundReflectionThreadCheckStrategy.class.getName();
109                    }
110                    else if (a.equals("-X")) {
111                        if (argIndex + 1 > args.length) {
112                            System.err.println("Error: <s> parameter missing.");
113                            help(System.err);
114                            System.exit(1);
115                        }
116                        _parameters.add(args[argIndex++]);
117                    }
118                }
119                return argIndex;
120            }
121    
122            /**
123             * Print out a help message.
124             * @param out stream to output to
125             */
126            protected void help(PrintStream out) {
127                //           ---------1---------2---------3---------4---------5---------6---------7---------8
128                out.println("Flags : [-h] [-quiet] [-output <txt>] [-rt] [-rts <s>] [-rtm <m>] [-rtd <d>]");
129                out.println("        [-nobackup] [-r] [-X <s>]");
130                out.println("        [-noannot] [<filename> ...]");
131                out.println("-h                Show this help");
132                out.println("-quiet            No output");
133                out.println("-output <txt>     Save output in the text file <txt>");
134                out.println("-rt               Instrument the rt.jar file <s> using the instrumentor,");
135                out.println("                  instrument the Concutest jar file <m> using the");
136                out.println("                  system instrumentor, and write to the jar file <d>");
137                out.println("-rts <s>          Specify the source rt.jar file <s> (rt.jar)");
138                out.println("-rtm <m>          Specify the Concutest jar file <m> (cunitrt.jar)");
139                out.println("-rtd <d>          Specify the destination jar file <d> (rt_i.jar)");
140                out.println("-nobackup         Do not create backup files (*.jar~ or *.class~)");
141                out.println("-r                Use the reflection-based instrumentor");
142                out.println("-X <s>            Pass <s> as parameter to instrumentors; may be repeated");
143                out.println("-noannot          Do not annotate class files with the instrumentors used");
144                out.println("-unpackjars       Unpack/repack all jar files into temporary directories");
145                out.println("<filename> ...    List of class or jar file names to instrument using");
146                out.println("                  the instrumentor");
147                out.println();
148                out.println("If -unpackjars is used and -X class-path= has been specified, then the");
149                out.println("jar files on the class path will be replaced by their temporary directories.");
150                out.println("Jar files inside jar files will not be unpacked.");
151            }
152        }
153    
154        /**
155         * Creates a new instrumenting class loader using specified instrumentors.
156         * @param instrumentors array of instrumentors
157         */
158        public TCFileInstrumentor(IInstrumentationStrategy[] instrumentors) {
159            super(instrumentors);
160        }
161    }