arthas原理系列文章:
前言
在深入到 Arthas 的原理之前我们先看一个有趣的示例,我们依然使用前面文章中用到的代码示例,
public class DemoApplication {
public static void main(String[] args) {
for (int i = 0; i < Integer.MAX_VALUE; i++) {
System.out.println("times:" + i + " , result:" + testExceptionTrunc());
}
}
public static boolean testExceptionTrunc() {
try {
// 人工构造异常抛出的场景
((Object)null).getClass();
} catch (Exception e) {
if (e.getStackTrace().length == 0) {
try {
// 堆栈消失的时候当前线程休眠5秒,便于观察
Thread.sleep(5000);
} catch (InterruptedException interruptedException) {
// do nothing
}
return true;
}
}
return false;
}
}
运行这段代码,然后我们使用 Arthas 的tt命令记录testExceptionTrunc的每次调用的情况
tt -t com.idealism.demo.DemoApplication testExceptionTrunc
再新开一个窗口,打开 Arthas,使用dump命令把正在运行的字节码输出到本地文件后查看此时的字节码

本文介绍了如何利用JVM的Attach机制实现一个简单的watch命令,类似Arthas的功能,通过代码插装统计目标方法运行时间。讲解了JVM Attach的原理,并展示了使用javassist库进行字节码操作的过程。
最低0.47元/天 解锁文章
2455

被折叠的 条评论
为什么被折叠?



