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…...

简易版抽奖活动的设计技术方案
1.前言 本技术方案旨在设计一套完整且可靠的抽奖活动逻辑,确保抽奖活动能够公平、公正、公开地进行,同时满足高并发访问、数据安全存储与高效处理等需求,为用户提供流畅的抽奖体验,助力业务顺利开展。本方案将涵盖抽奖活动的整体架构设计、核心流程逻辑、关键功能实现以及…...

黑马Mybatis
Mybatis 表现层:页面展示 业务层:逻辑处理 持久层:持久数据化保存 在这里插入图片描述 Mybatis快速入门 作为一种标准化的信息聚合技术,被广泛用于网站内容的发布和订阅。通过 RSS,用户可以方便地获取网站更新的内容,而无需频繁访问各个网站。 然而,互联网…...

定时器任务——若依源码分析
分析util包下面的工具类schedule utils: ScheduleUtils 是若依中用于与 Quartz 框架交互的工具类,封装了定时任务的 创建、更新、暂停、删除等核心逻辑。 createScheduleJob createScheduleJob 用于将任务注册到 Quartz,先构建任务的 JobD…...
Qwen3-Embedding-0.6B深度解析:多语言语义检索的轻量级利器
第一章 引言:语义表示的新时代挑战与Qwen3的破局之路 1.1 文本嵌入的核心价值与技术演进 在人工智能领域,文本嵌入技术如同连接自然语言与机器理解的“神经突触”——它将人类语言转化为计算机可计算的语义向量,支撑着搜索引擎、推荐系统、…...
Unit 1 深度强化学习简介
Deep RL Course ——Unit 1 Introduction 从理论和实践层面深入学习深度强化学习。学会使用知名的深度强化学习库,例如 Stable Baselines3、RL Baselines3 Zoo、Sample Factory 和 CleanRL。在独特的环境中训练智能体,比如 SnowballFight、Huggy the Do…...
汇编常见指令
汇编常见指令 一、数据传送指令 指令功能示例说明MOV数据传送MOV EAX, 10将立即数 10 送入 EAXMOV [EBX], EAX将 EAX 值存入 EBX 指向的内存LEA加载有效地址LEA EAX, [EBX4]将 EBX4 的地址存入 EAX(不访问内存)XCHG交换数据XCHG EAX, EBX交换 EAX 和 EB…...
OpenLayers 分屏对比(地图联动)
注:当前使用的是 ol 5.3.0 版本,天地图使用的key请到天地图官网申请,并替换为自己的key 地图分屏对比在WebGIS开发中是很常见的功能,和卷帘图层不一样的是,分屏对比是在各个地图中添加相同或者不同的图层进行对比查看。…...

USB Over IP专用硬件的5个特点
USB over IP技术通过将USB协议数据封装在标准TCP/IP网络数据包中,从根本上改变了USB连接。这允许客户端通过局域网或广域网远程访问和控制物理连接到服务器的USB设备(如专用硬件设备),从而消除了直接物理连接的需要。USB over IP的…...

视觉slam十四讲实践部分记录——ch2、ch3
ch2 一、使用g++编译.cpp为可执行文件并运行(P30) g++ helloSLAM.cpp ./a.out运行 二、使用cmake编译 mkdir build cd build cmake .. makeCMakeCache.txt 文件仍然指向旧的目录。这表明在源代码目录中可能还存在旧的 CMakeCache.txt 文件,或者在构建过程中仍然引用了旧的路…...