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 /*
  25  * @test
  26  * @bug 8025692
  27  * @modules java.base/jdk.internal.misc
  28  *          java.management
  29  * @library /testlibrary
  30  * @compile TestLogTouchedMethods.java PrintTouchedMethods.java
  31  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+LogTouchedMethods PrintTouchedMethods
  32  */
  33 
  34 import java.io.File;
  35 import java.util.List;
  36 import jdk.test.lib.*;
  37 
  38 public class PrintTouchedMethods {
  39 
  40     public static void main(String args[]) throws Exception {
  41       String[] javaArgs1 = {"-XX:-UnlockDiagnosticVMOptions", "-XX:+LogTouchedMethods", "-XX:+PrintTouchedMethodsAtExit", "TestLogTouchedMethods"};
  42       ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(javaArgs1);
  43 
  44       // UnlockDiagnostic turned off, should fail
  45       OutputAnalyzer output = new OutputAnalyzer(pb.start());
  46       output.shouldContain("Error: VM option 'LogTouchedMethods' is diagnostic and must be enabled via -XX:+UnlockDiagnosticVMOptions.");
  47       output.shouldContain("Error: Could not create the Java Virtual Machine.");
  48 
  49       String[] javaArgs2 = {"-XX:+UnlockDiagnosticVMOptions", "-XX:+LogTouchedMethods", "-XX:+PrintTouchedMethodsAtExit", "TestLogTouchedMethods"};
  50       pb = ProcessTools.createJavaProcessBuilder(javaArgs2);
  51       output = new OutputAnalyzer(pb.start());
  52       // check order:
  53       // 1 "# Method::print_touched_methods version 1" is the first in first line
  54       // 2 should contain TestLogMethods.methodA:()V
  55       // 3 should not contain TestLogMethods.methodB:()V
  56       // Repeat above for another run with -Xint
  57       List<String> lines = output.asLines();
  58 
  59       if (lines.size() < 1) {
  60         throw new Exception("Empty output");
  61       }
  62 
  63       String first = lines.get(0);
  64       if (!first.equals("# Method::print_touched_methods version 1")) {
  65         throw new Exception("First line mismatch");
  66       }
  67 
  68       output.shouldContain("TestLogTouchedMethods.methodA:()V");
  69       output.shouldNotContain("TestLogTouchedMethods.methodB:()V");
  70       output.shouldHaveExitValue(0);
  71 
  72       String[] javaArgs3 = {"-XX:+UnlockDiagnosticVMOptions", "-Xint", "-XX:+LogTouchedMethods", "-XX:+PrintTouchedMethodsAtExit", "TestLogTouchedMethods"};
  73       pb = ProcessTools.createJavaProcessBuilder(javaArgs3);
  74       output = new OutputAnalyzer(pb.start());
  75       lines = output.asLines();
  76 
  77       if (lines.size() < 1) {
  78         throw new Exception("Empty output");
  79       }
  80 
  81       first = lines.get(0);
  82       if (!first.equals("# Method::print_touched_methods version 1")) {
  83         throw new Exception("First line mismatch");
  84       }
  85 
  86       output.shouldContain("TestLogTouchedMethods.methodA:()V");
  87       output.shouldNotContain("TestLogTouchedMethods.methodB:()V");
  88       output.shouldHaveExitValue(0);
  89 
  90       String[] javaArgs4 = {"-XX:+UnlockDiagnosticVMOptions", "-Xint", "-XX:+LogTouchedMethods", "-XX:+PrintTouchedMethodsAtExit", "-XX:-TieredCompilation", "TestLogTouchedMethods"};
  91       pb = ProcessTools.createJavaProcessBuilder(javaArgs4);
  92       output = new OutputAnalyzer(pb.start());
  93       lines = output.asLines();
  94 
  95       if (lines.size() < 1) {
  96         throw new Exception("Empty output");
  97       }
  98 
  99       first = lines.get(0);
 100       if (!first.equals("# Method::print_touched_methods version 1")) {
 101         throw new Exception("First line mismatch");
 102       }
 103 
 104       output.shouldContain("TestLogTouchedMethods.methodA:()V");
 105       output.shouldNotContain("TestLogTouchedMethods.methodB:()V");
 106       output.shouldHaveExitValue(0);
 107 
 108       // Test jcmd PrintTouchedMethods VM.print_touched_methods
 109       String pid = Long.toString(ProcessTools.getProcessId());
 110       pb = new ProcessBuilder();
 111       pb.command(new String[] {JDKToolFinder.getJDKTool("jcmd"), pid, "VM.print_touched_methods"});
 112       output = new OutputAnalyzer(pb.start());
 113       try {
 114         output.shouldContain("PrintTouchedMethods.main:([Ljava/lang/String;)V");
 115       } catch (RuntimeException e) {
 116         output.shouldContain("Unknown diagnostic command");
 117       }
 118   }
 119 }