zoukankan      html  css  js  c++  java
  • VisualVM实现不中断程序和修改代码的前提下打印函数的入参和返回值

    在VisualVM中下载BTrace插件

    -w900

    安装此插件并激活

    写调试程序

    public class BTraceTest {
    
        public int add(int a, int b) {
            return a + b;
        }
    
        public static void main(String[] args) throws IOException {
            BTraceTest test = new BTraceTest();
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            for (int i=0; i < 10; i++) {
                reader.readLine();
                int a = (int) Math.round(Math.random() * 1000);
                int b = (int) Math.round(Math.random() * 1000);
                System.out.println(test.add(a, b));
            }
        }
    
    }
    

    运行并写在BTrace中写调试程序

    用VisualVM模式下进行调试

    -w1226

    找到运行的程序

    -w1190

    右击添加BTrace调试代码

    -w530

    -w1083

    /* BTrace Script Template */
    import com.sun.btrace.annotations.*;
    import static com.sun.btrace.BTraceUtils.*;
    
    @BTrace
    public class TracingScript {
    	/* put your code here */
      @OnMethod(
          clazz="com.baiyuliuguang.test.JVMTest.BTraceTest",
          method="add",
          location=@Location(Kind.RETURN)
        )
        public static void func(@Self com.baiyuliuguang.test.JVMTest.BTraceTest instance,int a, int b,@Return int result){
              
              println("调用堆栈:");
              jstack();
              println(strcat("方法参数A:", str(a)));
              println(strcat("方法参数B:", str(b)));
              println(strcat("方法结果:", str(result)));
            }
    }
    

    点击start开启调试

    -w1109

    运行结果

    -w1247

    -w1007

    可以对比看到运行结果与BTrace调试输出结果一致,但是我们没有增加任何源代码,只需要在BTrace中增加调试即可。

  • 相关阅读:
    TCP/IP 网路基础
    三、Django之请求与响应-Part 1
    二、Django快速安装
    Linux 优化详解
    缓存的正确使用方式
    HTML从入门到放弃
    Ansible开发之路
    程序猿数据库学习指南
    MySQL错误代码大全
    Python之网络编程
  • 原文地址:https://www.cnblogs.com/yantt/p/visualvm-shi-xian-bu-zhong-duan-cheng-xu-he-xiu-ga.html
Copyright © 2011-2022 走看看