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

71 lines
2.7 KiB
Markdown

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

在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}}
```bash
$ sc -d com.example.demo.arthas.user.UserController | grep classLoaderHash
classLoaderHash 1be6f5c3
```
注意hashcode是变化的需要先查看当前的ClassLoader信息提取对应ClassLoader的hashcode。
如果你使用`-c`你需要手动输入hashcode`-c <hashcode>`
```bash
$ ognl -c 1be6f5c3 @com.example.demo.arthas.user.UserController@logger
```
对于只有唯一实例的ClassLoader可以通过`--classLoaderClass`指定class name使用起来更加方便
```bash
$ ognl --classLoaderClass org.springframework.boot.loader.LaunchedURLClassLoader @org.springframework.boot.SpringApplication@logger
@Slf4jLocationAwareLog[
FQCN=@String[org.apache.commons.logging.LogAdapter$Slf4jLocationAwareLog],
name=@String[org.springframework.boot.SpringApplication],
logger=@Logger[Logger[org.springframework.boot.SpringApplication]],
]
```
`--classLoaderClass` 的值是ClassLoader的类名只有匹配到唯一的ClassLoader实例时才能工作目的是方便输入通用命令而`-c <hashcode>`是动态变化的。
### 获取静态类的静态字段
获取`UserController`类里的`logger`字段:
`ognl --classLoaderClass org.springframework.boot.loader.LaunchedURLClassLoader @com.example.demo.arthas.user.UserController@logger`{{execute T2}}
还可以通过`-x`参数控制返回值的展开层数。比如:
`ognl --classLoaderClass org.springframework.boot.loader.LaunchedURLClassLoader -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}}
```bash
$ 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`表达式。
一些更复杂的用法,可以参考:
* OGNL特殊用法请参考https://github.com/alibaba/arthas/issues/71
* OGNL表达式官方指南https://commons.apache.org/proper/commons-ognl/language-guide.html