src/share/vm/prims/jvmtiEnvFill.java

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 1907
c18cbe5936b8
parent 0
f90c822e73f8
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 import java.io.*;
aoqi@0 26 import java.util.*;
aoqi@0 27
aoqi@0 28 class jvmtiEnvFill {
aoqi@0 29
aoqi@0 30 public static void main(String[] args) throws IOException {
aoqi@0 31 if (args.length != 3) {
aoqi@0 32 System.err.println("usage: <filledFile> <stubFile> <resultFile>");
aoqi@0 33 System.exit(1);
aoqi@0 34 }
aoqi@0 35 String filledFN = args[0];
aoqi@0 36 String stubFN = args[1];
aoqi@0 37 String resultFN = args[2];
aoqi@0 38
aoqi@0 39 SourceFile filledSF = new SourceFile(filledFN);
aoqi@0 40 SourceFile stubSF = new SourceFile(stubFN);
aoqi@0 41
aoqi@0 42
aoqi@0 43 stubSF.fill(filledSF);
aoqi@0 44
aoqi@0 45 PrintWriter out = new PrintWriter(new FileWriter(resultFN));
aoqi@0 46 stubSF.output(out);
aoqi@0 47 out.close();
aoqi@0 48 }
aoqi@0 49 }
aoqi@0 50
aoqi@0 51 class SourceFile {
aoqi@0 52
aoqi@0 53 static final String endFilePrefix = "// end file prefix";
aoqi@0 54 static final String functionPrefix = "JvmtiEnv::";
aoqi@0 55
aoqi@0 56 final String fn;
aoqi@0 57 LineNumberReader in;
aoqi@0 58 String line;
aoqi@0 59 List<String> top = new ArrayList<String>();
aoqi@0 60 List<String> before = new ArrayList<String>();
aoqi@0 61 boolean inFilePrefix = true;
aoqi@0 62 List<Function> functions = new ArrayList<Function>();
aoqi@0 63 Map<String, Function> functionMap = new HashMap<String, Function>();
aoqi@0 64
aoqi@0 65 class Function {
aoqi@0 66 String name;
aoqi@0 67 String args;
aoqi@0 68 String compareArgs;
aoqi@0 69 List comment;
aoqi@0 70 List<String> body = new ArrayList<String>();
aoqi@0 71
aoqi@0 72 Function() throws IOException {
aoqi@0 73 line = in.readLine();
aoqi@0 74 String trimmed = line.trim();
aoqi@0 75 if (!trimmed.startsWith(functionPrefix)) {
aoqi@0 76 error("expected '" + functionPrefix + "'");
aoqi@0 77 }
aoqi@0 78 int index = trimmed.indexOf('(', functionPrefix.length());
aoqi@0 79 if (index == -1) {
aoqi@0 80 error("missing open paren");
aoqi@0 81 }
aoqi@0 82 name = trimmed.substring(functionPrefix.length(), index);
aoqi@0 83 int index2 = trimmed.indexOf(')', index);
aoqi@0 84 if (index2 == -1) {
aoqi@0 85 error("missing close paren - must be on same line");
aoqi@0 86 }
aoqi@0 87 args = trimmed.substring(index+1, index2);
aoqi@0 88 compareArgs = args.replaceAll("\\s", "");
aoqi@0 89 String tail = trimmed.substring(index2+1).trim();
aoqi@0 90 if (!tail.equals("{")) {
aoqi@0 91 error("function declaration first line must end with open bracket '{', instead got '" +
aoqi@0 92 tail + "'");
aoqi@0 93 }
aoqi@0 94 while(true) {
aoqi@0 95 line = in.readLine();
aoqi@0 96 if (line == null) {
aoqi@0 97 line = ""; // so error does not look wierd
aoqi@0 98 error("unexpected end of file");
aoqi@0 99 }
aoqi@0 100 if (line.startsWith("}")) {
aoqi@0 101 break;
aoqi@0 102 }
aoqi@0 103 body.add(line);
aoqi@0 104 }
aoqi@0 105 String expected = "} /* end " + name + " */";
aoqi@0 106 trimmed = line.replaceAll("\\s","");
aoqi@0 107 if (!trimmed.equals(expected.replaceAll("\\s",""))) {
aoqi@0 108 error("function end is malformed - should be: " + expected);
aoqi@0 109 }
aoqi@0 110 // copy over the comment prefix
aoqi@0 111 comment = before;
aoqi@0 112 before = new ArrayList<String>();
aoqi@0 113 }
aoqi@0 114
aoqi@0 115 void remove() {
aoqi@0 116 functionMap.remove(name);
aoqi@0 117 }
aoqi@0 118
aoqi@0 119 String fileName() {
aoqi@0 120 return fn;
aoqi@0 121 }
aoqi@0 122
aoqi@0 123 void fill(Function filledFunc) {
aoqi@0 124 if (filledFunc == null) {
aoqi@0 125 System.err.println("Warning: function " + name + " missing from filled file");
aoqi@0 126 body.add(0, " /*** warning: function added and not filled in ***/");
aoqi@0 127 } else {
aoqi@0 128 int fbsize = filledFunc.body.size();
aoqi@0 129 int bsize = body.size();
aoqi@0 130 if (fbsize > bsize || !body.subList(bsize-fbsize,bsize).equals(filledFunc.body)) {
aoqi@0 131 // it has actually been filled in
aoqi@0 132 body = filledFunc.body;
aoqi@0 133 if (!compareArgs.equals(filledFunc.compareArgs)) {
aoqi@0 134 System.err.println("Warning: function " + name +
aoqi@0 135 ": filled and stub arguments differ");
aoqi@0 136 System.err.println(" old (filled): " + filledFunc.args);
aoqi@0 137 System.err.println(" new (stub): " + args);
aoqi@0 138 body.add(0, " /*** warning: arguments changed, were: " +
aoqi@0 139 filledFunc.args + " ***/");
aoqi@0 140 }
aoqi@0 141 }
aoqi@0 142 filledFunc.remove(); // mark used
aoqi@0 143 }
aoqi@0 144 }
aoqi@0 145
aoqi@0 146 void output(PrintWriter out) {
aoqi@0 147 Iterator it = comment.iterator();
aoqi@0 148 while (it.hasNext()) {
aoqi@0 149 out.println(it.next());
aoqi@0 150 }
aoqi@0 151 out.println("jvmtiError");
aoqi@0 152 out.print(functionPrefix);
aoqi@0 153 out.print(name);
aoqi@0 154 out.print('(');
aoqi@0 155 out.print(args);
aoqi@0 156 out.println(") {");
aoqi@0 157 it = body.iterator();
aoqi@0 158 while (it.hasNext()) {
aoqi@0 159 out.println(it.next());
aoqi@0 160 }
aoqi@0 161 out.print("} /* end ");
aoqi@0 162 out.print(name);
aoqi@0 163 out.println(" */");
aoqi@0 164 }
aoqi@0 165 }
aoqi@0 166
aoqi@0 167 SourceFile(String fn) throws IOException {
aoqi@0 168 this.fn = fn;
aoqi@0 169 Reader reader = new FileReader(fn);
aoqi@0 170 in = new LineNumberReader(reader);
aoqi@0 171
aoqi@0 172 while (readGaps()) {
aoqi@0 173 Function func = new Function();
aoqi@0 174 functionMap.put(func.name, func);
aoqi@0 175 functions.add(func);
aoqi@0 176 }
aoqi@0 177
aoqi@0 178 in.close();
aoqi@0 179 }
aoqi@0 180
aoqi@0 181 void error(String msg) {
aoqi@0 182 System.err.println("Fatal error parsing file: " + fn);
aoqi@0 183 System.err.println("Line number: " + in.getLineNumber());
aoqi@0 184 System.err.println("Error message: " + msg);
aoqi@0 185 System.err.println("Source line: " + line);
aoqi@0 186 System.exit(1);
aoqi@0 187 }
aoqi@0 188
aoqi@0 189 boolean readGaps() throws IOException {
aoqi@0 190 while(true) {
aoqi@0 191 line = in.readLine();
aoqi@0 192 if (line == null) {
aoqi@0 193 return false; // end of file
aoqi@0 194 }
aoqi@0 195 if (!inFilePrefix && line.startsWith("}")) {
aoqi@0 196 error("unexpected close bracket in first column, outside of function.\n");
aoqi@0 197 }
aoqi@0 198 String trimmed = line.trim();
aoqi@0 199 if (line.startsWith("jvmtiError")) {
aoqi@0 200 if (trimmed.equals("jvmtiError")) {
aoqi@0 201 if (inFilePrefix) {
aoqi@0 202 error("unexpected 'jvmtiError' line in file prefix.\n" +
aoqi@0 203 "is '" + endFilePrefix + "'... line missing?");
aoqi@0 204 }
aoqi@0 205 return true; // beginning of a function
aoqi@0 206 } else {
aoqi@0 207 error("extra characters at end of 'jvmtiError'");
aoqi@0 208 }
aoqi@0 209 }
aoqi@0 210 if (inFilePrefix) {
aoqi@0 211 top.add(line);
aoqi@0 212 } else {
aoqi@0 213 trimmed = line.trim();
aoqi@0 214 if (!trimmed.equals("") && !trimmed.startsWith("//") && !trimmed.startsWith("#")) {
aoqi@0 215 error("only comments and blank lines allowed between functions");
aoqi@0 216 }
aoqi@0 217 before.add(line);
aoqi@0 218 }
aoqi@0 219 if (line.replaceAll("\\s","").toLowerCase().startsWith(endFilePrefix.replaceAll("\\s",""))) {
aoqi@0 220 if (!inFilePrefix) {
aoqi@0 221 error("excess '" + endFilePrefix + "'");
aoqi@0 222 }
aoqi@0 223 inFilePrefix = false;
aoqi@0 224 }
aoqi@0 225 }
aoqi@0 226 }
aoqi@0 227
aoqi@0 228 void fill(SourceFile filledSF) {
aoqi@0 229 // copy beginning of file straight from filled file
aoqi@0 230 top = filledSF.top;
aoqi@0 231
aoqi@0 232 // file in functions
aoqi@0 233 Iterator it = functions.iterator();
aoqi@0 234 while (it.hasNext()) {
aoqi@0 235 Function stubFunc = (Function)(it.next());
aoqi@0 236 Function filledFunc = (Function)filledSF.functionMap.get(stubFunc.name);
aoqi@0 237 stubFunc.fill(filledFunc);
aoqi@0 238 }
aoqi@0 239 if (filledSF.functionMap.size() > 0) {
aoqi@0 240 System.err.println("Warning: the following functions were present in the " +
aoqi@0 241 "filled file but missing in the stub file and thus not copied:");
aoqi@0 242 it = filledSF.functionMap.values().iterator();
aoqi@0 243 while (it.hasNext()) {
aoqi@0 244 System.err.println(" " + ((Function)(it.next())).name);
aoqi@0 245 }
aoqi@0 246 }
aoqi@0 247 }
aoqi@0 248
aoqi@0 249 void output(PrintWriter out) {
aoqi@0 250 Iterator it = top.iterator();
aoqi@0 251 while (it.hasNext()) {
aoqi@0 252 out.println(it.next());
aoqi@0 253 }
aoqi@0 254 it = functions.iterator();
aoqi@0 255 while (it.hasNext()) {
aoqi@0 256 Function stubFunc = (Function)(it.next());
aoqi@0 257 stubFunc.output(out);
aoqi@0 258 }
aoqi@0 259 }
aoqi@0 260 }

mercurial