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  * @library /test/lib
  41  * @run main/othervm jdk.jfr.startupargs.TestRetransformUsingLog
  42  */
  43 public class TestRetransformUsingLog {
  44 
  45     private static final String FILE_READ_FORCED_CLASS_LOAD = "Adding forced instrumentation for event type " + EventNames.FileRead + " during initial class load";
  46     private static final String SIMPLE_EVENT_FORCED_CLASS_LOAD = "Adding forced instrumentation for event type jdk.test.lib.jfr.SimpleEvent during initial class load";
  47     private static final String SIMPLE_EVENT_UNFORCED_CLASS_LOAD = "Adding instrumentation for event type jdk.test.lib.jfr.SimpleEvent during initial class load";
  48 
  49     public static class TestApp {
  50         public static void main(String[] args) throws Exception {
  51             SimpleEvent event = new SimpleEvent();
  52             event.commit();
  53         }
  54     }
  55 
  56     public static void main(String[] args) throws Exception {
  57         testRecordingRetransFormFalse();
  58         testRecordingRetransFormTrue();
  59         testNoRecordingRetransFormFalse();
  60         testNoRecordingRetransFormTrue();
  61     }
  62 
  63     private static void testRecordingRetransFormFalse() throws Exception {
  64         startApp(true, false, out -> {
  65             out.shouldContain(FILE_READ_FORCED_CLASS_LOAD);
  66             out.shouldContain(SIMPLE_EVENT_FORCED_CLASS_LOAD);
  67         });
  68     }
  69 
  70     private static void testRecordingRetransFormTrue() throws Exception {
  71         startApp(true, true, out -> {
  72             out.shouldContain(FILE_READ_FORCED_CLASS_LOAD);
  73             out.shouldContain(SIMPLE_EVENT_UNFORCED_CLASS_LOAD);
  74         });
  75     }
  76 
  77     private static void testNoRecordingRetransFormFalse() throws Exception {
  78         startApp(false, false, out -> {
  79             out.shouldNotContain(FILE_READ_FORCED_CLASS_LOAD);
  80             out.shouldContain(SIMPLE_EVENT_FORCED_CLASS_LOAD);
  81         });
  82     }
  83 
  84     private static void testNoRecordingRetransFormTrue() throws Exception {
  85         startApp(false, true, out -> {
  86             out.shouldNotContain(FILE_READ_FORCED_CLASS_LOAD);
  87             out.shouldNotContain(SIMPLE_EVENT_FORCED_CLASS_LOAD);
  88             out.shouldNotContain(SIMPLE_EVENT_UNFORCED_CLASS_LOAD);
  89         });
  90     }
  91 
  92     private static void startApp(boolean recording, boolean retransform, Consumer<OutputAnalyzer> verifier) throws Exception {
  93         List<String> args = new ArrayList<>();
  94         args.add("-Xlog:jfr+system");
  95         args.add("-XX:FlightRecorderOptions=retransform=" + retransform);
  96         if (recording) {
  97             args.add("-XX:StartFlightRecording");
  98         }
  99         args.add(TestApp.class.getName());
 100         System.out.println();
 101         System.out.println("Starting test app:");
 102         System.out.print("java ");
 103         for (String arg : args) {
 104             System.out.print(arg + " ");
 105         }
 106         System.out.println();
 107         System.out.println();
 108         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, args.toArray(new String[0]));
 109         OutputAnalyzer out = ProcessTools.executeProcess(pb);
 110         System.out.println(out.getOutput());
 111         verifier.accept(out);
 112     }
 113 }