为了支持更好的分析数据,我希望我的 JVMTI 代理启用几个 JVM 标志。 有问题的代理人是Honest-Profiler并且它只能在启动时加载。
我想启用标志:-XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints
根据记录的问题 here如果标志没有打开,我们会得到一个有偏见的配置文件。最好不要警告用户而只启用标志。
最佳答案
至于DebugNonSafepoints
,您甚至不需要设置此标志。看debugInfoRec.cpp :
static inline bool compute_recording_non_safepoints() {
if (JvmtiExport::should_post_compiled_method_load()
&& FLAG_IS_DEFAULT(DebugNonSafepoints)) {
// The default value of this flag is taken to be true,
// if JVMTI is looking at nmethod codes.
// We anticipate that JVMTI may wish to participate in profiling.
return true;
}
// If the flag is set manually, use it, whether true or false.
// Otherwise, if JVMTI is not in the picture, use the default setting.
// (This is true in debug, just for the exercise, false in product mode.)
return DebugNonSafepoints;
}
如果不设置该标志,当 JVMTI CompiledMethodLoad 时仍会记录调试信息通知已启用。您只需请求 can_generate_compiled_method_load_events
功能并打开 JVMTI_EVENT_COMPILED_METHOD_LOAD
通知。
这正是我在 async-profiler
中处理它的方式项目。
没有安全的方法可以在运行时更改难以管理的 JVM 标志。但是,在 Linux 上有一个丑陋的 hack 可以做到这一点。
- 读取
/proc/self/maps
找到libjvm.so
的基地址。 - 使用ELF format reader在动态库中发现所需符号的偏移量。
- 直接写入这个符号的地址。
这里是 a sample code为了这个技巧。
关于java - JVMTI 代理如何在启动时设置 JVM 标志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37298962/