author | egahlin |
Wed, 31 Oct 2018 02:10:21 +0100 | |
changeset 52334 | a181612f0715 |
parent 50113 | caf115bb98ad |
permissions | -rw-r--r-- |
50113 | 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 |
package jdk.jfr.internal; |
|
26 |
||
27 |
import java.lang.reflect.Modifier; |
|
28 |
||
29 |
import jdk.jfr.internal.handlers.EventHandler; |
|
30 |
import jdk.jfr.internal.instrument.JDKEvents; |
|
31 |
||
32 |
/** |
|
33 |
* All upcalls from the JVM should go through this class. |
|
34 |
* |
|
35 |
*/ |
|
36 |
// Called by native |
|
37 |
final class JVMUpcalls { |
|
38 |
/** |
|
39 |
* Called by the JVM when a retransform happens on a tagged class |
|
40 |
* |
|
41 |
* @param traceId |
|
42 |
* Id of the class |
|
43 |
* @param dummy |
|
44 |
* (not used but needed since invoke infrastructure in native |
|
45 |
* uses same signature bytesForEagerInstrumentation) |
|
46 |
* @param clazz |
|
47 |
* class being retransformed |
|
48 |
* @param oldBytes |
|
49 |
* byte code |
|
50 |
* @return byte code to use |
|
51 |
* @throws Throwable |
|
52 |
*/ |
|
53 |
static byte[] onRetransform(long traceId, boolean dummy, Class<?> clazz, byte[] oldBytes) throws Throwable { |
|
54 |
try { |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
55 |
if (jdk.internal.event.Event.class.isAssignableFrom(clazz) && !Modifier.isAbstract(clazz.getModifiers())) { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
56 |
EventHandler handler = Utils.getHandler(clazz.asSubclass(jdk.internal.event.Event.class)); |
50113 | 57 |
if (handler == null) { |
58 |
Logger.log(LogTag.JFR_SYSTEM, LogLevel.INFO, "No event handler found for " + clazz.getName() + ". Ignoring instrumentation request."); |
|
59 |
// Probably triggered by some other agent |
|
60 |
return oldBytes; |
|
61 |
} |
|
62 |
Logger.log(LogTag.JFR_SYSTEM, LogLevel.INFO, "Adding instrumentation to event class " + clazz.getName() + " using retransform"); |
|
63 |
EventInstrumentation ei = new EventInstrumentation(clazz.getSuperclass(), oldBytes, traceId); |
|
64 |
byte[] bytes = ei.buildInstrumented(); |
|
65 |
ASMToolkit.logASM(clazz.getName(), bytes); |
|
66 |
return bytes; |
|
67 |
} |
|
68 |
return JDKEvents.retransformCallback(clazz, oldBytes); |
|
69 |
} catch (Throwable t) { |
|
70 |
Logger.log(LogTag.JFR_SYSTEM, LogLevel.WARN, "Unexpected error when adding instrumentation to event class " + clazz.getName()); |
|
71 |
} |
|
72 |
return oldBytes; |
|
73 |
||
74 |
} |
|
75 |
||
76 |
/** |
|
77 |
* Called by the JVM when requested to do an "eager" instrumentation. Would |
|
78 |
* normally happen when JVMTI retransform capabilities are not available. |
|
79 |
* |
|
80 |
* @param traceId |
|
81 |
* Id of the class |
|
82 |
* @param forceInstrumentation |
|
83 |
* add instrumentation regardless if event is enabled or not. |
|
84 |
* @param superClazz |
|
85 |
* the super class of the class being processed |
|
86 |
* @param oldBytes |
|
87 |
* byte code |
|
88 |
* @return byte code to use |
|
89 |
* @throws Throwable |
|
90 |
*/ |
|
91 |
static byte[] bytesForEagerInstrumentation(long traceId, boolean forceInstrumentation, Class<?> superClass, byte[] oldBytes) throws Throwable { |
|
92 |
if (JVMSupport.isNotAvailable()) { |
|
93 |
return oldBytes; |
|
94 |
} |
|
95 |
String eventName = "<Unknown>"; |
|
96 |
try { |
|
97 |
EventInstrumentation ei = new EventInstrumentation(superClass, oldBytes, traceId); |
|
98 |
eventName = ei.getEventName(); |
|
99 |
if (!forceInstrumentation) { |
|
100 |
// Assume we are recording |
|
101 |
MetadataRepository mr = MetadataRepository.getInstance(); |
|
102 |
// No need to generate bytecode if: |
|
103 |
// 1) Event class is disabled, and there is not an external configuration that overrides. |
|
104 |
// 2) Event class has @Registered(false) |
|
105 |
if (!mr.isEnabled(ei.getEventName()) && !ei.isEnabled() || !ei.isRegistered()) { |
|
106 |
Logger.log(LogTag.JFR_SYSTEM, LogLevel.INFO, "Skipping instrumentation for event type " + eventName + " since event was disabled on class load"); |
|
107 |
return oldBytes; |
|
108 |
} |
|
109 |
} |
|
110 |
// Corner case when we are forced to generate bytecode. We can't reference the event |
|
111 |
// handler in #isEnabled() before event class has been registered, so we add a |
|
112 |
// guard against a null reference. |
|
113 |
ei.setGuardHandler(true); |
|
114 |
Logger.log(LogTag.JFR_SYSTEM, LogLevel.INFO, "Adding " + (forceInstrumentation ? "forced " : "") + "instrumentation for event type " + eventName + " during initial class load"); |
|
115 |
EventHandlerCreator eh = new EventHandlerCreator(traceId, ei.getSettingInfos(), ei.getFieldInfos()); |
|
116 |
// Handler class must be loaded before instrumented event class can |
|
117 |
// be used |
|
118 |
eh.makeEventHandlerClass(); |
|
119 |
byte[] bytes = ei.buildInstrumented(); |
|
120 |
ASMToolkit.logASM(ei.getClassName() + "(" + traceId + ")", bytes); |
|
121 |
return bytes; |
|
122 |
} catch (Throwable t) { |
|
123 |
Logger.log(LogTag.JFR_SYSTEM, LogLevel.WARN, "Unexpected error when adding instrumentation for event type " + eventName); |
|
124 |
return oldBytes; |
|
125 |
} |
|
126 |
} |
|
127 |
||
128 |
/** |
|
129 |
* Called by the JVM to create the recorder thread. |
|
130 |
* |
|
131 |
* @param systemThreadGroup |
|
132 |
* the system thread group |
|
133 |
* |
|
134 |
* @param contextClassLoader |
|
135 |
* the context class loader. |
|
136 |
* |
|
137 |
* @return a new thread |
|
138 |
*/ |
|
139 |
static Thread createRecorderThread(ThreadGroup systemThreadGroup, ClassLoader contextClassLoader) { |
|
140 |
return SecuritySupport.createRecorderThread(systemThreadGroup, contextClassLoader); |
|
141 |
} |
|
142 |
} |