ava千百问_08JDK详解(014)_如何编写JVMTI agent程序

1、如何编写JVMTI agent程序

了解JPDA看这里:JPDA是什么 
了解JVMTI看这里:JVMTI是什么 
我们需要使用C++编写agent程序,JVM在不同时机回调下面的接口函数:

<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*jvm</span>, char <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*options</span>, void <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*reserved</span>)

JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*jvm</span>, char <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*options</span>, void <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*reserved</span>);

JNIEXPORT void JNICALL Agent_OnUnload(JavaVM <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*jvm</span>)</code><code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">
</code><code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">http://blog.tianya.cn/post-7268325-116516510-1.shtml
http://www.xici.net/d231821300.htm
http://club.mil.news.sohu.com/man/thread/47be5m612q5
http://club.mil.news.sohu.com/man/thread/47beaywlh4x
http://www.xici.net/d231824527.htm
</code><code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"></code><p style="margin-top: 0px; margin-bottom: 1.1em; padding-top: 0px; padding-bottom: 0px; box-sizing: border-box; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 14px; line-height: 35px;">给出<span style="color:#ff0000;box-sizing: border-box;">运行加载模式</span>实现的一个简单功能:打印jvm内所有已经装载成功的class。具体如下:</p><pre class="prettyprint" name="code" style="white-space: nowrap; word-wrap: break-word; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro', monospace; padding: 5px 5px 5px 60px; font-size: 14px; line-height: 1.45; word-break: break-all; color: rgb(51, 51, 51); border: 1px solid rgba(128, 128, 128, 0.0745098); border-radius: 0px; background-color: rgba(128, 128, 128, 0.0470588);"><code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">/*
*  JVMTI agent
*
*  Created on: 2016-07-02
*      Author: sunjie
*/</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <jvmti.h></span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <string></span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <cstring></span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <iostream></span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <list></span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <map></span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <set></span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <stdlib.h></span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <jni_md.h></span></code>
http://blog.tianya.cn/post-7268325-116516510-1.shtml
http://www.xici.net/d231821300.htm
http://club.mil.news.sohu.com/man/thread/47be5m612q5
http://club.mil.news.sohu.com/man/thread/47beaywlh4x
http://www.xici.net/d231824527.htm
jvmtiEnv *jvmti; jint result = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1); if (result != JNI_OK) { printf("ERROR: Unable to access JVMTI!\n"); } jvmtiError err = (jvmtiError) 0; jclass *classes; jint count; err = jvmti->GetLoadedClasses(&count, &classes);//获取class if (err) { printf("ERROR: JVMTI GetLoadedClasses failed!\n"); } for (int i = 0; i < count; i++) { char *sig;

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值