author | mgronlun |
Mon, 25 Nov 2019 18:38:01 +0100 | |
changeset 59259 | 127ca611f19b |
parent 52899 | 325c95779368 |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
52899
diff
changeset
|
2 |
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. |
50113 | 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.jfr.Event; |
|
32 |
import jdk.jfr.events.ActiveRecordingEvent; |
|
33 |
import jdk.jfr.events.ActiveSettingEvent; |
|
34 |
import jdk.jfr.events.ErrorThrownEvent; |
|
35 |
import jdk.jfr.events.ExceptionStatisticsEvent; |
|
36 |
import jdk.jfr.events.ExceptionThrownEvent; |
|
37 |
import jdk.jfr.events.FileForceEvent; |
|
38 |
import jdk.jfr.events.FileReadEvent; |
|
39 |
import jdk.jfr.events.FileWriteEvent; |
|
52621
f7309a1491d9
8148188: Enhance the security libraries to record events of interest
coffeys
parents:
52334
diff
changeset
|
40 |
import jdk.jfr.events.SecurityPropertyModificationEvent; |
50113 | 41 |
import jdk.jfr.events.SocketReadEvent; |
42 |
import jdk.jfr.events.SocketWriteEvent; |
|
52621
f7309a1491d9
8148188: Enhance the security libraries to record events of interest
coffeys
parents:
52334
diff
changeset
|
43 |
import jdk.jfr.events.TLSHandshakeEvent; |
f7309a1491d9
8148188: Enhance the security libraries to record events of interest
coffeys
parents:
52334
diff
changeset
|
44 |
import jdk.jfr.events.X509CertificateEvent; |
f7309a1491d9
8148188: Enhance the security libraries to record events of interest
coffeys
parents:
52334
diff
changeset
|
45 |
import jdk.jfr.events.X509ValidationEvent; |
50113 | 46 |
import jdk.jfr.internal.JVM; |
47 |
import jdk.jfr.internal.LogLevel; |
|
48 |
import jdk.jfr.internal.LogTag; |
|
49 |
import jdk.jfr.internal.Logger; |
|
50 |
import jdk.jfr.internal.RequestEngine; |
|
51 |
import jdk.jfr.internal.SecuritySupport; |
|
52 |
||
53 |
public final class JDKEvents { |
|
54 |
||
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
55 |
private static final Class<?>[] mirrorEventClasses = { |
52621
f7309a1491d9
8148188: Enhance the security libraries to record events of interest
coffeys
parents:
52334
diff
changeset
|
56 |
SecurityPropertyModificationEvent.class, |
f7309a1491d9
8148188: Enhance the security libraries to record events of interest
coffeys
parents:
52334
diff
changeset
|
57 |
TLSHandshakeEvent.class, |
f7309a1491d9
8148188: Enhance the security libraries to record events of interest
coffeys
parents:
52334
diff
changeset
|
58 |
X509CertificateEvent.class, |
f7309a1491d9
8148188: Enhance the security libraries to record events of interest
coffeys
parents:
52334
diff
changeset
|
59 |
X509ValidationEvent.class |
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
60 |
}; |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
61 |
|
50113 | 62 |
private static final Class<?>[] eventClasses = { |
63 |
FileForceEvent.class, |
|
64 |
FileReadEvent.class, |
|
65 |
FileWriteEvent.class, |
|
66 |
SocketReadEvent.class, |
|
67 |
SocketWriteEvent.class, |
|
68 |
ExceptionThrownEvent.class, |
|
69 |
ExceptionStatisticsEvent.class, |
|
70 |
ErrorThrownEvent.class, |
|
71 |
ActiveSettingEvent.class, |
|
52621
f7309a1491d9
8148188: Enhance the security libraries to record events of interest
coffeys
parents:
52334
diff
changeset
|
72 |
ActiveRecordingEvent.class, |
f7309a1491d9
8148188: Enhance the security libraries to record events of interest
coffeys
parents:
52334
diff
changeset
|
73 |
jdk.internal.event.SecurityPropertyModificationEvent.class, |
f7309a1491d9
8148188: Enhance the security libraries to record events of interest
coffeys
parents:
52334
diff
changeset
|
74 |
jdk.internal.event.TLSHandshakeEvent.class, |
f7309a1491d9
8148188: Enhance the security libraries to record events of interest
coffeys
parents:
52334
diff
changeset
|
75 |
jdk.internal.event.X509CertificateEvent.class, |
f7309a1491d9
8148188: Enhance the security libraries to record events of interest
coffeys
parents:
52334
diff
changeset
|
76 |
jdk.internal.event.X509ValidationEvent.class |
50113 | 77 |
}; |
78 |
||
79 |
// This is a list of the classes with instrumentation code that should be applied. |
|
80 |
private static final Class<?>[] instrumentationClasses = new Class<?>[] { |
|
81 |
FileInputStreamInstrumentor.class, |
|
82 |
FileOutputStreamInstrumentor.class, |
|
83 |
RandomAccessFileInstrumentor.class, |
|
84 |
FileChannelImplInstrumentor.class, |
|
85 |
SocketInputStreamInstrumentor.class, |
|
86 |
SocketOutputStreamInstrumentor.class, |
|
87 |
SocketChannelImplInstrumentor.class |
|
88 |
}; |
|
89 |
||
90 |
private static final Class<?>[] targetClasses = new Class<?>[instrumentationClasses.length]; |
|
91 |
private static final JVM jvm = JVM.getJVM(); |
|
92 |
private static final Runnable emitExceptionStatistics = JDKEvents::emitExceptionStatistics; |
|
93 |
private static boolean initializationTriggered; |
|
94 |
||
95 |
@SuppressWarnings("unchecked") |
|
96 |
public synchronized static void initialize() { |
|
97 |
try { |
|
98 |
if (initializationTriggered == false) { |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
99 |
for (Class<?> mirrorEventClass : mirrorEventClasses) { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
100 |
SecuritySupport.registerMirror(((Class<? extends Event>)mirrorEventClass)); |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
101 |
} |
50113 | 102 |
for (Class<?> eventClass : eventClasses) { |
103 |
SecuritySupport.registerEvent((Class<? extends Event>) eventClass); |
|
104 |
} |
|
105 |
initializationTriggered = true; |
|
52899
325c95779368
8207829: FlightRecorderMXBeanImpl is leaking the first classloader which calls it
egahlin
parents:
52698
diff
changeset
|
106 |
RequestEngine.addTrustedJDKHook(ExceptionStatisticsEvent.class, emitExceptionStatistics); |
50113 | 107 |
} |
108 |
} catch (Exception e) { |
|
109 |
Logger.log(LogTag.JFR_SYSTEM, LogLevel.WARN, "Could not initialize JDK events. " + e.getMessage()); |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
public static void addInstrumentation() { |
|
114 |
try { |
|
115 |
List<Class<?>> list = new ArrayList<>(); |
|
116 |
for (int i = 0; i < instrumentationClasses.length; i++) { |
|
117 |
JIInstrumentationTarget tgt = instrumentationClasses[i].getAnnotation(JIInstrumentationTarget.class); |
|
118 |
Class<?> clazz = Class.forName(tgt.value()); |
|
119 |
targetClasses[i] = clazz; |
|
120 |
list.add(clazz); |
|
121 |
} |
|
122 |
list.add(java.lang.Throwable.class); |
|
123 |
list.add(java.lang.Error.class); |
|
124 |
Logger.log(LogTag.JFR_SYSTEM, LogLevel.INFO, "Retransformed JDK classes"); |
|
125 |
jvm.retransformClasses(list.toArray(new Class<?>[list.size()])); |
|
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
52899
diff
changeset
|
126 |
} catch (IllegalStateException ise) { |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
52899
diff
changeset
|
127 |
throw ise; |
50113 | 128 |
} catch (Exception e) { |
129 |
Logger.log(LogTag.JFR_SYSTEM, LogLevel.WARN, "Could not add instrumentation for JDK events. " + e.getMessage()); |
|
130 |
} |
|
131 |
} |
|
132 |
||
133 |
private static void emitExceptionStatistics() { |
|
134 |
ExceptionStatisticsEvent t = new ExceptionStatisticsEvent(); |
|
135 |
t.throwables = ThrowableTracer.numThrowables(); |
|
136 |
t.commit(); |
|
137 |
} |
|
138 |
||
139 |
@SuppressWarnings("deprecation") |
|
140 |
public static byte[] retransformCallback(Class<?> klass, byte[] oldBytes) throws Throwable { |
|
141 |
if (java.lang.Throwable.class == klass) { |
|
142 |
Logger.log(LogTag.JFR_SYSTEM, LogLevel.TRACE, "Instrumenting java.lang.Throwable"); |
|
143 |
return ConstructorTracerWriter.generateBytes(java.lang.Throwable.class, oldBytes); |
|
144 |
} |
|
145 |
||
146 |
if (java.lang.Error.class == klass) { |
|
147 |
Logger.log(LogTag.JFR_SYSTEM, LogLevel.TRACE, "Instrumenting java.lang.Error"); |
|
148 |
return ConstructorTracerWriter.generateBytes(java.lang.Error.class, oldBytes); |
|
149 |
} |
|
150 |
||
151 |
for (int i = 0; i < targetClasses.length; i++) { |
|
152 |
if (targetClasses[i].equals(klass)) { |
|
153 |
Class<?> c = instrumentationClasses[i]; |
|
154 |
Logger.log(LogTag.JFR_SYSTEM, LogLevel.TRACE, () -> "Processing instrumentation class: " + c); |
|
155 |
return new JIClassInstrumentation(instrumentationClasses[i], klass, oldBytes).getNewBytes(); |
|
156 |
} |
|
157 |
} |
|
158 |
return oldBytes; |
|
159 |
} |
|
160 |
||
161 |
public static void remove() { |
|
162 |
RequestEngine.removeHook(JDKEvents::emitExceptionStatistics); |
|
163 |
} |
|
164 |
} |