JVMTI之上的Java API?

Zub*_*air 15 java jvmti

是否有一个很好的Java API我可以在JVMTI之上使用?

Abd*_*oof 11

JVMTI不是为了拥有Java API而构建的.JVM TI定义本身说:

JVM工具接口(JVM TI)是一种标准本机API,允许本机库捕获事件并控制Java平台的Java虚拟机(JVM).

由于它是为本机API构建的,用于捕获事件和控件,因此我认为不存在API.你可以解释一下你想要实现的目标吗?

我不知道JVM TI之上的任何Java API.

  • 只是想要在Java中构建一个控制另一个JVM的工具.想一想分析器等.这将使调试一些奇怪的问题更容易. (2认同)

小智 5

好的...刚刚尝试过...似乎按预期工作....在现实生活中,VMInit回调将返回一个类的实例,该类实现了一个镜像C JVMTI接口的接口...... C代理会存储此实例并在事件需要时调用它......此外,在 VMInit Java 返回之前,它会设置功能和回调并注册事件等......你可能能够获得大约 90% 的 JVMTI API 覆盖率...... ...这只是一个输入的情况....如果你有一个强有力的案例,我可以在周末完成:-)

下面的代码产生这个:

C:VMInit,准备回调Java方法
Java:JVMTI回调类,VMInit()。
C: VMInit, 回调 Java 方法成功返回
Java: 最后... 你好,我是 Java 主


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");
    }

}
Run Code Online (Sandbox Code Playgroud)

和 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;
}
Run Code Online (Sandbox Code Playgroud)