0

I am trying to record the state of the application when an Exception occurs within the application, i am specifically interested with the method local variable values(Both Primitive and User Defined)? i have gone through the documentation of ASM 5.1, but i couldn't find any interfaces which lets me access the values? is there a way to do this, if not using ASM are there any other Interfaces which would let me do this ? The solution should be independent of the application, it should be a plug and play type solution, using which i should be able to plug it to any java application.

Getting Field Ids using JNI:

char *klazName;
                        error = (*jvmti)->GetLocalObject(jvmti, thread, i,
                                table_ptr[j].slot, &value_ptr);
                        check_jvmti_error(jvmti, error,
                                "Cannot Get Local Variable Object");
                        if(!error){
                            klaz = (*env)->GetObjectClass(env,value_ptr);
                            error = (*jvmti)->GetClassSignature(jvmti, klaz,
                    &klazName, NULL);
                            if(strstr(klazName,"String")!=NULL){
                                printf("...%s\n",klazName);
                                field = (*env)->GetFieldID(env,declaring_class_ptr,table_ptr[j].name,"S");
                                value = (jstring)(*env)->GetObjectField(env,value_ptr,field);
                                stringVal = (*env)->GetStringUTFChars(env,value,0);
                                printf("Value of Field %s is .\n", stringVal);
kumarD
  • 574
  • 2
  • 9
  • 23

1 Answers1

0

JVM TI GetLocal* functions should help.

apangin
  • 92,924
  • 10
  • 193
  • 247
  • I have used these functions, the problem is i get a jobject using getlocalObject function, i wanna know how to extract the fields within this object ? – kumarD Feb 21 '17 at 05:23
  • @kumarD 1. Using Reflection. Invoke Java method and pass the given `jobject` to it. – apangin Feb 21 '17 at 07:19
  • but in order to use reflection i would have to modify the application code right? i don't want to modify the application code. I want a solution to be independent of the application code, would dynamic BCI work ? – kumarD Feb 21 '17 at 07:21
  • @kumarD Not necessarily. Your JVM TI agent may load auxiliaryJava classes if needed. – apangin Feb 21 '17 at 07:23
  • 2. The alternative way is using JNI [Get*Field](http://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html#Get_type_Field_routines) functions. Call [GetObjectClass](http://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html#GetObjectClass) + [GetClassFields](http://docs.oracle.com/javase/8/docs/platform/jvmti/jvmti.html#GetClassFields) to obtain `jfieldIDs`. – apangin Feb 21 '17 at 07:24
  • I have attached the snippet which i used to do this, but this is resulting in core dumps, can you please have a look into it – kumarD Feb 21 '17 at 07:27
  • @kumarD Find a line that causes the crash. I'm afraid I can't debug it for you. At least, the code will crash when `value_ptr == NULL`. – apangin Feb 21 '17 at 07:47
  • The direction is right, keep going this way. If you get stuck, ask a separate question providing a [minimal, complete, verifiable example](http://stackoverflow.com/help/mcve). – apangin Feb 21 '17 at 07:48
  • stuck with parsing values of Exception and Thread objects, please check http://stackoverflow.com/questions/42383800/invalid-slot-error-when-trying-to-access-value-of-exception-and-thread-objects-u – kumarD Feb 22 '17 at 06:01