hprof_init.c

来自「一个小公司要求给写的很简单的任务管理系统。」· C语言 代码 · 共 1,986 行 · 第 1/5 页

C
1,986
字号
		jobject loader;				loader = getClassLoader(klass);		event_class_load(env, thread, klass, loader);	    } END_WITH_LOCAL_REFS;	} rawMonitorExit(gdata->data_access_lock);    } END_CALLBACK();}/* JVMTI_EVENT_CLASS_PREPARE */static void JNICALLcbClassPrepare(jvmtiEnv *jvmti, JNIEnv *env, jthread thread, jclass klass){    /* WARNING: This will be called before VM_INIT. */    LOG("cbClassPrepare");        BEGIN_CALLBACK() {	rawMonitorEnter(gdata->data_access_lock); {	    	    WITH_LOCAL_REFS(env, 1) {		jobject loader;				loader = NULL;		loader = getClassLoader(klass);		event_class_prepare(env, thread, klass, loader);	    } END_WITH_LOCAL_REFS;		} rawMonitorExit(gdata->data_access_lock);    } END_CALLBACK();}/* JVMTI_EVENT_DATA_DUMP_REQUEST */static void JNICALLcbDataDumpRequest(jvmtiEnv *jvmti){    jboolean need_to_dump;        LOG("cbDataDumpRequest");        BEGIN_CALLBACK() {	need_to_dump = JNI_FALSE;	rawMonitorEnter(gdata->dump_lock); {	    if (!gdata->dump_in_process) {		need_to_dump    = JNI_TRUE;		gdata->dump_in_process = JNI_TRUE;	    }	} rawMonitorExit(gdata->dump_lock);		if (need_to_dump) {	    dump_all_data(getEnv());	    rawMonitorEnter(gdata->dump_lock); {		gdata->dump_in_process = JNI_FALSE;	    } rawMonitorExit(gdata->dump_lock);	    if (gdata->cpu_sampling && !gdata->jvm_shut_down) {		cpu_sample_on(NULL, 0); /* resume sampling */	    }	}    } END_CALLBACK();}/* JVMTI_EVENT_EXCEPTION_CATCH */static void JNICALLcbExceptionCatch(jvmtiEnv *jvmti, JNIEnv* env,                jthread thread, jmethodID method, jlocation location,		jobject exception){    LOG("cbExceptionCatch");        BEGIN_CALLBACK() {	event_exception_catch(env, thread, method, location, exception);    } END_CALLBACK();}/* JVMTI_EVENT_MONITOR_WAIT */static void JNICALLcbMonitorWait(jvmtiEnv *jvmti, JNIEnv* env,                jthread thread, jobject object, jlong timeout){    LOG("cbMonitorWait");        BEGIN_CALLBACK() {	monitor_wait_event(env, thread, object, timeout);    } END_CALLBACK();}/* JVMTI_EVENT_MONITOR_WAITED */static void JNICALLcbMonitorWaited(jvmtiEnv *jvmti, JNIEnv* env,                jthread thread, jobject object, jboolean timed_out){    LOG("cbMonitorWaited");        BEGIN_CALLBACK() {	monitor_waited_event(env, thread, object, timed_out);    } END_CALLBACK();}/* JVMTI_EVENT_MONITOR_CONTENDED_ENTER */static void JNICALLcbMonitorContendedEnter(jvmtiEnv *jvmti, JNIEnv* env,                jthread thread, jobject object){    LOG("cbMonitorContendedEnter");        BEGIN_CALLBACK() {	monitor_contended_enter_event(env, thread, object);    } END_CALLBACK();}/* JVMTI_EVENT_MONITOR_CONTENDED_ENTERED */static void JNICALLcbMonitorContendedEntered(jvmtiEnv *jvmti, JNIEnv* env,                jthread thread, jobject object){    LOG("cbMonitorContendedEntered");        BEGIN_CALLBACK() {	monitor_contended_entered_event(env, thread, object);    } END_CALLBACK();}/* JVMTI_EVENT_GARBAGE_COLLECTION_START */static void JNICALLcbGarbageCollectionStart(jvmtiEnv *jvmti){    LOG("cbGarbageCollectionStart");        /* Only calls to Allocate, Deallocate, RawMonitorEnter & RawMonitorExit     *   are allowed here (see the JVMTI Spec).     */    gdata->gc_start_time = md_get_timemillis();}/* JVMTI_EVENT_GARBAGE_COLLECTION_FINISH */static void JNICALLcbGarbageCollectionFinish(jvmtiEnv *jvmti){    LOG("cbGarbageCollectionFinish");       /* Only calls to Allocate, Deallocate, RawMonitorEnter & RawMonitorExit     *   are allowed here (see the JVMTI Spec).     */    if ( gdata->gc_start_time != -1L ) {	gdata->time_in_gc += (md_get_timemillis() - gdata->gc_start_time);	gdata->gc_start_time = -1L;    }       /* Increment gc_finish counter, notify watcher thread */    rawMonitorEnter(gdata->gc_finish_lock); {	/* If VM_DEATH is trying to shut it down, don't do anything at all.	 *    Never send notify if VM_DEATH wants the watcher thread to quit.	 */	if ( gdata->gc_finish_active ) {	    gdata->gc_finish++;	    rawMonitorNotifyAll(gdata->gc_finish_lock);	}    } rawMonitorExit(gdata->gc_finish_lock);}/* JVMTI_EVENT_OBJECT_FREE */static void JNICALLcbObjectFree(jvmtiEnv *jvmti, jlong tag){    LOG3("cbObjectFree", "tag", (int)tag);       /* Only calls to Allocate, Deallocate, RawMonitorEnter & RawMonitorExit     *   are allowed here (see the JVMTI Spec).     */    HPROF_ASSERT(tag!=(jlong)0);    rawMonitorEnter(gdata->object_free_lock); {	if ( !gdata->jvm_shut_down ) {	    Stack *stack;	    	    stack = gdata->object_free_stack;	    if ( stack == NULL ) {		gdata->object_free_stack = stack_init(512, 512, sizeof(jlong));		stack = gdata->object_free_stack;	    }	    stack_push(stack, (void*)&tag);	}    } rawMonitorExit(gdata->object_free_lock);}static void set_callbacks(jboolean on) {    jvmtiEventCallbacks callbacks;        (void)memset(&callbacks,0,sizeof(callbacks));    if ( ! on ) {        setEventCallbacks(&callbacks);	return;    }        /* JVMTI_EVENT_VM_INIT */    callbacks.VMInit                     = &cbVMInit;    /* JVMTI_EVENT_VM_DEATH */    callbacks.VMDeath                    = &cbVMDeath;    /* JVMTI_EVENT_THREAD_START */    callbacks.ThreadStart                = &cbThreadStart;    /* JVMTI_EVENT_THREAD_END */    callbacks.ThreadEnd                  = &cbThreadEnd;    /* JVMTI_EVENT_CLASS_FILE_LOAD_HOOK */    callbacks.ClassFileLoadHook          = &cbClassFileLoadHook;    /* JVMTI_EVENT_CLASS_LOAD */    callbacks.ClassLoad                  = &cbClassLoad;    /* JVMTI_EVENT_CLASS_PREPARE */    callbacks.ClassPrepare               = &cbClassPrepare;    /* JVMTI_EVENT_DATA_DUMP_REQUEST */    callbacks.DataDumpRequest		 = &cbDataDumpRequest;    /* JVMTI_EVENT_EXCEPTION_CATCH */    callbacks.ExceptionCatch             = &cbExceptionCatch;    /* JVMTI_EVENT_MONITOR_WAIT */    callbacks.MonitorWait                = &cbMonitorWait;    /* JVMTI_EVENT_MONITOR_WAITED */    callbacks.MonitorWaited              = &cbMonitorWaited;    /* JVMTI_EVENT_MONITOR_CONTENDED_ENTER */    callbacks.MonitorContendedEnter      = &cbMonitorContendedEnter;    /* JVMTI_EVENT_MONITOR_CONTENDED_ENTERED */    callbacks.MonitorContendedEntered    = &cbMonitorContendedEntered;    /* JVMTI_EVENT_GARBAGE_COLLECTION_START */    callbacks.GarbageCollectionStart     = &cbGarbageCollectionStart;    /* JVMTI_EVENT_GARBAGE_COLLECTION_FINISH */    callbacks.GarbageCollectionFinish    = &cbGarbageCollectionFinish;    /* JVMTI_EVENT_OBJECT_FREE */    callbacks.ObjectFree                 = &cbObjectFree;    setEventCallbacks(&callbacks);    }static void getCapabilities(void){    jvmtiCapabilities needed_capabilities;    jvmtiCapabilities potential_capabilities;    /* Fill in ones that we must have */    (void)memset(&needed_capabilities,0,sizeof(needed_capabilities));    needed_capabilities.can_generate_garbage_collection_events   = 1;    needed_capabilities.can_tag_objects                          = 1;    if (gdata->bci) {        needed_capabilities.can_generate_all_class_hook_events   = 1;    }    if (gdata->obj_watch) {        needed_capabilities.can_generate_object_free_events      = 1;    }    if (gdata->cpu_timing || gdata->cpu_sampling) {	#if 0 /* Not needed until we call JVMTI for CpuTime */        needed_capabilities.can_get_thread_cpu_time              = 1;        needed_capabilities.can_get_current_thread_cpu_time      = 1;	#endif        needed_capabilities.can_generate_exception_events        = 1;    }    if (gdata->monitor_tracing) {	#if 0 /* Not needed until we call JVMTI for CpuTime */        needed_capabilities.can_get_thread_cpu_time              = 1;        needed_capabilities.can_get_current_thread_cpu_time      = 1;	#endif        needed_capabilities.can_get_owned_monitor_info           = 1;        needed_capabilities.can_get_current_contended_monitor    = 1;        needed_capabilities.can_get_monitor_info                 = 1;        needed_capabilities.can_generate_monitor_events          = 1;    }    /* Get potential capabilities */    getPotentialCapabilities(&potential_capabilities);       /* Some capabilities would be nicer to have */     needed_capabilities.can_get_source_file_name        =         potential_capabilities.can_get_source_file_name;    needed_capabilities.can_get_line_numbers    =         potential_capabilities.can_get_line_numbers;    /* Add the capabilities */    addCapabilities(&needed_capabilities);}/* Dynamic library loading */static void *load_library(char *name){    char  lname[FILENAME_MAX+1];    char  err_buf[256+FILENAME_MAX+1];    char *boot_path;    void *handle;    handle = NULL;    /* The library may be located in different ways, try both, but     *   if it comes from outside the SDK/jre it isn't ours.     */    getSystemProperty("sun.boot.library.path", &boot_path);    md_build_library_name(lname, FILENAME_MAX, boot_path, name);    handle = md_load_library(lname, err_buf, (int)sizeof(err_buf));    if ( handle == NULL ) {	/* This may be necessary on Windows. */	md_build_library_name(lname, FILENAME_MAX, "", name);	handle = md_load_library(lname, err_buf, (int)sizeof(err_buf));	if ( handle == NULL ) {	    HPROF_ERROR(JNI_TRUE, err_buf);	}    }    return handle;}/* Lookup dynamic function pointer in shared library */static void *lookup_library_symbol(void *library, char **symbols, int nsymbols){    void *addr;    int   i;    addr = NULL;    for( i = 0 ; i < nsymbols; i++ ) {	addr = md_find_library_entry(library, symbols[i]);	if ( addr != NULL ) {	    break;	}    }    if ( addr == NULL ) {	char errmsg[256];		(void)md_snprintf(errmsg, sizeof(errmsg), 		    "Cannot find library symbol '%s'", symbols[0]);	HPROF_ERROR(JNI_TRUE, errmsg);    }    return addr;}/* ------------------------------------------------------------------- *//* The OnLoad interface */JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *vm, char *options, void *reserved){    /* See if it's already loaded */    if ( gdata!=NULL && gdata->isLoaded==JNI_TRUE ) {        HPROF_ERROR(JNI_TRUE, "Cannot load this JVM TI agent twice, check your java command line for duplicate hprof options.");        return JNI_ERR;    }        gdata = get_gdata();    gdata->isLoaded = JNI_TRUE;    error_setup();    LOG2("Agent_OnLoad", "gdata setup");        gdata->jvm = vm;#ifndef SKIP_NPT    /* Load in NPT library for character conversions */    NPT_INITIALIZE(&(gdata->npt), NPT_VERSION, NULL);    if ( gdata->npt == NULL ) {        HPROF_ERROR(JNI_TRUE, "Cannot load npt library");    }    gdata->npt->utf = (gdata->npt->utfInitialize)(NULL);    if ( gdata->npt->utf == NULL ) {        HPROF_ERROR(JNI_TRUE, "Cannot initialize npt utf functions");    }#endif    /* Get the JVMTI environment */    getJvmti();       /* Lock needed to protect debug_malloc() code, which is not MT safe */    #ifdef DEBUG	gdata->debug_malloc_lock = createRawMonitor("HPROF debug_malloc lock");    #endif    parse_options(options);    LOG2("Agent_OnLoad", "Has jvmtiEnv and options parsed");    /* Initialize machine dependent code (micro state accounting) */    md_init();    string_init();	/* Table index values look like: 0x10000000 */        class_init();	/* Table index values look like: 0x20000000 */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?