minst.c

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

C
479
字号
/* * @(#)minst.c	1.1 06/01/28 *  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. *  * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: *  * -Redistribution of source code must retain the above copyright notice, this *  list of conditions and the following disclaimer. *  * -Redistribution in binary form must reproduce the above copyright notice,  *  this list of conditions and the following disclaimer in the documentation *  and/or other materials provided with the distribution. *  * Neither the name of Sun Microsystems, Inc. or the names of contributors may  * be used to endorse or promote products derived from this software without  * specific prior written permission. *  * This software is provided "AS IS," without a warranty of any kind. ALL  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. *  * You acknowledge that this software is not designed, licensed or intended * for use in the design, construction, operation or maintenance of any * nuclear facility. */#include "stdlib.h"#include "minst.h"#include "java_crw_demo.h"	    	   /* ------------------------------------------------------------------- *//* Some constant maximum sizes */#define MAX_TOKEN_LENGTH	80#define MAX_METHOD_NAME_LENGTH  256/* Some constant names that tie to Java class/method names. *    We assume the Java class whose static methods we will be calling *    looks like: * * public class Minst { *     private static int engaged;  *     private static native void _method_entry(Object thr, int cnum, int mnum); *     public static void method_entry(int cnum, int mnum) *     { *   	   ... *     } * } * */#define MINST_class	   Minst	    /* Name of class we are using */#define MINST_entry        method_entry    /* Name of java entry method */#define MINST_engaged      engaged         /* Name of java static field *//* C macros to create strings from tokens */#define _STRING(s) #s#define STRING(s) _STRING(s)/* ------------------------------------------------------------------- *//* Global agent data structure */typedef struct {    /* JVMTI Environment */    jvmtiEnv      *jvmti;    jboolean       vm_is_dead;    jboolean       vm_is_started;    /* Data access Lock */    jrawMonitorID  lock;    /* Options */    char           *include;    char           *exclude;    /* Class Count/ID */    jint            ccount;} GlobalAgentData;static GlobalAgentData *gdata;/* Enter a critical section by doing a JVMTI Raw Monitor Enter */static voidenter_critical_section(jvmtiEnv *jvmti){    jvmtiError error;        error = (*jvmti)->RawMonitorEnter(jvmti, gdata->lock);    check_jvmti_error(jvmti, error, "Cannot enter with raw monitor");}/* Exit a critical section by doing a JVMTI Raw Monitor Exit */static voidexit_critical_section(jvmtiEnv *jvmti){    jvmtiError error;        error = (*jvmti)->RawMonitorExit(jvmti, gdata->lock);    check_jvmti_error(jvmti, error, "Cannot exit with raw monitor");}/* Callback for JVMTI_EVENT_VM_START */static void JNICALLcbVMStart(jvmtiEnv *jvmti, JNIEnv *env){    enter_critical_section(jvmti); {        /* Indicate VM has started */        gdata->vm_is_started = JNI_TRUE;    } exit_critical_section(jvmti);}/* Callback for JVMTI_EVENT_VM_INIT */static void JNICALLcbVMInit(jvmtiEnv *jvmti, JNIEnv *env, jthread thread){    enter_critical_section(jvmti); {        jclass   klass;	jfieldID field;	/* Register Natives for class whose methods we use */	klass = (*env)->FindClass(env, STRING(MINST_class));	if ( klass == NULL ) {	    fatal_error("ERROR: JNI: Cannot find %s with FindClass\n", 			STRING(MINST_class));	}		/* Engage calls. */	field = (*env)->GetStaticFieldID(env, klass, STRING(MINST_engaged), "I");	if ( field == NULL ) {	    fatal_error("ERROR: JNI: Cannot get field from %s\n", 			STRING(MINST_class));	}	(*env)->SetStaticIntField(env, klass, field, 1);    } exit_critical_section(jvmti);}/* Callback for JVMTI_EVENT_VM_DEATH */static void JNICALLcbVMDeath(jvmtiEnv *jvmti, JNIEnv *env){    enter_critical_section(jvmti); {        jclass   klass;	jfieldID field;	/* The VM has died. */	stdout_message("VMDeath\n");	/* Disengage calls in MINST_class. */	klass = (*env)->FindClass(env, STRING(MINST_class));	if ( klass == NULL ) {	    fatal_error("ERROR: JNI: Cannot find %s with FindClass\n", 			STRING(MINST_class));	}	field = (*env)->GetStaticFieldID(env, klass, STRING(MINST_engaged), "I");	if ( field == NULL ) {	    fatal_error("ERROR: JNI: Cannot get field from %s\n", 			STRING(MINST_class));	}	(*env)->SetStaticIntField(env, klass, field, -1);	/* The critical section here is important to hold back the VM death	 *    until all other callbacks have completed.	 */	/* Since this critical section could be holding up other threads	 *   in other event callbacks, we need to indicate that the VM is	 *   dead so that the other callbacks can short circuit their work.	 *   We don't expect any further events after VmDeath but we do need	 *   to be careful that existing threads might be in our own agent	 *   callback code.	 */	gdata->vm_is_dead = JNI_TRUE;    } exit_critical_section(jvmti);	}  /* Callback for JVMTI_EVENT_CLASS_FILE_LOAD_HOOK */static void JNICALLcbClassFileLoadHook(jvmtiEnv *jvmti, JNIEnv* env,                jclass class_being_redefined, jobject loader,                const char* name, jobject protection_domain,                jint class_data_len, const unsigned char* class_data,                jint* new_class_data_len, unsigned char** new_class_data){    enter_critical_section(jvmti); {	/* It's possible we get here right after VmDeath event, be careful */	if ( !gdata->vm_is_dead ) {	    const char *classname;            /* Name could be NULL */	    if ( name == NULL ) {		classname = java_crw_demo_classname(class_data, class_data_len,			NULL);		if ( classname == NULL ) {		    fatal_error("ERROR: No classname inside classfile\n");		}	    } else {		classname = strdup(name);		if ( classname == NULL ) {		    fatal_error("ERROR: Out of malloc memory\n");		}	    }	    	    *new_class_data_len = 0;            *new_class_data     = NULL;            /* The tracker class itself? */            if ( interested((char*)classname, "", gdata->include, gdata->exclude) 		  &&  strcmp(classname, STRING(MINST_class)) != 0 ) {                jint           cnum;                int            system_class;                unsigned char *new_image;                long           new_length;                /* Get unique number for every class file image loaded */                cnum = gdata->ccount++;                /* Is it a system class? If the class load is before VmStart		 *   then we will consider it a system class that should		 *   be treated carefully. (See java_crw_demo)		 */                system_class = 0;                if ( !gdata->vm_is_started ) {                    system_class = 1;                }                new_image = NULL;                new_length = 0;

⌨️ 快捷键说明

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