<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>{Complexity} &#187; JVMTI</title>
	<atom:link href="http://technote.wsjoung.com/tag/jvmti/feed/" rel="self" type="application/rss+xml" />
	<link>http://technote.wsjoung.com</link>
	<description>simple note</description>
	<lastBuildDate>Wed, 09 Nov 2011 02:12:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>The Java Virtual Machine Tool Interface (JVMTI)</title>
		<link>http://technote.wsjoung.com/2006/11/17/the-java-virtual-machine-tool-interface-jvmti/</link>
		<comments>http://technote.wsjoung.com/2006/11/17/the-java-virtual-machine-tool-interface-jvmti/#comments</comments>
		<pubDate>Fri, 17 Nov 2006 01:03:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[JVMTI]]></category>

		<guid isPermaLink="false">http://wsjoung.wordpress.com/2006/11/17/the-java-virtual-machine-tool-interface-jvmti/</guid>
		<description><![CDATA[JVMTI is a new native interface available in J2SDK1.5.0. JVMPI is still also available though, SUN plan to drop JVMPI soon or later. &#8220;JVMTI provides for all the functional capabilities of the previous native interfaces JVMDI and JVMPI. However, it does not have many of the limitations of those older interfaces. Some of the JVMPI [...]]]></description>
			<content:encoded><![CDATA[<p>JVMTI is a new native interface available in J2SDK1.5.0. JVMPI is still also available though, SUN plan to drop JVMPI soon or later.</p>
<p>&#8220;JVMTI provides for all the functional capabilities of the previous native interfaces JVMDI and JVMPI. However, it does not have many of the limitations of those older interfaces. Some of the JVMPI capabilities require the use of JVMTI and the technique called Byte Code Insertion (<a href="http://java.sun.com/developer/technicalArticles/Programming/jvmpitransition/index.html#BCI">BCI</a>), sometimes referred to as Byte Code Injection or Byte Code Instrumentation. JVMTI allows for all JVM functionality to continue to operate, like JIT or JVM compilation and different GC implementations. Certain JVMTI features are controlled by asking for JVMTI Capabilities, and some of these can cause changes in JVM performance, but most features are available while the JVM is running &#8220;full speed&#8221;. All JVMTI object handles are <a href="http://java.sun.com/j2se/1.5.0/docs/guide/jni">JNI</a> handles, and JVMTI Event callbacks always include a <code>JNIEnv*</code> argument to facilitate JNI usage. Multiple JVMTI agents can operate in a single JVM and all interfaces return an error code to determine success or failure of the request. It is the intention of JVMTI to displace both JVMPI and JVMDI, and to ultimately be the single native tool interface into the JVM. &#8221; <a href="http://java.sun.com/developer/technicalArticles/Programming/jvmpitransition/index.html">The JVMPI Transition to JVMTI</a></p>
<p>There is my sample code.</p>
<p>simpleAgent.c</p>
<p>#include<br />
#include<br />
#include<br />
#include &#8220;jvmti.h&#8221;</p>
<p>static jvmtiEnv *jvmti = NULL;<br />
static jvmtiCapabilities capa;</p>
<p>//****** Implementation from here ******//</p>
<p>static void<br />
check_jvmti_error(jvmtiEnv *jvmti, jvmtiError errnum, const char *str)<br />
{<br />
if ( errnum != JVMTI_ERROR_NONE ) {<br />
char       *errnum_str;</p>
<p>errnum_str = NULL;<br />
(void)(*jvmti)-&gt;GetErrorName(jvmti, errnum, &amp;errnum_str);</p>
<p>printf(&#8220;ERROR: JVMTI: %d(%s): %s\n&#8221;, errnum, (errnum_str==NULL?&#8221;Unknown&#8221;:errnum_str), (str==NULL?&#8221;":str));<br />
}<br />
}</p>
<p>static void JNICALL<br />
callbackVMInit(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread)<br />
{<br />
printf(&#8220;JVMTI_EVENT_VM_INIT is captured\n&#8221;);<br />
}</p>
<p>static void<br />
JNICALL callbackVMDeath(jvmtiEnv *jvmti_env, JNIEnv* jni_env)<br />
{<br />
printf(&#8220;JVMTI_EVENT_VM_DEATH is captured\n&#8221;);<br />
}</p>
<p>static void JNICALL<br />
callbackVMObjectAlloc(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, jobject object, jclass object_klass, jlong size)<br />
{<br />
printf(&#8220;JVMTI_EVENT_VM_OBJECT_ALLOC is captured:\n&#8221;);<br />
}</p>
<p>/* Agent_OnLoad() is called first, we prepare for a VM_INIT event here. */<br />
JNIEXPORT jint JNICALL<br />
Agent_OnLoad(JavaVM *jvm, char *options, void *reserved)<br />
{</p>
<p>jint                res;<br />
jvmtiEventCallbacks callbacks;<br />
jvmtiError          error;</p>
<p>res = (*jvm)-&gt;GetEnv(jvm, (void **) &amp;jvmti, JVMTI_VERSION);</p>
<p>if (res != JNI_OK || jvmti == NULL) {<br />
/* This means that the VM was unable to obtain this version of the<br />
* JVMTI interface, this is a fatal error.<br />
*/<br />
printf(&#8220;ERROR: Unable to access JVMTI Version 1 (0x%x),&#8221;<br />
&#8221; is your J2SE a 1.5 or newer version?&#8221;<br />
&#8221; JNIEnv&#8217;s GetEnv() returned %d\n&#8221;,<br />
JVMTI_VERSION_1, res);</p>
<p>}</p>
<p>memset(&amp;capa, 0, sizeof(jvmtiCapabilities));<br />
error = (*jvmti)-&gt;GetPotentialCapabilities(jvmti, &amp;capa);<br />
if (error == JVMTI_ERROR_NONE) {<br />
error = (*jvmti)-&gt;AddCapabilities(jvmti, &amp;capa);<br />
check_jvmti_error(jvmti, error, &#8220;Unable to get necessary JVMTI capabilities.&#8221;);<br />
}</p>
<p>memset(&amp;callbacks, 0, sizeof(callbacks));<br />
callbacks.VMInit = &amp;callbackVMInit; /* JVMTI_EVENT_VM_INIT */<br />
callbacks.VMDeath = &amp;callbackVMDeath; /* JVMTI_EVENT_VM_DEATH */<br />
callbacks.VMObjectAlloc = &amp;callbackVMObjectAlloc;/* JVMTI_EVENT_VM_OBJECT_ALLOC */</p>
<p>error = (*jvmti)-&gt;SetEventCallbacks(jvmti, &amp;callbacks, (jint)sizeof(callbacks));<br />
check_jvmti_error(jvmti, error, &#8220;Cannot set jvmti callbacks&#8221;);</p>
<p>error = (*jvmti)-&gt;SetEventNotificationMode(jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, (jthread)NULL);<br />
error = (*jvmti)-&gt;SetEventNotificationMode(jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, (jthread)NULL);<br />
error = (*jvmti)-&gt;SetEventNotificationMode(jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_OBJECT_ALLOC, (jthread)NULL);<br />
check_jvmti_error(jvmti, error, &#8220;Cannot set event notification&#8221;);</p>
<p>return JNI_OK;</p>
<p>}</p>
<p>/* Agent_OnUnload() is called last */<br />
JNIEXPORT void JNICALL<br />
Agent_OnUnload(JavaVM *vm)<br />
{<br />
}</p>
<p><a href="http://java.sun.com/j2se/1.5.0/docs/guide/jvmti/jvmti.html">JVMTI Reference</a></p>
]]></content:encoded>
			<wfw:commentRss>http://technote.wsjoung.com/2006/11/17/the-java-virtual-machine-tool-interface-jvmti/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

