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

linux之kylin系统nginx的安装

一、nginx的作用 1.可做高性能的web服务器 直接处理静态资源&#xff08;HTML/CSS/图片等&#xff09;&#xff0c;响应速度远超传统服务器类似apache支持高并发连接 2.反向代理服务器 隐藏后端服务器IP地址&#xff0c;提高安全性 3.负载均衡服务器 支持多种策略分发流量…...

通过Wrangler CLI在worker中创建数据库和表

官方使用文档&#xff1a;Getting started Cloudflare D1 docs 创建数据库 在命令行中执行完成之后&#xff0c;会在本地和远程创建数据库&#xff1a; npx wranglerlatest d1 create prod-d1-tutorial 在cf中就可以看到数据库&#xff1a; 现在&#xff0c;您的Cloudfla…...

MFC内存泄露

1、泄露代码示例 void X::SetApplicationBtn() {CMFCRibbonApplicationButton* pBtn GetApplicationButton();// 获取 Ribbon Bar 指针// 创建自定义按钮CCustomRibbonAppButton* pCustomButton new CCustomRibbonAppButton();pCustomButton->SetImage(IDB_BITMAP_Jdp26)…...

DAY 47

三、通道注意力 3.1 通道注意力的定义 # 新增&#xff1a;通道注意力模块&#xff08;SE模块&#xff09; class ChannelAttention(nn.Module):"""通道注意力模块(Squeeze-and-Excitation)"""def __init__(self, in_channels, reduction_rat…...

定时器任务——若依源码分析

分析util包下面的工具类schedule utils&#xff1a; ScheduleUtils 是若依中用于与 Quartz 框架交互的工具类&#xff0c;封装了定时任务的 创建、更新、暂停、删除等核心逻辑。 createScheduleJob createScheduleJob 用于将任务注册到 Quartz&#xff0c;先构建任务的 JobD…...

基于当前项目通过npm包形式暴露公共组件

1.package.sjon文件配置 其中xh-flowable就是暴露出去的npm包名 2.创建tpyes文件夹&#xff0c;并新增内容 3.创建package文件夹...

从零开始打造 OpenSTLinux 6.6 Yocto 系统(基于STM32CubeMX)(九)

设备树移植 和uboot设备树修改的内容同步到kernel将设备树stm32mp157d-stm32mp157daa1-mx.dts复制到内核源码目录下 源码修改及编译 修改arch/arm/boot/dts/st/Makefile&#xff0c;新增设备树编译 stm32mp157f-ev1-m4-examples.dtb \stm32mp157d-stm32mp157daa1-mx.dtb修改…...

Neo4j 集群管理:原理、技术与最佳实践深度解析

Neo4j 的集群技术是其企业级高可用性、可扩展性和容错能力的核心。通过深入分析官方文档,本文将系统阐述其集群管理的核心原理、关键技术、实用技巧和行业最佳实践。 Neo4j 的 Causal Clustering 架构提供了一个强大而灵活的基石,用于构建高可用、可扩展且一致的图数据库服务…...

工业自动化时代的精准装配革新:迁移科技3D视觉系统如何重塑机器人定位装配

AI3D视觉的工业赋能者 迁移科技成立于2017年&#xff0c;作为行业领先的3D工业相机及视觉系统供应商&#xff0c;累计完成数亿元融资。其核心技术覆盖硬件设计、算法优化及软件集成&#xff0c;通过稳定、易用、高回报的AI3D视觉系统&#xff0c;为汽车、新能源、金属制造等行…...

vue3+vite项目中使用.env文件环境变量方法

vue3vite项目中使用.env文件环境变量方法 .env文件作用命名规则常用的配置项示例使用方法注意事项在vite.config.js文件中读取环境变量方法 .env文件作用 .env 文件用于定义环境变量&#xff0c;这些变量可以在项目中通过 import.meta.env 进行访问。Vite 会自动加载这些环境变…...