当前位置: 首页 > news >正文

用javaagent和javassist实现Arthas的watch功能

一、被监控的服务 spring-boot-demo

 

1、 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>com.techhf</groupId><artifactId>spring-boot-demo</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.3.4.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-undertow</artifactId><version>2.3.4.RELEASE</version></dependency></dependencies></project>

2、DemoController

package com.techhf.demo.controller;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author shiweijia* @date 2024/8/1 17:31*/
@RequestMapping("/demo")
@RestController
public class DemoController {@RequestMapping("/hello")public String hello(String name) {return "hello world " + name;}}

3、DemoApplication

 

package com.techhf.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @author shiweijia* @date 2024/8/1 17:30*/
@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}

4、运行DemoApplication

-javaagent:D:/temp/jvm-agent-1.0-SNAPSHOT-jar-with-dependencies.jar 

 二、jvm-agent

1、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>com.techhf</groupId><artifactId>jvm-agent</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.javassist</groupId><artifactId>javassist</artifactId><version>3.30.2-GA</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><version>3.3.0</version><configuration><descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs><archive><manifestEntries><Premain-Class>com.techhf.agent.WatchAgent</Premain-Class><Agent-Class>com.techhf.agent.WatchAgent</Agent-Class><Can-Redefine-Classes>true</Can-Redefine-Classes><Can-Retransform-Classes>true</Can-Retransform-Classes></manifestEntries></archive></configuration><executions><execution><id>make-assembly</id><phase>package</phase><goals><goal>single</goal></goals></execution></executions></plugin></plugins></build></project>

2、WatchAgent

package com.techhf.agent;import java.lang.instrument.Instrumentation;/*** @author shiweijia* @date 2024/8/1 16:00*/
public class WatchAgent {public static void premain(String agentArgs, Instrumentation inst) {agentArgs = "com.techhf.demo.controller.DemoController,hello";System.out.println("premain attached with args: " + agentArgs);transform(agentArgs, inst);}public static void agentmain(String agentArgs, Instrumentation inst) {System.out.println("agentmain attached with args: " + agentArgs);transform(agentArgs, inst);}private static void transform(String agentArgs, Instrumentation inst) {// 解析 agentArgs 来获取类名和方法名String[] parts = agentArgs.split(",");String className = parts[0];String methodName = parts[1];inst.addTransformer(new WatchTransformer(className, methodName));}}

3、WatchTransformer

package com.techhf.agent;import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;/*** @author shiweijia* @date 2024/8/3 16:56*/
public class WatchTransformer implements ClassFileTransformer {private String className;private String methodName;public WatchTransformer(String className, String methodName) {this.className = className;this.methodName = methodName;}@Overridepublic byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {className = className.replace('/', '.');if (this.className.equals(className)) {try {ClassPool pool = ClassPool.getDefault();CtClass cc = pool.get(className);CtMethod method = cc.getDeclaredMethod(methodName);String beforeCode = "{ System.out.print(\"Before Method Name: \" + \"" + method.getName() + "\" + \", Parameters: \"); " +"for (int i = 0; i < $args.length; i++) { " +"    System.out.print($args[i]); " +"} " +"System.out.println(); }";method.insertBefore(beforeCode);method.insertAfter("{ System.out.println(\"After Method Name: \"+\"" + method.getName()+ "\" + \", Return: \" + $_); }");return cc.toBytecode();} catch (Exception e) {e.printStackTrace();}}return classfileBuffer;}
}

三、启动时监控

如上述实例,运行时加入-javaagent相关参数即可。

四、运行时监控

import com.sun.tools.attach.VirtualMachine;/*** @author shiweijia* @date 2024/8/1 17:35*/
public class AttachLoader {public static void main(String[] args) {if (args.length < 2) {System.out.println("Usage: AttachLoader <pid> <agent-path> <agent-args>");System.exit(1);}String pid = args[0];String agentPath = args[1];String agentArgs = args[2];try {// 获取目标 JVM 进程VirtualMachine vm = VirtualMachine.attach(pid);vm.loadAgent(agentPath, agentArgs);// 可选:在需要时分离(detach)// vm.detach();System.out.println("Agent loaded into PID " + pid);} catch (Exception e) {e.printStackTrace();}}}

运行参数为:

34496 D:\temp\jvm-agent-1.0-SNAPSHOT-jar-with-dependencies.jar com.techhf.demo.controller.DemoController,hello

采用运行时监控需要注意:spring 是动态代理的,agentmain无法直接监控具体的类。需要优化相关的实现。

http://www.lryc.cn/news/416840.html

相关文章:

  • golang 图片转1bit color bmp图片
  • Leetcode75-5 反转字符串的元音字母
  • static关键字在Java中的作用与用法
  • 50etf期权行权采用什么交割方式 ?
  • ts-node 报错 ERR_UNKNOWN_FILE_EXTENSION
  • 水域救援设备,保护水域安全_鼎跃安全
  • openmetadata本地编译环境搭建
  • LeetCode Hard|【25. K 个一组翻转链表】
  • python爬虫预备知识三-多进程
  • 【zlm】针对单个设备的音频的编码的设置
  • 文案人的梦工场,网易入职指南!
  • 做一个能和你互动玩耍的智能机器人之七-接入对话和大模型
  • 阿里巴巴商家联系方式采集软件使用教程
  • Golang | Leetcode Golang题解之第326题3的幂
  • 人大金仓(Kingbase)数据库高阶函数详解
  • 云原生真机实验
  • 【电子电路学习笔记】——模电笔记
  • 部署伪分布式 Hadoop集群
  • 十九、虚拟机VMware Workstation(CentOSDebian)的安装
  • 理解Android framework之AOSP:从内核到应用层
  • 杂谈c语言——3.内存对齐
  • 瑞芯微Android设备指定应用开启性能模式
  • ASP.NET Core基础 - 简介
  • Flask+LayUI开发手记(一):LayUI表格的前端数据分页展现
  • Vulnhub靶场DC-9练习
  • Java对象内存布局和对象头
  • python:基于YOLO框架和遥感图像的目标检测
  • DAMA学习笔记(十一)-元数据管理
  • 密码学基本理论
  • 【深度学习】【语音TTS】vits 论文,Variational Inference Text-to-Speech(1)