1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/prims/jvmtiEnvFill.java Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,260 @@ 1.4 +/* 1.5 + * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +import java.io.*; 1.29 +import java.util.*; 1.30 + 1.31 +class jvmtiEnvFill { 1.32 + 1.33 + public static void main(String[] args) throws IOException { 1.34 + if (args.length != 3) { 1.35 + System.err.println("usage: <filledFile> <stubFile> <resultFile>"); 1.36 + System.exit(1); 1.37 + } 1.38 + String filledFN = args[0]; 1.39 + String stubFN = args[1]; 1.40 + String resultFN = args[2]; 1.41 + 1.42 + SourceFile filledSF = new SourceFile(filledFN); 1.43 + SourceFile stubSF = new SourceFile(stubFN); 1.44 + 1.45 + 1.46 + stubSF.fill(filledSF); 1.47 + 1.48 + PrintWriter out = new PrintWriter(new FileWriter(resultFN)); 1.49 + stubSF.output(out); 1.50 + out.close(); 1.51 + } 1.52 +} 1.53 + 1.54 +class SourceFile { 1.55 + 1.56 + static final String endFilePrefix = "// end file prefix"; 1.57 + static final String functionPrefix = "JvmtiEnv::"; 1.58 + 1.59 + final String fn; 1.60 + LineNumberReader in; 1.61 + String line; 1.62 + List<String> top = new ArrayList<String>(); 1.63 + List<String> before = new ArrayList<String>(); 1.64 + boolean inFilePrefix = true; 1.65 + List<Function> functions = new ArrayList<Function>(); 1.66 + Map<String, Function> functionMap = new HashMap<String, Function>(); 1.67 + 1.68 + class Function { 1.69 + String name; 1.70 + String args; 1.71 + String compareArgs; 1.72 + List comment; 1.73 + List<String> body = new ArrayList<String>(); 1.74 + 1.75 + Function() throws IOException { 1.76 + line = in.readLine(); 1.77 + String trimmed = line.trim(); 1.78 + if (!trimmed.startsWith(functionPrefix)) { 1.79 + error("expected '" + functionPrefix + "'"); 1.80 + } 1.81 + int index = trimmed.indexOf('(', functionPrefix.length()); 1.82 + if (index == -1) { 1.83 + error("missing open paren"); 1.84 + } 1.85 + name = trimmed.substring(functionPrefix.length(), index); 1.86 + int index2 = trimmed.indexOf(')', index); 1.87 + if (index2 == -1) { 1.88 + error("missing close paren - must be on same line"); 1.89 + } 1.90 + args = trimmed.substring(index+1, index2); 1.91 + compareArgs = args.replaceAll("\\s", ""); 1.92 + String tail = trimmed.substring(index2+1).trim(); 1.93 + if (!tail.equals("{")) { 1.94 + error("function declaration first line must end with open bracket '{', instead got '" + 1.95 + tail + "'"); 1.96 + } 1.97 + while(true) { 1.98 + line = in.readLine(); 1.99 + if (line == null) { 1.100 + line = ""; // so error does not look wierd 1.101 + error("unexpected end of file"); 1.102 + } 1.103 + if (line.startsWith("}")) { 1.104 + break; 1.105 + } 1.106 + body.add(line); 1.107 + } 1.108 + String expected = "} /* end " + name + " */"; 1.109 + trimmed = line.replaceAll("\\s",""); 1.110 + if (!trimmed.equals(expected.replaceAll("\\s",""))) { 1.111 + error("function end is malformed - should be: " + expected); 1.112 + } 1.113 + // copy over the comment prefix 1.114 + comment = before; 1.115 + before = new ArrayList<String>(); 1.116 + } 1.117 + 1.118 + void remove() { 1.119 + functionMap.remove(name); 1.120 + } 1.121 + 1.122 + String fileName() { 1.123 + return fn; 1.124 + } 1.125 + 1.126 + void fill(Function filledFunc) { 1.127 + if (filledFunc == null) { 1.128 + System.err.println("Warning: function " + name + " missing from filled file"); 1.129 + body.add(0, " /*** warning: function added and not filled in ***/"); 1.130 + } else { 1.131 + int fbsize = filledFunc.body.size(); 1.132 + int bsize = body.size(); 1.133 + if (fbsize > bsize || !body.subList(bsize-fbsize,bsize).equals(filledFunc.body)) { 1.134 + // it has actually been filled in 1.135 + body = filledFunc.body; 1.136 + if (!compareArgs.equals(filledFunc.compareArgs)) { 1.137 + System.err.println("Warning: function " + name + 1.138 + ": filled and stub arguments differ"); 1.139 + System.err.println(" old (filled): " + filledFunc.args); 1.140 + System.err.println(" new (stub): " + args); 1.141 + body.add(0, " /*** warning: arguments changed, were: " + 1.142 + filledFunc.args + " ***/"); 1.143 + } 1.144 + } 1.145 + filledFunc.remove(); // mark used 1.146 + } 1.147 + } 1.148 + 1.149 + void output(PrintWriter out) { 1.150 + Iterator it = comment.iterator(); 1.151 + while (it.hasNext()) { 1.152 + out.println(it.next()); 1.153 + } 1.154 + out.println("jvmtiError"); 1.155 + out.print(functionPrefix); 1.156 + out.print(name); 1.157 + out.print('('); 1.158 + out.print(args); 1.159 + out.println(") {"); 1.160 + it = body.iterator(); 1.161 + while (it.hasNext()) { 1.162 + out.println(it.next()); 1.163 + } 1.164 + out.print("} /* end "); 1.165 + out.print(name); 1.166 + out.println(" */"); 1.167 + } 1.168 + } 1.169 + 1.170 + SourceFile(String fn) throws IOException { 1.171 + this.fn = fn; 1.172 + Reader reader = new FileReader(fn); 1.173 + in = new LineNumberReader(reader); 1.174 + 1.175 + while (readGaps()) { 1.176 + Function func = new Function(); 1.177 + functionMap.put(func.name, func); 1.178 + functions.add(func); 1.179 + } 1.180 + 1.181 + in.close(); 1.182 + } 1.183 + 1.184 + void error(String msg) { 1.185 + System.err.println("Fatal error parsing file: " + fn); 1.186 + System.err.println("Line number: " + in.getLineNumber()); 1.187 + System.err.println("Error message: " + msg); 1.188 + System.err.println("Source line: " + line); 1.189 + System.exit(1); 1.190 + } 1.191 + 1.192 + boolean readGaps() throws IOException { 1.193 + while(true) { 1.194 + line = in.readLine(); 1.195 + if (line == null) { 1.196 + return false; // end of file 1.197 + } 1.198 + if (!inFilePrefix && line.startsWith("}")) { 1.199 + error("unexpected close bracket in first column, outside of function.\n"); 1.200 + } 1.201 + String trimmed = line.trim(); 1.202 + if (line.startsWith("jvmtiError")) { 1.203 + if (trimmed.equals("jvmtiError")) { 1.204 + if (inFilePrefix) { 1.205 + error("unexpected 'jvmtiError' line in file prefix.\n" + 1.206 + "is '" + endFilePrefix + "'... line missing?"); 1.207 + } 1.208 + return true; // beginning of a function 1.209 + } else { 1.210 + error("extra characters at end of 'jvmtiError'"); 1.211 + } 1.212 + } 1.213 + if (inFilePrefix) { 1.214 + top.add(line); 1.215 + } else { 1.216 + trimmed = line.trim(); 1.217 + if (!trimmed.equals("") && !trimmed.startsWith("//") && !trimmed.startsWith("#")) { 1.218 + error("only comments and blank lines allowed between functions"); 1.219 + } 1.220 + before.add(line); 1.221 + } 1.222 + if (line.replaceAll("\\s","").toLowerCase().startsWith(endFilePrefix.replaceAll("\\s",""))) { 1.223 + if (!inFilePrefix) { 1.224 + error("excess '" + endFilePrefix + "'"); 1.225 + } 1.226 + inFilePrefix = false; 1.227 + } 1.228 + } 1.229 + } 1.230 + 1.231 + void fill(SourceFile filledSF) { 1.232 + // copy beginning of file straight from filled file 1.233 + top = filledSF.top; 1.234 + 1.235 + // file in functions 1.236 + Iterator it = functions.iterator(); 1.237 + while (it.hasNext()) { 1.238 + Function stubFunc = (Function)(it.next()); 1.239 + Function filledFunc = (Function)filledSF.functionMap.get(stubFunc.name); 1.240 + stubFunc.fill(filledFunc); 1.241 + } 1.242 + if (filledSF.functionMap.size() > 0) { 1.243 + System.err.println("Warning: the following functions were present in the " + 1.244 + "filled file but missing in the stub file and thus not copied:"); 1.245 + it = filledSF.functionMap.values().iterator(); 1.246 + while (it.hasNext()) { 1.247 + System.err.println(" " + ((Function)(it.next())).name); 1.248 + } 1.249 + } 1.250 + } 1.251 + 1.252 + void output(PrintWriter out) { 1.253 + Iterator it = top.iterator(); 1.254 + while (it.hasNext()) { 1.255 + out.println(it.next()); 1.256 + } 1.257 + it = functions.iterator(); 1.258 + while (it.hasNext()) { 1.259 + Function stubFunc = (Function)(it.next()); 1.260 + stubFunc.output(out); 1.261 + } 1.262 + } 1.263 +}