账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Java Agent 和 Javassist 进行修改字节码加入监控
    49
    0

    问题描述

    1、我们意图在实际操作之前加入一些字节码进行监控,例如对 慢sql, dubbo 并发 ,开发发布对时候对mybatis/ibatis对配置文件做检查

    问题出现的环境背景及自己尝试过哪些方法

    2、我们使用 Instrumentation 在启动的时候,将我们监控开发的 Agent jar 包导入即可,但是发现使用 javassist 的如下代码不起作用

            ClassPool pool = new ClassPool();//这一行之后就没有运行了,
            byte[] transformed = new byte[0];
            CtClass cl = null;
            try {
                cl = pool.get(className.replaceAll("/", "."));
                System.out.println("four");
                CtBehavior[] methods = cl.getDeclaredBehaviors();
                for (CtBehavior method : methods) {
                    if (!method.isEmpty()) {
                        doMethod(method);
                    }
                }
                transformed = cl.toBytecode();
            } catch (CannotCompileException | NotFoundException | IOException io) {
                io.printStackTrace();
            } finally {
                if (cl != null) {
                    cl.detach();
                }
            }
            return transformed;

    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>top.huzhurong.agent</groupId>
        <artifactId>test</artifactId>
        <version>1.0-SNAPSHOT</version>
    
    
        <dependencies>
            <dependency>
                <groupId>org.javassist</groupId>
                <artifactId>javassist</artifactId>
                <version>3.23.1-GA</version>
            </dependency>
        </dependencies>
        
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.6.1</version>
                    <configuration>
                        <target>1.8</target>
                        <source>1.8</source>
                        <encoding>utf-8</encoding>
                    </configuration>
                </plugin>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.3.1</version>
                    <configuration>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                            </manifest>
                            <manifestEntries>
                                <Premain-Class>test.Agent</Premain-Class>
                                <Boot-Class-Path>javassist-3.23.1-GA.jar</Boot-Class-Path>
                                <Can-Redefine-Classes>true</Can-Redefine-Classes>
                                <Can-Set-Native-Method-Prefix>true</Can-Set-Native-Method-Prefix>
                            </manifestEntries>
                        </archive>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>

    google,百度也没有找到什么,本身也不太好调试,以前使用 ASM 是可以的。提前感谢

    3
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 简sheet 普通会员 1楼
      502 Bad Gateway

      502 Bad Gateway


      nginx
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部