Advanced Arthas Usage: Trace, Watch, OGNL Expressions and Spring Integration
This article provides a comprehensive guide to using Arthas for Java backend debugging, covering installation, core trace and watch commands, OGNL expression syntax, practical examples for inspecting method calls, accessing static fields, and integrating with Spring context via advanced command techniques.
The article begins with the installation steps for Arthas, showing how to download the startup script curl -L https://alibaba.github.io/arthas/install.sh | sh and configure an alias for easy execution.
It then introduces the online tutorial and a local demo plugin, encouraging readers to try the https://alibaba.github.io/arthas/arthas-tutorials?language=cn tutorial or the https://github.com/WangJi92/arthas-plugin-demo repository.
Global command options are explained, such as -x for result depth, -n for execution count, and -c for classloader hash.
Trace is presented with a basic example: trace com.wangji92.arthas.plugin.demo.controller.CommonController getRandomInteger -n 5 '1==1' The article notes that trace only follows matched method calls and can be extended with the -E option for multi‑class tracing.
Watch is described similarly, with an example that observes parameters, return values and exceptions: watch com.wangji92.arthas.plugin.demo.controller.CommonController traceE '{params,returnObj,throwExp}' -n 5 -x 3 '1==1' It also shows how to filter by condition, e.g., watch com.wangji92.arthas.plugin.demo.controller.CommonController traceE '{params,returnObj,throwExp}' -n 5 -x 3 'params.length > 0' .
The article explains the core variables used in OGNL expressions (params, returnObj, throwExp, etc.) and provides the source snippet of the Advice class that holds these variables.
Practical expression examples demonstrate how to retrieve static fields and Spring beans, such as: ognl -x 3 '@com.wangji92.arthas.plugin.demo.controller.StaticTest@INVOKE_STATIC_DOUBLE' -c e374b99 and watch com.wangji92.arthas.plugin.demo.controller.StaticTest * '{params,returnObj,throwExp,@com.wangji92.arthas.plugin.demo.controller.StaticTest@INVOKE_STATIC_DOUBLE}' -n 5 -x 3 '1==1' .
Advanced usage shows how to obtain the Spring application context and query configuration properties: ognl -x 3 '#springContext=@com.wangji92.arthas.plugin.demo.common.ApplicationContextProvider@context,#springContext.getEnvironment().getProperty("custom.name")' -c e374b99 and how to list all environment properties using a complex OGNL expression.
Finally, the article promotes the Arthas IDEA plugin ( https://plugins.jetbrains.com/plugin/13581-arthas-idea ) that helps generate commands automatically, integrates static variable access, trace‑E, tt, and other advanced features directly within the IDE.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.