如何测试agent程序

来源:互联网 发布:淘宝流量分配 编辑:程序博客网 时间:2024/05/29 15:16

我们已经有了agent,下面我们测试一下。测试需要有一个目标JVM,所以我们简单模拟一个,具体如下:

<code class="hljs cs 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-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> TestMain {    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">main</span>(String[] args) throws InterruptedException {        System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"JVMTI agent Test start"</span>);        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>) {            Thread.sleep(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1000</span>);            i++;        }    }}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul><div class="save_code tracking-ad" data-mod="popu_249" style="box-sizing: border-box; position: absolute; height: 60px; right: 30px; top: 5px; color: rgb(255, 255, 255); cursor: pointer; z-index: 2;"><a target=_blank target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"><img src="http://static.blog.csdn.net/images/save_snippets.png" style="border: none; box-sizing: border-box;" alt="" /></a></div>

死循环的目的在于不能让它结束,方便我们使用JVMTI agent进行操作。运行后结果如下:

JVMTI agent Test start

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

了解更多 AttachAPI看这里:[AttachAPI是什么][4]

<code class="hljs cs 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-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> TestAgent {    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">main</span>(String[] args) throws AttachNotSupportedException, IOException, AgentLoadException,            AgentInitializationException {        String pid = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"831"</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// java进程pid</span>        String agentPath = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/Users/sunjie/Desktop/libagent.so"</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// agent.so的路径</span>        String options = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 传入agent的参数</span>        VirtualMachine virtualMachine = com.sun.tools.attach.VirtualMachine.attach(pid);        virtualMachine.loadAgentPath(agentPath, options);        virtualMachine.detach();    }}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul><div class="save_code tracking-ad" data-mod="popu_249" style="box-sizing: border-box; position: absolute; height: 60px; right: 30px; top: 5px; color: rgb(255, 255, 255); cursor: pointer; z-index: 2;"><a target=_blank target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"><img src="http://static.blog.csdn.net/images/save_snippets.png" style="border: none; box-sizing: border-box;" alt="" /></a></div>

这里我们需要引入${JAVA_HOME}/lib/

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

JVMTI agent Test start 
cls sig=Ljava/lang/ClassLoader$NativeLibrary; 
cls sig=Ljava/util/concurrent/ConcurrentMap; 
cls sig=Ljava/lang/Error; 
cls sig=[Ljava/lang/Error; 
cls sig=Ljava/util/Set; 
cls sig=Ljava/util/WeakHashMap; 
cls sig=Ljava/lang/ref/Reference; 
cls sig=[Ljava/lang/ref/Reference; 
cls sig=Ljava/lang/StackOverflowError; 

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

注意,这里传入了*options参数:opt1,不过,我们的agent并没有使用他。

了解更多JVMTI的功能看这里:[JVMTI提供哪些功能][5]


0 0
原创粉丝点击