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