我正在尝试开发一个javaagent。它还提供了对注释的支持。
在preMain方法中,我试图扫描类路径中的注释。然后使用instrumentatino.addTransformer()方法添加转换器。
因为类已经在批注处理期间加载,所以转换不会发生(如果我删除批注处理代码并重新转换代码{参见下文},则一切正常)。
为了克服这个问题,我现在正在尝试重新转换类。与清单条目和在addTransformer方法中启用canRetransform标志相关的所有必要设置都已完成。
我的代码大致如下所示:
annotationProcessor.processAnnotation();
instrumentation.addTransformer(new DummyTransformer(), true);
try {
instrumentation.retransformClasses(instrumentation.getAllLoadedClasses());
} catch (UnmodifiableClassException e) {
e.printStackTrace();
}DummyTransformer不做任何事情。它只是按原样返回classBytes (我也尝试过在每次调用时返回null )
这里发生的情况是,我收到了这个错误
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x000000010c5254dd, pid=5912, tid=0x0000000000001603
#
# JRE version: OpenJDK Runtime Environment (8.0_282) (build 1.8.0_282-bre_2021_01_20_16_06-b00)
# Java VM: OpenJDK 64-Bit Server VM (25.282-b00 mixed mode bsd-amd64 compressed oops)
# Problematic frame:
# V [libjvm.dylib+0x5254dd] Symbol::as_C_string() const+0xd
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /some/obfuscated/path/hs_err_pid5912.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
#
zsh: abort java -cp build/libs/jvm-agent-0.1-all.jar 最初,我认为转换器运行不正常,但当我尝试使用DummyTransformer时,返回null或相同的bytes会产生类似的结果。
发布于 2021-02-04 14:38:28
问题是我试图使用instrumentation.getAllLoadedClasses()重新转换所有的类。相反,我根据包名过滤掉了我感兴趣的类,它只适用于一组有限的类。
也许有一些被加载的类不应该被重新转换。
https://stackoverflow.com/questions/66029984
复制相似问题