使用 Java 虚拟机工具接口(JVMTI)创建调试和分析代理

来源:互联网 发布:windows自带脚本编程 编辑:程序博客网 时间:2024/05/16 08:07
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

  Java 虚拟机工具接口Java Virtual Machine Tool Interface,JVMTI)提供了一种编程接口,允许软件开发人员创建软件代理以监视和控制 Java 编程语言应用程序。JVMTIJava 2 Software Development Kit (SDK), Standard Edition, 版本 1.5.0 中的一种新增功能。它取代了 Java Virtual Machine Profiling Interface (JVMPI),从版本 1.1 起即作为 Java 2 SDK 的一种实验功能包括在内。在 JSR-163 中对 JVMTI 进行了有关说明。

  本文阐述如何使用 JVMTI 创建 Java 应用程序的调试和分析工具。这种工具(也称作代理)在应用程序中发生事件时,能够使用该接口提供的功能对事件通知进行注册,并查询和控制该应用程序。Java.sun.com/j2se/1.5.0/docs/guide/JVMTI/JVMTI.html" />这里提供了 JVMTI 的文档资料。JVMTI 代理对于调试和调优应用程序十分有用。它可以对应用程序的各个方面予以说明,如内存分配情况、CPU 利用情况及锁争夺情况。

  尽管 JVMPI 现在仍处于实验阶段,很多 Java 技术开发人员已经在使用它了,而且已经把它应用到多种市场上提供的 Java 应用程序 Profiler。请注意,极力鼓励开发人员使用 JVMTI 而不使用 JVMPI。JVMPI 在不久的将来将被废止。

  JVMTI 在多个方面改进了 JVMPI 的功能和性能。例如:

  JVMTI 依赖于每个事件的回调。这比 JVMPI 设计使用需要编组和取消编组的事件结构更有效。

  JVMTI 包含四倍于 JVMPI 的函数(包括用于获取关于变量、字段、方法和类的信息的更多函数)。有关 JVMTI 函数的完整索引,请参见Java.sun.com/j2se/1.5.0/docs/guide/JVMTI/JVMTI.html#functionsection" />函数索引页。

  JVMTI 比 JVMPI 提供更多类型的事件通知,包括异常事件、字段访问和修改事件、断点和单步骤事件等。

  有些从未被充分利用的 JVMPI 事件,如 Arena 的 new 和 delete,或者通过字节码工具很容易就能获得的内容,或者 JVMTI 函数本身(如 heap dump 和 object allocation)往往被 丢掉。 对这些事件的描述位于Java.sun.com/j2se/1.5.0/docs/guide/JVMTI/JVMTI.html#eventindex" />事件索引页。

  JVMTI 是基于功能的,而 JVMPI 对于相应性能影响却是“要么全有,要么全无”。

  JVMPI 堆功能不可伸缩。

  JVMPI 没有错误返回信息。

  JVMPI 在 VM 实现方面具有很强的侵入性,容易导致维护问题和性能受损。

  JVMPI 是个实验产品,不久将废止。

  在本文的以下部分,我们介绍一个简单代理,它使用 JVMTI 函数从 Java 应用程序提取信息。 代理的编写必须使用本地代码。这里给出的示例代理是使用 C 语言编写的。您可以JVMTIagent.c" />于此下载完整的示例代理代码。下面几段介绍如何初始化一个代理,以及代理如何使用 JVMTI 函数提取关于 Java 应用程序的信息,以及如何编译和运行代理。此示例代码和编译步骤特定于 UNIX 环境,但是经过修改后也可用于 Windows。这里介绍的代理可用于在任何 Java 应用程序中分析线程和确定 JVM 内存使用情况。

  这里包含一个用 Java 语言编写的简单程序,称作 SimpleThread.Java,并可Java" />从这里下载。我们使用 ThreadSample.Java 演示此代理的预期输出。

  JVMTI 的功能很多,在此无法详述;但本文中的代码可以提供一个出发点,让您去开发符合自己特定需求的分析工具。

  代理初始化

  本节介绍用于初始化代理的代码。首先,代理必须包括 JVMTI.h 文件,语句为 #include <JVMTI.h>。

  另外,代理必须包含一个名为 Agent_OnLoad 的函数,加载库时要调用这一函数。Agent_OnLoad 函数用于在初始化 Java virtual machine (JVM) 之前设置所需的功能。Agent_OnLoad 签名如下所示:

 

  JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {

  ...

  /* We return JNI_OK to signify success */

  return JNI_OK;

  }

 

 

 

  在我们的示例代码中,我们必须为将要使用的 JVMTI 函数和事件启用多种功能。一般情况下均需(在某些情况下必须)将这些功能添加到 Agent_OnLoad 函数中。有关每种函数或事件所需的功能的说明,参见 Java.sun.com/j2se/1.5.0/docs/guide/JVMTI/JVMTI.html" />Java 虚拟机工具接口页。例如,要使用 InterruptThread 函数,can_signal_thread 功能必须为 true。我们把示例所需的全部功能都设置为 true,然后使用 AddCapabilities 函数将它们添加到 JVMTI 环境中:

 

  static JVMTIEnv *JVMTI = NULL;

  static JVMTICapabilities capa;

  JVMTIError error;

  ...

  (void)memset(&capa, 0, sizeof(JVMTICapabilities));

  capa.can_signal_thread = 1;

  capa.can_get_owned_monitor_info = 1;

  capa.can_generate_method_entry_events = 1;

  capa.can_generate_exception_events = 1;

  capa.can_generate_vm_object_alloc_events = 1;

  capa.can_tag_objects = 1;

  error = (*JVMTI)->AddCapabilities(JVMTI, &capa);

  check_JVMTI_error(JVMTI, error, "Unable to get necessary JVMTI capabilities.");

  ...

 

 

 

  此外,Agent_OnLoad 函数通常用于注册事件通知。在此示例中,我们在使用 SetEventNotificationMode 函数的 Agent_OnLoad 中启用了多个事件,如 VM Initialization Event、VM Death Event 和 VM Object Allocation, 如下所示:

 

  error = (*JVMTI)->SetEventNotificationMode

  (JVMTI, JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, (jthread)NULL);

  error = (*JVMTI)->SetEventNotificationMode

  (JVMTI, JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, (jthread)NULL);

  error = (*JVMTI)->SetEventNotificationMode

  (JVMTI, JVMTI_ENABLE, JVMTI_EVENT_VM_OBJECT_ALLOC, (jthread)NULL);

  check_JVMTI_error(JVMTI, error, "Cannot set event notification");

  ...

 

 

 

  注意,在此示例中,NULL 是作为第三个参数传递的,它可以全局地启用事件通知。如果需要,可以为某个特殊线程启用或禁用某些事件。

  我们为其注册的每个事件还都必须具有一个指定的回调函数,当该事件发生时将调用它。例如,如果一个 Exception 类型的 JVMTI Event 发生,示例代理会将其发送到回调方法 callbackException() 中。

  使用 JVMTIEventCallbacks 结构和 SetEventCallbacks 函数可以完成此任务:

 

  JVMTIEventCallbacks callbacks;

  ...

  (void)memset(&callbacks, 0, sizeof(callbacks));

  callbacks.VMInit = &callbackVMInit; /* JVMTI_EVENT_VM_INIT */

  callbacks.VMDeath = &callbackVMDeath; /* JVMTI_EVENT_VM_DEATH */

  callbacks.Exception = &callbackException;/* JVMTI_EVENT_EXCEPTION */

  callbacks.VMObjectAlloc = &callbackVMObjectAlloc;/* JVMTI_EVENT_VM_OBJECT_ALLOC */

  error = (*JVMTI)->SetEventCallbacks(JVMTI, &callbacks,(jint)sizeof(callbacks));

  check_JVMTI_error(JVMTI, error, "Cannot set JVMTI callbacks");

  ...

 

 

 

  我们还将设置一个全局代理数据区域以在整个代码中使用。

 

  /* Global agent data structure */

  typedef struct {

  /* JVMTI Environment */

  JVMTIEnv *JVMTI;

  jboolean vm_is_started;

  /* Data access Lock */

  jrawMonitorID lock;

  } GlobalAgentData;

  static GlobalAgentData *gdata;

 

 

 

  在 Agent_OnLoad 函数中,我们执行以下设置:

 

  /* Setup initial global agent data area

  * Use of static/extern data should be handled carefully here.

  * We need to make sure that we are able to cleanup after

  * ourselves so anything allocated in this library needs to be

  * freed in the Agent_OnUnload() function.

  */

  static GlobalAgentData data;

  (void)memset((void*)&data, 0, sizeof(data));

  gdata = &data;

  ...

  /* Here we save the JVMTIEnv* for Agent_OnUnload(). */

  gdata->JVMTI = JVMTI;

  ...

 

 

 

  我们在 Agent_OnLoad() 中创建一个原始监视器,然后把代码 VM_INIT、VM_DEATH 和 EXCEPTION 包装于 JVMTI RawMonitorEnter() 和 RawMonitorExit() 接口 。

 

  /* Here we create a raw monitor for our use in this agent to

  * protect critical sections of code.

  */

  error = (*JVMTI)->CreateRawMonitor(JVMTI, "agent data", &(gdata->lock));

  /* Enter a critical section by doing a JVMTI Raw Monitor Enter */

  static void

  enter_critical_section(JVMTIEnv *JVMTI)

  {

  JVMTIError error;

  error = (*JVMTI)->RawMonitorEnter(JVMTI, gdata->lock);

  check_JVMTI_error(JVMTI, error, "Cannot enter with raw monitor");

  }

  /* Exit a critical section by doing a JVMTI Raw Monitor Exit */

  static void

  exit_critical_section(JVMTIEnv *JVMTI)

  {

  JVMTIError error;

  error = (*JVMTI)->RawMonitorExit(JVMTI, gdata->lock);

  check_JVMTI_error(JVMTI, error, "Cannot exit with raw monitor");

  }

 

 

 

  卸载代理时,VM 将调用 Agent_OnUnload。此函数用于清理在 Agent_OnLoad 期间分配的资源。

 

  /* Agent_OnUnload: This is called immediately before the shared library

  * is unloaded. This is the last code executed.

  */

  JNIEXPORT void JNICALL Agent_OnUnload(JavaVM *vm)

  {

  /* Make sure all malloc/calloc/strdup space is freed */

  }

 

 

 

  使用 JVMTI 分析线程

  本节介绍如何获取关于在 JVM 中运行的用户线程的信息。如前所述,启动 JVM 时,JVMTI 代理库中的启动函数 Agent_OnLoad 将被调用。在 VM 初始化过程中,JVMTI_EVENT_VM_INIT 类型的 JVMTI Event 将生成并被发送到代理代码的 callbackVMInit 例程中。一旦 VM 初始化事件被接收(即 调用VMInit 回调),代理即可结束其初始化。现在,此代理可以自由调用任何 Java Native Interface (JNI) 或 JVMTI 函数。此时,我们已经处于活动阶段,将启用本 VMInit 回调例程中的 Exception 事件(JVMTI_EVENT_EXCEPTION)。

 

  error = (*JVMTI)->SetEventNotificationMode

  (JVMTI, JVMTI_ENABLE, JVMTI_EVENT_EXCEPTION, (jthread)NULL);

 

 

 

  无论何时,只要在 Java 编程语言方法中首次探测到异常,就会生成 Exception 事件。此异常可能由 Java 编程语言抛出,也可能由本地方法抛出;但是如果由本地方法抛出,直到 Java 编程语言方法首次发现此异常时该事件才会生成。如果异常已被处理并清除,则异常事件不会生成。

  出于演示目的,下面给出了所用的示例 Java 应用程序。主线程创建了 5 个线程,这 5 个线程退出前各自抛出一个异常。一旦启动 JVM,JVMTI_EVENT_VM_INIT 将生成并被发送到代理代码中进行处理,因为我们已经在代理代码中启用了 VMInit 和 Exception 事件。随后,当 Java 线程抛出一个异常时,JVMTI_EVENT_EXCEPTION 将被发送到代理代码中。然后,代理代码 会分析此线程信息并显示当前线程名、它所属的线程组、此线程所拥有的监视器、线程状态、线程堆栈跟踪及 JVM 中的所有用户线程。

 

  public class SimpleThread {

  static MyThread t;

  public static void main(String args[]) throws Throwable{

  t = new MyThread();

  System.out.println("Creating and running 10 threads...");

  for(int i = 0; i < 5; i++) {

  Thread thr = new Thread(t,"MyThread"+i);

  thr.start();

  try {

  thr.join();

  } catch (Throwable t) {

  }

  }

  }

  }

  class MyThread implements Runnable {

  Thread t;

  public MyThread() {

  }

  public void run() {

  /* NO-OP */

  try {

  "a".getBytes("ASCII");

  throwException();

  Thread.sleep(1000);

  } catch (Java.lang.InterruptedException e){

  e.printStackTrace();

  } catch (Throwable t) {

  }

  }

  public void throwException() throws Throwable{

  throw new Exception("Thread Exception from MyThread");

  }

  }

 

 

 

  我们来看一下 Java 应用程序内部抛出一个异常时 JVMTI 代理代码的执行情况。

 

  throw new Exception("Thread Exception from MyThread");

 

 

 

  JVMTI 异常事件生成后将被发送到代理代码的 Exception 回调例程中。代理必须添加 can_generate_exception_events 功能才能启用异常事件。我们使用 JVMTI GetMethodName 接口来显示生成异常的方法名和例程签名。

 

  err3 = (*JVMTI)->GetMethodName(JVMTI, method, &name, &sig, &gsig);

  printf("Exception in Method:%s%s/n", name, sig);

 

 

 

  我们使用 JVMTI GetThreadInfo 和 GetThreadGroupInfo 接口来显示当前线程和组详细信息。

 

  err = (*JVMTI)->GetThreadInfo(JVMTI, thr, &info);

  if (err == JVMTI_ERROR_NONE) {

  err1 = (*JVMTI)->GetThreadGroupInfo(JVMTI,info.thread_group, &groupInfo);

  ...

  if ((err == JVMTI_ERROR_NONE) && (err1 == JVMTI_ERROR_NONE ))

  {

  printf("Got Exception event, Current Thread is : %s and Thread Group is: %s/n",

  ((info.name==NULL) ? ""

  : info.name), groupInfo.name);

  }

  }

 

 

 

  这将在您的终端上产生以下输出:

  Got Exception event, Current Thread is : MyThread0 and Thread Group is: main

  使用 JVMTI GetOwnedMonitorInfo 接口可以获取关于指定线程所拥有的监视器的信息。此函数 不要求挂起线程。

 

  err = (*JVMTI)->GetOwnedMonitorInfo(JVMTI, thr, νm_monitors, &arr_monitors);

  printf("Number of Monitors returned : %d/n", num_monitors);

 

 

 

  使用 JVMTI GetThreadState 接口可以获取线程的状态信息。

  线程状态可以为以下值之一:

  线程已终止

  线程活动

  线程可运行

  线程休眠

  线程在等待通知

  线程处于对象等待状态

  线程为本地状态

  线程已挂起

  线程已中断

 

  err = (*JVMTI)->GetThreadState(JVMTI, thr, &thr_st_ptr);

  if ( thr_st_ptr & JVMTI_THREAD_STATE_RUNNABLE ) {

  printf("Thread: %s is Runnable/n", ((info.name==NULL) ? "" : info.name));

  flag = 1;

  }

 

 

 

  使用 JVMTI 显示 JVM 中的所有用户线程

 

 

 

 

  JVMTI 函数 GetAllThreads 用于显示 JVM 已知的所有活动线程。这些线程是关联到 VM 的 Java 编程语言线程。

  以下代码对此进行了说明:

 

  /* Get All Threads */

  err = (*JVMTI)->GetAllThreads(JVMTI, &thr_count, &thr_ptr);

  if (err != JVMTI_ERROR_NONE) {

  printf("(GetAllThreads) Error expected: %d, got: %d/n", JVMTI_ERROR_NONE, err);

  describe(err);

  printf("/n");

  }

  if (err == JVMTI_ERROR_NONE && thr_count >= 1) {

  int i = 0;

  printf("Thread Count: %d/n", thr_count);

  for ( i=0; i < thr_count; i++) {

  /* Make sure the stack variables are garbage free */

  (void)memset(&info1,0, sizeof(info1));

  err1 = (*JVMTI)->GetThreadInfo(JVMTI, thr_ptr[i], &info1);

  if (err1 != JVMTI_ERROR_NONE) {

  printf("(GetThreadInfo) Error expected: %d, got: %d/n", JVMTI_ERROR_NONE, err1);

  describe(err1);

  printf("/n");

  }

  printf("Running Thread#%d: %s, Priority: %d, context class loader:%s/n", i+1,info1.name,

  info1.priority,(info1.context_class_loader == NULL ? ": NULL" : "Not Null"));

  /* Every string allocated by JVMTI needs to be freed */

  err2 = (*JVMTI)->Deallocate(JVMTI, (void*)info1.name);

  if (err2 != JVMTI_ERROR_NONE) {

  printf("(GetThreadInfo) Error expected: %d, got: %d/n", JVMTI_ERROR_NONE, err2);

  describe(err2);

  printf("/n");

  }

  }

  }

 

 

 

  这将在您的终端上产生以下输出:

 

  Thread Count: 5

  Running Thread#1: MyThread4, Priority: 5, context class loader:Not Null

  Running Thread#2: Signal Dispatcher, Priority: 10, context class loader:Not Null

  Running Thread#3: Finalizer, Priority: 8, context class loader:: NULL

  Running Thread#4: Reference Handler, Priority: 10, context class loader:: NULL

  Running Thread#5: main, Priority: 5, context class loader:Not Null

 

 

 

  获取 JVM 线程堆栈跟踪

 

 

 

 

  JVMTI 接口 GetStackTrace 可用于获取关于线程堆栈的信息。如果 max_count 小于堆栈的深度,最深框架的 max_count 数将返回,否则返回整个堆栈。调用此函数无需挂起线程。

  下例产生至多 5 个最深框架。如果存在任何框架,则还将输出当前执行的方法名。

 

  /* Get Stack Trace */

  err = (*JVMTI)->GetStackTrace(JVMTI, thr, 0, 5, &frames, &count);

  if (err != JVMTI_ERROR_NONE) {

  printf("(GetThreadInfo) Error expected: %d, got: %d/n", JVMTI_ERROR_NONE, err);

  describe(err);

  printf("/n");

  }

  printf("Number of records filled: %d/n", count);

  if (err == JVMTI_ERROR_NONE && count >=1) {

  char *methodName;

  methodName = "yet_to_call()";

  char *declaringClassName;

  jclass declaring_class;

  int i=0;

  printf("Exception Stack Trace/n");

  printf("=====================/n");

  printf("Stack Trace Depth: %d/n", count);

  for ( i=0; i < count; i++) {

  err = (*JVMTI)->GetMethodName

  (JVMTI, frames[i].method, &methodName, NULL, NULL);

  if (err == JVMTI_ERROR_NONE) {

  err = (*JVMTI)->GetMethodDeclaringClass(JVMTI, frames[i].method, &declaring_class);

  err = (*JVMTI)->GetClassSignature(JVMTI, declaring_class, &declaringClassName, NULL);

  if (err == JVMTI_ERROR_NONE) {

  printf("at method %s() in class %s/n", methodName, declaringClassName);

  }

  }

  }

  }

 

 

 

  这将使您的终端产生以下输出:

 

  Number of records filled: 3

  Thread Stack Trace

  =====================

  Stack Trace Depth: 3

  at method throwException() in class LmyThread;

  at method run() in class LMyThread;

  at method run() in class LJava/lang/Thread;

 

 

 

  使用 JVMTI 分析堆

  本节介绍如何获取关于使用堆的信息的示例代码。例如,我们已经按“代理初始化”一节中所述为 VM Object Allocation 事件进行了注册。当 JVM 分配了 Java 编程语言可见但其他工具机制不能探测到的对象时,我们将得到通知。这一点与 JVMPI 截然不同,JVMPI 在分配任何对象时都将发送事件。在 JVMTI 中,针对用户分配的对象不会发送任何事件,因为它期望使用的是字节码工具。例如,在 SimpleThread.Java 程序中,分配 MyThread 或 Thread 对象时,我们是不会得到通知的。以后将单独发表一篇文章,描写如何使用字节码工具获取此信息。

  VM Object Allocation 事件对于确定有关由 JVM 分配的对象的信息十分有用。在 Agent_OnLoad 方法中,我们将 callbackVMObjectAlloc 注册为发送 VM Object Allocation 事件时调用的函数。回调函数参数包含关于已分配对象的信息,如对象类和对象大小的 JNI 本地参考。借助于 jclass 参数 object_klass,我们可以使用 GetClassSignature 函数获取关于类名的信息。我们可以把下面给出的对象类及其大小打印出来。注意避免过多的输出,我们仅需输出超过 50 个字节的对象信息就行了。

 

  /* Callback function for VM Object Allocation events */

  static void JNICALL callbackVMObjectAlloc

  (JVMTIEnv *JVMTI_env, JNIEnv* jni_env, jthread thread,

  jobject object, jclass object_klass, jlong size) {

  ...

  char *className;

  ...

  if (size > 50) {

  err = (*JVMTI)->GetClassSignature(JVMTI, object_klass, &className, NULL);

  if (className != NULL) {

  printf("/ntype %s object allocated with size %d/n", className, (jint)size);

  }

  ...

 

 

 

  我们使用JVMTI.html#obtaining" />上面所介绍的 GetStackTrace 方法来输出正在分配该对象的线程的堆栈跟踪。我们依照该节所述获取指定深度的 框架。这些框架将作为 JVMTIFrameInfo 结构返回,这些结构包含每个框架的 jmethodID(即 frames[x].method)。GetMethodName 函数可以将 jmethodID 映射到特殊的方法名中。在此示例的最后部分,我们还将使用 GetMethodDeclaringClass 和 GetClassSignature 函数获取从其中调用过此方法的类的名称。

 

  char *methodName;

  char *declaringClassName;

  jclass declaring_class;

  JVMTIError err;

  //print stack trace

  JVMTIFrameInfo frames[5];

  jint count;

  int i;

  err = (*JVMTI)->GetStackTrace(JVMTI, NULL, 0, 5, &frames, &count);

  if (err == JVMTI_ERROR_NONE && count >= 1) {

  for (i = 0; i < count; i++) {

  err = (*JVMTI)->GetMethodName(JVMTI, frames[i].method, &methodName, NULL, NULL);

  if (err == JVMTI_ERROR_NONE) {

  err = (*JVMTI)->GetMethodDeclaringClass(JVMTI, frames[i].method, &declaring_class);

  err = (*JVMTI)->GetClassSignature(JVMTI, declaring_class, &declaringClassName, NULL);

  if (err == JVMTI_ERROR_NONE) {

  printf("at method %s in class %s/n", methodName, declaringClassName);

  }

  }

  }

  }

  ...

 

 

 

  注意,完成任务时应释放由这些函数分配给 char 数组的内存:

 

  err = (*JVMTI)->Deallocate(JVMTI, (void*)className);

  err = (*JVMTI)->Deallocate(JVMTI, (void*)methodName);

  err = (*JVMTI)->Deallocate(JVMTI, (void*)declaringClassName);

  ...

 

 

 

  此代码的输出如下所示:

 

  type LJava/lang/reflect/Constructor; object allocated with size 64

  at method getDeclaredConstructors0 in class LJava/lang/Class;

  at method privateGetDeclaredConstructors in class LJava/lang/Class;

  at method getConstructor0 in class LJava/lang/Class;

  at method getDeclaredConstructor in class LJava/lang/Class;

  at method run in class LJava/util/zip/ZipFile$1;

 

 

 

  原始类的返回名称是相应原始类型的签名字符类型。例如,Java.lang.Integer.TYPE 为“I”。

  在 VM Object Allocation 的回调方法中,我们仍将使用 IterateOverObjectsReachableFromObject 函数演示如何获取关于堆的附加信息。在此示例中,我们将 JNI 参考作为一个参数传递给刚刚分配的对象,该函数将在此新分配对象所能直接或间接到达的所有对象中迭代。对于每个可到达的对象,另外还有一个定义的回调函数可对其进行描述。在此示例中,传递到 IterateOverObjectsReachableFromObject 的回调函数名为 reference_object:

 

  err = (*JVMTI)->IterateOverObjectsReachableFromObject

  (JVMTI, object, &reference_object, NULL);

  if ( err != JVMTI_ERROR_NONE ) {

  printf("Cannot iterate over reachable objects/n");

  }

  ...

 

 

 

  reference_object 函数定义如下:

 

  /* JVMTI callback function. */

  static JVMTIIterationControl JNICALL

  reference_object(JVMTIObjectReferenceKind reference_kind,

  jlong class_tag, jlong size, jlong* tag_ptr,

  jlong referrer_tag, jint referrer_index, void *user_data)

  {

  ...

  return JVMTI_ITERATION_CONTINUE;

  }

  ...

 

 

 

  在此示例中,我们使用 IterateOverObjectsReachableFromObject 函数计算新分配对象所能到达的所有对象的 总的大小,以及它们的对象类型。对象类型可以从 reference_kind 参数中确定。然后打印此信息以接收如下输出:

 

  This object has references to objects of combined size 21232

  This includes 45 classes, 9 fields, 1 arrays, 0 classloaders, 0 signers arrays,

  0 protection domains, 19 interfaces, 13 static fields, and 2 constant pools.

 

 

 

  注意,位于 JVMTI 中的类似迭代函数允许迭代的对象有:整个堆(可到达的和不可到达的);根目录对象和根目录对象所能直接或间接到达的所有对象;堆中 是指定类的实例的所有对象。使用这些函数的技巧和前面所介绍的类似。在执行这些函数期间,堆的状态没有任何变化:没有分配任何对象,没有对任何对象进行垃圾收集,并且对象的状态(包括堆值)也没有任何变化。结果,执行 Java 编程语言代码的线程、尝试恢复执行 Java 编程语言代码的线程和尝试执行 JNI 函数的线程都完全停了下来。所以,在对象参考回调函数中,不能使用任何 JNI 函数;在没有特别允许的情况下,也不允许使用任何 JVMTI 函数。

  编译和执行示例代码

  要编译并运行这里描述的示例应用程序的代码,请按以下步骤操作:

  设置 JDK_PATH 为指向 J2SE 1.5 发行版

 

  JDK_PATH="/home/xyz/j2sdk1.5.0/bin"

 

 

 

  使用 C 语言编译器构建共享库。我们使用的是 Sun Studio 8 C 编译器。

 

  CC="/net/compilers/S1Studio_8.0/SUNWspro/bin/cc"

  echo "...creating liba.so"

  ${CC} -G -KPIC -o liba.so

  -I${JDK_PATH}/include -I${JDK_PATH}/include/solaris a.c

 

 

 

  要加载并运行代理库,请在 VM 启动过程中使用下面的命令行参数之一。

 

  -agentlib:<JVMTI-agent-library-name>

  -agentpath:/home/foo/JVMTI/<JVMTI-agent-library-name>

 

 

 

  然后如下运行示例 Java 应用程序:

 

  echo "...creating SimpleThread.class"

  ${JDK_PATH}/bin/Javac -g -d . SimpleThread.Java

  echo "...running SimpleThread.class"

  LD_LIBRARY_PATH=. CLASSPATH=. ${JDK_PATH}/bin/Java

  -showversion -agentlib:a SimpleThread

 

 

 

  注意:此示例代理代码是在 Solaris 9 Operating System 上构建和测试的。

  结束语

  在本文中,我们演示了 JVMTI 提供用于监控和管理 JVM 的一些接口。JVMTI 规范 (JSR-163) 旨在为需要访问 VM 状态的广泛的工具提供一个 VM 接口,这些工具包括但不限于:分析、调试、监控、线程分析和覆盖率分析工具。

  建议开发人员不要使用 JVMPI 接口开发工具或调试实用工具,因为 JVMPI 是一种不受支持的实验技术。应考虑使用 JVMTI 编写 Java 虚拟机的所有

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>