arthas进阶使用-1
本篇主要记录arthas除了watch、trace、stack、sc、thread等常用命令之外的用法。
为什么琢磨这些“高级”用法,源于我在阅读一些其他代码(或者定位问题时),我要分析诊断初始化过程,包括main方法刚刚启动时,如果是传统的attach玩法,main方法早已跑了多远下去了。好在arthas本身就支持agent模式,我改了两行代码加挖掘了他一些用法达成了我的目的,现记录如下。
使用指定本地版本的arthas
通常我们在用java -jar arthas-boot.jar时,他会自动从远程他的仓库下载他新的版本,那么此时如果自己有些修改编译后的版本,就不能一直使用。 此问题可以指定version搞定:
java -jar arthas-boot.jar --use-version 3.5.0
此种方式可以指定使用特定的arthas版本,避免其自动从远程服务器下载,覆盖你自己定制的arthas,版本对应的lib在如下路径查找
~/.arthas/lib/3.5.0/arthas
同时该路径下的arthas.properties文件 可以配置http port等,http port也可以配置成0,这样http port监听在随机端口,具体可以在日志中找到。这样可以达成一个机器上跑多个的目的。
java agent模式
arthas天生支持,-javaagent:/Users/simon/.arthas/lib/3.5.0/arthas/arthas-agent.jar,这种模式用到的配置文件(properties)跟agent lib在一个目录下。
如何在执行main方法前停住
我希望在执行main方法前停住,因为我想在执行main方法前注入一些我的诊断操作。
修改com.taobao.arthas.agent334.AgentBootstrap.main(String, Instrumentation)代码实现:
System.out.println("wait init diag operation...");
System.in.read();
这样在你要诊断的程序的控制台出现wait init diag operation…时,你就去准备诊断操作,然后在控制台输入任意字符回车即可。当然这个办法有点粗糙。 打包编译,替换arthas-agent.jar。注意打出来的包是target下的arthas-agent-jar-with-dependencies.jar。
执行main方法前停住了,但是main方法所在类没加载watch不了怎么办
使用他的classloader指令把你需要的class先行load起来再去准备诊断操作
[arthas@26835]$ classloader -l
name loadedCount hash parent
BootstrapClassLoader 2274 null null
com.taobao.arthas.agent.ArthasClassloader@4dfcc858 1995 4dfcc858 sun.misc.Launcher$ExtClassLoader@617e76e0
sun.misc.Launcher$AppClassLoader@18b4aac2 3 18b4aac2 sun.misc.Launcher$ExtClassLoader@617e76e0
sun.misc.Launcher$ExtClassLoader@617e76e0 45 617e76e0 null
classloader -c 18b4aac2 --load com.code260.ss.demo.ArthasTestMain
watch -f com.code260.ss.demo.ArthasTestMain test {params,returnObj} -x 2
诊断时输出数据太多
可以使用options save-result true 打开日志,日志会存放在/Users/simon/logs/arthas-cache下。
如果是线上环境,注意及时关闭,不要把磁盘打爆了。
也可以使用 >> 重定向。
本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!