JDI,Java字节码工具和Java代理(JWDP,JVMTI) [英] JDI, Java Byte code instrumentation and Java agents (JWDP, JVMTI)

查看:433
本文介绍了JDI,Java字节码工具和Java代理(JWDP,JVMTI)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是调试器,工具和JVMTI领域的新手. 因此,我对它们的问题很少.

I am new in the realm of debuggers, instrumentation and JVMTI. So I have few questions about them.

  1. JDI(java调试器接口),JWDP,javaagent和本机代理(JVMTI)之间有什么区别. 以及Java工具API在图片中的位置.

  1. What is the difference between JDI(java debugger interface), JWDP, javaagent and native agent(JVMTI). and where does the java instrumentation API fit in picture.

我正在使用JDI拦截目标Java应用程序中的异常. 但是我发现,如果我们谈论JDI对目标应用程序性能的影响,JDI还不够好. 我读到,大多数优秀的应用程序都是通过将JVMTI与字节码检测结合起来来实现的.但是我不明白如何将字节码检测与JVMTI一起使用. 那么,我们如何与JVMTI一起进行字节码检测呢? 任何示例都会有所帮助.

I am using JDI to intercept exceptions in target java application. but I found out that JDI is not good enough if we talk about how it impacts the performance of target app. I read that most good applications does this with combining JVMTI with byte code instrumentation. But I can't understand how byte code instrumentation can be used with JVMTI. So, how can we do byte code instrumentation along with JVMTI ? any example will be helpful.

我们可以在Java中同时检测字节码和机器码吗?

Can we instrument both byte code and machine code in java ?

可以将静态字节码分析与JVMTI一起使用.如果是,那怎么办?

Can static byte code analysis be used along with JVMTI. If yes then how ?

如果有任何疑问或不对,请通知我.

If any question is irrelavent or wrong, let me know.

推荐答案

1-我认为该站点很好地解释了这一区别:

1 - I think that this site explains the distinction pretty well: http://docs.oracle.com/javase/1.5.0/docs/guide/jpda/architecture.html - these are basically 3 layers of abstraction built on top of each other, with JVMTI interfacing directly with the running JVM, then JDWP being used as a communication protocol, then JDI as an interface to that remote JVM. You can use a javaagent to perform bytecode instrumentation (orthogonal to the implementation of these 3 things).

2-我认为执行此操作最有效的方法是检测所有代码,以在每个方法中添加try/catch来处理异常-当捕获到异常时,您可以处理它(无论您想如何处理) ,然后将其重新抛出.进行检测的最简单方法是使用javaagent方法(然后使用javaassist或asm或其他方法).您也可以通过JVMTI来检测字节码,但是它麻烦得多.如果您只关心特定的异常(即,明确抛出的异常,而不是解释器内部抛出的异常,例如NullPointerException,ArrayIndexOutOfBoundsException等),那么处理这些异常的最简单方法就是拦截ATHROW指令(该指令用于引发异常).我没有特定经验,但是创建为异常事件,但是我不确定它的性能(可能与您的JDI方法一样慢,可能会更好).

2 - I think that the most performant way to do this would be to instrument all code to add a try/catch in each method to handle an exception - when the exception is caught, you handle it (however you wanted to), then re-throw it. The easiest way to do instrumentation is with the javaagent approach (then using javaassist or asm or whatever). You can instrument bytecode from JVMTI too, but it is much more cumbersome. If you only care about specific exceptions (namely, those that are explicitly thrown, and not those that are thrown internally by the interpreter, like NullPointerException, ArrayIndexOutOfBoundsException etc), then the easiest way to handle these would be to intercept ATHROW instructions (the instruction used to throw an exception). I have no specific experience, but it might be reasonable to create a JVMTI agent that is registered for the Exception event, but I am not sure of its performance (could be just as slow as your JDI approach, could be better).

3-否:您只能检测以Java运行的字节码.如果您想检测机器代码,则可以尝试使用别针之类的方法来完成,但是我认为这可能与您正在寻找的东西格格不入.

3 - No: you can only instrument bytecode running in Java. If you wanted to instrument the machine code, you could try to do that with something like pin, but I think this is probably getting way out of hand for what you are looking for.

4-当然:您对哪种静态分析感兴趣?当然,您可以使用烟灰之类的东西,也可以使用JVMTI.

4 - Sure: what sort of static analysis are you interested in? You could certainly use something like soot, and also use JVMTI.

这篇关于JDI,Java字节码工具和Java代理(JWDP,JVMTI)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆