Skip to content
Advertisement

JVMTI Invoke toString(); on an Java object from C++ code

So, I was trying to invoke the toString(); method on an object (in this case an enum). This immediately crashed my application. I am assuming that, since the toString(); method isn’t declared directly in the enum but inherited from the Object class (Enum in this case) it is not able to find the method and therefore causes an error.

JavaScript

How m_jenv is declared:

JavaScript

And it’s initialization:

JavaScript

However, the initialization works and the m_jenv field is accessed through a global variable holding a class that holds the JNIEnv* field. It’s accessed by countless methods, even within the class of the is_miss() method and it’s only this method causing an error.

How would I invoke the toString method on a enum (or Java object in general) via JVMTI?

Advertisement

Answer

I have been experimenting a bit, and now that I found a solution it makes sense why it didn’t work.

Instead of trying to find the toString() method in the class directly, I passed the class found at the classpath of java/lang/Enum (or java/lang/Object for non-enum types) to the getMethodID() function.

JavaScript

And then just passed my jobject to CallObjectMethod().

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement