Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tracing/dtrace/DTraceProvider.java
38918 views
/*1* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tracing.dtrace;2627import java.lang.reflect.Method;28import java.lang.reflect.Modifier;29import java.lang.reflect.Constructor;30import java.lang.reflect.InvocationHandler;31import java.lang.reflect.InvocationTargetException;32import java.lang.annotation.Annotation;3334import sun.tracing.ProviderSkeleton;35import sun.tracing.ProbeSkeleton;36import com.sun.tracing.Provider;37import com.sun.tracing.ProbeName;38import com.sun.tracing.dtrace.Attributes;39import com.sun.tracing.dtrace.ModuleName;40import com.sun.tracing.dtrace.FunctionName;41import com.sun.tracing.dtrace.StabilityLevel;42import com.sun.tracing.dtrace.DependencyClass;4344import sun.misc.ProxyGenerator;4546class DTraceProvider extends ProviderSkeleton {4748private Activation activation;49private Object proxy;5051// For proxy generation52private final static Class[] constructorParams = { InvocationHandler.class };53private final String proxyClassNamePrefix = "$DTraceTracingProxy";5455static final String DEFAULT_MODULE = "java_tracing";56static final String DEFAULT_FUNCTION = "unspecified";5758private static long nextUniqueNumber = 0;59private static synchronized long getUniqueNumber() {60return nextUniqueNumber++;61}6263protected ProbeSkeleton createProbe(Method m) {64return new DTraceProbe(proxy, m);65}6667DTraceProvider(Class<? extends Provider> type) {68super(type);69}7071void setProxy(Object p) {72proxy = p;73}7475void setActivation(Activation a) {76this.activation = a;77}7879public void dispose() {80if (activation != null) {81activation.disposeProvider(this);82activation = null;83}84super.dispose();85}8687/**88* Magic routine which creates an implementation of the user's interface.89*90* This method uses the ProxyGenerator directly to bypass the91* java.lang.reflect.proxy cache so that we get a unique class each92* time it's called and can't accidently reuse a $Proxy class.93*94* @return an implementation of the user's interface95*/96@SuppressWarnings("unchecked")97public <T extends Provider> T newProxyInstance() {98/*99* Choose a name for the proxy class to generate.100*/101long num = getUniqueNumber();102103String proxyPkg = "";104if (!Modifier.isPublic(providerType.getModifiers())) {105String name = providerType.getName();106int n = name.lastIndexOf('.');107proxyPkg = ((n == -1) ? "" : name.substring(0, n + 1));108}109110String proxyName = proxyPkg + proxyClassNamePrefix + num;111112/*113* Generate the specified proxy class.114*/115Class<?> proxyClass = null;116byte[] proxyClassFile = ProxyGenerator.generateProxyClass(117proxyName, new Class<?>[] { providerType });118try {119proxyClass = JVM.defineClass(120providerType.getClassLoader(), proxyName,121proxyClassFile, 0, proxyClassFile.length);122} catch (ClassFormatError e) {123/*124* A ClassFormatError here means that (barring bugs in the125* proxy class generation code) there was some other126* invalid aspect of the arguments supplied to the proxy127* class creation (such as virtual machine limitations128* exceeded).129*/130throw new IllegalArgumentException(e.toString());131}132133/*134* Invoke its constructor with the designated invocation handler.135*/136try {137Constructor cons = proxyClass.getConstructor(constructorParams);138return (T)cons.newInstance(new Object[] { this });139} catch (ReflectiveOperationException e) {140throw new InternalError(e.toString(), e);141}142}143144// In the normal case, the proxy object's method implementations will call145// this method (it usually calls the ProviderSkeleton's version). That146// method uses the passed 'method' object to lookup the associated147// 'ProbeSkeleton' and calls uncheckedTrigger() on that probe to cause the148// probe to fire. DTrace probes are different in that the proxy class's149// methods are immediately overridden with native code to fire the probe150// directly. So this method should never get invoked. We also wire up the151// DTraceProbe.uncheckedTrigger() method to call the proxy method instead152// of doing the work itself.153protected void triggerProbe(Method method, Object[] args) {154assert false : "This method should have been overridden by the JVM";155}156157public String getProviderName() {158return super.getProviderName();159}160161String getModuleName() {162return getAnnotationString(163providerType, ModuleName.class, DEFAULT_MODULE);164}165166static String getProbeName(Method method) {167return getAnnotationString(168method, ProbeName.class, method.getName());169}170171static String getFunctionName(Method method) {172return getAnnotationString(173method, FunctionName.class, DEFAULT_FUNCTION);174}175176DTraceProbe[] getProbes() {177return probes.values().toArray(new DTraceProbe[0]);178}179180StabilityLevel getNameStabilityFor(Class<? extends Annotation> type) {181Attributes attrs = (Attributes)getAnnotationValue(182providerType, type, "value", null);183if (attrs == null) {184return StabilityLevel.PRIVATE;185} else {186return attrs.name();187}188}189190StabilityLevel getDataStabilityFor(Class<? extends Annotation> type) {191Attributes attrs = (Attributes)getAnnotationValue(192providerType, type, "value", null);193if (attrs == null) {194return StabilityLevel.PRIVATE;195} else {196return attrs.data();197}198}199200DependencyClass getDependencyClassFor(Class<? extends Annotation> type) {201Attributes attrs = (Attributes)getAnnotationValue(202providerType, type, "value", null);203if (attrs == null) {204return DependencyClass.UNKNOWN;205} else {206return attrs.dependency();207}208}209}210211212