IBM User Guide for Java V7 on Windows > Troubleshooting and support > Using diagnostic tools > Using the JVMTI > IBM JVMTI extensions



Sample JVMTI agent

A sample JVMTI agent, written in C/C++, using the IBM JVMTI extensions.

/*
 * tiSample.c
 *
 * Sample JVMTI agent to demonstrate the IBM JVMTI dump extensions
 */

#include "jvmti.h"
#include "ibmjvmti.h"

/* Forward declarations for JVMTI callback functions */
void JNICALL VMInitCallback(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread);
void JNICALL DumpStartCallback(jvmtiEnv *jvmti_env, char* label, char* event, char* detail, ...);


/*
 * Agent_Onload()
 * 
 * JVMTI agent initialisation function, invoked as agent is loaded by the JVM
 */
JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {

  jvmtiEnv *jvmti = NULL;
	jvmtiError rc;
	jint extensionEventCount = 0;
	jvmtiExtensionEventInfo *extensionEvents = NULL;
	jint extensionFunctionCount = 0;
	jvmtiExtensionFunctionInfo *extensionFunctions = NULL;
	int i = 0, j = 0;

  printf("tiSample: Loading JVMTI sample agent\n");

    /* Get access to JVMTI */
    (*jvm)->GetEnv(jvm, (void **)&jvmti, JVMTI_VERSION_1_0);  

   	/* Look up all the JVMTI extension events and functions */ 	
   (*jvmti)->GetExtensionEvents(jvmti, &extensionEventCount, &extensionEvents); 	
   (*jvmti)->GetExtensionFunctions(jvmti, &extensionFunctionCount, &extensionFunctions);  	

    printf("tiSample: Found %i JVMTI extension events, %i extension functions\n", extensionEventCount, 
extensionFunctionCount);  	

   /* Find the JVMTI extension event we want */ 	
   while (i++ < extensionEvenCount) {  		
       
        if (strcmp(extensionEvents->id, COM_IBM_VM_DUMP_START) == 0) { 			
            /* Found the dump start extension event, now set up a callback for it */ 			
            rc = (*jvmti)->SetExtensionEventCallback(jvmti, extensionEvents->extension_event_index, 
&DumpStartCallback);			
            printf("tiSample: Setting JVMTI event callback %s, rc=%i\n", COM_IBM_VM_DUMP_START, rc); 			
            break; 		
        } 		
        extensionEvents++; /* move on to the next extension event */ 	
    }  	

    /* Find the JVMTI extension function we want */ 	
    while (j++ < extensionFunctionCount) { 		
        jvmtiExtensionFunction function = extensionFunctions->func; 			 		

        if (strcmp(extensionFunctions->id, COM_IBM_SET_VM_DUMP) == 0) { 			
            /* Found the set dump extension function, now set a dump option to generate javadumps on 
					thread starts */ 			
            rc = function(jvmti, "java:events=thrstart"); 			
            printf("tiSample: Calling JVMTI extension %s, rc=%i\n", COM_IBM_SET_VM_DUMP, rc); 			
            break; 		
        }  		
        extensionFunctions++; /* move on to the next extension function */ 	
     }      

     return JNI_OK;}   


/*   
 * DumpStartCallback()  
 * JVMTI callback for dump start event (IBM JVMTI extension)  */ 
void JNICALL  
DumpStartCallback(jvmtiEnv *jvmti_env, char* label, char* event, char* detail, ...) { 	  
			printf("tiSample: Received JVMTI event callback, for event %s\n", event);  }


Parent: IBM JVMTI extensions








Error 404 - Not Found

Error 404 - Not Found

The document you are looking for may have been removed or re-named. Please contact the web site owner for further assistance.