当前位置: 首页 > 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…...

2026届学术党必备的六大AI辅助论文方案横评

Ai论文网站排名&#xff08;开题报告、文献综述、降aigc率、降重综合对比&#xff09; TOP1. 千笔AI TOP2. aipasspaper TOP3. 清北论文 TOP4. 豆包 TOP5. kimi TOP6. deepseek 知网的AI内容调整&#xff0c;得严格依照学术规范要求&#xff0c;其关键要点是回归自主研究…...

如何快速入门Three.js:10个基础案例带你上手三维开发 [特殊字符]

如何快速入门Three.js&#xff1a;10个基础案例带你上手三维开发 &#x1f680; 【免费下载链接】three-cesium-examples WebGL Three.js Cesium.js Examples And Demo - WebGL 的 Three.js 和 Cesium.js 案例 --- Star ---点星星 项目地址: https://gitcode.com/gh_mirrors/…...

别再瞎加密网格了!用Numeca AutoGrid5做叶轮机械CFD,这样验证网格无关性才靠谱

别再瞎加密网格了&#xff01;用Numeca AutoGrid5做叶轮机械CFD&#xff0c;这样验证网格无关性才靠谱 在叶轮机械CFD仿真中&#xff0c;网格质量直接决定了计算结果的可靠性。许多工程师习惯性地认为"网格越密越好"&#xff0c;却忽略了网格无关性验证的科学方法。这…...

开发者技能编织:从点状学习到系统构建的成长框架

1. 项目概述&#xff1a;编织你的开发者技能树“plaited/development-skills”这个项目标题&#xff0c;乍一看可能有点抽象&#xff0c;但如果你把它拆开&#xff0c;就能立刻明白它的核心价值。“Plaited”是“编织”的意思&#xff0c;而“development-skills”直译就是“开…...

绝杀,OpenAI正式接管人类耳朵,首个GPT-5级推理音频模型来了

OpenAI又给世界带来一次震撼。 这一次&#xff0c;他们不卷文字&#xff0c;不卷视频&#xff0c;而是要把那个曾让无数人惊艳、又让无数人遗憾的Samantha——电影《Her》中的AI——彻底带进现实。 OpenAI正式宣布&#xff0c;推出GPT-Realtime-2。 这不仅仅是一次音频模型的…...

【AI测试智能体4】测试全过,上线后全崩:14年测试老兵的测试集踩坑指南

测试全过&#xff0c;上线后全崩&#xff1a;14年测试老兵的测试集踩坑指南我踩过的一个坑转到 AI测试之前&#xff0c;我做了多年传统测试&#xff0c;写过 500 多个接口测试用例&#xff0c;API 通过率一直保持在 99% 以上。按我们的习惯&#xff0c;拿到一个系统&#xff0c…...

Claude Code 在 JetBrains IDE 的用户指南

Claude Code 是由 Anthropic 推出的智能编码工具&#xff0c;作为全球最强大的编程助手之一&#xff0c;Claude Code 提供了专门的 JetBrains IDE 插件&#xff0c;支持包括 IntelliJ IDEA、PyCharm、WebStorm、GoLand、PhpStorm 和 Android Studio 在内的整个 JetBrains IDE 系…...

为Hermes Agent自定义配置Taotoken作为AI能力供应商的详细步骤

&#x1f680; 告别海外账号与网络限制&#xff01;稳定直连全球优质大模型&#xff0c;限时半价接入中。 &#x1f449; 点击领取海量免费额度 为Hermes Agent自定义配置Taotoken作为AI能力供应商的详细步骤 本文面向使用Hermes Agent的开发者&#xff0c;介绍如何将其后端AI…...

从零构建私有化AI智能体中枢:Comobot部署、编排与生产实践

1. 项目概述&#xff1a;从零构建你的私有化智能体中枢如果你和我一样&#xff0c;对市面上的AI助手既爱又恨——爱其智能&#xff0c;恨其不可控、数据隐私的担忧以及无法深度融入自己的工作流——那么&#xff0c;Comobot这个项目或许能让你眼前一亮。它不是一个简单的聊天机…...

如何评估你的 Agent 是否真的在思考

重新审视智能:如何用科学、工程与可量化标准评估你的 Agent 是否真的在思考 警告:全文约 12.7 万字,由 8 个核心章节组成,单节最低字数超过 1.1 万字。建议分段阅读,配合工具与项目实践,可获得最佳学习效果。 0. 章节导航与阅读建议 为了帮助不同背景的读者(从 AI 产品…...