arthas监控工具笔记(一)

文章目录


arthas是一款监控工具,内存、进程等。还是阿里的,阿里出品,必定精品,来一起看一下吧。

其实官网文档已经写的非常好了,这里再写一遍主要为了加深记忆 。

由于arthas内容太多,博客容易出bug,分为几个文章吧。

启动 math-game
curl -O https://arthas.aliyun.com/math-game.jar
java -jar math-game.jar

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

刚看到这,我也蒙了,是不是进错网站了?后来才发现原来这是个进程,用来测试的。

启动 arthas

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

curl -O https://arthas.aliyun.com/arthas-boot.jar
java -jar arthas-boot.jar

执行该程序的用户需要和目标进程具有相同的权限。比如以admin用户来执行:sudo su admin && java -jar arthas-boot.jar 或 sudo -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 进程:

$ $ java -jar arthas-boot.jar
* [1]: 35542
  [2]: 71560 math-game.jar

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

[INFO] Try to attach process 71560
[INFO] Attach process 71560 success.
[INFO] arthas-client connect 127.0.0.1 3658
  ,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---.
 /  O  \ |  .--. ''--.  .--'|  '--'  | /  O  \ '   .-'
|  .-.  ||  '--'.'   |  |   |  .--.  ||  .-.  |`.  `-.
|  | |  ||  |\  \    |  |   |  |  |  ||  | |  |.-'    |
`--' `--'`--' '--'   `--'   `--'  `--'`--' `--'`-----'


wiki: https://arthas.aliyun.com/doc
version: 3.0.5.20181127201536
pid: 71560
time: 2018-11-28 19:16:24

$

出现这个$符号,说明挂上了。(实测在自己电脑上成功,在linux服务器上挂不上,估计是哪里有问题)

查看 dashboard

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

通过 thread 命令来获取到math-game进程的 Main Class

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

$ thread 1 | grep 'main('
    at demo.MathGame.main(MathGame.java:17)
通过 jad 来反编译 Main Class
$ jad demo.MathGame

牛,居然有这个功能。而且反编译出来和源代码一模一样。。。

退出 arthas

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

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

界面

arthas是支持界面的,默认端口是3658。

linux服务器挂不上进程怎么办?

服务可以启动,输入序号按回车,发现没有反应。

核心表达式变量

这里列一个表格来说明不同变量的含义

变量名 变量解释

loader 本次调用类所在的 ClassLoader
clazz 本次调用类的 Class 引用
method 本次调用方法反射引用
target 本次调用类的实例
params 本次调用参数列表,这是一个数组,如果方法是无参方法则为空数组
returnObj 本次调用返回的对象。当且仅当 isReturn==true 成立时候有效,表明方法调用是以正常返回的方式结束。如果当前方法无返回值 void,则值为 null
throwExp 本次调用抛出的异常。当且仅当 isThrow==true 成立时有效,表明方法调用是以抛出异常的方式结束。
isBefore 辅助判断标记,当前的通知节点有可能是在方法一开始就通知,此时 isBeforetrue 成立,同时 #### isThrowfalse 和 isReturn==false,因为在方法刚开始时,还无法确定方法调用将会如何结束。
isThrow 辅助判断标记,当前的方法调用以抛异常的形式结束。
isReturn 辅助判断标记,当前的方法调用以正常返回的形式结束。

所有变量都可以在表达式中直接使用,如果在表达式中编写了不符合 OGNL 脚本语法或者引入了不在表格中的变量,则退出命令的执行;用户可以根据当前的异常信息修正条件表达式或观察表达式

命令列表

命令列表和核心表达式应该是互通的,这个我试验下。

在arthas命令窗口输入help也可以查看。

jvm 相关。

dashboard - 当前系统的实时数据面板
getstatic - 查看类的静态属性

查看类的静态属性。
要点:
1、必须是属性(不能看整个类)。
2、必须是静态。
如果输入非静态方法,肯定找不到。提示:
getstatic: no matched static field was found

例1:
getstatic java.math.BigDecimal ZERO # 这个对象有点复杂,因为返回的不是一个值,而是一个BigDecimal.ZERO实例。

例2:
getstatic java.lang.String serialVersionUID

[arthas@23052]$ getstatic java.lang.String serialVersionUID
输出:
field: serialVersionUID
@Long[-6849794470754667710]
Affect(row-cnt:1) cost in 4 ms.

当然getstatic也可以用ognl替代。
如:

$ ognl @java.lang.String@serialVersionUID
输出:
@Long[-6849794470754667710]

对比下写法吧。

getstatic写法:
getstatic java.lang.String serialVersionUID
ognl写法:
ognl @java.lang.String@serialVersionUID

heapdump - dump java heap, 类似 jmap 命令的 heap dump 功能
jvm - 查看当前 JVM 的信息
logger - 查看和修改 logger
mbean - 查看 Mbean 的信息
memory - 查看 JVM 的内存信息
ognl - 执行 ognl 表达式
perfcounter - 查看当前 JVM 的 Perf Counter 信息
sysenv - 查看 JVM 的环境变量
sysprop - 查看和修改 JVM 的系统属性
thread - 查看当前 JVM 的线程堆栈信息
vmoption - 查看和修改 JVM 里诊断相关的 option
vmtool - 从 jvm 里查询对象,执行 forceGc

class/classloader 相关

classloader - 查看 classloader 的继承树,urls,类加载信息,使用 classloader 去 getResource
dump - dump 已加载类的 byte code 到特定目录
jad - 反编译指定已加载类的源码
mc - 内存编译器,内存编译.java文件为.class文件

mc例子:
mc c:/Users/PC/Desktop/jad-demo/StartupApplication.Java

mc报错乱码
redefine - 加载外部的.class文件,redefine 到 JVM 里
retransform - 加载外部的.class文件,retransform 到 JVM 里
sc - 查看 JVM 已加载的类信息

sc 不加任何参数
sc sc com.example.controller.RedisController # 那么只会显示几条结果

sc -d 显示详细信息
-d --detail

[arthas@18344]$ sc -d com.example.controller.RedisController
输出:
 class-info        com.example.controller.RedisController
 code-source       /D:/longhu/code4/BW_LongHu-list/apollo_improve_etax6/BW_LongHu/custom-web/target/classes/
 name              com.example.controller.RedisController
 isInterface       false
 isAnnotation      false
 isEnum            false
 isAnonymousClass  false
 isArray           false
 isLocalClass      false
 isMemberClass     false
 isPrimitive       false
 isSynthetic       false
 simple-name       RedisController
 modifier          public
 annotation        org.springframework.web.bind.annotation.RestController,org.springframework.web.bind.annotation.RequestMapping
 interfaces
 super-class       +-java.lang.Object
 class-loader      +-sun.misc.Launcher$AppClassLoader@18b4aac2
                     +-sun.misc.Launcher$ExtClassLoader@54bff557
 classLoaderHash   18b4aac2

Affect(row-cnt:1) cost in 11 ms.

sc -f 显示变量信息
sc -d -f com.example.controller.RedisController
-f --field 显示所有变量信息,注:需要和-d一起使用,单独使用-f没有效果
报文这里略了。
变量都会展示,如:

  • 成员变量
  • logger
  • 注入的对象等

sc -cs 查找类加载器

sc文档(界面)
参数名称参数说明
class-pattern类名表达式匹配
method-pattern方法名表达式匹配
[d]输出当前类的详细信息,包括这个类所加载的原始文件来源、类的声明、加载的 ClassLoader 等详细信息。
如果一个类被多个 ClassLoader 所加载,则会出现多次
[E]开启正则表达式匹配,默认为通配符匹配
[f]输出当前类的成员变量信息(需要配合参数-d 一起使用)
[x:]指定输出静态变量时属性的遍历深度,默认为 0,即直接使用 toString 输出
[c:]指定 class 的 ClassLoader 的 hashcode
[classLoaderClass:]指定执行表达式的 ClassLoader 的 class name
[n:]具有详细信息的匹配类的最大数量(默认为 100)
[cs <arg>]指定 class 的 ClassLoader#toString() 返回值。长格式[classLoaderStr <arg>]

提示:
class-pattern 支持全限定名,如 com.taobao.test.AAA,也支持 com/taobao/test/AAA 这样的格式,这样,我们从异常堆栈里面把类名拷贝过来的时候,不需要在手动把/替换为.啦。

提示:
sc 默认开启了子类匹配功能,也就是说所有当前类的子类也会被搜索出来,想要精确的匹配,请打开options disable-sub-class true开关

sc文档(命令行)
[arthas@18344]$ help sc
 USAGE:
   sc [-c <value>] [--classLoaderClass <value>] [-cs <value>] [-d] [-x <value>] [-f] [-h] [-n <value>] [-E] class-pattern

 SUMMARY:
   Search all the classes loaded by JVM

 EXAMPLES:
   sc -d org.apache.commons.lang.StringUtils
   sc -d org/apache/commons/lang/StringUtils
   sc -d *StringUtils
   sc -d -f org.apache.commons.lang.StringUtils
   sc -E org\\.apache\\.commons\\.lang\\.StringUtils

 WIKI:
   https://arthas.aliyun.com/doc/sc

 OPTIONS:
 -c, --classloader <value>                   The hash code of the special class's classLoader
     --classLoaderClass <value>              The class name of the special class's classLoader.
 -cs, --classLoaderStr <value>               The return value of the special class's ClassLoader#toString().
 -d, --details                               Display the details of class
 -x, --expand <value>                        Expand level of object (0 by default)
 -f, --field                                 Display all the member variables
 -h, --help                                  this help
 -n, --limits <value>                        Maximum number of matching classes with details (100 by default)
 -E, --regex                                 Enable regular expression to match (wildcard matching by default)
 <class-pattern>                             Class name pattern, use either '.' or '/' as separator
sm - 查看已加载类的方法信息

monitor/watch/trace

options

options是设置选项用的,有很多有用的功能。

options unsafe

unsafe开关。
options unsafe true # 打开(慎用,会允许一些不安全的操作)
options unsafe false # 关闭(默认)

这个居然把值改了,厉害:

ognl '@com.example.controller.RedisController@setAge(2)' # 设置值(前提有这个方法)
ognl '@com.example.controller.RedisController@age' # 查看值

注:这个值应该是静态变量,而且要加上get、set方法。非静态变量不能直接获取。(这难道就是为什么叫做get方法的原因吗?)

options strict
options文档(界面)

直接把官网界面复制过来(勿动)

名称默认值描述
unsafefalse是否支持对系统级别的类进行增强,打开该开关可能导致把 JVM 搞挂,请慎重选择!
dumpfalse是否支持被增强了的类 dump 到外部文件中,如果打开开关,class 文件会被 dump 到/${application working dir}/arthas-class-dump/目录下,具体位置详见控制台输出
batch-re-transformtrue是否支持批量对匹配到的类执行 retransform 操作
json-formatfalse是否支持 json 化的输出
disable-sub-classfalse是否禁用子类匹配,默认在匹配目标类的时候会默认匹配到其子类,如果想精确匹配,可以关闭此开关
support-default-methodtrue是否支持匹配到 default method, 默认会查找 interface,匹配里面的 default method。参考 #1105 在新窗口打开
save-resultfalse是否打开执行结果存日志功能,打开之后所有命令的运行结果都将保存到~/logs/arthas-cache/result.log
job-timeout1d异步后台任务的默认超时时间,超过这个时间,任务自动停止;比如设置 1d, 2h, 3m, 25s,分别代表天、小时、分、秒
print-parent-fieldstrue是否打印在 parent class 里的 filed
verbosefalse是否打印更多详细信息
stricttrue是否启用 strict 模式

options文档(命令行)

[arthas@22560]$ options
输出:
 LEVEL    TYPE      NAME               VALUE    SUMMARY                     DESCRIPTION
------------------------------------------------------------------------------------------------------------------------------------
 0        boolean   unsafe             false    Option to support system-l  This option enables to proxy functionality of JVM class
                                                evel class                  es. Due to serious security risk a JVM crash is possibl
                                                                            y be introduced. Do not activate it unless you are able
                                                                             to manage.
 1        boolean   dump               false    Option to dump the enhance  This option enables the enhanced classes to be dumped t
                                                d classes                   o external file for further de-compilation and analysis
                                                                            .
 1        boolean   batch-re-transfor  true     Option to support batch re  This options enables to reTransform classes with batch
                    m                           Transform Class             mode.
 2        boolean   json-format        false    Option to support JSON for  This option enables to format object output with JSON w
                                                mat of object output        hen -x option selected.
 1        boolean   disable-sub-class  false    Option to control include   This option disable to include sub class when matching
                                                sub class when class match  class.
                                                ing
 1        boolean   support-default-m  true     Option to control include   This option disable to include default method in interf
                    ethod                       default method in interfac  ace when matching class.
                                                e when class matching
 1        boolean   save-result        false    Option to print command's   This option enables to save each command's result to lo
                                                result to log file          g file, which path is ${user.home}/logs/arthas-cache/re
                                                                            sult.log.
 2        String    job-timeout        1d       Option to job timeout       This option setting job timeout,The unit can be d, h, m
                                                                            , s for day, hour, minute, second. 1d is one day in def
                                                                            ault
 1        boolean   print-parent-fiel  true     Option to print all fileds  This option enables print files in parent class, defaul
                    ds                           in parent class            t value true.
 1        boolean   verbose            false    Option to print verbose in  This option enables print verbose information, default
                                                formation                   value false.
 1        boolean   strict             true     Option to strict mode       By default, strict mode is true, not allowed to set obj
                                                                            ect properties. Want to set object properties, execute
                                                                            `options strict false`
当前目录是哪里

有时候会比较迷惑。
实际比较简单,打开cmd路径是什么,就是当前的home目录。
例如在桌面打开cmd:
C:\Users\PC> # C:\Users\PC 就是home目录。

其他

官网地址

https://arthas.aliyun.com/doc/
官网信息已经很全了,从用法到例子一应俱全。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值