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