I write a sample code to get some information via JVMTI, but error
JVMTI_ERROR_INVALID_ENVIRONMENT is always here.
HelloWorld.java,
class HelloWorld {
private native void print();
public static void main(String[] args) {
new HelloWorld().print();
}
}
libjvmtiagent.c,
....
JNIEXPORT void JNICALL
Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
jvmtiError err;
jint owned_monitor_count, res;
jobject* owned_monitors_ptr;
/*
JavaVM* jvm;
res = (*env)->GetJavaVM(env, &jvm);
res = (*jvm)->GetEnv(jvm, (void **) &jvmti, JVMTI_VERSION_1_0);
if (res != JNI_OK || jvmti == NULL)
{
printf("ERROR: Unable to access JVMTI Version 1 (0x%x),"
" is your J2SE a 1.5 or newer version?"
" JNIEnv's GetEnv() returned %d\n",
JVMTI_VERSION_1, res);
}
*/
err = (*jvmti)->GetOwnedMonitorInfo (env, NULL, &owned_monitor_count, &owned_monitors_ptr);
check_jvmti_error(jvmti, err, "Cannot get owned monitor infor\n");
printf ("owned monitors is %d\n", (int)owned_monitor_count);
err = (*jvmti)->Deallocate (env, owned_monitors_ptr);
check_jvmti_error(jvmti, err, "Cannot deallocate\n");
return;
}
Agent_OnLoad has been implemented properly, however when I run my
program like this,
$ LD_LIBRARY_PATH=. java -agentlib:jvmtiagent HelloWorld
jvmti = 0x831a7a8Got VM init event
callbackVMInit: main thread
ERROR: JVMTI: 116(JVMTI_ERROR_INVALID_ENVIRONMENT): Cannot get owned monitor infor
owned monitors is -1309188692
ERROR: JVMTI: 116(JVMTI_ERROR_INVALID_ENVIRONMENT): Cannot deallocate
Got VM Death event
Agent_OnUnload
Even I remove these comments in Java_HelloWorld_print above, this error
does not go away.
I am a newibe in JNI, and googled JVMTI_ERROR_INVALID_ENVIRONMENT for a
long time without any useful results.
Could anyone here give me some help on this? Thanks in advance.

Signature
Yao Qi <qiyaoltc@gmail.com> GNU/Linux Developer
http://duewayqi.googlepages.com/
To give of yourself, you must first know yourself.
Gordon Beaton - 10 May 2007 08:29 GMT
> err = (*jvmti)->GetOwnedMonitorInfo (env, NULL, &owned_monitor_count, &owned_monitors_ptr);
I don't know JVMTI, but suspect strongly that the first argument to
the JVMTI functions should be jvmti, not env.
/gordon
--
Yao Qi - 10 May 2007 17:51 GMT
>> err = (*jvmti)->GetOwnedMonitorInfo (env, NULL, &owned_monitor_count,
> &owned_monitors_ptr);
>
> I don't know JVMTI, but suspect strongly that the first argument to
> the JVMTI functions should be jvmti, not env.
Yes, that is right. My code works when I replace env by jvmti in the
first argument. Thanks.

Signature
Yao Qi <qiyaoltc@gmail.com> GNU/Linux Developer
http://duewayqi.googlepages.com/
One doesn't have a sense of humor. It has you.
-- Larry Gelbart