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,即启动是调用的 Arthas 的 main 方法 attach JVM JVM提供了 Java Attach 功能,能够让客户端与目标JVM进行通讯从而获取JVM运行时的数据,甚至可以…...

【分享】格力手机色界G0245D 刷REC、root、 救砖、第三方rom教程和资源
开门见山 帮别人弄了一台 格力G0245D,把找到的资源和教程分享一下 教程 这个写的很详细了格力手机色界G0245D-Root-最简指南 不过教程里刷rec这一步漏了加上电源键,加上就行了。 附加参考:格力手机2刷机 格力手机二代刷机 GREE G0215D刷机…...

开学必备清单来啦!大学好物合集推荐!每一个都能帮你提升幸福感
随着开学季的到来,好多学生都在忙着准备各类学习与生活必需品,以迎接新的大学生活到来。以下是一些开学季必备的好物推荐,每一个都很实用,可以帮你提升学习和生活的幸福感! 1、西圣电容笔 一句话推荐:公认…...
已解决:javax.xml.transform.TransformerFactoryConfigurationError 异常的正确解决方法,亲测有效!!!
1. 问题描述 javax.xml.transform.TransformerFactoryConfigurationError 是在使用 Java 的 XML 处理库时,配置 TransformerFactory 出错时抛出的异常。通常,这个异常发生在应用程序试图创建一个 TransformerFactory 实例时,由于无法找到合适…...

商品价格与优惠信息在API返回值中的位置
在API返回值中,商品价格与优惠信息的具体位置可能因不同的电商平台和API设计而有所不同。然而,一般来说,这些信息会以结构化的方式呈现,通常包含在一个包含多个字段的JSON对象或XML文档中。以下是根据多个电商平台(如阿…...
Oracle Index Partition索引分区的管理
Oracle索引分区的管理是数据库管理中的重要任务之一,它涉及索引的创建、维护、重建以及优化等多个方面。以下是对Oracle索引分区管理的详细解析: 一、索引分区的概念 索引分区(Partitioned Index)是针对分区表而言的,…...

统信UOS系统访问windows共享目录
问题背景 当我们使用UOS系统的时候,想要访问windows系统的一些资料并将其拷贝下来使用的话,应该怎么操作呢?这个需求是可以实现的,统信UOS系统是基于Linux系统开发的,Linux系统和windows系统之间可以通过SMB协议来共享…...
单一职责原则与REST API设计:如何定义清晰的资源与职责
在软件设计中,单一职责原则(Single Responsibility Principle, SRP)和 REST API 设计是两个重要的概念。单一职责原则是一种设计原则,它强调一个类或模块应当只有一个单一的职责,这有助于提高系统的可维护性和扩展性。…...

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

《C/C++实战专栏》介绍
🚀 前言 本文是《C/C实战专栏》专栏的说明贴(点击链接,跳转到专栏主页,欢迎订阅,持续更新…)。 专栏介绍:以多年的开发实战为基础,总结并讲解一些的C/C基础与项目实战进阶内容&…...
前端跨域2
前端跨域2 前端跨域解决方案(11种方案) 1.JSONP跨域解决方案的底层原理 script、img、link、iframe...<script src"https://cdn.bootcss.com/jquery/3.4.1/core.js"></script>// 这个就是因为script标签没有跨域限制࿰…...

electron仿微信,新建贴合窗口
说明 在写electron项目时,只有一个主窗口不足以满足需求,我们通常还会打开很多个窗口。 怎么打开一个子窗口像微信的聊天界面一样,全贴合在一起,看起来像一个整体呢: 分析 这个窗口有点像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的高效导出
在数据处理和分析工作中,经常需要将数据库中的数据导出到Excel文件中。本文将提供一个Java实现的示例,展示如何边从数据库读取数据,边将其写入Excel文件,同时注重内存效率。 环境配置: Java 1.8 或更高版本MySQL 5.7…...

python之matplotlib (8 极坐标)-圆与心
极坐标 极坐标图像的绘制类似于三维图像的绘制,只需要将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调度基础
在传统架构中,我们总在考虑或者面临一个问题,我们的应用需要部署在哪里,我们的应用下载在哪里运行着?有一个服务不可访问了,去哪里排査?诸如此类的问题总是会出现在工作中。 但是在使用 Kubernetes 部署应用后ÿ…...

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

OCC安装、VS2019编译运行(新手教程)
OCC安装、VS2019编译运行(新手教程) 简介1、OpenCasCade的下载和安装官网下载安装2、OpenCasCade的运行和编译(VS2019)修改配置文件环境变量配置3、验证代码项目配置运行cpp文件简介 作为一个刚接触OCC的程序员,可能会不知所措,无从下手,甚至在OCC的安装使用都困难重重…...
Mojo 实现排序功能
sort排序 实现排序功能。 您可以从包中导入这些 API。例如: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…...

基于距离变化能量开销动态调整的WSN低功耗拓扑控制开销算法matlab仿真
目录 1.程序功能描述 2.测试软件版本以及运行结果展示 3.核心程序 4.算法仿真参数 5.算法理论概述 6.参考文献 7.完整程序 1.程序功能描述 通过动态调整节点通信的能量开销,平衡网络负载,延长WSN生命周期。具体通过建立基于距离的能量消耗模型&am…...
leetcodeSQL解题:3564. 季节性销售分析
leetcodeSQL解题:3564. 季节性销售分析 题目: 表:sales ---------------------- | Column Name | Type | ---------------------- | sale_id | int | | product_id | int | | sale_date | date | | quantity | int | | price | decimal | -…...

前端开发面试题总结-JavaScript篇(一)
文章目录 JavaScript高频问答一、作用域与闭包1.什么是闭包(Closure)?闭包有什么应用场景和潜在问题?2.解释 JavaScript 的作用域链(Scope Chain) 二、原型与继承3.原型链是什么?如何实现继承&a…...

全志A40i android7.1 调试信息打印串口由uart0改为uart3
一,概述 1. 目的 将调试信息打印串口由uart0改为uart3。 2. 版本信息 Uboot版本:2014.07; Kernel版本:Linux-3.10; 二,Uboot 1. sys_config.fex改动 使能uart3(TX:PH00 RX:PH01),并让boo…...

mysql已经安装,但是通过rpm -q 没有找mysql相关的已安装包
文章目录 现象:mysql已经安装,但是通过rpm -q 没有找mysql相关的已安装包遇到 rpm 命令找不到已经安装的 MySQL 包时,可能是因为以下几个原因:1.MySQL 不是通过 RPM 包安装的2.RPM 数据库损坏3.使用了不同的包名或路径4.使用其他包…...

【VLNs篇】07:NavRL—在动态环境中学习安全飞行
项目内容论文标题NavRL: 在动态环境中学习安全飞行 (NavRL: Learning Safe Flight in Dynamic Environments)核心问题解决无人机在包含静态和动态障碍物的复杂环境中进行安全、高效自主导航的挑战,克服传统方法和现有强化学习方法的局限性。核心算法基于近端策略优化…...
Go 语言并发编程基础:无缓冲与有缓冲通道
在上一章节中,我们了解了 Channel 的基本用法。本章将重点分析 Go 中通道的两种类型 —— 无缓冲通道与有缓冲通道,它们在并发编程中各具特点和应用场景。 一、通道的基本分类 类型定义形式特点无缓冲通道make(chan T)发送和接收都必须准备好࿰…...

【p2p、分布式,区块链笔记 MESH】Bluetooth蓝牙通信 BLE Mesh协议的拓扑结构 定向转发机制
目录 节点的功能承载层(GATT/Adv)局限性: 拓扑关系定向转发机制定向转发意义 CG 节点的功能 节点的功能由节点支持的特性和功能决定。所有节点都能够发送和接收网格消息。节点还可以选择支持一个或多个附加功能,如 Configuration …...

elementUI点击浏览table所选行数据查看文档
项目场景: table按照要求特定的数据变成按钮可以点击 解决方案: <el-table-columnprop"mlname"label"名称"align"center"width"180"><template slot-scope"scope"><el-buttonv-if&qu…...
Docker拉取MySQL后数据库连接失败的解决方案
在使用Docker部署MySQL时,拉取并启动容器后,有时可能会遇到数据库连接失败的问题。这种问题可能由多种原因导致,包括配置错误、网络设置问题、权限问题等。本文将分析可能的原因,并提供解决方案。 一、确认MySQL容器的运行状态 …...