1 /*
   2  * Copyright (c) 2015, 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 package compiler.compilercontrol.share.processors;
  25 
  26 import com.sun.management.HotSpotDiagnosticMXBean;
  27 import compiler.compilercontrol.share.method.MethodDescriptor;
  28 import compiler.compilercontrol.share.method.MethodGenerator;
  29 import compiler.compilercontrol.share.scenario.State;
  30 import jdk.test.lib.OutputAnalyzer;
  31 import jdk.test.lib.Pair;
  32 import pool.PoolHelper;
  33 
  34 import java.lang.management.ManagementFactory;
  35 import java.lang.reflect.Executable;
  36 import java.util.List;
  37 import java.util.Map;
  38 import java.util.function.Consumer;
  39 import java.util.regex.Matcher;
  40 import java.util.regex.Pattern;
  41 import java.util.stream.Collectors;
  42 
  43 /**
  44  * Process output to find compiled methods assemblies printed by print command
  45  */
  46 public class PrintProcessor implements Consumer<OutputAnalyzer> {
  47     /**
  48      * Compiled method string pattern.
  49      * Capturing groups are
  50      * 1. Compiler used to compile this method
  51      * 2. Time stamp
  52      * 3. Compile ID
  53      * 4. Method attributes
  54      * 5. Compilation level
  55      * 6. Method name
  56      */
  57     private static final Pattern COMPILED_METHOD
  58             = Pattern.compile("Compiled method (?<compiler>\\(.*\\))[ ]+"
  59             + "(?<time>[0-9]+)[ ]+(?<id>[0-9]+) (?<attr>[ !%sbn]{6})"
  60             + "(?<level>[0-9]+)[ ]+(?<name>[^ ]+).*");
  61     private final List<String> printMethods;
  62     private final List<String> testMethods;
  63 
  64     public PrintProcessor(Map<Executable, State> states) {
  65         printMethods = states.keySet().stream()
  66                 .filter(x -> states.get(x).isPrintAssembly())
  67                 .map(MethodGenerator::logDescriptor)
  68                 .map(MethodDescriptor::getString)
  69                 .map(s -> s.replaceFirst("\\(.*", "")) // remove signature
  70                 .collect(Collectors.toList());
  71         testMethods = new PoolHelper().getAllMethods()
  72                 .stream()
  73                 .map(pair -> pair.first)
  74                 .map(MethodGenerator::logDescriptor)
  75                 .map(MethodDescriptor::getString)
  76                 .map(s -> s.replaceFirst("\\(.*", "")) // remove signature
  77                 .collect(Collectors.toList());
  78     }
  79 
  80     @Override
  81     public void accept(OutputAnalyzer outputAnalyzer) {
  82         boolean wizardMode = false;
  83         try {
  84             wizardMode = Boolean.parseBoolean(ManagementFactory
  85                     .getPlatformMXBean(HotSpotDiagnosticMXBean.class)
  86                     .getVMOption("WizardMode").getValue());
  87         } catch (IllegalArgumentException e) {
  88             // ignore exception because WizardMode exists in debug only builds
  89         }
  90         if (wizardMode) {
  91             System.out.println("SKIP: WizardMode's output are not supported");
  92             return;
  93         }
  94         for (String line : outputAnalyzer.asLines()) {
  95             Matcher matcher = COMPILED_METHOD.matcher(line);
  96             if (matcher.matches()) {
  97                 String method = normalize(matcher.group("name"));
  98                 if (!printMethods.contains(normalize(method))
  99                         && testMethods.contains(method)) {
 100                     System.out.println(outputAnalyzer.getOutput());
 101                     throw new AssertionError("FAILED: wrong method "
 102                             + "was printed: " + method + " LINE: " + line);
 103                 }
 104             }
 105         }
 106     }
 107 
 108     // Normalize given signature to conform regular expression used in tests
 109     private String normalize(String method) {
 110         return method.replaceAll("\\.", "/") // replace dots in a class string
 111                 .replaceFirst("::", ".")     // replace :: between class and method
 112                 .replace("&lt;", "<")
 113                 .replace("&gt;", ">");
 114     }
 115 }