1 /*
   2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8173912
  27  * @requires vm.jvmci
  28  * @library / /test/lib
  29  * @library ../common/patches
  30  * @modules jdk.vm.ci/jdk.vm.ci.hotspot:+open
  31  * @build sun.hotspot.WhiteBox
  32  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  33  *                                sun.hotspot.WhiteBox$WhiteBoxPermission
  34  * @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper
  35  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  36  *                  -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  37  *                  compiler.jvmci.compilerToVM.GetFlagValueTest
  38  */
  39 
  40 package compiler.jvmci.compilerToVM;
  41 
  42 import jdk.test.lib.Asserts;
  43 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  44 import jdk.test.lib.process.OutputAnalyzer;
  45 import jdk.test.lib.process.ProcessTools;
  46 import java.math.BigInteger;
  47 import java.util.Arrays;
  48 import java.util.regex.Pattern;
  49 import java.util.regex.Matcher;
  50 import sun.hotspot.WhiteBox;
  51 
  52 public class GetFlagValueTest {
  53     public static void main(String[] args) throws Exception {
  54         try {
  55             CompilerToVMHelper.getFlagValue(null);
  56             Asserts.fail("Expected NullPointerException when calling getFlagValue(null)");
  57         } catch (NullPointerException e) {
  58             // expected
  59         }
  60 
  61         Object missing = CompilerToVMHelper.getFlagValue("this is surely not a flag");
  62         Asserts.assertEquals(CompilerToVMHelper.CTVM, missing);
  63 
  64         ProcessBuilder pb;
  65         OutputAnalyzer out;
  66 
  67         String[] arguments = {"-XX:+UnlockExperimentalVMOptions", "-XX:+EnableJVMCI", "-XX:+PrintFlagsFinal", "-version"};
  68         pb = ProcessTools.createJavaProcessBuilder(arguments);
  69         out = new OutputAnalyzer(pb.start());
  70 
  71         out.shouldHaveExitValue(0);
  72         String[] lines = out.getStdout().split("\\r?\\n");
  73         Asserts.assertTrue(lines.length > 1, "Expected output from -XX:+PrintFlagsFinal");
  74 
  75         final WhiteBox wb = WhiteBox.getWhiteBox();
  76 
  77         // Line example: ccstr PrintIdealGraphAddress = 127.0.0.1 {C2 notproduct} {default}
  78         Pattern flagLine = Pattern.compile("(\\w+)\\s+(\\w+)\\s+:?= (?:(.+))\\{[^}]+\\}\\s+\\{[^}]+\\}");
  79         for (String line : lines) {
  80             if (line.indexOf('=') != -1) {
  81                 line = line.trim();
  82                 Matcher m = flagLine.matcher(line);
  83                 Asserts.assertTrue(m.matches(), "Unexpected line in -XX:+PrintFlagsFinal output: " + line);
  84                 String type = m.group(1);
  85                 String name = m.group(2);
  86                 String expect = m.group(3).trim();
  87                 Object value = CompilerToVMHelper.getFlagValue(name);
  88                 Object wbValue = wb.getVMFlag(name);
  89                 Asserts.assertEquals(value, wbValue, "Value of flag " + name);
  90             }
  91         }
  92     }
  93 }