arthas 深入使用技巧

介绍arthas使用的一些技巧

arthas有一下几个默认对象:

1
2
3
4
params 参数
target 当前对象
returnObj 返回值
throwExp 异常

调用具体时间

1
-b(调用前)、 -e(异常时)、-s(返回后)、-f(结束后)

通用参数

1
2
3
-n 限制打印的条数,如程序执行中,可能有些方法会疯狂打印。
-i 设置打印间隔时间,单位毫秒
-x 属性遍历深度,默认为1。

1、tt 命令详细使用

1
2
3
4
5
6
过滤制定参数
tt -t *UserLoanBiz getOvdRecordCnt 'params[0].toString()=="386134842398343168"'

修改第一个参数,然后调用实例的getIContracts方法
tt -i 1101 -w "#a=params[0].setLimit(1),target.getIContracts(#a)"

2、获取系统bean

2.1

通过spring mvc 获取bean

1
2
3
tt -t org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter invokeHandlerMethod
# 之后通过如下命令来获取具具体bean
tt -i 1000 -w 'target.getApplicationContext().getBean("YourBeanName").fun()'

2.2

自定义类获取Spring ApplicationContent 想办法拿到项目中 ApplicationContext 对象。ognl只获取静态属性,所以我们一般需要查找项目中是否存在静态的ApplicationContext对象。

这里面我就自己创建了一个类来提供静态的ApplicationContext。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class BeanFactory implements ApplicationContextAware, DisposableBean {
private static ApplicationContext ctx;
private static BeanFactory singleton = new BeanFactory();

private BeanFactory() {
}

public static BeanFactory getInstance() {
return singleton;
}

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ctx = applicationContext;
}

public void destroy() throws Exception {
ctx = null;
}

public static <T> T getBean(String name) {
return ctx.getBean(name);
}

public static <T> T getBean(Class<T> clazz) {
return clazz.cast(BeanFactoryUtils.beanOfTypeIncludingAncestors(ctx, clazz));
}
}

然后通过如下的命令获取任务想要的bean,simpleMockContainer为你想要获取的bean名称

1
ognl '@com.xxx.admin.utils.BeanFactory@ctx.getBean("simpleMockContainer")' -x 2

参考

Alibaba Arthas实践--获取到Spring Context,然后为所欲为

使用Arthas 获取Spring ApplicationContext还原问题现场