Arthas底层bytekit

    public static void main(String[] args) throws Throwable {
        Sample sample = new Sample();
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        TimeUnit.SECONDS.sleep(3);
                        int rand = RandomUtils.nextInt(0, 2);
                        String result = sample.hello("test", rand == 0);
                        System.out.println("call result: " + result);
                    } catch (Throwable e) {
                    }
                }
            }
        });
        t.start();

        AgentUtils.install();

        // 解析定义的 Interceptor类 和相关的注解
        DefaultInterceptorClassParser interceptorClassParser = new DefaultInterceptorClassParser();
        List<InterceptorProcessor> processors = interceptorClassParser.parse(SampleInterceptor.class);

        // 加载字节码
        ClassNode classNode = AsmUtils.loadClass(Sample.class);

        // 对加载到的字节码做增强处理
        for (MethodNode methodNode : classNode.methods) {
            if (AsmUtils.isConstructor(methodNode)) {
                continue;
            }
            if (AsmUtils.isAbstract(methodNode)) {
                continue;
            }
            MethodProcessor methodProcessor = new MethodProcessor(classNode, methodNode);
            for (InterceptorProcessor interceptor : processors) {
                interceptor.process(methodProcessor);
            }
        }

        // 获取增强后的字节码
        byte[] bytes = AsmUtils.toBytes(classNode);

        // 通过 reTransform 增强类
        AgentUtils.reTransform(Sample.class, bytes);

        CountDownLatch countDownLatch = new CountDownLatch(1);
        countDownLatch.await();
    }
public class Sample {

    public String hello(String str, boolean exception) {
        if (exception) {
            throw new RuntimeException();
        }
        return "hello " + str;
    }
}
public class SampleInterceptor {

    @AtEnter(inline = true)
    public static void atEnter(@Binding.This Object target,
                               @Binding.Class Class<?> clazz,
                               @Binding.MethodName String method,
                               @Binding.Args Object[] args) {
        System.out.println(String.format("atEnter: %s %s", method, args));
    }

    @AtInvoke(inline = true, name = "atInvoke")
    public static void atInvoke(@Binding.This Object target,
                                @Binding.Class Class<?> clazz,
                                @Binding.Method Method method,
                                @Binding.Args Object[] args) {
        System.out.println(String.format("atInvoke: %s %s", method, args));
    }

    @AtExit(inline = true)
    public static void atExit(@Binding.This Object target,
                              @Binding.Class Class<?> clazz,
                              @Binding.MethodName String method,
                              @Binding.Args Object[] args,
                              @Binding.Return Object returnObj) {
        System.out.println(String.format("atExit: %s %s %s", method, args, returnObj));
    }

    @AtExceptionExit(inline = true)
    public static void atExceptionExit(@Binding.This Object target,
                                       @Binding.Class Class<?> clazz,
                                       @Binding.MethodName String method,
                                       @Binding.Args Object[] args,
                                       @Binding.Throwable Throwable throwable) {
        System.out.println(String.format("atExceptionExit: %s %s %s", method, args, throwable));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值