jvmtidumper.c

来自「This is a resource based on j2me embedde」· C语言 代码 · 共 1,612 行 · 第 1/4 页

C
1,612
字号
    CVMassert(CVMjvmtiIsGCOwner() || !isGCOwner);    JVMTI_LOCK(ee);    slot = hashRef(object, isGCOwner);    node = CVMglobals.jvmti.statics.objectsByRef[slot];    prev = NULL;    env = CVMexecEnv2JniEnv(ee);    while (node != NULL) {        if (isGCOwner) {            if (!CVMID_icellIsNull(node->ref) &&                (CVMjvmtiGetICellDirect(ee, object) ==                 CVMjvmtiGetICellDirect(ee, node->ref))) {                break;            }        } else {            if (!CVMID_icellIsNull(node->ref) &&                (*env)->IsSameObject(env, object, node->ref)) {                break;            }        }	prev = node;	node = node->next;    }    if (node == NULL) {	if (CVMjvmtiAllocate(sizeof(CVMJvmtiTagNode),			     (unsigned char **)&node) !=	    JVMTI_ERROR_NONE) {	    JVMTI_UNLOCK(ee);	    return JVMTI_ERROR_OUT_OF_MEMORY;	}	node->ref = NULL;    }    if (tag == 0L && node->ref != NULL) {	/* clearing tag, deallocate it */	if (prev == NULL) {	    CVMglobals.jvmti.statics.objectsByRef[slot] = node->next;	} else {	    prev->next = node->next;	}	(*env)->DeleteWeakGlobalRef(env, node->ref);	CVMjvmtiDeallocate((unsigned char *)node);    } else {        jobject objRef;        if (isGCOwner) {            CVMObject *obj = CVMjvmtiGetICellDirect(ee, object);            objRef = CVMID_getWeakGlobalRoot(ee);            if (objRef != NULL) {                CVMjvmtiSetICellDirect(ee, objRef, obj);            }        } else {            objRef = (*env)->NewWeakGlobalRef(env, object);        }        if (node->ref != NULL) {            (*env)->DeleteWeakGlobalRef(env, node->ref);        } else {            node->next = CVMglobals.jvmti.statics.objectsByRef[slot];            CVMglobals.jvmti.statics.objectsByRef[slot] = node;        }	node->ref = objRef;	node->tag = tag;    }    JVMTI_UNLOCK(ee);    return JVMTI_ERROR_NONE;}voidCVMjvmtiPostCallbackUpdateTag(CVMObject *obj, jlong tag){    CVMExecEnv *ee = CVMgetEE();    CVMObjectICell objRef;    CVMassert(CVMD_isgcSafe(ee));    CVMassert(CVMjvmtiIsGCOwner());    /* When we callback into the agent we pass a pointer to the tag.     * The agent may change the actual value of the tag or create a     * a new tag if one doesn't exist.     */    JVMTI_LOCK(ee);    CVMjvmtiSetICellDirect(ee, &objRef, obj);    CVMjvmtiTagSetTag(&objRef, tag, CVM_TRUE);    JVMTI_UNLOCK(ee);}static CVMBooljvmtiCheckForVisit(CVMObject *obj){    CVMjvmtiVisitStackPush(obj);    return CVM_TRUE;}static CVMBooljvmtiIsFilteredByHeapFilter(jlong objTag, jlong objKlassTag,			   jint heapFilter){    /* apply the heap filter */    if (objTag != 0) {	/* filter out tagged objects */	if (heapFilter & JVMTI_HEAP_FILTER_TAGGED) {	    return CVM_TRUE;	}    } else {	/* filter out untagged objects */	if (heapFilter & JVMTI_HEAP_FILTER_UNTAGGED) {	    return CVM_TRUE;	}    }    if (objKlassTag != 0) {	/* filter out objects with tagged classes */	if (heapFilter & JVMTI_HEAP_FILTER_CLASS_TAGGED) {	    return CVM_TRUE;	}    } else {	/* filter out objects with untagged classes. */	if (heapFilter & JVMTI_HEAP_FILTER_CLASS_UNTAGGED) {	    return CVM_TRUE;	}    }    return CVM_FALSE;}static CVMBooljvmtiIsFilteredByKlassFilter(CVMClassBlock * objCb, jclass klass){    if (klass != NULL) {	if (objCb != CVMjvmtiClassRef2ClassBlock(CVMgetEE(), klass)) {	    return CVM_TRUE;	}    }    return CVM_FALSE;}/* invoke the object reference callback to report a reference */static CVMBooljvmtiObjectRefCallback(jvmtiHeapReferenceKind refKind,		    CVMObject *referrer,		    const CVMClassBlock *referrerCb,		    CVMObject *obj, 		    CVMJvmtiDumpContext *dc,		    jint index,		    CVMBool isRoot) {    jint len;    int res;    jlong referrerKlassTag = 0, referrerTag = 0;    jlong objTag, objKlassTag;    jsize objSize;    jvmtiHeapReferenceCallback cb = dc->callbacks->heap_reference_callback;    /* field index is only valid field in reference_info */    jvmtiHeapReferenceInfo refInfo, *refInfoPtr;    CVMClassBlock *objCb;    refInfoPtr = &refInfo;    /* check that callback is provider */    if (cb == NULL) {	return jvmtiCheckForVisit(obj);    }    if (!isRoot) {	CVMjvmtiTagGetTag((jobject)&referrer, &referrerTag, CVM_TRUE);	CVMjvmtiTagGetTag(CVMcbJavaInstance(referrerCb),                          &referrerKlassTag, CVM_TRUE);    }    objCb = CVMobjectGetClass(obj);    /* apply class filter */    if (jvmtiIsFilteredByKlassFilter(objCb, dc->klass)) {	return jvmtiCheckForVisit(obj);    }        CVMjvmtiTagGetTag((jobject)&obj, &objTag, CVM_TRUE);    CVMjvmtiTagGetTag(CVMcbJavaInstance(objCb), &objKlassTag, CVM_TRUE);    /* apply tag filter */    if (jvmtiIsFilteredByHeapFilter(objTag, objKlassTag, dc->heapFilter)) {	return jvmtiCheckForVisit(obj);    }    objSize = CVMobjectSizeGivenClass(obj, objCb);    switch(refKind) {    case JVMTI_HEAP_REFERENCE_FIELD:    case JVMTI_HEAP_REFERENCE_STATIC_FIELD:	/* field index is only valid field in reference_info */	refInfo.field.index = index;	break;    case JVMTI_HEAP_REFERENCE_ARRAY_ELEMENT:	refInfo.array.index = index;	break;    case JVMTI_HEAP_REFERENCE_CONSTANT_POOL:	refInfo.constant_pool.index = index;	break;    default:	refInfoPtr = NULL;	break;    }    /* for arrays we need the length, otherwise -1 */    if (CVMisArrayClass(objCb)) {	len = CVMD_arrayGetLength((CVMArrayOfAnyType *) obj);    } else {	len = -1;    }    /* invoke the callback */    res = (*cb)(refKind, 		refInfoPtr,		objKlassTag,		referrerKlassTag,		objSize,		&objTag,		isRoot ? NULL : &referrerTag,		len,		(void*)dc->userData);    CVMjvmtiPostCallbackUpdateTag(obj, objTag);    if (!isRoot) {	CVMjvmtiPostCallbackUpdateTag(referrer, referrerTag);    }    if (res & JVMTI_VISIT_ABORT) {	return CVM_FALSE;    }    if (res & JVMTI_VISIT_OBJECTS) {	jvmtiCheckForVisit(obj);    }    return CVM_TRUE;}/* invoke the stack reference callback to report a stack reference */static CVMBooljvmtiStackRefCallback(jvmtiHeapReferenceKind refKind,		   CVMObject *obj, 		   CVMJvmtiDumpContext *dc,		   jint index){    jint len;    int res;    jlong objTag, objKlassTag, threadTag;    jsize objSize;    jvmtiHeapReferenceCallback cb = dc->callbacks->heap_reference_callback;    /* field index is only valid field in reference_info */    jvmtiHeapReferenceInfo refInfo;    CVMClassBlock *objCb;    CVMExecEnv *ee = dc->ee;     /* check that callback is provider */    if (cb == NULL) {	return jvmtiCheckForVisit(obj);    }    objCb = CVMobjectGetClass(obj);    /* apply class filter */    if (jvmtiIsFilteredByKlassFilter(objCb, dc->klass)) {	return jvmtiCheckForVisit(obj);    }        CVMjvmtiTagGetTag((jobject)&obj, &objTag, CVM_TRUE);    CVMjvmtiTagGetTag(CVMcbJavaInstance(objCb), &objKlassTag, CVM_TRUE);    CVMjvmtiTagGetTag(CVMcurrentThreadICell(ee), &threadTag, CVM_TRUE);    /* apply tag filter */    if (jvmtiIsFilteredByHeapFilter(objTag, objKlassTag, dc->heapFilter)) {	return jvmtiCheckForVisit(obj);    }    objSize = CVMobjectSizeGivenClass(obj, objCb);    switch(refKind) {    case JVMTI_HEAP_REFERENCE_STACK_LOCAL:	refInfo.stack_local.thread_tag = threadTag;	refInfo.stack_local.thread_id = ee->threadID;	refInfo.stack_local.depth = dc->frameCount;	refInfo.stack_local.method = CVMframeGetMb(dc->frame);	refInfo.stack_local.location = (jlong)((int)CVMframePc(dc->frame));	refInfo.stack_local.slot = index;	break;    case JVMTI_HEAP_REFERENCE_JNI_LOCAL:	refInfo.stack_local.thread_tag = threadTag;	refInfo.stack_local.thread_id = ee->threadID;	refInfo.stack_local.depth = dc->frameCount;	refInfo.stack_local.method = CVMframeGetMb(dc->frame);	break;    default:	break;    }    len = -1;    /* for arrays we need the length, otherwise -1 */    if (CVMisArrayClass(objCb)) {	len = CVMD_arrayGetLength((CVMArrayOfAnyType *) obj);    }    /* invoke the callback */    res = (*cb)(refKind, 		&refInfo,		objKlassTag,		0,		objSize,		&objTag,		NULL,		len,		(void*)dc->userData);    CVMjvmtiPostCallbackUpdateTag(obj, objTag);    if (res & JVMTI_VISIT_ABORT) {	return CVM_FALSE;    }    if (res & JVMTI_VISIT_OBJECTS) {	jvmtiCheckForVisit(obj);    }    return CVM_TRUE;}static jvmtiPrimitiveType CVMtype2jvmtiPrimitiveType[]= {0,				      0,				      0,				      JVMTI_PRIMITIVE_TYPE_INT,				      JVMTI_PRIMITIVE_TYPE_SHORT,				      JVMTI_PRIMITIVE_TYPE_CHAR,				      JVMTI_PRIMITIVE_TYPE_LONG,				      JVMTI_PRIMITIVE_TYPE_BYTE,				      JVMTI_PRIMITIVE_TYPE_FLOAT,				      JVMTI_PRIMITIVE_TYPE_DOUBLE,				      JVMTI_PRIMITIVE_TYPE_BOOLEAN};/* invoke the primitive reference callback to report a primitive reference */static CVMBooljvmtiPrimitiveRefCallback(jvmtiHeapReferenceKind refKind,		       CVMObject *obj, 		       CVMJvmtiDumpContext *dc,		       jint index,		       int CVMtype,		       jvalue v){    int res;    jlong objTag, objKlassTag;    jvmtiPrimitiveFieldCallback cb = dc->callbacks->primitive_field_callback;    /* field index is only valid field in reference_info */    jvmtiHeapReferenceInfo refInfo;    CVMClassBlock *objCb;    objCb = CVMobjectGetClass(obj);    if (objCb == CVMsystemClass(java_lang_Class)) {	objCb = CVMsystemClass(java_lang_Class);    }    /* apply class filter */    if (jvmtiIsFilteredByKlassFilter(objCb, dc->klass)) {	return jvmtiCheckForVisit(obj);    }        CVMjvmtiTagGetTag((jobject)&obj, &objTag, CVM_TRUE);    CVMjvmtiTagGetTag(CVMcbJavaInstance(objCb), &objKlassTag, CVM_TRUE);    /* apply tag filter */    if (jvmtiIsFilteredByHeapFilter(objTag, objKlassTag, dc->heapFilter)) {	return jvmtiCheckForVisit(obj);    }    refInfo.field.index = index;    /* invoke the callback */    res = (*cb)(refKind, 		&refInfo,		objKlassTag,		&objTag,		v,		CVMtype2jvmtiPrimitiveType[CVMtype],		(void*)dc->userData);    CVMjvmtiPostCallbackUpdateTag(obj, objTag);    if (res & JVMTI_VISIT_ABORT) {	return CVM_FALSE;    }    return CVM_TRUE;}/* invoke the array primitive callback to report a primitive array */static CVMBooljvmtiArrayPrimCallback(CVMObject *obj, 		    CVMClassBlock *objCb,		    void *elements,		    CVMJvmtiDumpContext *dc,		    jint count,		    int jvmtiType){    int res;    jlong objTag, objKlassTag;    jvmtiArrayPrimitiveValueCallback callback;    /* field index is only valid field in reference_info */    jlong objSize;    callback = dc->callbacks->array_primitive_value_callback;    if (objCb == CVMsystemClass(java_lang_Class)) {	objCb = CVMsystemClass(java_lang_Class);    }    /* apply class filter */    if (jvmtiIsFilteredByKlassFilter(objCb, dc->klass)) {	return jvmtiCheckForVisit(obj);    }        CVMjvmtiTagGetTag((jobject)&obj, &objTag, CVM_TRUE);    CVMjvmtiTagGetTag(CVMcbJavaInstance(objCb), &objKlassTag, CVM_TRUE);    /* apply tag filter */    if (jvmtiIsFilteredByHeapFilter(objTag, objKlassTag, dc->heapFilter)) {	return jvmtiCheckForVisit(obj);    }    objSize = CVMobjectSizeGivenClass(obj, objCb);    /* invoke the callback */

⌨️ 快捷键说明

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