跳至主要內容

使用Arthas定位线上问题

chenkun问题定位大约 1 分钟

背景:有一个导出PDF的功能,在本地运行正常,在线上异常,抛出的异常无法直接定位到问题。

1、使用arthas来跟踪

  1. 进入arths,attach进入项目
$ as.sh
Arthas script version: 3.5.5
[INFO] JAVA_HOME: /usr/local/jdk1.8.0_72
[INFO] Process 57135 already using port 3658
[INFO] Process 57135 already using port 8563
Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
  [1]: 134242 ccs-gateway.jar
  [2]: 47802 ccs-data-biz.jar
2

  1. 根据线上log定位到异常大致的位置

image-20220428111351818

  1. 到本地IDE打开源码

image-20220428111556978

createPDF方法空指针,那首先肯定想到的是参数htmlStr是null

  1. 进入freemarkerRender方法查看,为何返回null

image-20220428112248916

  1. 直接查看关键处理函数template.process(dataMap, out);

  2. 使用arthas的watch命令,追踪参数、返回值、异常信息

watch freemarker.template.Template process '{params,returnObj,throwExp}'  -n 5  -x 3 '1==1'

追踪发现

image-20220428113034007

  1. 源码中因为异常未指定捕获TemplateModelException所以template.process(dataMap, out);方法报错后直接返回了null,所以直接把异常捕获改成ExcelpTion即可

    image-20220428113408828