2

I'm writing JVMTI agent and I'm trying to access specific class fields values at some point when I got jobject. I know they are located on heap instead of stack.

Looking at this unfortunately doesn't help me.

https://docs.oracle.com/javase/7/docs/platform/jvmti/jvmti.html#object

jclass jcls = env->GetObjectClass(object_value);

jint fields_count;
jfieldID *fields;
jvmti->GetClassFields(jcls, &fields_count, &fields);

jfieldID field;
char *field_name;
char *field_sig;
for (int i = 0; i < fields_count; i++) {
    error = jvmti->GetFieldName(jcls, fields[i], &field_name, &field_sig, NULL);
    if (error != JVMTI_ERROR_NONE) {
        printf("GetFieldName error %d\n", error);
    }
    printf("Field %s has sig %s\n", field_name, field_sig);
    if (strcmp("I", field_sig) == 0) {
        int_value = env->GetIntField(object_value, fields[i]); <-- BREAKS
        printf("Value %d\n", int_value);
    }
}

This breaks because it's called on static int field. How can I determine is field static in here?

fritz
  • 23
  • 4

1 Answers1

1

You can use the below code to get the Field Modifier and then check for STATIC modifier -

   jint modifiersPtr;
   jvmti->GetFieldModifiers(classPtr, jfieldID, &modifiersPtr);

   if (modifiersPtr & 0x0008) {
        // STATIC 
        jint jIntVal = (jbyte) env->GetStaticIntField(clazz, fieldId);

    } else {
        // NON-STATIC
        jint jIntVal = (jbyte) env->GetIntField(objectValuePtr, fieldId);
    }
Sachin
  • 394
  • 1
  • 5