You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
arthas/tutorials/katacoda/arthas-advanced-cn/ognl.md

1.6 KiB

在Arthas里有一个单独的ognl命令,可以动态执行代码。

调用static函数

ognl '@java.lang.System@out.println("hello ognl")'{{execute T2}}

可以检查Terminal 1里的进程输出,可以发现打印出了hello ognl

查找UserController的ClassLoader

sc -d com.example.demo.arthas.user.UserController | grep classLoaderHash{{execute T2}}

$ sc -d com.example.demo.arthas.user.UserController | grep classLoaderHash
 classLoaderHash   1be6f5c3

获取静态类的静态字段

获取UserController类里的logger字段:

ognl -c 1be6f5c3 @com.example.demo.arthas.user.UserController@logger{{execute T2}}

还可以通过-x参数控制返回值的展开层数。比如:

ognl -c 1be6f5c3 -x 2 @com.example.demo.arthas.user.UserController@logger{{execute T2}}

执行多行表达式赋值给临时变量返回一个List

ognl '#value1=@System@getProperty("java.home"), #value2=@System@getProperty("java.runtime.name"), {#value1, #value2}'{{execute T2}}

$ ognl '#value1=@System@getProperty("java.home"), #value2=@System@getProperty("java.runtime.name"), {#value1, #value2}'
@ArrayList[
    @String[/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/jre],
    @String[Java(TM) SE Runtime Environment],
]

更多

在Arthas里ognl表达式是很重要的功能,在很多命令里都可以使用ognl表达式。

一些更复杂的用法,可以参考: