javaagent

instrumentation 功能介绍(javaagent)

浪尽此生 提交于 2019-11-29 13:04:42
Instrumentation JDK中对它介绍如下:这个类为JVM上运行时的程序提供测量手段。很多工具通过Instrumenation 修改方法字节码 实现收集数据目的。这些通过Instrumentaion搜集数据的工具不会改变程序的状态和行为。这些良好的工具包括 monitoring agents , ,profilers, coverage analyzers, 和 event loggers。 有两种方式来获取Instrumentation接口实例: 启动JVM时指定agent类。这种方式,Instrumentation的实例通过agent class的premain方法被传入。 JVM提供一种当JVM启动完成后开启agent机制。这种情况下,Instrumention实例通过agent代码中的的agentmain传入。 java agent 在JDK package specification中解释:一个agent 是被作为Jar 文件形式来部署的。在Jar文件中manifest中指定哪个类作为agent类。具体的实现包括 通过命令行直接指定选项开启agent,也支持JVM启动程序后,通过工具attach到该程序上。 下面通过例子来说明javaagent + Instrumentation的用法。 通过在程序启动前 preagent方式 :