1 /*
   2  * Copyright (c) 2016, 2018, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.jfr.startupargs;
  27 
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 import java.util.function.Consumer;
  31 
  32 import jdk.test.lib.jfr.EventNames;
  33 import jdk.test.lib.jfr.SimpleEvent;
  34 import jdk.test.lib.process.OutputAnalyzer;
  35 import jdk.test.lib.process.ProcessTools;
  36 
  37 /**
  38  * @test
  39  * @key jfr
  40  * 
  41  * @library /lib /
  42  * @run main/othervm jdk.jfr.startupargs.TestRetransformUsingLog
  43  */
  44 public class TestRetransformUsingLog {
  45 
  46     private static final String FILE_READ_FORCED_CLASS_LOAD = "Adding forced instrumentation for event type " + EventNames.FileRead + " during initial class load";
  47     private static final String SIMPLE_EVENT_FORCED_CLASS_LOAD = "Adding forced instrumentation for event type jdk.test.lib.jfr.SimpleEvent during initial class load";
  48     private static final String SIMPLE_EVENT_UNFORCED_CLASS_LOAD = "Adding instrumentation for event type jdk.test.lib.jfr.SimpleEvent during initial class load";
  49 
  50     public static class TestApp {
  51         public static void main(String[] args) throws Exception {
  52             SimpleEvent event = new SimpleEvent();
  53             event.commit();
  54         }
  55     }
  56 
  57     public static void main(String[] args) throws Exception {
  58         testRecordingRetransFormFalse();
  59         testRecordingRetransFormTrue();
  60         testNoRecordingRetransFormFalse();
  61         testNoRecordingRetransFormTrue();
  62     }
  63 
  64     private static void testRecordingRetransFormFalse() throws Exception {
  65         startApp(true, false, out -> {
  66             out.shouldContain(FILE_READ_FORCED_CLASS_LOAD);
  67             out.shouldContain(SIMPLE_EVENT_FORCED_CLASS_LOAD);
  68         });
  69     }
  70 
  71     private static void testRecordingRetransFormTrue() throws Exception {
  72         startApp(true, true, out -> {
  73             out.shouldContain(FILE_READ_FORCED_CLASS_LOAD);
  74             out.shouldContain(SIMPLE_EVENT_UNFORCED_CLASS_LOAD);
  75         });
  76     }
  77 
  78     private static void testNoRecordingRetransFormFalse() throws Exception {
  79         startApp(false, false, out -> {
  80             out.shouldNotContain(FILE_READ_FORCED_CLASS_LOAD);
  81             out.shouldContain(SIMPLE_EVENT_FORCED_CLASS_LOAD);
  82         });
  83     }
  84 
  85     private static void testNoRecordingRetransFormTrue() throws Exception {
  86         startApp(false, true, out -> {
  87             out.shouldNotContain(FILE_READ_FORCED_CLASS_LOAD);
  88             out.shouldNotContain(SIMPLE_EVENT_FORCED_CLASS_LOAD);
  89             out.shouldNotContain(SIMPLE_EVENT_UNFORCED_CLASS_LOAD);
  90         });
  91     }
  92 
  93     private static void startApp(boolean recording, boolean retransform, Consumer<OutputAnalyzer> verifier) throws Exception {
  94         List<String> args = new ArrayList<>();
  95         args.add("-XX:+LogJFR");
  96         args.add("-XX:FlightRecorderOptions=retransform=" + retransform);
  97         if (recording) {
  98             args.add("-XX:StartFlightRecording");
  99         }
 100         args.add(TestApp.class.getName());
 101         System.out.println();
 102         System.out.println("Starting test app:");
 103         System.out.print("java ");
 104         for (String arg : args) {
 105             System.out.print(arg + " ");
 106         }
 107         System.out.println();
 108         System.out.println();
 109         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, args.toArray(new String[0]));
 110         OutputAnalyzer out = ProcessTools.executeProcess(pb);
 111         System.out.println(out.getOutput());
 112         verifier.accept(out);
 113     }
 114 }