快速入门

1. 启动Demo

  1. wget https://alibaba.github.io/arthas/arthas-demo.jar
  2. java -jar arthas-demo.jar

arthas-demo是一个简单的程序,每隔一秒生成一个随机数,再执行质因式分解,并打印出分解结果。

arthas-demo源代码:查看

2. 启动arthas

在命令行下面执行:

  1. wget https://alibaba.github.io/arthas/arthas-boot.jar
  2. java -jar arthas-boot.jar
  • 执行该程序的用户需要和目标进程具有相同的权限。比如以admin用户来执行:sudo su admin && java -jar arthas-boot.jarsudo -u admin -EH java -jar arthas-boot.jar
  • 如果attatch不上目标进程,可以查看~/logs/arthas/ 目录下的日志。
  • 如果下载速度比较慢,可以使用aliyun的镜像:java -jar arthas-boot.jar —repo-mirror aliyun —use-http
  • java -jar arthas-boot.jar -h 打印更多参数信息。
    选择应用java进程:
  1. $ $ java -jar arthas-boot.jar
  2. * [1]: 35542
  3. [2]: 71560 arthas-demo.jar

Demo进程是第2个,则输入2,再输入回车/enter。Arthas会attach到目标进程上,并输出日志:

  1. [INFO] Try to attach process 71560
  2. [INFO] Attach process 71560 success.
  3. [INFO] arthas-client connect 127.0.0.1 3658
  4. ,---. ,------. ,--------.,--. ,--. ,---. ,---.
  5. / O \ | .--. ''--. .--'| '--' | / O \ ' .-'
  6. | .-. || '--'.' | | | .--. || .-. |`. `-.
  7. | | | || |\ \ | | | | | || | | |.-' |
  8. `--' `--'`--' '--' `--' `--' `--'`--' `--'`-----'
  9.  
  10. wiki: https://alibaba.github.io/arthas
  11. version: 3.0.5.20181127201536
  12. pid: 71560
  13. time: 2018-11-28 19:16:24
  14.  
  15. $

3. 查看dashboard

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

  1. $ dashboard
  2. ID NAME GROUP PRIORI STATE %CPU TIME INTERRU DAEMON
  3. 17 pool-2-thread-1 system 5 WAITIN 67 0:0 false false
  4. 27 Timer-for-arthas-dashb system 10 RUNNAB 32 0:0 false true
  5. 11 AsyncAppender-Worker-a system 9 WAITIN 0 0:0 false true
  6. 9 Attach Listener system 9 RUNNAB 0 0:0 false true
  7. 3 Finalizer system 8 WAITIN 0 0:0 false true
  8. 2 Reference Handler system 10 WAITIN 0 0:0 false true
  9. 4 Signal Dispatcher system 9 RUNNAB 0 0:0 false true
  10. 26 as-command-execute-dae system 10 TIMED_ 0 0:0 false true
  11. 13 job-timeout system 9 TIMED_ 0 0:0 false true
  12. 1 main main 5 TIMED_ 0 0:0 false false
  13. 14 nioEventLoopGroup-2-1 system 10 RUNNAB 0 0:0 false false
  14. 18 nioEventLoopGroup-2-2 system 10 RUNNAB 0 0:0 false false
  15. 23 nioEventLoopGroup-2-3 system 10 RUNNAB 0 0:0 false false
  16. 15 nioEventLoopGroup-3-1 system 10 RUNNAB 0 0:0 false false
  17. Memory used total max usage GC
  18. heap 32M 155M 1820M 1.77% gc.ps_scavenge.count 4
  19. ps_eden_space 14M 65M 672M 2.21% gc.ps_scavenge.time(m 166
  20. ps_survivor_space 4M 5M 5M s)
  21. ps_old_gen 12M 85M 1365M 0.91% gc.ps_marksweep.count 0
  22. nonheap 20M 23M -1 gc.ps_marksweep.time( 0
  23. code_cache 3M 5M 240M 1.32% ms)
  24. Runtime
  25. os.name Mac OS X
  26. os.version 10.13.4
  27. java.version 1.8.0_162
  28. java.home /Library/Java/JavaVir
  29. tualMachines/jdk1.8.0
  30. _162.jdk/Contents/Hom
  31. e/jre

4. 通过sysenv命令来获取到进程的Main Class

  1. $ sysenv | grep MAIN
  2. JAVA_MAIN_CLASS_71560 demo.MathGame

5. 通过jad来反编绎Main Class

  1. $ jad demo.MathGame
  2.  
  3. ClassLoader:
  4. +-sun.misc.Launcher$AppClassLoader@3d4eac69
  5. +-sun.misc.Launcher$ExtClassLoader@66350f69
  6.  
  7. Location:
  8. /tmp/arthas-demo.jar
  9.  
  10. /*
  11. * Decompiled with CFR 0_132.
  12. */
  13. package demo;
  14.  
  15. import java.io.PrintStream;
  16. import java.util.ArrayList;
  17. import java.util.Iterator;
  18. import java.util.List;
  19. import java.util.Random;
  20. import java.util.concurrent.TimeUnit;
  21.  
  22. public class MathGame {
  23. private static Random random = new Random();
  24. private int illegalArgumentCount = 0;
  25.  
  26. public static void main(String[] args) throws InterruptedException {
  27. MathGame game = new MathGame();
  28. do {
  29. game.run();
  30. TimeUnit.SECONDS.sleep(1L);
  31. } while (true);
  32. }
  33.  
  34. public void run() throws InterruptedException {
  35. try {
  36. int number = random.nextInt();
  37. List<Integer> primeFactors = this.primeFactors(number);
  38. MathGame.print(number, primeFactors);
  39. }
  40. catch (Exception e) {
  41. System.out.println(String.format("illegalArgumentCount:%3d, ", this.illegalArgumentCount) + e.getMessage());
  42. }
  43. }
  44.  
  45. public static void print(int number, List<Integer> primeFactors) {
  46. StringBuffer sb = new StringBuffer("" + number + "=");
  47. Iterator<Integer> iterator = primeFactors.iterator();
  48. while (iterator.hasNext()) {
  49. int factor = iterator.next();
  50. sb.append(factor).append('*');
  51. }
  52. if (sb.charAt(sb.length() - 1) == '*') {
  53. sb.deleteCharAt(sb.length() - 1);
  54. }
  55. System.out.println(sb);
  56. }
  57.  
  58. public List<Integer> primeFactors(int number) {
  59. if (number < 2) {
  60. ++this.illegalArgumentCount;
  61. throw new IllegalArgumentException("number is: " + number + ", need >= 2");
  62. }
  63. ArrayList<Integer> result = new ArrayList<Integer>();
  64. int i = 2;
  65. while (i <= number) {
  66. if (number % i == 0) {
  67. result.add(i);
  68. number /= i;
  69. i = 2;
  70. continue;
  71. }
  72. ++i;
  73. }
  74. return result;
  75. }
  76. }
  77.  
  78. Affect(row-cnt:1) cost in 970 ms.

6. watch

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

  1. $ watch demo.MathGame primeFactors returnObj
  2. Press Ctrl+C to abort.
  3. Affect(class-cnt:1 , method-cnt:1) cost in 107 ms.
  4. ts=2018-11-28 19:22:30; [cost=1.715367ms] result=null
  5. ts=2018-11-28 19:22:31; [cost=0.185203ms] result=null
  6. ts=2018-11-28 19:22:32; [cost=19.012416ms] result=@ArrayList[
  7. @Integer[5],
  8. @Integer[47],
  9. @Integer[2675531],
  10. ]
  11. ts=2018-11-28 19:22:33; [cost=0.311395ms] result=@ArrayList[
  12. @Integer[2],
  13. @Integer[5],
  14. @Integer[317],
  15. @Integer[503],
  16. @Integer[887],
  17. ]
  18. ts=2018-11-28 19:22:34; [cost=10.136007ms] result=@ArrayList[
  19. @Integer[2],
  20. @Integer[2],
  21. @Integer[3],
  22. @Integer[3],
  23. @Integer[31],
  24. @Integer[717593],
  25. ]
  26. ts=2018-11-28 19:22:35; [cost=29.969732ms] result=@ArrayList[
  27. @Integer[5],
  28. @Integer[29],
  29. @Integer[7651739],
  30. ]

更多的功能可以查看进阶使用

5. 退出arthas

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

如果想完全退出arthas,可以执行shutdown命令。

原文: https://alibaba.github.io/arthas/quick-start.html