有没有一个很好的Java API可以在JVMTI之上使用?
发布于 2011-02-08 02:59:26
好的..。刚试过..。看起来像预期的那样……在现实生活中,VMInit回调将返回一个类的实例,该实例实现了一个镜像C JVMTI接口的接口。C代理将存储此实例,并在事件需要时调用它。此外,在VMInit Java返回之前,它将设置功能、回调和注册事件等。您可能能够获得大约90%的JVMTI API覆盖率.....这只是一个输入它的案例……如果你有充分的理由,我可以在一个周末完成:-)
下面的代码将生成以下代码:
C: VMInit,准备回调Java方法
JVMTI回调类,VMInit()。
C: VMInit,返回成功的回调Java方法
Java:最后……大家好,我是Java main
package com.stackoverflow;
public class JVMTICallback {
public static void VMInit() {
System.out.println("Java:\tJVMTI callback class, VMInit().");
}
public static void main(String[] args) {
// This main is only here to give us something to run for the test
System.out.println("Java:\tAnd Finally... Hello, I'm the Java main");
}
}和C语言
#include <stdlib.h>
#include "jvmti.h"
jvmtiEnv *globalJVMTIInterface;
void JNICALL
vmInit(jvmtiEnv * jvmti_env, JNIEnv * jni_env, jthread thread)
{
printf("C:\tVMInit, preparing to callback Java method\n");
char *className = "com/stackoverflow/JVMTICallback";
char *methodName = "VMInit";
char *descriptor = "()V";
jclass callbackClass = (*jni_env)->FindClass(jni_env, className);
if (!callbackClass) {
fprintf(stderr,"C:\tUnable to locate callback class.\n");
return;
}
jmethodID callbackMethodID = (*jni_env)->GetStaticMethodID(jni_env, callbackClass, methodName, descriptor);
if (!callbackMethodID)
{
fprintf(stderr, "C:\tUnable to locate callback VMInit method\n");
return;
}
(*jni_env)->CallStaticVoidMethodV(jni_env, callbackClass, callbackMethodID, NULL);
printf("C:\tVMInit, callback Java method returned successfully\n");
}
JNIEXPORT jint JNICALL
Agent_OnLoad(JavaVM * jvm, char *options, void *reserved)
{
jint returnCode = (*jvm)->GetEnv(jvm, (void **) &globalJVMTIInterface,
JVMTI_VERSION_1_0);
if (returnCode != JNI_OK)
{
fprintf(stderr,
"The version of JVMTI requested (1.0) is not supported by this JVM.\n");
return JVMTI_ERROR_UNSUPPORTED_VERSION;
}
jvmtiEventCallbacks *eventCallbacks;
eventCallbacks = calloc(1, sizeof(jvmtiEventCallbacks));
if (!eventCallbacks)
{
fprintf(stderr, "Unable to allocate memory\n");
return JVMTI_ERROR_OUT_OF_MEMORY;
}
eventCallbacks->VMInit = &vmInit;
returnCode = (*globalJVMTIInterface)->SetEventCallbacks(globalJVMTIInterface,
eventCallbacks, (jint) sizeof(*eventCallbacks));
if (returnCode != JNI_OK)
{
fprintf(stderr, "C:\tJVM does not have the required capabilities (%d)\n",
returnCode);
exit(-1);
}
returnCode = (*globalJVMTIInterface)->SetEventNotificationMode(
globalJVMTIInterface, JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, (jthread) NULL);
if (returnCode != JNI_OK)
{
fprintf(
stderr,
"C:\tJVM does not have the required capabilities, JVMTI_ENABLE, JVMTI_EVENT_VM_INIT (%d)\n",
returnCode);
exit(-1);
}
return JVMTI_ERROR_NONE;
}发布于 2011-01-31 13:41:03
JVMTI不是为了在上面有Java API而构建的。JVM TI定义本身说:
Java JVM tool interface (JVM)是一个标准的本机
,它允许本机库为Java平台捕获事件和控制Java虚拟机。
因为它是为捕获事件和控件的原生API而构建的,所以我不认为它上面有API。你能解释一下你正在努力实现的目标吗?
我不知道JVM TI上有任何Java API。
发布于 2011-02-04 19:24:17
我四处寻找,不幸的是在JVMTI之上找不到任何Java API库。看起来你不太走运。
但是,您可以做的是从Java代码中调用本地库。我不太擅长C/C++,但是从JVMTI文档中我看到可以从provided headers构建一个小型共享库。然后,您可以使用JNA**调用它。它将为您提供一个围绕本机库的很好的API包装器。
请看JNA Getting Started page上的示例
这个页面还链接到JNAerator,它可以为您生成所有必要的Java绑定。
这种方法的缺点是必须为您的目标平台维护这个薄的本地层。
**与通常的JNI相比,JNA处理运行时开销,但开发的简易性超过了IMO的性能优势。只有在必要时才切换到JNI。
https://stackoverflow.com/questions/4819099
复制相似问题