Arthas(阿尔萨斯)--(一)

目录

一、Arthas环境准备

1、概述

2、运行环境要求

3、安装&卸载

二、Arthas命令学习

1、Arthas前置环境

2、常用命令

1、dashboard仪表板

2、thread命令

3、jad命令

4、watch命令

5、退出arthas

3、基础命令一

1、help

2、cat

3、grep

4、pwd

5、cls

4、基础命令二

1、session

2、reset

3、version

4、history

5、quit

6、stop

7、keymap


一、Arthas环境准备

1、概述

Arthas是Alibaba开源的Java诊断工具,深受开发者喜爱。

当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决:

1.这个类从哪个jar包加载的?为什么会报各种类相关的Exception?

2.我改的代码为什么没有执行到?

3.遇到问题无法在线上Debug,难道只能通过日志再重新发布吗?

4.线上遇到某个用户的数据处理有问题,但线上同样无法Debug,线下无法重现!

5.是否有一个全局视角来查看系统的运行状况?

6.有什么办法可以监控到JVM的实时运行状态?

7.怎么快速定位应用的热点,生成火焰图?

Arthas官方文档

Arthas代码开源地址

2、运行环境要求

Arthas支持JDK6+,支持Linux/Mac/Windows,采用命令行交互模式,同时提供丰富的Tab自动补全功能,进一步方便进行问题的定位和诊断。

3、安装&卸载

下载arthas-boot.jar,然后用java -jar的方式启动:

1.在线安装:
curl -O https://alibaba.github.io/arthas/arthas-boot.jar
$>java -jar arthas-boot.jar
执行后按1,下载文件到C:\Users\Administrator\.arthas目录下
2.离线安装下载安装包直接解压使用
从Maven仓库下载全量包
https://maven.aliyun.com/repository/public/com/taobao/arthas/arthas-packaging/3.6.2/arthas-packaging-3.6.2-bin.zip
注:1.curl:windows/Linux一个命令,获取指定地址资源下载下来。
2.在运行java -jar arthas-boot.jar命令之前,先运行一个java进程在内存中,
不然会出现找不到java进程的错误。

Windows下安装&启动

1、C:\arthas目录下进行执行命令下载
C:\arthas>curl -O https://alibaba.github.io/arthas/arthas-boot.jar
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  138k  100  138k    0     0  70945      0  0:00:02  0:00:02 --:--:-- 56038

C:\arthas>dir
 驱动器 C 中的卷没有标签。
 卷的序列号是 2C79-F95A

 C:\arthas 的目录

2022/07/09  20:56    <DIR>          .
2022/07/09  20:56    <DIR>          ..
2022/07/09  20:56           141,890 arthas-boot.jar
               1 个文件        141,890 字节
               2 个目录 68,148,006,912 可用字节
注:会把arthas-boot.jar下载到C:\arthas目录下。

2、启动arthas-boot.jar之前,需要开启一个java进程,开启idea也行,否则,会把找不到进程等错误退出
启动,需要选择需要监视的进程[1][2]....等等序号。选择一个数字,回车。
第一次,会进行拉取安装arthas:会进入arthas启动页面,会把arthas安装到
[INFO] arthas home: C:\Users\Administrator\.arthas\lib\3.6.2\arthas目录下。
C:\arthas>java -jar arthas-boot.jar
[INFO] arthas-boot version: 3.6.1
[INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
* [1]: 12080
  [2]: 11944 org.jetbrains.jps.cmdline.Launcher
  [3]: 7116 org.jetbrains.idea.maven.server.RemoteMavenServer
2
[INFO] Start download arthas from remote server: https://arthas.aliyun.com/download/3.6.2?mirror=aliyun
[INFO] File size: 12.88 MB, downloaded size: 1.54 MB, downloading ...
[INFO] File size: 12.88 MB, downloaded size: 3.58 MB, downloading ...
[INFO] File size: 12.88 MB, downloaded size: 5.12 MB, downloading ...
[INFO] File size: 12.88 MB, downloaded size: 6.92 MB, downloading ...
[INFO] File size: 12.88 MB, downloaded size: 8.78 MB, downloading ...
[INFO] File size: 12.88 MB, downloaded size: 10.80 MB, downloading ...
[INFO] Download arthas success.
[INFO] arthas home: C:\Users\Administrator\.arthas\lib\3.6.2\arthas
[INFO] Try to attach process 11944
[INFO] Found java home from System Env JAVA_HOME: C:\Program Files\Java\jdk1.8.0_202
[INFO] Attach process 11944 success.
[INFO] arthas-client connect 127.0.0.1 3658
  ,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---.
 /  O  \ |  .--. ''--.  .--'|  '--'  | /  O  \ '   .-'
|  .-.  ||  '--'.'   |  |   |  .--.  ||  .-.  |`.  `-.
|  | |  ||  |\  \    |  |   |  |  |  ||  | |  |.-'    |
`--' `--'`--' '--'   `--'   `--'  `--'`--' `--'`-----'

wiki       https://arthas.aliyun.com/doc
tutorials  https://arthas.aliyun.com/doc/arthas-tutorials.html
version    3.6.2
main_class
pid        11944
time       2022-07-09 21:01:13

[arthas@11944]$

Linux下安装和在Windows下安装arthas操作步骤一样。此时省略..

Arthas卸载

Arthas是绿色版,直接删除安装目录.arthas和logs目录就删除成功了。

二、Arthas命令学习

Arthas命令详细参考

1、Arthas前置环境

Arthas安装目录环境中已经提供一直执行的进行示例math-game.jar以前版本叫arthas-demo.jar名称。//C:\Users\Administrator\.arthas目录下

代码MathGame,功能:分解质因数

每隔一秒生成一个随机数,再执行质因数分解,并打印分解结果。

package demo;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

public class MathGame
{
  private static Random random = new Random();
  //用于统计生成不合法变量的个数
  private int illegalArgumentCount = 0;
  
  public static void main(String[] args)
    throws InterruptedException
  {
    MathGame game = new MathGame();
    //死循环,每过1秒调用一次下面的的方法
    for (;;)
    {
      game.run();
      TimeUnit.SECONDS.sleep(1L);
    }
  }
  
  //分解质因数
  public void run()
    throws InterruptedException
  {
    try
    {
      //随机生成一个整数,有可能正,有可能负
      int number = random.nextInt() / 10000;
      //调用方法进行质因数分解
      List<Integer> primeFactors = primeFactors(number);
      //打印结果
      print(number, primeFactors);
    }
    catch (Exception e)
    {
      System.out.println(String.format("illegalArgumentCount:%3d, ", new Object[] { Integer.valueOf(this.illegalArgumentCount) }) + e.getMessage());
    }
  }
  
  //打印质因数分解结果
  public static void print(int number, List<Integer> primeFactors)
  {
    StringBuffer sb = new StringBuffer(number + "=");
    for (Iterator localIterator = primeFactors.iterator(); localIterator.hasNext();)
    {
      int factor = ((Integer)localIterator.next()).intValue();
      sb.append(factor).append('*');
    }
    if (sb.charAt(sb.length() - 1) == '*') {
      sb.deleteCharAt(sb.length() - 1);
    }
    System.out.println(sb);
  }
  
  //计算number的质因数分解
  public List<Integer> primeFactors(int number)
  {
    //如果小于2,则抛出异常,并且计数加1
    if (number < 2)
    {
      this.illegalArgumentCount += 1;
      throw new IllegalArgumentException("number is: " + number + ", need >= 2");
    }
    //用于保存每个质数
    List<Integer> result = new ArrayList();
    //分解过程,从2开始看能不能整除
    int i = 2;
    while (i <= number) {//如果1大于number就退出循环
      //能整除,则i为一个因数,number为整除的结果再继续从2开始除
      if (number % i == 0)
      {
        result.add(Integer.valueOf(i));
        number /= i;
        i = 2;
      }
      else
      {
        i++;
      }
    }
    return result;
  }
}

启动Demo,一直运行

1、在arthas安装目录下启动math-game.jar
java -jar math-game.jar

再换个窗口启动arthas

1、在arthas安装目录下启动arthas-boot.jar
java -jar arthas-boot.jar

如果端口号被占用,通过以下命令更换端口号执行

java -jar arthas-boot.jar --telnet-port 9998  --http-port -1

启动完成后,如下图显示127.0.0.1  3658端口,可以通过浏览器进行访问arthas

http://127.0.0.1:3658  进行命令执行

[INFO] arthas-client connect 127.0.0.1 3658
  ,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---.
 /  O  \ |  .--. ''--.  .--'|  '--'  | /  O  \ '   .-'
|  .-.  ||  '--'.'   |  |   |  .--.  ||  .-.  |`.  `-.
|  | |  ||  |\  \    |  |   |  |  |  ||  | |  |.-'    |
`--' `--'`--' '--'   `--'   `--'  `--'`--' `--'`-----'

2、常用命令

1、dashboard仪表板

输入dashboard(仪表板),按回车/enter,会展示当前进程的信息,按ctrl+c/q可以中断执行。

注:输入前面部分字母,按tab可以自动补全命令

  1. 第一部分是现实JVM中运行的所有线程;所在线程组,优先级,线程的状态,CPU的占用率,是否是后台进程等
  2. 第二部分显示的JVM内存的使用情况
  3. 第三部分是操作系统的一些信息和Java版本号

dashboard:显示JVM中内存的情况,JVM中环境信息

2、thread命令

通过thread命令来获取到math-game.jar进程的arthas-timer

thread:显示当前进程所有线程信息

3、jad命令

通过jad来反编译MathGame Class

jad:反编译指定的类或方法

// 说明:jad 包名.类名 :反编译出如下内容 :1.类加载器信息2.定位到相应的jar3.反编译的源代码
[arthas@1120]$ jad demo.MathGame

ClassLoader:
+-sun.misc.Launcher$AppClassLoader@5c647e05
  +-sun.misc.Launcher$ExtClassLoader@48ff3a66

Location:
/D:/.m2/arthas/arthas-packaging-3.6.2-bin/math-game.jar

       /*
        * Decompiled with CFR.
        */
       package demo;

       import java.util.ArrayList;
       import java.util.List;
       import java.util.Random;
       import java.util.concurrent.TimeUnit;

       public class MathGame {
           private static Random random = new Random();
           private int illegalArgumentCount = 0;

           public static void main(String[] args) throws InterruptedException {
               MathGame game = new MathGame();
               while (true) {
/*16*/             game.run();
/*17*/             TimeUnit.SECONDS.sleep(1L);
               }
           }

           public void run() throws InterruptedException {
               try {
/*23*/             int number = random.nextInt() / 10000;
/*24*/             List<Integer> primeFactors = this.primeFactors(number);
/*25*/             MathGame.print(number, primeFactors);
               }
               catch (Exception e) {
/*28*/             System.out.println(String.format("illegalArgumentCount:%3d, ", this.illegalArgumentCount) + e.getMessage());
               }
           }

           public static void print(int number, List<Integer> primeFactors) {
               StringBuffer sb = new StringBuffer(number + "=");
/*34*/         for (int factor : primeFactors) {
/*35*/             sb.append(factor).append('*');
               }
/*37*/         if (sb.charAt(sb.length() - 1) == '*') {
/*38*/             sb.deleteCharAt(sb.length() - 1);
               }
/*40*/         System.out.println(sb);
           }

           public List<Integer> primeFactors(int number) {
/*44*/         if (number < 2) {
/*45*/             ++this.illegalArgumentCount;
                   throw new IllegalArgumentException("number is: " + number + ", need >= 2");
               }
               ArrayList<Integer> result = new ArrayList<Integer>();
/*50*/         int i = 2;
/*51*/         while (i <= number) {
/*52*/             if (number % i == 0) {
/*53*/                 result.add(i);
/*54*/                 number /= i;
/*55*/                 i = 2;
                       continue;
                   }
/*57*/             ++i;
               }
/*61*/         return result;
           }
       }

Affect(row-cnt:1) cost in 559 ms.
[arthas@1120]$

4、watch命令

watch监视:监视某个方法的执行情况,监视返回值

通过watch命令来查看demo.MathGame#primeFactors函数的返回值

// 说明:watch 包名.类名 方法名 返回值信息
$>watch demo.MathGame primeFactors returnObj

[arthas@1120]$ watch demo.MathGame primeFactors returnObj
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 90 ms, listenerId: 1
method=demo.MathGame.primeFactors location=AtExit
ts=2023-11-11 22:46:47; [cost=2.4119ms] result=@ArrayList[
    @Integer[2],
    @Integer[2],
    @Integer[2],
    @Integer[3],
    @Integer[11],
    @Integer[467],
]
method=demo.MathGame.primeFactors location=AtExceptionExit
ts=2023-11-11 22:46:48; [cost=0.2033ms] result=null
method=demo.MathGame.primeFactors location=AtExceptionExit
ts=2023-11-11 22:46:49; [cost=0.0686ms] result=null
method=demo.MathGame.primeFactors location=AtExit
ts=2023-11-11 22:46:50; [cost=0.0977ms] result=@ArrayList[
    @Integer[2],
    @Integer[29],
    @Integer[773],
]
....

按q或者ctrl+c退出

5、退出arthas

如果只是退出当前的连接,可以用quit或者exit命令。Attach到目标进程上的arthas还会继续运行,端口会保持开放,下次连接时可以直接连接上。

如果想完全退出arthas,可以执行stop命令。stop结束session会话

[arthas@1120]$ stop
Resetting all enhanced classes ...
Affect(class count: 1 , method count: 0) cost in 10 ms, listenerId: 0
Arthas Server is going to shutdown...
[arthas@1120]$ session (1647c4f3-2bf2-4ae3-897a-b706c385477b) is closed because server is going to shutdown.

3、基础命令一

1、help

help:查看命令帮助信息

2、cat

cat:打印文件内容,和linux里的cat命令类似,显示指定文件的内容

如果没有写路径,则显示当前目录下的文件

3、grep

grep:匹配查找,和linux里的grep命令类似,但它只能用于管道命令

参数列表作用
-n显示行号
-i忽略大小写查找
-m 行数最大显示行数,要与查询字符串一起使用
-e "正则表达式"使用正则表达式查找
只显示包含java字符串的行系统属性
[arthas@3144]$ sysprop | grep java

只显示包含java字符串的行和行号的系统属性
[arthas@3144]$ sysprop | grep java -n

使用正则表达式,显示包含2个数字以上字符的信息
[arthas@3144]$ sysprop | grep -e "\d{2,}"

使用正则表达式,显示包含两个o字符的线程信息
[arthas@3144]$ thread | grep -e "o{2}"
15   arthas-NettyHttpTelnetBootstr system         5         RUNNABLE 0.0       0.000     0:0.015   false     true
16   arthas-NettyWebsocketTtyBoots system         5         RUNNABLE 0.0       0.000     0:0.000   false     true
17   arthas-NettyWebsocketTtyBoots system         5         RUNNABLE 0.0       0.000     0:0.000   false     true
22   arthas-NettyHttpTelnetBootstr system         5         RUNNABLE 0.0       0.000     0:0.250   false     true
[arthas@3144]$
显示包含system字符串的10行信息
[arthas@3144]$ thread | grep system -m 10
2    Reference Handler             system         10        WAITING  0.0       0.000     0:0.000   false     true
3    Finalizer                     system         8         WAITING  0.0       0.000     0:0.000   false     true
4    Signal Dispatcher             system         9         RUNNABLE 0.0       0.000     0:0.000   false     true
5    Attach Listener               system         5         RUNNABLE 0.0       0.000     0:0.046   false     true
12   arthas-timer                  system         5         WAITING  0.0       0.000     0:0.000   false     true
15   arthas-NettyHttpTelnetBootstr system         5         RUNNABLE 0.0       0.000     0:0.015   false     true
16   arthas-NettyWebsocketTtyBoots system         5         RUNNABLE 0.0       0.000     0:0.000   false     true
17   arthas-NettyWebsocketTtyBoots system         5         RUNNABLE 0.0       0.000     0:0.000   false     true
18   arthas-shell-server           system         5         TIMED_WA 0.0       0.000     0:0.015   false     true
19   arthas-session-manager        system         5         TIMED_WA 0.0       0.000     0:0.000   false     true
[arthas@3144]$

4、pwd

pwd:返回当前的工作目录,和linux命令类似

[arthas@3144]$ pwd
D:\.m2\arthas\arthas-packaging-3.6.2-bin

5、cls

cls:清空当前屏幕区域

4、基础命令二

1、session

session:查看当前会话的信息

[arthas@3144]$ session
 Name        Value
--------------------------------------------------
 JAVA_PID    3144
 SESSION_ID  9b8601fe-48d1-4ed2-a8ee-1bf07e388492
[arthas@3144]$

2、reset

reset:重置增强类,将Arthas增强过的类全部还原,Arthas服务端关闭时会重置所有增强过的类

reset 类:还原指定类
reset *List:还原所有以List结尾的类
reset:还原所有的类

3、version

version:输出当前目标java进程所加载的Arthas版本号

[arthas@3144]$ version
3.7.1
[arthas@3144]$

4、history

history:打印命令历史

[arthas@3144]$ history
    1  thread
    2  thread 1
    3  jad demo.MathGame
    4  watch demo.MathGame primeFactors returnObj
    5  stop
    6  help
    7  ls
    8  cls
    9  cat d:/arthas.txt
   10  pwd
   11  sysprop | grep java
   12  sysprop | grep java -n
   13  sysprop | grep -e "\d{2,}"
   14  thread | grep -e "o{2}"
   15  thread | grep system -m 10
   16  session
   17  trace demo.MathGame print
   18  trace demo.MathGame print
   19  trace demo.MathGame print
   20  reset
   21  version
   22  history
[arthas@3144]$

5、quit

quit:退出当前Arthas客户端,其他Arthas客户端不受影响

[arthas@3144]$ quit

D:\.m2\arthas>

6、stop

stop:关闭Arthas服务端,所有Arthas客户端全部退出

[arthas@3144]$ stop
Resetting all enhanced classes ...
Affect(class count: 0 , method count: 0) cost in 10 ms, listenerId: 0
Arthas Server is going to shutdown...
[arthas@3144]$ session (0eab20c5-6706-4338-8e97-43d56bb54de3) is closed because server is going to shutdown.

7、keymap

keymap:Arthas快捷键列表及自定义快捷键

[arthas@3144]$ keymap
 Shortcut                      Description                    Name
---------------------------------------------------------------------------------------
 "\C-a"                        Ctrl + a                        beginning-of-line
 "\C-e"                        Ctrl + e                        end-of-line
 "\C-f"                        Ctrl + f                        forward-word
 "\C-b"                        Ctrl + b                        backward-word
 "\e[D"                        Left arrow                      backward-char
 "\e[C"                        Right arrow                     forward-char
 "\e[A"                        Up arrow                        history-search-backward
 "\e[B"                        Down arrow                      history-search-forward
 "\C-h"                        Ctrl + h                        backward-delete-char
 "\C-?"                        Ctrl + ?                        backward-delete-char
 "\C-u"                        Ctrl + u                        undo
 "\C-d"                        Ctrl + d                        delete-char
 "\C-k"                        Ctrl + k                        kill-line
 "\C-i"                        Ctrl + i                        complete
 "\C-j"                        Ctrl + j                        accept-line
 "\C-m"                        Ctrl + m                        accept-line
 "\C-w"                        Ctrl + w                        backward-delete-word
 "\C-x\e[3~"                   "\C-x\e[3~"                     backward-kill-line
 "\e\C-?"                      "\e\C-?"                        backward-kill-word
 "\e[1~"                       "\e[1~"                         beginning-of-line
 "\e[4~"                       "\e[4~"                         end-of-line
 "\e[5~"                       "\e[5~"                         beginning-of-history
 "\e[6~"                       "\e[6~"                         end-of-history
 "\e[3~"                       "\e[3~"                         delete-char
 "\e[2~"                       "\e[2~"                         quoted-insert
 "\e[7~"                       "\e[7~"                         beginning-of-line
 "\e[8~"                       "\e[8~"                         end-of-line
 "\eOH"                        "\eOH"                          beginning-of-line
 "\eOF"                        "\eOF"                          end-of-line
 "\e[H"                        "\e[H"                          beginning-of-line
 "\e[F"                        "\e[F"                          end-of-line
[arthas@3144]$

Arthas(阿尔萨斯)--(二)

一个程序员最重要的能力是:写出高质量的代码!!
有道无术,术尚可求也,有术无道,止于术。
无论你是年轻还是年长,所有程序员都需要记住:时刻努力学习新技术,否则就会被时代抛弃!

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杀神lwz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值