问题描述
我正在寻找一种方法,通过使用 Java 代理或检测类(最好是比用户类更低级别的东西)来拦截 JVM 中的所有对象创建(new 或任何其他创建对象的方法),有一个 类似问题 不关注 Java代理或低于检测用户类的东西
Java 对象可以通过多种不同的方式创建.
- 从 Java 代码,当解释或编译的 Java 方法执行以下字节码指令之一时:
新,newarray,anewarray,multianewarray. - 从本机代码,当本机方法(包括标准类库中的方法)调用 JNI 函数之一时:
NewObject,NewObjectArray,NewStringUTF,NewDirectByteBuffer等 - 直接从VM运行时,当JVM内部创建新对象时,例如响应
Object.clone(),Throwable.getStackTrace()、Class.getInterfaces()等
不幸的是,没有一个点可以让您从所有这些来源收集对象.但是,有办法拦截所有这些.
从 Java 实例化的对象可以被 仪表代理.代理需要定义一个ClassFileTransformer这将扫描所有已加载类的字节码以获取对象创建指令并对其进行修改.
注意:不需要拦截所有的
new指令,您可以使用Object()构造函数代替.但是你仍然需要拦截数组分配指令.JNI 函数可以被 JVMTI 代理拦截.您需要为
NewObjectArray、NewStringUTF等定义自己的原生钩子,然后替换 JNI 函数表.有关详细信息,请参阅 JVMTI 参考.p>VM 创建的对象可以被 JVMTI 事件回调机制.所需的事件是 VMObjectAlloc.
注意:JVM 不会为从 Java 或 JNI 函数分配的对象发布
VMObjectAlloc事件.
对象实例化的所有其他方式(克隆、反射、反序列化)都属于上述类别之一.
从 Oracle 获取 JDK 8 演示和示例 Java SE 下载 网站.
对于这个问题,有一个示例 JVMTI 代理.
往下看
jvmti/heapTrackerjvmti/hprof
I am looking towards some approach where by using Java agent or instrumenting classes (preferably something at lower level than user classes) to intercept all object creation in JVM (new or any alternative ways to create Object), There is a similar question which doesn't focus on Java agent or something lower than instrumenting user classes
Java Objects can be created in several different ways.
- From Java code, when a Java method, either interpreted or compiled, executes one of the following bytecode instructions:
new,newarray,anewarray,multianewarray. - From native code, when native methods, including those in standard class library, call one of JNI functions:
NewObject,NewObjectArray,NewStringUTF,NewDirectByteBuffer, etc. - Directly from VM runtime, when a new object is created internally by JVM, for example, in response to
Object.clone(),Throwable.getStackTrace(),Class.getInterfaces(), etc.
Unfortunately, there is no single point where you can collect objects from all these sources. However, there are means for intercepting all of them.
Objects instantiated from Java can be caught by an Instrumentation agent. The agent needs to define a ClassFileTransformer that will scan the bytecode of all loaded classes for object-creating instructions and modify it.
Note: there is no need to intercept all
newinstructions, you can instrumentObject()constructor instead. But you still need to intercept array allocation instructions.JNI functions can be intercepted by JVMTI agent. You need to define your own native hooks for
NewObjectArray,NewStringUTFetc. and then replace JNI function table. See JVMTI Reference for the details.Objects created by the VM can be caught by JVMTI Event Callback mechanism. The desired event is VMObjectAlloc.
Note: JVM will not post
VMObjectAllocevent for objects allocated from Java or by JNI functions.
All other ways of object instantiation (cloning, reflection, deserialization) fall into one of the above categories.
Get JDK 8 Demos and Samples from Oracle Java SE Downloads website.
There is a sample JVMTI agent for exactly this question.
Look under
jvmti/heapTrackerjvmti/hprof
这篇关于如何在Java中拦截低于用户类级别的对象创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)