1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * 
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * The contents of this file are subject to the terms of either the Universal Permissive License
   7  * v 1.0 as shown at http://oss.oracle.com/licenses/upl
   8  *
   9  * or the following license:
  10  *
  11  * Redistribution and use in source and binary forms, with or without modification, are permitted
  12  * provided that the following conditions are met:
  13  * 
  14  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions
  15  * and the following disclaimer.
  16  * 
  17  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
  18  * conditions and the following disclaimer in the documentation and/or other materials provided with
  19  * the distribution.
  20  * 
  21  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to
  22  * endorse or promote products derived from this software without specific prior written permission.
  23  * 
  24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  26  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  31  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32  */
  33 package org.openjdk.jmc.agent.jfr.impl;
  34 
  35 import java.lang.reflect.Constructor;
  36 import java.lang.reflect.Method;
  37 import java.net.URI;
  38 import java.util.logging.Level;
  39 import java.util.logging.Logger;
  40 
  41 import org.objectweb.asm.Type;
  42 
  43 /**
  44  * Utility class to support JDK 7 and JDK 8 style JFR.
  45  */
  46 @SuppressWarnings("deprecation")
  47 public class JFRUtils {
  48         public final static String INAME = Type.getInternalName(JFRUtils.class);
  49         public final static Object PRODUCER;
  50         public final static Method REGISTER_METHOD;
  51 
  52         static {
  53                 URI producerURI = URI.create("http://jmc.openjdk.org/jfragent/"); //$NON-NLS-1$
  54                 PRODUCER = createProducerReflectively("JMC Dynamic JFR Producer",
  55                                 "A byte code instrumentation based JFR event producer.", producerURI);
  56                 REGISTER_METHOD = getRegisterMethod(PRODUCER.getClass());
  57         }
  58 
  59         public static Object register(Class<?> clazz) {
  60                 try {
  61                         if (REGISTER_METHOD != null) {
  62                                 return REGISTER_METHOD.invoke(PRODUCER, clazz);
  63                         }
  64                 } catch (Exception e) {
  65                         Logger.getLogger(JFRUtils.class.getName()).log(Level.SEVERE,
  66                                         "Failed to register the event class " + clazz.getName() //$NON-NLS-1$
  67                                                         + ". Event will not be available. Please check your configuration.", //$NON-NLS-1$
  68                                         e);
  69                 }
  70                 return null;
  71         }
  72 
  73         private static Method getRegisterMethod(Class<?> producerClass) {
  74                 try {
  75                         return producerClass.getDeclaredMethod("addEvent", Class.class);
  76                 } catch (NoSuchMethodException | SecurityException e) {
  77                         // This should never happen
  78                         System.err.println("Failed to find the addEvent method of the producer.");
  79                         System.err.println("No BCI generated JFR events will be available.");
  80                         e.printStackTrace();
  81                 }
  82                 return null;
  83         }
  84 
  85         private static Object createProducerReflectively(String name, String description, URI producerURI) {
  86                 try {
  87                         Class<?> producerClass = Class.forName("com.oracle.jrockit.jfr.Producer");
  88                         Constructor<?> constructor = producerClass.getConstructor(String.class, String.class, String.class);
  89                         Object producer = constructor.newInstance(name, description, producerURI.toString());
  90                         Method registerMethod = producerClass.getDeclaredMethod("register");
  91                         registerMethod.invoke(producer);
  92                         return producer;
  93                 } catch (Exception e) {
  94                         System.err.println(
  95                                         "Failed to create producer for Oracle JDK7/8 JVM. Ensure that the JVM was started with -XX:+UnlockCommercialFeatures and -XX:+FlightRecorder.");
  96                         System.err.println("No BCI generated JFR events will be available.");
  97                         e.printStackTrace();
  98                 }
  99                 return null;
 100         }
 101 }