为了安全,关闭一切无关选项。
阅读 这个( kailing pub/article/index/arcid/211.html),Arthas 是阿里巴巴最近开源的一款在线诊断java应用程序的工具,是 greys 工具的升级版本,深受开发者喜爱。当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决:
原理解析
然后 arthas 和其他诊断工具一样,都是先通过 attach 链接上目标应用,通过 instrument 动态修改应用程序的字节码达到不重启应用而监控应用的目的。
继续阅读 这里( lovestblog 国内网/blog/2014/06/18/jvm-attach/)
Attach 机制说简单点就是 jvm 提供一种 jvm 进程间通信的能力,能让一个进程传命令给另外一个进程,并让它执行内部的一些操作,比如说我们为了让另外一个 jvm 进程把线程 dump 出来,那么我们跑了一个 jstack 的进程,然后传了个 pid 的参数,告诉它要哪个进程进行线程 dump,既然是两个进程,那肯定涉及到进程间通信,以及传输协议的定义,比如要执行什么操作,传了什么参数等。
Attach 能做些什么?总结起来说,比如内存 dump,线程 dump,类信息统计(比如加载的类及大小以及实例个数等),动态加载 agent(使用过 btrace 的应该不陌生),动态设置 vm flag(但是并不是所有的 flag 都可以设置的,因为有些 flag 是在jvm启动过程中使用的,是一次性的),打印 vm flag,获取系统属性等,这些对应的源码 attachListener.cpp( github /corretto/corretto-8/blob/79d2ce063acaa09607109750115c3e5a33f1e4e3/src/hotspot/src/share/vm/services/attachListener.cpp) 如下
// names must be of length <= AttachOperation::name_length_max
static AttachOperationFunctionInfo funcs[] = {
{ "agentProperties", get_agent_properties },
{ "datadump", data_dump },
{ "dumpheap", dump_heap },
{ "load", JvmtiExport::load_agent_library },
{ "properties", get_system_properties },
{ "threaddump", thread_dump },
{ "inspectheap", heap_inspection },
{ "setflag", set_flag },
{ "printflag", print_flag },
{ "jcmd", jcmd },
{ NULL, NULL }
};
根据 文档( docs.oracle 商业网/javase/8/docs/technotes/tools/unix/java.html)
-XX:+DisableAttachMechanism
Enables the option that disables the mechanism that lets tools attach to the JVM. By default, this option is disabled, meaning that the attach mechanism is enabled and you can use tools such as jcmd, jstack, jmap, and jinfo.
根据 一篇博客( zeroproductionincidents.wordpress 商业网/tag/disable-dynamic-attach/)
Attach API can be disabled on your JVM by setting -XX:+DisableAttachMechanism Java property. When we set the options while starting the Tomcat ,
JAVA_OPTS="-XX:+DisableAttachMechanism"
下载验证包:curl -L -O https://alibaba.github.io/arthas/arthas-demo.jar
启动 Demo: java -jar arthas-demo.jar
启动 arthas: java -jar arthas-boot.jar(现行下载 curl -L -O https://alibaba.github.io/arthas/arthas-boot.jar),可以看到 arthas 可以 attach 成功。
$ java -jar arthas-boot.jar
[INFO] arthas-boot version: 3.1.1
[INFO] Found existing java process, please choose one and hit RETURN.
* [1]: 23769 arthas-demo.jar
1
[INFO] arthas home: /Users/bingoobjca/.arthas/lib/3.1.1/arthas
[INFO] Try to attach process 23769
[INFO] Attach process 23769 success.
[INFO] arthas-client connect 127.0.0.1 3658
,---. ,------. ,--------.,--. ,--. ,---. ,---.
/ O \ | .--. ''--. .--'| '--' | / O \ ' .-'
| .-. || '--'.' | | | .--. || .-. |`. `-.
| | | || |\ \ | | | | | || | | |.-' |
`--' `--'`--' '--' `--' `--' `--'`--' `--'`-----'
wiki https://alibaba.github.io/arthas
tutorials https://alibaba.github.io/arthas/arthas-tutorials
version 3.1.1
pid 23769
time 2019-05-29 15:01:24
同时,Demo 程序端,有如下打印:
Wed May 29 15:01:24 CST 2019 com.taobao.arthas.agent.ArthasClassloader@43d1f1b7 JM.Log:INFO Log root path: /Users/bingoobjca/logs/
Wed May 29 15:01:24 CST 2019 com.taobao.arthas.agent.ArthasClassloader@43d1f1b7 JM.Log:INFO Set arthas log path: /Users/bingoobjca/logs/arthas
重新启动 Demo java -XX:+DisableAttachMechanism -jar arthas-demo.jar
重新启动 arthas,可以看到,attach 失败
$ java -jar arthas-boot.jar
[INFO] arthas-boot version: 3.1.1
[INFO] Found existing java process, please choose one and hit RETURN.
* [1]: 24086 arthas-demo.jar
1
[INFO] arthas home: /Users/bingoobjca/.arthas/lib/3.1.1/arthas
[INFO] Try to attach process 24086
[ERROR] Start arthas failed, exception stack trace:
com.sun.tools.attach.AttachNotSupportedException: The VM does not support the attach mechanism
at sun.tools.attach.HotSpotAttachProvider.testAttachable(HotSpotAttachProvider.java:162)
at sun.tools.attach.BsdAttachProvider.attachVirtualMachine(BsdAttachProvider.java:61)
at com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:208)
at com.taobao.arthas.core.Arthas.attachAgent(Arthas.java:73)
at com.taobao.arthas.core.Arthas.<init>(Arthas.java:28)
at com.taobao.arthas.core.Arthas.main(Arthas.java:113)
[ERROR] attach fail, targetPid: 24086
export JAVA_OPTS="-XX:+DisableAttachMechanism"
java $JAVA_OPTS -jar arthas-demo.jar

