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

arthas源码刨析:arthas-core (2)

文章目录

  • attach JVM
  • agent
  • **ArthasBootstrap**

arthas-core的启动可以从上一篇做参考
在这里插入图片描述
参考 pom,即启动是调用的 Arthas 的 main 方法
在这里插入图片描述

attach JVM

JVM提供了 Java Attach 功能,能够让客户端与目标JVM进行通讯从而获取JVM运行时的数据,甚至可以通过Java Attach 加载自定义的代理工具,实现AOP、运行时class热更新等功能。

private void attachAgent(Configure configure) throws Exception {VirtualMachineDescriptor virtualMachineDescriptor = null;for (VirtualMachineDescriptor descriptor : VirtualMachine.list()) {String pid = descriptor.id();if (pid.equals(Long.toString(configure.getJavaPid()))) {virtualMachineDescriptor = descriptor;break;}}VirtualMachine virtualMachine = null;try {if (null == virtualMachineDescriptor) { // 使用 attach(String pid) 这种方式virtualMachine = VirtualMachine.attach("" + configure.getJavaPid());} else {virtualMachine = VirtualMachine.attach(virtualMachineDescriptor);}……try {virtualMachine.loadAgent(arthasAgentPath,configure.getArthasCore() + ";" + configure.toString());} catch (IOException e) {}

VirtualMachine.list() 相当于 jps 的功能:
在这里插入图片描述
attach是依靠这条代码:

virtualMachine = VirtualMachine.attach(virtualMachineDescriptor);

之后就是 loadAgent 并传入 core 和 configure 参数。

agent

agent 的代码很简单,只有2个类:
在这里插入图片描述
在这里插入图片描述

核心就是 AgentBootstrap ,对于 javaagent来说核心的启动入口:

    public static void premain(String args, Instrumentation inst) {main(args, inst);}public static void agentmain(String args, Instrumentation inst) {main(args, inst);} 

神奇不,在 arthas-core 里传入的 -core 参数实际上是透传给agent的。整的很绕。 通过 bind 方法进行线程绑定,这一步和 arthas-boot 很像。

private static synchronized void main(String args, final Instrumentation inst) {// 尝试判断arthas是否已在运行,如果是的话,直接就退出try {Class.forName("java.arthas.SpyAPI"); // 加载不到会抛异常if (SpyAPI.isInited()) {ps.println("Arthas server already stared, skip attach.");ps.flush();return;}} catch (Throwable e) {// ignore}try {ps.println("Arthas server agent start...");// 传递的args参数分两个部分:arthasCoreJar路径和agentArgs, 分别是Agent的JAR包路径和期望传递到服务端的参数if (args == null) {args = "";}args = decodeArg(args);String arthasCoreJar;final String agentArgs;int index = args.indexOf(';');if (index != -1) {arthasCoreJar = args.substring(0, index);agentArgs = args.substring(index);} else {arthasCoreJar = "";agentArgs = args;}File arthasCoreJarFile = new File(arthasCoreJar);if (!arthasCoreJarFile.exists()) {ps.println("Can not find arthas-core jar file from args: " + arthasCoreJarFile);// try to find from arthas-agent.jar directoryCodeSource codeSource = AgentBootstrap.class.getProtectionDomain().getCodeSource();if (codeSource != null) {try {File arthasAgentJarFile = new File(codeSource.getLocation().toURI().getSchemeSpecificPart());arthasCoreJarFile = new File(arthasAgentJarFile.getParentFile(), ARTHAS_CORE_JAR);if (!arthasCoreJarFile.exists()) {ps.println("Can not find arthas-core jar file from agent jar directory: " + arthasAgentJarFile);}} catch (Throwable e) {ps.println("Can not find arthas-core jar file from " + codeSource.getLocation());e.printStackTrace(ps);}}}if (!arthasCoreJarFile.exists()) {return;}/*** Use a dedicated thread to run the binding logic to prevent possible memory leak. #195*/final ClassLoader agentLoader = getClassLoader(inst, arthasCoreJarFile);Thread bindingThread = new Thread() {@Overridepublic void run() {try {bind(inst, agentLoader, agentArgs);} catch (Throwable throwable) {throwable.printStackTrace(ps);}}};bindingThread.setName("arthas-binding-thread");bindingThread.start();bindingThread.join();} catch (Throwable t) {t.printStackTrace(ps);try {if (ps != System.err) {ps.close();}} catch (Throwable tt) {// ignore}throw new RuntimeException(t);}}

ArthasBootstrap

bind 有调用了 core 里面 ArthasBootstrap.getInstance

private static void bind(Instrumentation inst, ClassLoader agentLoader, String args) throws Throwable {/*** <pre>* ArthasBootstrap bootstrap = ArthasBootstrap.getInstance(inst);* </pre>*/Class<?> bootstrapClass = agentLoader.loadClass(ARTHAS_BOOTSTRAP);Object bootstrap = bootstrapClass.getMethod(GET_INSTANCE, Instrumentation.class, String.class).invoke(null, inst, args);boolean isBind = (Boolean) bootstrapClass.getMethod(IS_BIND).invoke(bootstrap);if (!isBind) {String errorMsg = "Arthas server port binding failed! Please check $HOME/logs/arthas/arthas.log for more details.";ps.println(errorMsg);throw new RuntimeException(errorMsg);}ps.println("Arthas server already bind.");}

而在 ArthasBootstrap中 我们用的 terminal,也就是 ShellServer, 就在 6. start agent server

private ArthasBootstrap(Instrumentation instrumentation, Map<String, String> args) throws Throwable {this.instrumentation = instrumentation;initFastjson();// 1. initSpy()initSpy();// 2. ArthasEnvironmentinitArthasEnvironment(args);String outputPathStr = configure.getOutputPath();if (outputPathStr == null) {outputPathStr = ArthasConstants.ARTHAS_OUTPUT;}outputPath = new File(outputPathStr);outputPath.mkdirs();// 3. init loggerloggerContext = LogUtil.initLogger(arthasEnvironment);// 4. 增强ClassLoaderenhanceClassLoader();// 5. init beansinitBeans();// 6. start agent serverbind(configure);executorService = Executors.newScheduledThreadPool(1, new ThreadFactory() {@Overridepublic Thread newThread(Runnable r) {final Thread t = new Thread(r, "arthas-command-execute");t.setDaemon(true);return t;}});shutdown = new Thread("as-shutdown-hooker") {@Overridepublic void run() {ArthasBootstrap.this.destroy();}};transformerManager = new TransformerManager(instrumentation);Runtime.getRuntime().addShutdownHook(shutdown);}

ShellServerImpl 的 bind 这段实现中,BuiltinCommandPack 完成了命名的声明并与 cli 输入内容进行命名绑定。并用 Netty 开启server。

 /*** Bootstrap arthas server** @param configure 配置信息* @throws IOException 服务器启动失败*/private void bind(Configure configure) throws Throwable {long start = System.currentTimeMillis();if (!isBindRef.compareAndSet(false, true)) {throw new IllegalStateException("already bind");}// init random portif (configure.getTelnetPort() != null && configure.getTelnetPort() == 0) {int newTelnetPort = SocketUtils.findAvailableTcpPort();configure.setTelnetPort(newTelnetPort);logger().info("generate random telnet port: " + newTelnetPort);}if (configure.getHttpPort() != null && configure.getHttpPort() == 0) {int newHttpPort = SocketUtils.findAvailableTcpPort();configure.setHttpPort(newHttpPort);logger().info("generate random http port: " + newHttpPort);}// try to find appNameif (configure.getAppName() == null) {configure.setAppName(System.getProperty(ArthasConstants.PROJECT_NAME,System.getProperty(ArthasConstants.SPRING_APPLICATION_NAME, null)));}try {if (configure.getTunnelServer() != null) {tunnelClient = new TunnelClient();tunnelClient.setAppName(configure.getAppName());tunnelClient.setId(configure.getAgentId());tunnelClient.setTunnelServerUrl(configure.getTunnelServer());tunnelClient.setVersion(ArthasBanner.version());ChannelFuture channelFuture = tunnelClient.start();channelFuture.await(10, TimeUnit.SECONDS);}} catch (Throwable t) {logger().error("start tunnel client error", t);}try {ShellServerOptions options = new ShellServerOptions().setInstrumentation(instrumentation).setPid(PidUtils.currentLongPid()).setWelcomeMessage(ArthasBanner.welcome());if (configure.getSessionTimeout() != null) {options.setSessionTimeout(configure.getSessionTimeout() * 1000);}this.httpSessionManager = new HttpSessionManager();if (IPUtils.isAllZeroIP(configure.getIp()) && StringUtils.isBlank(configure.getPassword())) {// 当 listen 0.0.0.0 时,强制生成密码,防止被远程连接String errorMsg = "Listening on 0.0.0.0 is very dangerous! External users can connect to your machine! "+ "No password is currently configured. " + "Therefore, a default password is generated, "+ "and clients need to use the password to connect!";AnsiLog.error(errorMsg);configure.setPassword(StringUtils.randomString(64));AnsiLog.error("Generated arthas password: " + configure.getPassword());logger().error(errorMsg);logger().info("Generated arthas password: " + configure.getPassword());}this.securityAuthenticator = new SecurityAuthenticatorImpl(configure.getUsername(), configure.getPassword());shellServer = new ShellServerImpl(options);List<String> disabledCommands = new ArrayList<String>();if (configure.getDisabledCommands() != null) {String[] strings = StringUtils.tokenizeToStringArray(configure.getDisabledCommands(), ",");if (strings != null) {disabledCommands.addAll(Arrays.asList(strings));}}BuiltinCommandPack builtinCommands = new BuiltinCommandPack(disabledCommands);List<CommandResolver> resolvers = new ArrayList<CommandResolver>();resolvers.add(builtinCommands);//worker groupworkerGroup = new NioEventLoopGroup(new DefaultThreadFactory("arthas-TermServer", true));// TODO: discover user provided command resolverif (configure.getTelnetPort() != null && configure.getTelnetPort() > 0) {logger().info("try to bind telnet server, host: {}, port: {}.", configure.getIp(), configure.getTelnetPort());shellServer.registerTermServer(new HttpTelnetTermServer(configure.getIp(), configure.getTelnetPort(),options.getConnectionTimeout(), workerGroup, httpSessionManager));} else {logger().info("telnet port is {}, skip bind telnet server.", configure.getTelnetPort());}if (configure.getHttpPort() != null && configure.getHttpPort() > 0) {logger().info("try to bind http server, host: {}, port: {}.", configure.getIp(), configure.getHttpPort());shellServer.registerTermServer(new HttpTermServer(configure.getIp(), configure.getHttpPort(),options.getConnectionTimeout(), workerGroup, httpSessionManager));} else {// listen local address in VM communicationif (configure.getTunnelServer() != null) {shellServer.registerTermServer(new HttpTermServer(configure.getIp(), configure.getHttpPort(),options.getConnectionTimeout(), workerGroup, httpSessionManager));}logger().info("http port is {}, skip bind http server.", configure.getHttpPort());}for (CommandResolver resolver : resolvers) {shellServer.registerCommandResolver(resolver);}shellServer.listen(new BindHandler(isBindRef));if (!isBind()) {throw new IllegalStateException("Arthas failed to bind telnet or http port! Telnet port: "+ String.valueOf(configure.getTelnetPort()) + ", http port: "+ String.valueOf(configure.getHttpPort()));}//http api session managersessionManager = new SessionManagerImpl(options, shellServer.getCommandManager(), shellServer.getJobController());//http api handlerhttpApiHandler = new HttpApiHandler(historyManager, sessionManager);logger().info("as-server listening on network={};telnet={};http={};timeout={};", configure.getIp(),configure.getTelnetPort(), configure.getHttpPort(), options.getConnectionTimeout());// 异步回报启动次数if (configure.getStatUrl() != null) {logger().info("arthas stat url: {}", configure.getStatUrl());}UserStatUtil.setStatUrl(configure.getStatUrl());UserStatUtil.setAgentId(configure.getAgentId());UserStatUtil.arthasStart();try {SpyAPI.init();} catch (Throwable e) {// ignore}logger().info("as-server started in {} ms", System.currentTimeMillis() - start);} catch (Throwable e) {logger().error("Error during start as-server", e);destroy();throw e;}}

参考:

https://blog.csdn.net/tianjindong0804/article/details/128423819

相关文章:

arthas源码刨析:arthas-core (2)

文章目录 attach JVMagent**ArthasBootstrap** arthas-core的启动可以从上一篇做参考 参考 pom&#xff0c;即启动是调用的 Arthas 的 main 方法 attach JVM JVM提供了 Java Attach 功能&#xff0c;能够让客户端与目标JVM进行通讯从而获取JVM运行时的数据&#xff0c;甚至可以…...

【分享】格力手机色界G0245D 刷REC、root、 救砖、第三方rom教程和资源

开门见山 帮别人弄了一台 格力G0245D&#xff0c;把找到的资源和教程分享一下 教程 这个写的很详细了格力手机色界G0245D-Root-最简指南 不过教程里刷rec这一步漏了加上电源键&#xff0c;加上就行了。 附加参考&#xff1a;格力手机2刷机 格力手机二代刷机 GREE G0215D刷机…...

开学必备清单来啦!大学好物合集推荐!每一个都能帮你提升幸福感

随着开学季的到来&#xff0c;好多学生都在忙着准备各类学习与生活必需品&#xff0c;以迎接新的大学生活到来。以下是一些开学季必备的好物推荐&#xff0c;每一个都很实用&#xff0c;可以帮你提升学习和生活的幸福感&#xff01; 1、西圣电容笔 一句话推荐&#xff1a;公认…...

已解决:javax.xml.transform.TransformerFactoryConfigurationError 异常的正确解决方法,亲测有效!!!

1. 问题描述 javax.xml.transform.TransformerFactoryConfigurationError 是在使用 Java 的 XML 处理库时&#xff0c;配置 TransformerFactory 出错时抛出的异常。通常&#xff0c;这个异常发生在应用程序试图创建一个 TransformerFactory 实例时&#xff0c;由于无法找到合适…...

商品价格与优惠信息在API返回值中的位置

在API返回值中&#xff0c;商品价格与优惠信息的具体位置可能因不同的电商平台和API设计而有所不同。然而&#xff0c;一般来说&#xff0c;这些信息会以结构化的方式呈现&#xff0c;通常包含在一个包含多个字段的JSON对象或XML文档中。以下是根据多个电商平台&#xff08;如阿…...

Oracle Index Partition索引分区的管理

Oracle索引分区的管理是数据库管理中的重要任务之一&#xff0c;它涉及索引的创建、维护、重建以及优化等多个方面。以下是对Oracle索引分区管理的详细解析&#xff1a; 一、索引分区的概念 索引分区&#xff08;Partitioned Index&#xff09;是针对分区表而言的&#xff0c…...

统信UOS系统访问windows共享目录

问题背景 当我们使用UOS系统的时候&#xff0c;想要访问windows系统的一些资料并将其拷贝下来使用的话&#xff0c;应该怎么操作呢&#xff1f;这个需求是可以实现的&#xff0c;统信UOS系统是基于Linux系统开发的&#xff0c;Linux系统和windows系统之间可以通过SMB协议来共享…...

单一职责原则与REST API设计:如何定义清晰的资源与职责

在软件设计中&#xff0c;单一职责原则&#xff08;Single Responsibility Principle, SRP&#xff09;和 REST API 设计是两个重要的概念。单一职责原则是一种设计原则&#xff0c;它强调一个类或模块应当只有一个单一的职责&#xff0c;这有助于提高系统的可维护性和扩展性。…...

JAVA IO模型

我们在平常开发过程中接触最多的就是 磁盘 IO&#xff08;读写文件&#xff09; 和 网络 IO&#xff08;网络请求和响应&#xff09;。从应用程序的视角来看的话&#xff0c;我们的应用程序对操作系统的内核发起 IO 调用&#xff08;系统调用&#xff09;&#xff0c;操作系统负…...

《C/C++实战专栏》介绍

&#x1f680; 前言 本文是《C/C实战专栏》专栏的说明贴&#xff08;点击链接&#xff0c;跳转到专栏主页&#xff0c;欢迎订阅&#xff0c;持续更新…&#xff09;。 专栏介绍&#xff1a;以多年的开发实战为基础&#xff0c;总结并讲解一些的C/C基础与项目实战进阶内容&…...

前端跨域2

前端跨域2 前端跨域解决方案&#xff08;11种方案&#xff09; 1.JSONP跨域解决方案的底层原理 script、img、link、iframe...<script src"https://cdn.bootcss.com/jquery/3.4.1/core.js"></script>// 这个就是因为script标签没有跨域限制&#xff0…...

electron仿微信,新建贴合窗口

说明 在写electron项目时&#xff0c;只有一个主窗口不足以满足需求&#xff0c;我们通常还会打开很多个窗口。 怎么打开一个子窗口像微信的聊天界面一样&#xff0c;全贴合在一起&#xff0c;看起来像一个整体呢&#xff1a; 分析 这个窗口有点像element ui中的抽屉(drawe…...

uniapp微信小程序 分享功能

uniapp https://zh.uniapp.dcloud.io/api/plugins/share.html#onshareappmessage export default {onShareAppMessage(res) {if (res.from button) {// 来自页面内分享按钮console.log(res.target)}return {title: 自定义分享标题,path: /pages/test/test?id123}} }需要再真机…...

Java实现数据库数据到Excel的高效导出

在数据处理和分析工作中&#xff0c;经常需要将数据库中的数据导出到Excel文件中。本文将提供一个Java实现的示例&#xff0c;展示如何边从数据库读取数据&#xff0c;边将其写入Excel文件&#xff0c;同时注重内存效率。 环境配置&#xff1a; Java 1.8 或更高版本MySQL 5.7…...

python之matplotlib (8 极坐标)-圆与心

极坐标 极坐标图像的绘制类似于三维图像的绘制&#xff0c;只需要将projection参数由3d改为polar即可。 import numpy as np import matplotlib.pyplot as plt figplt.figure() axfig.add_subplot(projectionpolar)theta np.linspace(0, 2 * np.pi, 100) r np.sin(the…...

Kubernetes Pod调度基础

在传统架构中&#xff0c;我们总在考虑或者面临一个问题&#xff0c;我们的应用需要部署在哪里&#xff0c;我们的应用下载在哪里运行着?有一个服务不可访问了&#xff0c;去哪里排査?诸如此类的问题总是会出现在工作中。 但是在使用 Kubernetes 部署应用后&#xff…...

80页WORD方案深入了解大数据治理+大数据资产管理+数据运营

文档是一份80页可编辑的企业大数据智能管理与治理平台建设项目技术方案标书文档&#xff0c;涵盖了从项目需求分析、技术方案、建设方案、服务方案到类似案例介绍等多个方面的内容。 1. 项目需求分析 项目建设目标&#xff1a;旨在实现数据的可视化&#xff0c;确保决策者、行…...

OCC安装、VS2019编译运行(新手教程)

OCC安装、VS2019编译运行(新手教程) 简介1、OpenCasCade的下载和安装官网下载安装2、OpenCasCade的运行和编译(VS2019)修改配置文件环境变量配置3、验证代码项目配置运行cpp文件简介 作为一个刚接触OCC的程序员,可能会不知所措,无从下手,甚至在OCC的安装使用都困难重重…...

Mojo 实现排序功能

sort排序 实现排序功能。 您可以从包中导入这些 API。例如&#xff1a;algorithm from algorithm.sort import sortpartition partition[type: AnyRegType, cmp_fn: fn[AnyRegType]($0, $0, /) capturing -> Bool](buff: Pointer[*"type", 0], k: Int, size: …...

信息学奥赛一本通编程启蒙题解(3031~3035)

前言 Hello大家好我是文宇 正文 3031 #include<bits/stdc.h> using namespace std; double n,m,x; int main(){cin>>n>>m;xn-m*0.8;cout<<fixed<<setprecision(2)<<x;return 0; } 3032 #include<bits/stdc.h> using namespace…...

C++实现分布式网络通信框架RPC(3)--rpc调用端

目录 一、前言 二、UserServiceRpc_Stub 三、 CallMethod方法的重写 头文件 实现 四、rpc调用端的调用 实现 五、 google::protobuf::RpcController *controller 头文件 实现 六、总结 一、前言 在前边的文章中&#xff0c;我们已经大致实现了rpc服务端的各项功能代…...

C++初阶-list的底层

目录 1.std::list实现的所有代码 2.list的简单介绍 2.1实现list的类 2.2_list_iterator的实现 2.2.1_list_iterator实现的原因和好处 2.2.2_list_iterator实现 2.3_list_node的实现 2.3.1. 避免递归的模板依赖 2.3.2. 内存布局一致性 2.3.3. 类型安全的替代方案 2.3.…...

【JavaEE】-- HTTP

1. HTTP是什么&#xff1f; HTTP&#xff08;全称为"超文本传输协议"&#xff09;是一种应用非常广泛的应用层协议&#xff0c;HTTP是基于TCP协议的一种应用层协议。 应用层协议&#xff1a;是计算机网络协议栈中最高层的协议&#xff0c;它定义了运行在不同主机上…...

边缘计算医疗风险自查APP开发方案

核心目标:在便携设备(智能手表/家用检测仪)部署轻量化疾病预测模型,实现低延迟、隐私安全的实时健康风险评估。 一、技术架构设计 #mermaid-svg-iuNaeeLK2YoFKfao {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg…...

.Net框架,除了EF还有很多很多......

文章目录 1. 引言2. Dapper2.1 概述与设计原理2.2 核心功能与代码示例基本查询多映射查询存储过程调用 2.3 性能优化原理2.4 适用场景 3. NHibernate3.1 概述与架构设计3.2 映射配置示例Fluent映射XML映射 3.3 查询示例HQL查询Criteria APILINQ提供程序 3.4 高级特性3.5 适用场…...

线程同步:确保多线程程序的安全与高效!

全文目录&#xff1a; 开篇语前序前言第一部分&#xff1a;线程同步的概念与问题1.1 线程同步的概念1.2 线程同步的问题1.3 线程同步的解决方案 第二部分&#xff1a;synchronized关键字的使用2.1 使用 synchronized修饰方法2.2 使用 synchronized修饰代码块 第三部分&#xff…...

ffmpeg(四):滤镜命令

FFmpeg 的滤镜命令是用于音视频处理中的强大工具&#xff0c;可以完成剪裁、缩放、加水印、调色、合成、旋转、模糊、叠加字幕等复杂的操作。其核心语法格式一般如下&#xff1a; ffmpeg -i input.mp4 -vf "滤镜参数" output.mp4或者带音频滤镜&#xff1a; ffmpeg…...

第 86 场周赛:矩阵中的幻方、钥匙和房间、将数组拆分成斐波那契序列、猜猜这个单词

Q1、[中等] 矩阵中的幻方 1、题目描述 3 x 3 的幻方是一个填充有 从 1 到 9 的不同数字的 3 x 3 矩阵&#xff0c;其中每行&#xff0c;每列以及两条对角线上的各数之和都相等。 给定一个由整数组成的row x col 的 grid&#xff0c;其中有多少个 3 3 的 “幻方” 子矩阵&am…...

.Net Framework 4/C# 关键字(非常用,持续更新...)

一、is 关键字 is 关键字用于检查对象是否于给定类型兼容,如果兼容将返回 true,如果不兼容则返回 false,在进行类型转换前,可以先使用 is 关键字判断对象是否与指定类型兼容,如果兼容才进行转换,这样的转换是安全的。 例如有:首先创建一个字符串对象,然后将字符串对象隐…...

腾讯云V3签名

想要接入腾讯云的Api&#xff0c;必然先按其文档计算出所要求的签名。 之前也调用过腾讯云的接口&#xff0c;但总是卡在签名这一步&#xff0c;最后放弃选择SDK&#xff0c;这次终于自己代码实现。 可能腾讯云翻新了接口文档&#xff0c;现在阅读起来&#xff0c;清晰了很多&…...