/* Walk all references for an ObjectIndex and construct the hprof INST dump. */
static void
dump_instance(JNIEnv *env, ObjectIndex object_index, RefIndex list)
{
    jvmtiPrimitiveType primType;
    SiteIndex    site_index;
    SerialNumber trace_serial_num;
    RefIndex     index;
    ObjectIndex  class_index;
    jlong        size;
    ClassIndex   cnum;
    char        *sig;
    void        *elements;
    jint         num_elements;
    jint         num_bytes;
    ObjectIndex *values;
    FieldInfo   *fields;
    jvalue      *fvalues;
    jint         n_fields;
    jboolean     skip_fields;
    jint         n_fields_set;
    ObjectKind   kind;
    TraceIndex   trace_index;
    jboolean     is_array;
    jboolean     is_prim_array;

    HPROF_ASSERT(object_index!=0);
    kind        = object_get_kind(object_index);
    if ( kind == OBJECT_CLASS ) {
        return;
    }
    site_index       = object_get_site(object_index);
    HPROF_ASSERT(site_index!=0);
    cnum             = site_get_class_index(site_index);
    HPROF_ASSERT(cnum!=0);
    size             = (jlong)object_get_size(object_index);
    trace_index      = site_get_trace_index(site_index);
    HPROF_ASSERT(trace_index!=0);
    trace_serial_num = trace_get_serial_number(trace_index);
    sig              = string_get(class_get_signature(cnum));
    class_index      = class_get_object_index(cnum);
        
    values       = NULL;
    elements     = NULL;
    num_elements = 0;
    num_bytes    = 0;
    
    n_fields     = 0;
    skip_fields  = JNI_FALSE;
    n_fields_set = 0;
    fields       = NULL;
    fvalues      = NULL;
    
    index      = list;
    
    is_array      = JNI_FALSE;
    is_prim_array = JNI_FALSE;
    
    if ( sig[0] != JVM_SIGNATURE_ARRAY ) {
        if ( class_get_all_fields(env, cnum, &n_fields, &fields) == 1 ) {
	    /* Trouble getting all the fields, can't trust field index values */
	    skip_fields = JNI_TRUE;
	    /* It is assumed that the reason why we didn't get the fields
	     *     was because the class is not prepared.
	     */
	    if ( gdata->debugflags & DEBUGFLAG_UNPREPARED_CLASSES ) {
                if ( list != 0 ) {
		    dump_ref_list(list);
		    debug_message("Instance of unprepared class with refs: %s\n", 
				   sig);
		} else {
		    debug_message("Instance of unprepared class without refs: %s\n", 
				   sig);
		}
                HPROF_ERROR(JNI_FALSE, "Big Trouble with unprepared class instances");
	    }
	}
        if ( n_fields > 0 ) {
            fvalues = (jvalue*)HPROF_MALLOC(n_fields*(int)sizeof(jvalue));
            (void)memset(fvalues, 0, n_fields*(int)sizeof(jvalue));
        }
    } else {
        is_array = JNI_TRUE;
        if ( sig[0] != 0 && sigToPrimSize(sig+1) != 0 ) {
            is_prim_array = JNI_TRUE;
        }
    }

    while ( index != 0 ) {
        RefInfo *info;
        jvalue   ovalue;
        static jvalue empty_value;

        info = get_info(index);

        /* Process reference objects, many not used right now. */
        switch ( info->flavor ) {
            case INFO_OBJECT_REF_DATA:
                switch ( info->refKind ) {
                    case JVMTI_HEAP_REFERENCE_SIGNERS:
                    case JVMTI_HEAP_REFERENCE_PROTECTION_DOMAIN:
                    case JVMTI_HEAP_REFERENCE_CLASS_LOADER:
                    case JVMTI_HEAP_REFERENCE_INTERFACE:
                    case JVMTI_HEAP_REFERENCE_STATIC_FIELD:
                    case JVMTI_HEAP_REFERENCE_CONSTANT_POOL:
                        /* Should never be seen on an instance dump */
                        HPROF_ASSERT(0);
                        break;
                    case JVMTI_HEAP_REFERENCE_FIELD:
			if ( skip_fields == JNI_TRUE ) {
			    break;
			}
                        HPROF_ASSERT(is_array!=JNI_TRUE);
                        ovalue   = empty_value;
                        ovalue.i = info->object_index;
                        fill_in_field_value(list, fields, fvalues, n_fields, 
                                        info->index, ovalue, 0);
                        n_fields_set++;
                        HPROF_ASSERT(n_fields_set <= n_fields);
                        break;
                    case JVMTI_HEAP_REFERENCE_ARRAY_ELEMENT:
                        /* We get each object element one at a time.  */
                        HPROF_ASSERT(is_array==JNI_TRUE);
                        HPROF_ASSERT(is_prim_array!=JNI_TRUE);
                        if ( num_elements <= info->index ) {
                            int nbytes;
                            
                            if ( values == NULL ) {
                                num_elements = info->index + 1;
                                nbytes = num_elements*(int)sizeof(ObjectIndex);
                                values = (ObjectIndex*)HPROF_MALLOC(nbytes);
                                (void)memset(values, 0, nbytes);
                            } else {
                                void *new_values;
                                int   new_size;
                                int   obytes;

                                obytes = num_elements*(int)sizeof(ObjectIndex);
                                new_size = info->index + 1;
                                nbytes = new_size*(int)sizeof(ObjectIndex);
                                new_values = (void*)HPROF_MALLOC(nbytes);
                                (void)memcpy(new_values, values, obytes);
                                (void)memset(((char*)new_values)+obytes, 0, 
                                                        nbytes-obytes);
                                HPROF_FREE(values);
                                num_elements = new_size;
                                values =  new_values;
                            }
                        }
                        HPROF_ASSERT(values[info->index]==0);
                        values[info->index] = info->object_index;
                        break;
                    default:
                        /* Ignore, not needed */
                        break;
                }
                break;
            case INFO_PRIM_FIELD_DATA:
		if ( skip_fields == JNI_TRUE ) {
		    break;
		}
                HPROF_ASSERT(info->primType!=0);
                HPROF_ASSERT(info->length==-1);
                HPROF_ASSERT(info->refKind==JVMTI_HEAP_REFERENCE_FIELD);
                HPROF_ASSERT(is_array!=JNI_TRUE);
                ovalue = get_key_value(index);
                fill_in_field_value(list, fields, fvalues, n_fields, 
                                    info->index, ovalue, info->primType);
                n_fields_set++;
                HPROF_ASSERT(n_fields_set <= n_fields);
                break;
            case INFO_PRIM_ARRAY_DATA:
                /* Should only be one, and it's handled below */
                HPROF_ASSERT(info->refKind==0);
                /* We assert that nothing else was saved with this array */
                HPROF_ASSERT(index==list&&info->next==0);
                HPROF_ASSERT(is_array==JNI_TRUE);
                HPROF_ASSERT(is_prim_array==JNI_TRUE);
                primType = info->primType;
                elements = get_key_elements(index, primType, 
                                            &num_elements, &num_bytes);
                HPROF_ASSERT(info->length==num_elements);
                size = num_bytes;
                break;
            default:
                HPROF_ASSERT(0);
                break;
        }
        index = info->next;
    }
    
    if ( is_array == JNI_TRUE ) {
        if ( is_prim_array == JNI_TRUE ) {
            HPROF_ASSERT(values==NULL);
            io_heap_prim_array(object_index, trace_serial_num, 
                    (jint)size, num_elements, sig, elements);
        } else {
            HPROF_ASSERT(elements==NULL);
            io_heap_object_array(object_index, trace_serial_num,
                    (jint)size, num_elements, sig, values, class_index);
        }
    } else { 
        io_heap_instance_dump(cnum, object_index, trace_serial_num,
                    class_index, (jint)size, sig, fields, fvalues, n_fields);
    } 
    if ( values != NULL ) {
        HPROF_FREE(values);
    }
    if ( fvalues != NULL ) {
        HPROF_FREE(fvalues);
    }
    if ( elements != NULL ) {
        /* Do NOT free elements, it's a key in the table, leave it be */
    }
}
Example #2
0
/* Walk all references for an ObjectIndex and construct the hprof INST dump. */
static void
dump_instance(JNIEnv *env, ObjectIndex object_index, RefIndex list)
{
    SiteIndex    site_index;
    SerialNumber trace_serial_num;
    RefIndex     index;
    ObjectIndex  class_index;
    jlong        size;
    ClassIndex   cnum;
    char        *sig;
    jint         num_elements;
    jvalue      *values;
    FieldInfo   *fields;
    jvalue      *fvalues;
    jint         n_fields;
    ObjectKind   kind;
    TraceIndex   trace_index;
    jboolean     skip_fields;

    HPROF_ASSERT(object_index!=0);
    kind        = object_get_kind(object_index);
    if ( kind == OBJECT_CLASS ) {
	return;
    }
    site_index 	= object_get_site(object_index);
    HPROF_ASSERT(site_index!=0);
    cnum             = site_get_class_index(site_index);
    HPROF_ASSERT(cnum!=0);
    size             = (jlong)object_get_size(object_index);
    trace_index      = site_get_trace_index(site_index);
    HPROF_ASSERT(trace_index!=0);
    trace_serial_num = trace_get_serial_number(trace_index);
    sig              = string_get(class_get_signature(cnum));
    class_index      = class_get_object_index(cnum);
	
    values       = NULL;
    num_elements = 0;
    
    n_fields     = 0;
    fields       = NULL;
    fvalues      = NULL;
    
    index = list;
    
    skip_fields  = JNI_TRUE;
    if ( sig[0] != JVM_SIGNATURE_ARRAY ) {
	if ( class_get_all_fields(env, cnum, &n_fields, &fields) == 0 ) {
	    if ( n_fields > 0 ) {
                skip_fields  = JNI_FALSE;
		fvalues = (jvalue*)HPROF_MALLOC(n_fields*(int)sizeof(jvalue));
		(void)memset(fvalues, 0, n_fields*(int)sizeof(jvalue));
	    }
	}
    }

    while ( index != 0 ) {
	ObjectIndex field_object_index;
	RefInfo *info;

	info = get_info(index);

	/* Process reference objects, many not used right now. */
	switch ( info->kind ) {
	    case JVMTI_REFERENCE_FIELD:
		/* If the class_tag is 0, it is possible for 
		 *    info->element_index to be >= n_fields
		 *    and when this happens we just skip this field ref
		 *    for now. We probably have a java.lang.Object class
		 *    with n_fields==0, which is probably the wrong class.
		 */
		if (info->class_tag == (jlong)0 || skip_fields == JNI_TRUE ) {
		    break;
		}
		HPROF_ASSERT(info->element_index < n_fields);
		if (info->element_index < n_fields) {
		    /* Field index is referrer_index from referrer_tag */
		    field_object_index = tag_to_object_index(info->object_tag);
		    fvalues[info->element_index].i = field_object_index;
		}
		break;
	    case JVMTI_REFERENCE_ARRAY_ELEMENT:
		/* Array element index is referrer_index in referrer_tag */
		if ( num_elements <= info->element_index  ) {
		    int nbytes;
		    
		    if ( values == NULL ) {
		        num_elements = info->element_index + 1;
			nbytes = num_elements*(int)sizeof(jvalue);
			values = (jvalue*)HPROF_MALLOC(nbytes);
			(void)memset(values, 0, nbytes);
		    } else {
		        void *new_values;
			int   new_size;
			int   obytes;

			obytes = num_elements*(int)sizeof(jvalue);
			new_size = info->element_index + 1;
			nbytes = new_size*(int)sizeof(jvalue);
			new_values = (jvalue*)HPROF_MALLOC(nbytes);
		        (void)memcpy(new_values, values, obytes);
			(void)memset(((char*)new_values)+obytes, 0, 
						nbytes-obytes);
			HPROF_FREE(values);
			num_elements = new_size;
			values =  new_values;
		    }
		}
                field_object_index = tag_to_object_index(info->object_tag);
		HPROF_ASSERT(values[info->element_index].i==0);
		values[info->element_index].i = field_object_index;
		break;
	    default:
		break;
	}
	index = info->next;
    }
    
    if ( sig[0] == JVM_SIGNATURE_ARRAY ) {
	/* FIXUP: Fill primitive arrays? If requested? */
	switch ( sig[1] ) {
	    case JVM_SIGNATURE_CLASS:
	    case JVM_SIGNATURE_ENUM:
	    case JVM_SIGNATURE_ARRAY:
		io_heap_object_array(object_index, trace_serial_num,
			(jint)size, num_elements, class_index, values, sig);
		break;
	    case JVM_SIGNATURE_BYTE:
	    case JVM_SIGNATURE_BOOLEAN:
		io_heap_prim_array(object_index, (jint)size, 
			trace_serial_num, num_elements, sig, values);
		break;
	    case JVM_SIGNATURE_CHAR:
	    case JVM_SIGNATURE_SHORT:
		io_heap_prim_array(object_index, (jint)size, 
			trace_serial_num, num_elements, sig, values);
		break;
	    case JVM_SIGNATURE_INT:
	    case JVM_SIGNATURE_FLOAT:
		io_heap_prim_array(object_index, (jint)size, 
			    trace_serial_num, num_elements, sig, values);
		break;
	    case JVM_SIGNATURE_DOUBLE:
	    case JVM_SIGNATURE_LONG:
		io_heap_prim_array(object_index, (jint)size, 
			trace_serial_num, num_elements, sig, values);
		break;
	    default:
		HPROF_ASSERT(0);
		break;
	}
    } else { 
	/* FIXUP: Fill rest of primitive fields? If requested? */
        io_heap_instance_dump(cnum, object_index, trace_serial_num,
		    class_index, (jint)size, sig, fields, fvalues, n_fields);
    } 
    if ( values != NULL ) {
	HPROF_FREE(values);
    }
    if ( fvalues != NULL ) {
	HPROF_FREE(fvalues);
    }
}