JVMTM Tool InterfaceVersion 1.1 |
jvmti.h.
To use these definitions add the J2SETM include directory
to your include path and add
#include <jvmti.h>
to your source code.
.so file).
An agent may be started at VM startup by specifying the agent library
name using a command line option.
Some implementations may support a mechanism to
start agents in the live phase.
The details of how this is initiated are implementation specific.
JavaVMInitArgs argument
to the JNI_CreateJavaVM function of the JNI
Invocation API.
One of the two following
command-line options is used on VM startup to
properly load and run agents.
These arguments identify the library containing
the agent as well as an options
string to be passed in at startup.
-agentlib:<agent-lib-name>=<options>
-agentlib: is the name of the
library to load. Lookup of the library, both its full name and location,
proceeds in a platform-specific manner.
Typically, the <agent-lib-name> is expanded to an
operating system specific file name.
The <options> will be passed to the agent on start-up.
For example, if the option
-agentlib:foo=opt1,opt2 is specified, the VM will attempt to
load the shared library foo.dll from the system PATH
under WindowsTM or libfoo.so from the
LD_LIBRARY_PATH under the SolarisTM operating environment.
-agentpath:<path-to-agent>=<options>
-agentpath: is the absolute path from which
to load the library.
No library name expansion will occur.
The <options> will be passed to the agent on start-up.
For example, if the option
-agentpath:c:\myLibs\foo.dll=opt1,opt2 is specified, the VM will attempt to
load the shared library c:\myLibs\foo.dll.
Agent_OnLoad
in the library will be invoked.
Libraries loaded with -agentlib: or -agentpath:
will be searched for JNI native method implementations to facilitate the
use of Java programming language code in tools, as is needed for
bytecode instrumentation.
The agent libraries will be searched after all other libraries have been
searched (agents wishing to override or intercept the native method
implementations of non-agent methods can use the
NativeMethodBind event).
These switches do the above and nothing more - they do not change the
state of the VM or JVM TI. No command line options are needed
to enable JVM TI
or aspects of JVM TI, this is handled programmatically
by the use of
capabilities.
OnLoad
phase the function
Agent_OnLoad
will be invoked.
If the agent is started in the live
phase the function
Agent_OnAttach
will be invoked.
Exactly one call to a start-up function is made per agent.
OnLoad phase then its
agent library must export a start-up function with the following prototype:
The VM will start the agent by calling this function. It will be called early enough in VM initialization that:JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *vm, char *options, void *reserved)
Agent_OnLoad function with
<options> as the second argument -
that is, using the command-line option examples,
"opt1,opt2" will be passed to the char *options
argument of Agent_OnLoad.
The options argument is encoded as a
modified UTF-8 string.
If =<options> is not specified,
a zero length string is passed to options.
The lifespan of the options string is the Agent_OnLoad
call. If needed beyond this time the string or parts of the string must
be copied.
The period between when Agent_OnLoad is called and when it
returns is called the OnLoad phase.
Since the VM is not initialized during the OnLoad
phase,
the set of allowed operations
inside Agent_OnLoad is restricted (see the function descriptions for the
functionality available at this time).
The agent can safely process the options and set
event callbacks with SetEventCallbacks. Once
the VM initialization event is received
(that is, the VMInit
callback is invoked), the agent
can complete its initialization.
Rationale: Early startup is required so that agents can set the desired capabilities, many of which must be set before the VM is initialized. In JVMDI, the -Xdebug command-line option provided very coarse-grain control of capabilities. JVMPI implementations use various tricks to provide a single "JVMPI on" switch. No reasonable command-line option could provide the fine-grain of control required to balance needed capabilities vs performance impact. Early startup is also needed so that agents can control the execution environment - modifying the file system and system properties to install their functionality.
The return value fromAgent_OnLoad is used to indicate an error.
Any value other than zero indicates an error and causes termination of the VM.
The VM will start the agent by calling this function. It will be called in the context of a thread that is attached to the VM. The first argument <vm> is the Java VM. The <options> argument is the startup options provided to the agent. <options> is encoded as a modified UTF-8 string. If startup options were not provided, a zero length string is passed toJNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char *options, void *reserved)
options. The lifespan of the options string is the
Agent_OnAttach call. If needed beyond this time the string or parts of
the string must be copied.
Note that some capabilities
may not be available in the live phase.
The Agent_OnAttach function initializes the agent and returns a value
to the VM to indicate if an error occurred. Any value other than zero indicates an error.
An error does not cause the VM to terminate. Instead the VM ignores the error, or takes
some implementation specific action -- for example it might print an error to standard error,
or record the error in a system log.
This function will be called by the VM when the library is about to be unloaded. The library will be unloaded and this function will be called if some platform specific mechanism causes the unload (an unload mechanism is not specified in this document) or the library is (in effect) unloaded by the termination of the VM whether through normal termination or VM failure, including start-up failure. Uncontrolled shutdown is, of couse, an exception to this rule. Note the distinction between this function and the VM Death event: for the VM Death event to be sent, the VM must have run at least to the point of initialization and a valid JVM TI environment must exist which has set a callback for VMDeath and enabled the event None of these are required forJNIEXPORT void JNICALL Agent_OnUnload(JavaVM *vm)
Agent_OnUnload and this function
is also called if the library is unloaded for other reasons.
In the case that a VM Death event is sent, it will be sent before this
function is called (assuming this function is called due to VM termination).
This function can be used to clean-up resources allocated by the agent.
JAVA_TOOL_OPTIONS variable is
provided so that agents may be launched in these cases.
Platforms which support environment variables or other named strings, may support the
JAVA_TOOL_OPTIONS variable. This variable will be broken into options at white-space
boundaries. White-space characters include space, tab, carriage-return, new-line,
vertical-tab, and form-feed. Sequences of white-space characters are considered
equivalent to a single white-space character. No white-space is included in the options
unless quoted. Quoting is as follows:
JNI_CreateJavaVM (in the JNI Invocation API) will prepend these options to the options supplied
in its JavaVMInitArgs argument. Platforms may disable this feature in cases where security is
a concern; for example, the Reference Implementation disables this feature on Unix systems when
the effective user or group ID differs from the real ID.
This feature is intended to support the initialization of tools -- specifically including the
launching of native or Java programming language agents. Multiple tools may wish to use this
feature, so the variable should not be overwritten, instead, options should be appended to
the variable. Note that since the variable is processed at the time of the JNI Invocation
API create VM call, options processed by a launcher (e.g., VM selection options) will not be handled.
GetEnv from
Agent_OnLoad.
MyProfiler.methodEntered().
Since the changes are purely additive, they do not modify application
state or behavior.
Because the inserted agent code is standard bytecodes, the VM can run at full speed,
optimizing not only the target program but also the instrumentation. If the
instrumentation does not involve switching from bytecode execution, no expensive
state transitions are needed. The result is high performance events.
This approach also provides complete control to the agent: instrumentation can be
restricted to "interesting" portions of the code (e.g., the end user's code) and
can be conditional. Instrumentation can run entirely in Java programming language
code or can call into the native agent. Instrumentation can simply maintain
counters or can statistically sample events.
Instrumentation can be inserted in one of three ways:
*.class files which have been modified to add the instrumentation.
This method is extremely awkward and, in general, an agent cannot know
the origin of the class files which will be loaded.
ClassFileLoadHook
event, triggered by the class load,
provides this functionality. This mechanism provides efficient
and complete access to one-time instrumentation.
ClassFileLoadHook event, triggered by calling the
RetransformClasses function.
Classes can be modified multiple times and can be returned to their
original state.
The mechanism allows instrumentation which changes during the
course of execution.
ClassFileLoadHook event
and the RetransformClasses function)
and, during development, for fix-and-continue debugging
(the RedefineClasses function).
Care must be taken to avoid perturbing dependencies, especially when
instrumenting core classes. For example, an approach to getting notification
of every object allocation is to instrument the constructor on
Object. Assuming that the constructor is initially
empty, the constructor could be changed to:
public Object() {
MyProfiler.allocationTracker(this);
}
However, if this change was made using the
ClassFileLoadHook
event then this might impact a typical VM as follows:
the first created object will call the constructor causing a class load of
MyProfiler; which will then cause
object creation, and since MyProfiler isn't loaded yet,
infinite recursion; resulting in a stack overflow. A refinement of this
would be to delay invoking the tracking method until a safe time. For
example, trackAllocations could be set in the
handler for the VMInit event.
static boolean trackAllocations = false;
public Object() {
if (trackAllocations) {
MyProfiler.allocationTracker(this);
}
}
The SetNativeMethodPrefix allows native methods
to be instrumented by the use of wrapper methods.
jvmtiEnv*.
An environment has information about its JVM TI connection.
The first value in the environment is a pointer to the function table.
The function table is an array of pointers to JVM TI functions.
Every function pointer is at a predefined offset inside the
array.
When used from the C language:
double indirection is used to access the functions;
the environment pointer provides context and is the first
parameter of each function call; for example:
jvmtiEnv *jvmti;
...
jvmtiError err = (*jvmti)->GetLoadedClasses(jvmti, &class_count, &classes);
When used from the C++ language:
functions are accessed as member functions of jvmtiEnv;
the environment pointer is not passed to the function call; for example:
jvmtiEnv *jvmti;
...
jvmtiError err = jvmti->GetLoadedClasses(&class_count, &classes);
Unless otherwise stated, all examples and declarations in this
specification use the C language.
A JVM TI environment can be obtained through the JNI Invocation API
GetEnv function:
jvmtiEnv *jvmti;
...
(*jvm)->GetEnv(jvm, &jvmti, JVMTI_VERSION_1_0);
Each call to GetEnv
creates a new JVM TI connection and thus
a new JVM TI environment.
The version argument of GetEnv must be
a JVM TI version.
The returned environment may have a different version than the
requested version but the returned environment must be compatible.
GetEnv will return JNI_EVERSION if a
compatible version is not available, if JVM TI is not supported or
JVM TI is not supported in the current VM configuration.
Other interfaces may be added for creating JVM TI environments
in specific contexts.
Each environment has its own state (for example,
desired events,
event handling functions, and
capabilities).
An environment is released with
DisposeEnvironment.
Thus, unlike JNI which has one environment per thread, JVM TI environments work
across threads and are created dynamically.
jvmtiError function return value.
Some functions can return additional
values through pointers provided by the calling function.
In some cases, JVM TI functions allocate memory that your program must
explicitly deallocate. This is indicated in the individual JVM TI
function descriptions. Empty lists, arrays, sequences, etc are
returned as NULL.
In the event that the JVM TI function encounters
an error (any return value other than JVMTI_ERROR_NONE) the values
of memory referenced by argument pointers is undefined, but no memory
will have been allocated and no global references will have been allocated.
If the error occurs because of invalid input, no action will have occurred.
jobject and jclass)
and their derivatives
(jthread and jthreadGroup).
References passed to
JVM TI functions can be either global or local, but they must be
strong references. All references returned by JVM TI functions are
local references--these local references are created
during the JVM TI call.
Local references are a resource that must be managed (see the
JNI Documentation).
When threads return from native code all local references
are freed. Note that some threads, including typical
agent threads, will never return from native code.
A thread is ensured the ability to create sixteen local
references without the need for any explicit management.
For threads executing a limited number of JVM TI calls before
returning from native code
(for example, threads processing events),
it may be determined that no explicit management
is needed.
However, long running agent threads will need explicit
local reference management--usually with the JNI functions
PushLocalFrame and PopLocalFrame.
Conversely, to preserve references beyond the
return from native code, they must be converted to global references.
These rules do not apply to jmethodID and jfieldID
as they are not jobjects.
jvmtiError
Allocate(jvmtiEnv* env,
jlong size,
unsigned char** mem_ptr)
Allocate an area of memory through the JVM TI allocator.
The allocated
memory should be freed with Deallocate.
| Phase | Callback Safe | Position | Since |
| may be called during any phase |
This function may be called from the callbacks to the
Heap iteration functions, or from the
event handlers for the
GarbageCollectionStart,
GarbageCollectionFinish,
and ObjectFree events.
| 46 | 1.0 |
| Capabilities | |
| Required Functionality |
| Parameters | ||
| Name | Type | Description |
size | jlong |
The number of bytes to allocate.
Rationale:
|
mem_ptr | unsigned char** |
On return, a pointer to the beginning of the allocated memory.
If size is zero, NULL is returned.
Agent passes a pointer to a unsigned char*. On return, the unsigned char* points to a newly allocated array of size size. The array should be freed with Deallocate. |
| Errors | |
| This function returns either a universal error or one of the following errors | |
| Error | Description |
JVMTI_ERROR_OUT_OF_MEMORY | Memory request cannot be honored. |
JVMTI_ERROR_ILLEGAL_ARGUMENT |
size is less than zero.
|
JVMTI_ERROR_NULL_POINTER |
mem_ptr is NULL.
|
jvmtiError
Deallocate(jvmtiEnv* env,
unsigned char* mem)
Deallocate mem using the JVM TI allocator.
This function should
be used to deallocate any memory allocated and returned
by a JVM TI function
(including memory allocated with Allocate).
All allocated memory must be deallocated
or the memory cannot be reclaimed.
| Phase | Callback Safe | Position | Since |
| may be called during any phase |
This function may be called from the callbacks to the
Heap iteration functions, or from the
event handlers for the
GarbageCollectionStart,
GarbageCollectionFinish,
and ObjectFree events.
| 47 | 1.0 |
| Capabilities | |
| Required Functionality |
| Parameters | ||
| Name | Type | Description |
mem |
unsigned char
* |
A pointer to the beginning of the allocated memory.
Please ignore "On return, the elements are set."
Agent passes an array of unsigned char. The incoming values of the elements of the array are ignored. On return, the elements are set.
If
mem
is
NULL, the call is ignored.
|
| Errors | |
| This function returns a universal error |
jvmtiThreadInfo - Thread information structurejvmtiMonitorStackDepthInfo - Monitor stack depth information structure
jvmtiError
GetThreadState(jvmtiEnv* env,
jthread thread,
jint* thread_state_ptr)
Get the state of a thread. The state of the thread is represented by the
answers to the hierarchical set of questions below:
JVMTI_THREAD_STATE_TERMINATED)JVMTI_THREAD_STATE_ALIVE)
JVMTI_THREAD_STATE_SUSPENDED)JVMTI_THREAD_STATE_INTERRUPTED)JVMTI_THREAD_STATE_IN_NATIVE)JVMTI_THREAD_STATE_RUNNABLE)JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER)JVMTI_THREAD_STATE_WAITING)
JVMTI_THREAD_STATE_WAITING_INDEFINITELY
JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT)JVMTI_THREAD_STATE_IN_OBJECT_WAIT)JVMTI_THREAD_STATE_PARKED)JVMTI_THREAD_STATE_SLEEPING)The following definitions are used to convert JVM TI thread state to
Thread State Flags Constant Value Description JVMTI_THREAD_STATE_ALIVE0x0001 Thread is alive. Zero if thread is new (not started) or terminated. JVMTI_THREAD_STATE_TERMINATED0x0002 Thread has completed execution. JVMTI_THREAD_STATE_RUNNABLE0x0004 Thread is runnable. JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER0x0400 Thread is waiting to enter a synchronization block/method or, after an Object.wait(), waiting to re-enter a synchronization block/method.JVMTI_THREAD_STATE_WAITING0x0080 Thread is waiting. JVMTI_THREAD_STATE_WAITING_INDEFINITELY0x0010 Thread is waiting without a timeout. For example, Object.wait().JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT0x0020 Thread is waiting with a maximum time to wait specified. For example, Object.wait(long).JVMTI_THREAD_STATE_SLEEPING0x0040 Thread is sleeping -- Thread.sleep(long).JVMTI_THREAD_STATE_IN_OBJECT_WAIT0x0100 Thread is waiting on an object monitor -- Object.wait.JVMTI_THREAD_STATE_PARKED0x0200 Thread is parked, for example: LockSupport.park,LockSupport.parkUtilandLockSupport.parkNanos.JVMTI_THREAD_STATE_SUSPENDED0x100000 Thread suspended. java.lang.Thread.suspend()or a JVM TI suspend function (such asSuspendThread) has been called on the thread. If this bit is set, the other bits refer to the thread state before suspension.JVMTI_THREAD_STATE_INTERRUPTED0x200000 Thread has been interrupted. JVMTI_THREAD_STATE_IN_NATIVE0x400000 Thread is in native code--that is, a native method is running which has not called back into the VM or Java programming language code. This flag is not set when running VM compiled Java programming language code nor is it set when running VM code or VM support code. Native VM interface functions, such as JNI and JVM TI functions, may be implemented as VM code. JVMTI_THREAD_STATE_VENDOR_10x10000000 Defined by VM vendor. JVMTI_THREAD_STATE_VENDOR_20x20000000 Defined by VM vendor. JVMTI_THREAD_STATE_VENDOR_30x40000000 Defined by VM vendor.
java.lang.Thread.State style states.