快速入门

1. 启动Demo

  1. curl -O https://arthas.aliyun.com/arthas-demo.jar
  2. java -jar arthas-demo.jar

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

arthas-demo源代码:查看

2. 启动arthas

在命令行下面执行(使用和目标进程一致的用户启动,否则可能attach失败):

  1. curl -O https://arthas.aliyun.com/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

  • 如果attach不上目标进程,可以查看~/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. wiki: https://arthas.aliyun.com/doc
  10. version: 3.0.5.20181127201536
  11. pid: 71560
  12. time: 2018-11-28 19:16:24
  13. $

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. 通过thread命令来获取到arthas-demo进程的Main Class

thread 1会打印线程ID 1的栈,通常是main函数的线程。

  1. $ thread 1 | grep 'main('
  2. at demo.MathGame.main(MathGame.java:17)

5. 通过jad来反编译Main Class

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

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

7. 退出arthas

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

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