Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tracing/dtrace/DTraceProviderFactory.java
38918 views
/*1* Copyright (c) 2008, 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.util.Map;28import java.util.Set;29import java.util.HashMap;30import java.util.HashSet;31import java.security.Permission;3233import com.sun.tracing.ProviderFactory;34import com.sun.tracing.Provider;3536/**37* Factory class to create JSDT Providers.38*39* This class contains methods to create an instance of a Provider40* interface which can be used to place tracepoints in an application.41* Method calls upon that instance trigger DTrace probes that42* are visible from DTrace scripts. Such calls have no other43* side effects in the application.44* <p>45* The DTrace script mechanisms for listing and matching probes will not see46* nor match any probes until the provider they reside in is created by a47* call to {@code createProvider()} (or {@code createProviders()}).48* <p>49* Providers that are created should be disposed of when they are no longer50* needed to free up system resources, at which point the associated51* DTrace probes will no longer be available to DTrace. One disposes a52* provider by calling53* {@link com.sun.tracing.Provider#dispose Provider.dispose()} on a54* created provider instance.55*56* @since 1.757*/58public final class DTraceProviderFactory extends ProviderFactory {59/**60* Creates an instance of a provider which can then be used to trigger61* DTrace probes.62*63* The provider specification, provided as an argument, should only64* contain methods which have a 'void' return type and String or65* integer-based typed arguments (long, int, short, char, byte, or boolean).66*67* @param cls A user-defined interface which extends {@code Provider}.68* @return An instance of the interface which is used to trigger69* the DTrace probes.70* @throws java.lang.SecurityException if a security manager has been71* installed and it denies72* RuntimePermission("com.sun.dtrace.jsdt.createProvider")73* @throws java.lang.IllegalArgumentException if the interface contains74* methods that do not return null, or that contain arguments that are75* not String or integer types.76*/77public <T extends Provider> T createProvider(Class<T> cls) {78DTraceProvider jsdt = new DTraceProvider(cls);79T proxy = jsdt.newProxyInstance();80jsdt.setProxy(proxy);81jsdt.init();82new Activation(jsdt.getModuleName(), new DTraceProvider[] { jsdt });83return proxy;84}8586/**87* Creates multiple providers at once.88*89* This method batches together a number of provider instantiations.90* It works similarly91* to {@code createProvider}, but operates on a set of providers instead92* of one at a time. This method is in place since some DTrace93* implementations limit the number of times that providers can be94* created. When numerous providers can be created at once with this95* method, it will count only as a single creation point to DTrace, thus96* it uses less system resources.97* <p>98* All of the probes in the providers will be visible to DTrace after99* this call and all will remain visible until all of the providers100* are disposed.101* <p>102* The {@code moduleName} parameter will override any {@code ModuleName}103* annotation associated with any of the providers in the set.104* All of the probes created by this call will share the same105* module name.106* <p>107* @param providers a set of provider specification interfaces108* @param moduleName the module name to associate with all probes109* @return A map which maps the provider interface specification to an110* implementing instance.111* @throws java.lang.SecurityException if a security manager has been112* installed and it denies113* RuntimePermission("com.sun.dtrace.jsdt.createProvider")114* @throws java.lang.IllegalArgumentException if any of the interface115* contains methods that do not return null, or that contain arguments116* that are not String or integer types.117*/118public Map<Class<? extends Provider>,Provider> createProviders(119Set<Class<? extends Provider>> providers, String moduleName) {120HashMap<Class<? extends Provider>,Provider> map =121new HashMap<Class<? extends Provider>,Provider>();122HashSet<DTraceProvider> jsdts = new HashSet<DTraceProvider>();123for (Class<? extends Provider> cls : providers) {124DTraceProvider jsdt = new DTraceProvider(cls);125jsdts.add(jsdt);126map.put(cls, jsdt.newProxyInstance());127}128new Activation(moduleName, jsdts.toArray(new DTraceProvider[0]));129return map;130}131132/**133* Used to check the status of DTrace support in the underlying JVM and134* operating system.135*136* This is an informative method only - the Java-level effects of137* creating providers and triggering probes will not change whether or138* not DTrace is supported by the underlying systems.139*140* @return true if DTrace is supported141*/142public static boolean isSupported() {143try {144SecurityManager security = System.getSecurityManager();145if (security != null) {146Permission perm = new RuntimePermission(147"com.sun.tracing.dtrace.createProvider");148security.checkPermission(perm);149}150return JVM.isSupported();151} catch (SecurityException e) {152return false;153}154}155}156157158