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.internal.instrument; 27 28 import java.util.ArrayList; 29 import java.util.List; 30 31 import jdk.internal.module.Modules; 32 import jdk.jfr.Event; 33 import jdk.jfr.FlightRecorder; 34 import jdk.jfr.events.*; 35 import jdk.jfr.internal.JVM; 36 import jdk.jfr.internal.LogLevel; 37 import jdk.jfr.internal.LogTag; 38 import jdk.jfr.internal.Logger; 39 import jdk.jfr.internal.RequestEngine; 40 import jdk.jfr.internal.SecuritySupport; 41 import jdk.jfr.internal.Utils; 42 43 public final class JDKEvents { 44 45 private static final Class<?>[] mirrorEventClasses = { 46 CertificateChainEvent.class, 47 TLSHandshakeEvent.class, 48 SecurityPropertyEvent.class, 49 X509CertEvent.class 50 }; 51 52 private static final Class<?>[] eventClasses = { 53 FileForceEvent.class, 54 FileReadEvent.class, 55 FileWriteEvent.class, 56 SocketReadEvent.class, 57 SocketWriteEvent.class, 58 ExceptionThrownEvent.class, 59 ExceptionStatisticsEvent.class, 60 ErrorThrownEvent.class, 61 ActiveSettingEvent.class, 62 ActiveRecordingEvent.class, 63 jdk.internal.event.CertificateChainEvent.class, 64 jdk.internal.event.SecurityPropertyEvent.class, 65 jdk.internal.event.TLSHandshakeEvent.class, 66 jdk.internal.event.X509CertEvent.class 67 }; 68 69 // This is a list of the classes with instrumentation code that should be applied. 70 private static final Class<?>[] instrumentationClasses = new Class<?>[] { 71 FileInputStreamInstrumentor.class, 72 FileOutputStreamInstrumentor.class, 73 RandomAccessFileInstrumentor.class, 74 FileChannelImplInstrumentor.class, 75 SocketInputStreamInstrumentor.class, 76 SocketOutputStreamInstrumentor.class, 77 SocketChannelImplInstrumentor.class 78 }; 79 80 private static final Class<?>[] targetClasses = new Class<?>[instrumentationClasses.length]; 81 private static final JVM jvm = JVM.getJVM(); 82 private static final Runnable emitExceptionStatistics = JDKEvents::emitExceptionStatistics; 83 private static boolean initializationTriggered; 84 85 @SuppressWarnings("unchecked") 86 public synchronized static void initialize() { 87 try { 88 if (initializationTriggered == false) { 89 Module jdkJfrModule = Event.class.getModule(); 90 Module javaBaseModule = Object.class.getModule(); 91 Modules.addReads(javaBaseModule, jdkJfrModule); 92 Modules.addExports(jdkJfrModule, Utils.EVENTS_PACKAGE_NAME, javaBaseModule); 93 Modules.addExports(jdkJfrModule, Utils.INSTRUMENT_PACKAGE_NAME, javaBaseModule); 94 Modules.addExports(jdkJfrModule, Utils.HANDLERS_PACKAGE_NAME, javaBaseModule); 95 for (Class<?> mirrorEventClass : mirrorEventClasses) { 96 SecuritySupport.registerMirror(((Class<? extends Event>)mirrorEventClass)); 97 } 98 for (Class<?> eventClass : eventClasses) { 99 SecuritySupport.registerEvent((Class<? extends Event>) eventClass); 100 } 101 initializationTriggered = true; 102 FlightRecorder.addPeriodicEvent(ExceptionStatisticsEvent.class, emitExceptionStatistics); 103 } 104 } catch (Exception e) { 105 Logger.log(LogTag.JFR_SYSTEM, LogLevel.WARN, "Could not initialize JDK events. " + e.getMessage()); 106 } 107 } 108 109 public static void addInstrumentation() { 110 try { 111 List<Class<?>> list = new ArrayList<>(); 112 for (int i = 0; i < instrumentationClasses.length; i++) { 113 JIInstrumentationTarget tgt = instrumentationClasses[i].getAnnotation(JIInstrumentationTarget.class); 114 Class<?> clazz = Class.forName(tgt.value()); 115 targetClasses[i] = clazz; 116 list.add(clazz); 117 } 118 list.add(java.lang.Throwable.class); 119 list.add(java.lang.Error.class); 120 Logger.log(LogTag.JFR_SYSTEM, LogLevel.INFO, "Retransformed JDK classes"); 121 jvm.retransformClasses(list.toArray(new Class<?>[list.size()])); 122 } catch (Exception e) { 123 Logger.log(LogTag.JFR_SYSTEM, LogLevel.WARN, "Could not add instrumentation for JDK events. " + e.getMessage()); 124 } 125 } 126 127 private static void emitExceptionStatistics() { 128 ExceptionStatisticsEvent t = new ExceptionStatisticsEvent(); 129 t.throwables = ThrowableTracer.numThrowables(); 130 t.commit(); 131 } 132 133 @SuppressWarnings("deprecation") 134 public static byte[] retransformCallback(Class<?> klass, byte[] oldBytes) throws Throwable { 135 if (java.lang.Throwable.class == klass) { 136 Logger.log(LogTag.JFR_SYSTEM, LogLevel.TRACE, "Instrumenting java.lang.Throwable"); 137 return ConstructorTracerWriter.generateBytes(java.lang.Throwable.class, oldBytes); 138 } 139 140 if (java.lang.Error.class == klass) { 141 Logger.log(LogTag.JFR_SYSTEM, LogLevel.TRACE, "Instrumenting java.lang.Error"); 142 return ConstructorTracerWriter.generateBytes(java.lang.Error.class, oldBytes); 143 } 144 145 for (int i = 0; i < targetClasses.length; i++) { 146 if (targetClasses[i].equals(klass)) { 147 Class<?> c = instrumentationClasses[i]; 148 Logger.log(LogTag.JFR_SYSTEM, LogLevel.TRACE, () -> "Processing instrumentation class: " + c); 149 return new JIClassInstrumentation(instrumentationClasses[i], klass, oldBytes).getNewBytes(); 150 } 151 } 152 return oldBytes; 153 } 154 155 public static void remove() { 156 RequestEngine.removeHook(JDKEvents::emitExceptionStatistics); 157 } 158 }