0N/A/*
2362N/A * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
0N/A *
0N/A * Redistribution and use in source and binary forms, with or without
0N/A * modification, are permitted provided that the following conditions
0N/A * are met:
0N/A *
0N/A * - Redistributions of source code must retain the above copyright
0N/A * notice, this list of conditions and the following disclaimer.
0N/A *
0N/A * - Redistributions in binary form must reproduce the above copyright
0N/A * notice, this list of conditions and the following disclaimer in the
0N/A * documentation and/or other materials provided with the distribution.
0N/A *
2362N/A * - Neither the name of Oracle nor the names of its
0N/A * contributors may be used to endorse or promote products derived
0N/A * from this software without specific prior written permission.
0N/A *
0N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
0N/A * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
0N/A * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
0N/A * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0N/A * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0N/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0N/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0N/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0N/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0N/A */
0N/A
4378N/A/*
4378N/A * This source code is provided to illustrate the usage of a given feature
4378N/A * or technique and has been deliberately simplified. Additional steps
4378N/A * required for a production-quality application, such as security checks,
4378N/A * input validation and proper error handling, might not be present in
4378N/A * this sample code.
4378N/A */
4378N/A
4378N/A
0N/A/* Example of using JVMTI_EVENT_GARBAGE_COLLECTION_START and
0N/A * JVMTI_EVENT_GARBAGE_COLLECTION_FINISH events.
0N/A */
0N/A
0N/A#include <stdio.h>
0N/A#include <stdlib.h>
0N/A#include <string.h>
0N/A
0N/A#include "jni.h"
0N/A#include "jvmti.h"
0N/A
0N/A/* For stdout_message(), fatal_error(), and check_jvmti_error() */
0N/A#include "agent_util.h"
0N/A
0N/A/* Global static data */
0N/Astatic jvmtiEnv *jvmti;
0N/Astatic int gc_count;
0N/Astatic jrawMonitorID lock;
0N/A
0N/A/* Worker thread that waits for garbage collections */
0N/Astatic void JNICALL
0N/Aworker(jvmtiEnv* jvmti, JNIEnv* jni, void *p)
0N/A{
0N/A jvmtiError err;
0N/A
0N/A stdout_message("GC worker started...\n");
0N/A
0N/A for (;;) {
0N/A err = (*jvmti)->RawMonitorEnter(jvmti, lock);
0N/A check_jvmti_error(jvmti, err, "raw monitor enter");
0N/A while (gc_count == 0) {
0N/A err = (*jvmti)->RawMonitorWait(jvmti, lock, 0);
0N/A if (err != JVMTI_ERROR_NONE) {
0N/A err = (*jvmti)->RawMonitorExit(jvmti, lock);
0N/A check_jvmti_error(jvmti, err, "raw monitor wait");
0N/A return;
0N/A }
0N/A }
0N/A gc_count = 0;
0N/A
0N/A err = (*jvmti)->RawMonitorExit(jvmti, lock);
0N/A check_jvmti_error(jvmti, err, "raw monitor exit");
0N/A
0N/A /* Perform arbitrary JVMTI/JNI work here to do post-GC cleanup */
0N/A stdout_message("post-GarbageCollectionFinish actions...\n");
0N/A }
0N/A}
0N/A
0N/A/* Creates a new jthread */
0N/Astatic jthread
0N/Aalloc_thread(JNIEnv *env)
0N/A{
0N/A jclass thrClass;
0N/A jmethodID cid;
0N/A jthread res;
0N/A
0N/A thrClass = (*env)->FindClass(env, "java/lang/Thread");
0N/A if ( thrClass == NULL ) {
0N/A fatal_error("Cannot find Thread class\n");
0N/A }
0N/A cid = (*env)->GetMethodID(env, thrClass, "<init>", "()V");
0N/A if ( cid == NULL ) {
0N/A fatal_error("Cannot find Thread constructor method\n");
0N/A }
0N/A res = (*env)->NewObject(env, thrClass, cid);
0N/A if ( res == NULL ) {
0N/A fatal_error("Cannot create new Thread object\n");
0N/A }
0N/A return res;
0N/A}
0N/A
0N/A/* Callback for JVMTI_EVENT_VM_INIT */
0N/Astatic void JNICALL
0N/Avm_init(jvmtiEnv *jvmti, JNIEnv *env, jthread thread)
0N/A{
0N/A jvmtiError err;
0N/A
0N/A stdout_message("VMInit...\n");
0N/A
0N/A err = (*jvmti)->RunAgentThread(jvmti, alloc_thread(env), &worker, NULL,
0N/A JVMTI_THREAD_MAX_PRIORITY);
0N/A check_jvmti_error(jvmti, err, "running agent thread");
0N/A}
0N/A
0N/A/* Callback for JVMTI_EVENT_GARBAGE_COLLECTION_START */
0N/Astatic void JNICALL
0N/Agc_start(jvmtiEnv* jvmti_env)
0N/A{
0N/A stdout_message("GarbageCollectionStart...\n");
0N/A}
0N/A
0N/A/* Callback for JVMTI_EVENT_GARBAGE_COLLECTION_FINISH */
0N/Astatic void JNICALL
0N/Agc_finish(jvmtiEnv* jvmti_env)
0N/A{
0N/A jvmtiError err;
0N/A
0N/A stdout_message("GarbageCollectionFinish...\n");
0N/A
0N/A err = (*jvmti)->RawMonitorEnter(jvmti, lock);
0N/A check_jvmti_error(jvmti, err, "raw monitor enter");
0N/A gc_count++;
0N/A err = (*jvmti)->RawMonitorNotify(jvmti, lock);
0N/A check_jvmti_error(jvmti, err, "raw monitor notify");
0N/A err = (*jvmti)->RawMonitorExit(jvmti, lock);
0N/A check_jvmti_error(jvmti, err, "raw monitor exit");
0N/A}
0N/A
0N/A/* Agent_OnLoad() is called first, we prepare for a VM_INIT event here. */
0N/AJNIEXPORT jint JNICALL
0N/AAgent_OnLoad(JavaVM *vm, char *options, void *reserved)
0N/A{
0N/A jint rc;
0N/A jvmtiError err;
0N/A jvmtiCapabilities capabilities;
0N/A jvmtiEventCallbacks callbacks;
0N/A
0N/A /* Get JVMTI environment */
0N/A rc = (*vm)->GetEnv(vm, (void **)&jvmti, JVMTI_VERSION);
0N/A if (rc != JNI_OK) {
0N/A fatal_error("ERROR: Unable to create jvmtiEnv, rc=%d\n", rc);
0N/A return -1;
0N/A }
0N/A
0N/A /* Get/Add JVMTI capabilities */
0N/A (void)memset(&capabilities, 0, sizeof(capabilities));
0N/A capabilities.can_generate_garbage_collection_events = 1;
0N/A err = (*jvmti)->AddCapabilities(jvmti, &capabilities);
0N/A check_jvmti_error(jvmti, err, "add capabilities");
0N/A
0N/A /* Set callbacks and enable event notifications */
0N/A memset(&callbacks, 0, sizeof(callbacks));
0N/A callbacks.VMInit = &vm_init;
0N/A callbacks.GarbageCollectionStart = &gc_start;
0N/A callbacks.GarbageCollectionFinish = &gc_finish;
0N/A err = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(callbacks));
0N/A check_jvmti_error(jvmti, err, "set event callbacks");
0N/A err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
0N/A JVMTI_EVENT_VM_INIT, NULL);
0N/A check_jvmti_error(jvmti, err, "set event notification");
0N/A err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
0N/A JVMTI_EVENT_GARBAGE_COLLECTION_START, NULL);
0N/A check_jvmti_error(jvmti, err, "set event notification");
0N/A err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
0N/A JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, NULL);
0N/A check_jvmti_error(jvmti, err, "set event notification");
0N/A
0N/A /* Create the necessary raw monitor */
0N/A err = (*jvmti)->CreateRawMonitor(jvmti, "lock", &lock);
0N/A check_jvmti_error(jvmti, err, "create raw monitor");
0N/A return 0;
0N/A}
0N/A
0N/A/* Agent_OnUnload() is called last */
0N/AJNIEXPORT void JNICALL
0N/AAgent_OnUnload(JavaVM *vm)
0N/A{
0N/A}