Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/instrument/JPLISAgent.h
38767 views
/*1* Copyright (c) 2003, 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*/2425/*26* Copyright 2003 Wily Technology, Inc.27*/2829#ifndef _JPLISAGENT_H_30#define _JPLISAGENT_H_3132#include <jni.h>33#include <jvmti.h>3435#ifdef __cplusplus36extern "C" {37#endif3839/*40* The JPLISAgent manages the initialization all of the Java programming language Agents.41* It also supports the native method bridge between the JPLIS and the JVMTI.42* It maintains a single JVMTI Env that all JPL agents share.43* It parses command line requests and creates individual Java agents.44*/454647/*48* Forward definitions49*/50struct _JPLISAgent;5152typedef struct _JPLISAgent JPLISAgent;53typedef struct _JPLISEnvironment JPLISEnvironment;545556/* constants for class names and methods names and such57these all must stay in sync with Java code & interfaces58*/59#define JPLIS_INSTRUMENTIMPL_CLASSNAME "sun/instrument/InstrumentationImpl"60#define JPLIS_INSTRUMENTIMPL_CONSTRUCTOR_METHODNAME "<init>"61#define JPLIS_INSTRUMENTIMPL_CONSTRUCTOR_METHODSIGNATURE "(JZZ)V"62#define JPLIS_INSTRUMENTIMPL_PREMAININVOKER_METHODNAME "loadClassAndCallPremain"63#define JPLIS_INSTRUMENTIMPL_PREMAININVOKER_METHODSIGNATURE "(Ljava/lang/String;Ljava/lang/String;)V"64#define JPLIS_INSTRUMENTIMPL_AGENTMAININVOKER_METHODNAME "loadClassAndCallAgentmain"65#define JPLIS_INSTRUMENTIMPL_AGENTMAININVOKER_METHODSIGNATURE "(Ljava/lang/String;Ljava/lang/String;)V"66#define JPLIS_INSTRUMENTIMPL_TRANSFORM_METHODNAME "transform"67#define JPLIS_INSTRUMENTIMPL_TRANSFORM_METHODSIGNATURE \68"(Ljava/lang/ClassLoader;Ljava/lang/String;Ljava/lang/Class;Ljava/security/ProtectionDomain;[BZ)[B"697071/*72* Error messages73*/74#define JPLIS_ERRORMESSAGE_CANNOTSTART "processing of -javaagent failed"757677/*78* Our initialization errors79*/80typedef enum {81JPLIS_INIT_ERROR_NONE,82JPLIS_INIT_ERROR_CANNOT_CREATE_NATIVE_AGENT,83JPLIS_INIT_ERROR_FAILURE,84JPLIS_INIT_ERROR_ALLOCATION_FAILURE,85JPLIS_INIT_ERROR_AGENT_CLASS_NOT_SPECIFIED86} JPLISInitializationError;878889struct _JPLISEnvironment {90jvmtiEnv * mJVMTIEnv; /* the JVM TI environment */91JPLISAgent * mAgent; /* corresponding agent */92jboolean mIsRetransformer; /* indicates if special environment */93};9495struct _JPLISAgent {96JavaVM * mJVM; /* handle to the JVM */97JPLISEnvironment mNormalEnvironment; /* for every thing but retransform stuff */98JPLISEnvironment mRetransformEnvironment;/* for retransform stuff only */99jobject mInstrumentationImpl; /* handle to the Instrumentation instance */100jmethodID mPremainCaller; /* method on the InstrumentationImpl that does the premain stuff (cached to save lots of lookups) */101jmethodID mAgentmainCaller; /* method on the InstrumentationImpl for agents loaded via attach mechanism */102jmethodID mTransform; /* method on the InstrumentationImpl that does the class file transform */103jboolean mRedefineAvailable; /* cached answer to "does this agent support redefine" */104jboolean mRedefineAdded; /* indicates if can_redefine_classes capability has been added */105jboolean mNativeMethodPrefixAvailable; /* cached answer to "does this agent support prefixing" */106jboolean mNativeMethodPrefixAdded; /* indicates if can_set_native_method_prefix capability has been added */107char const * mAgentClassName; /* agent class name */108char const * mOptionsString; /* -javaagent options string */109};110111/*112* JVMTI event handlers113*/114115/* VMInit event handler. Installed during OnLoad, then removed during VMInit. */116extern void JNICALL117eventHandlerVMInit( jvmtiEnv * jvmtienv,118JNIEnv * jnienv,119jthread thread);120121/* ClassFileLoadHook event handler. Installed during VMInit, then left in place forever. */122extern void JNICALL123eventHandlerClassFileLoadHook( jvmtiEnv * jvmtienv,124JNIEnv * jnienv,125jclass class_being_redefined,126jobject loader,127const char* name,128jobject protectionDomain,129jint class_data_len,130const unsigned char* class_data,131jint* new_class_data_len,132unsigned char** new_class_data);133134/*135* Main entry points for the JPLIS JVMTI agent code136*/137138/* looks up the environment instance. returns null if there isn't one */139extern JPLISEnvironment *140getJPLISEnvironment(jvmtiEnv * jvmtienv);141142/* Creates a new JPLIS agent.143* Returns error if the agent cannot be created and initialized.144* The JPLISAgent* pointed to by agent_ptr is set to the new broker,145* or NULL if an error has occurred.146*/147extern JPLISInitializationError148createNewJPLISAgent(JavaVM * vm, JPLISAgent **agent_ptr);149150/* Adds can_redefine_classes capability */151extern void152addRedefineClassesCapability(JPLISAgent * agent);153154/* Add the can_set_native_method_prefix capability */155extern void156addNativeMethodPrefixCapability(JPLISAgent * agent);157158/* Add the can_maintain_original_method_order capability (for testing) */159extern void160addOriginalMethodOrderCapability(JPLISAgent * agent);161162163/* Our JPLIS agent is paralleled by a Java InstrumentationImpl instance.164* This routine uses JNI to create and initialized the Java instance.165* Returns true if it succeeds, false otherwise.166*/167extern jboolean168createInstrumentationImpl( JNIEnv * jnienv,169JPLISAgent * agent);170171172/* during OnLoad phase (command line parsing)173* record the parameters of -javaagent174*/175extern JPLISInitializationError176recordCommandLineData( JPLISAgent * agent,177const char * agentClass,178const char * optionsString );179180/* Swaps the start phase event handlers out and the live phase event handlers in.181* Also used in attach to enabled live phase event handlers.182* Returns true if it succeeds, false otherwise.183*/184extern jboolean185setLivePhaseEventHandlers( JPLISAgent * agent);186187/* Loads the Java agent according to the already processed command line. For each,188* loads the Java agent class, then calls the premain method.189* Returns true if all Java agent classes are loaded and all premain methods complete with no exceptions,190* false otherwise.191*/192extern jboolean193startJavaAgent( JPLISAgent * agent,194JNIEnv * jnienv,195const char * classname,196const char * optionsString,197jmethodID agentMainMethod);198199200/* during VMInit processing201* this is how the invocation engine (callback wrapper) tells us to start up all the javaagents202*/203extern jboolean204processJavaStart( JPLISAgent * agent,205JNIEnv * jnienv);206207/* on an ongoing basis,208* this is how the invocation engine (callback wrapper) tells us to process a class file209*/210extern void211transformClassFile( JPLISAgent * agent,212JNIEnv * jnienv,213jobject loader,214const char* name,215jclass classBeingRedefined,216jobject protectionDomain,217jint class_data_len,218const unsigned char* class_data,219jint* new_class_data_len,220unsigned char** new_class_data,221jboolean is_retransformer);222223/* on an ongoing basis,224* Return the environment with the retransformation capability.225* Create it if it doesn't exist.226*/227extern jvmtiEnv *228retransformableEnvironment(JPLISAgent * agent);229230/* on an ongoing basis,231* these are implementations of the Instrumentation services.232* Most are simple covers for JVMTI access services. These are the guts of the InstrumentationImpl233* native methods.234*/235extern jboolean236isModifiableClass(JNIEnv * jnienv, JPLISAgent * agent, jclass clazz);237238extern jboolean239isRetransformClassesSupported(JNIEnv * jnienv, JPLISAgent * agent);240241extern void242setHasRetransformableTransformers(JNIEnv * jnienv, JPLISAgent * agent, jboolean has);243244extern void245retransformClasses(JNIEnv * jnienv, JPLISAgent * agent, jobjectArray classes);246247extern void248redefineClasses(JNIEnv * jnienv, JPLISAgent * agent, jobjectArray classDefinitions);249250extern jobjectArray251getAllLoadedClasses(JNIEnv * jnienv, JPLISAgent * agent);252253extern jobjectArray254getInitiatedClasses(JNIEnv * jnienv, JPLISAgent * agent, jobject classLoader);255256extern jlong257getObjectSize(JNIEnv * jnienv, JPLISAgent * agent, jobject objectToSize);258259extern void260appendToClassLoaderSearch(JNIEnv * jnienv, JPLISAgent * agent, jstring jarFile, jboolean isBootLoader);261262extern void263setNativeMethodPrefixes(JNIEnv * jnienv, JPLISAgent * agent, jobjectArray prefixArray,264jboolean isRetransformable);265266#define jvmti(a) a->mNormalEnvironment.mJVMTIEnv267268/*269* A set of macros for insulating the JLI method callers from270* JVMTI_ERROR_WRONG_PHASE return codes.271*/272273/* for a JLI method where "blob" is executed before simply returning */274#define check_phase_blob_ret(ret, blob) \275if ((ret) == JVMTI_ERROR_WRONG_PHASE) { \276blob; \277return; \278}279280/* for a JLI method where simply returning is benign */281#define check_phase_ret(ret) \282if ((ret) == JVMTI_ERROR_WRONG_PHASE) { \283return; \284}285286/* for a JLI method where returning zero (0) is benign */287#define check_phase_ret_0(ret) \288if ((ret) == JVMTI_ERROR_WRONG_PHASE) { \289return 0; \290}291292/* for a JLI method where returning one (1) is benign */293#define check_phase_ret_1(ret) \294if ((ret) == JVMTI_ERROR_WRONG_PHASE) { \295return 1; \296}297298/* for a case where a specific "blob" must be returned */299#define check_phase_ret_blob(ret, blob) \300if ((ret) == JVMTI_ERROR_WRONG_PHASE) { \301return (blob); \302}303304/* for a JLI method where returning false is benign */305#define check_phase_ret_false(ret) \306if ((ret) == JVMTI_ERROR_WRONG_PHASE) { \307return (jboolean) 0; \308}309310#ifdef __cplusplus311} /* extern "C" */312#endif /* __cplusplus */313314315#endif316317318