Arthas help 命令完全指南:查看命令清单与单个命令用法详解
2026/9/19 21:40:46 网站建设 项目流程

Arthas help 命令完全指南:查看命令清单与单个命令用法详解

【免费下载链接】arthasAlibaba Java Diagnostic Tool Arthas/Alibaba Java诊断利器Arthas项目地址: https://gitcode.com/gh_mirrors/ar/arthas

本篇技术指南围绕 Alibaba Java 诊断工具 Arthas 的help命令展开,说明如何通过help查看当前 Arthas 服务器支持的全部命令清单,以及如何查看任意单个命令的详细用法(USAGE、SUMMARY、EXAMPLES、OPTIONS)。读完本文,你将掌握help命令 -help/--help/-h的关系,并理解 Arthas 帮助信息在源码层面的生成与补全机制,从而在日常排查中快速定位并正确使用各诊断命令。

help 命令是什么

help是 Arthas 内置的基础命令之一,用于显示帮助信息。它有两大能力:

  • 不带参数:列出当前 Arthas 服务器支持的所有命令及其描述(NAME / DESCRIPTION 列表);
  • 带命令名:展示指定命令的详细用法说明(USAGE、SUMMARY、EXAMPLES、WIKI、OPTIONS)。

在 Arthas 中,help 命令命令 -help是等价的,两者都会展示同一条命令的详细用法。

该命令在源码中定义于 HelpCommand.java,通过@Name("help")@Summary("Display Arthas Help")注解声明命令名与摘要,属于basic1000命令包下的基础命令。

参数说明

参数名称参数说明
不接参数查询当前 Arthas 版本支持的指令以及指令描述
[name:]查询具体指令的使用说明

其中name为具体的命令名,例如scsmwatchdashboard等。在源码中,该参数通过@Argument(index = 0, argName = "cmd", required = false)声明(见 HelpCommand.java),因此它是可选参数——不传时展示全部命令列表,传入时则精确匹配对应命令。

不带参数:查看当前 Arthas 支持的全部命令

在 Arthas 控制台中直接输入help,即可看到当前版本支持的命令清单,例如:

$ help NAME DESCRIPTION help Display Arthas Help auth Authenticates the current session keymap Display all the available keymap for the specified connection. sc Search all the classes loaded by JVM sm Search the method of classes loaded by JVM classloader Show classloader info jad Decompile class getstatic Show the static field of a class monitor Monitor method execution statistics, e.g. total/success/failure count, average rt, fail rate, etc. stack Display the stack trace for the specified class and method thread Display thread info, thread stack trace Trace the execution time of specified method invocation. watch Display the input/output parameter, return object, and thrown exception of specified method invocation tt Time Tunnel jvm Display the target JVM information perfcounter Display the perf counter information. ognl Execute ognl expression. mc Memory compiler, compiles java files into bytecode and class files in memory. redefine Redefine classes. @see Instrumentation#redefineClasses(ClassDefinition...) retransform Retransform classes. @see Instrumentation#retransformClasses(Class...) dashboard Overview of target jvm's thread, memory, gc, vm, tomcat info. dump Dump class byte array from JVM heapdump Heap dump options View and change various Arthas options cls Clear the screen reset Reset all the enhanced classes version Display Arthas version session Display current session information sysprop Display, and change the system properties. sysenv Display the system env. vmoption Display, and update the vm diagnostic options. logger Print logger info, and update the logger level history Display command history cat Concatenate and print files base64 Encode and decode using Base64 representation echo write arguments to the standard output pwd Return working directory name mbean Display the mbean information grep grep command for pipes. tee tee command for pipes. profiler Async Profiler. https://github.com/jvm-profiling-tools/async-profiler stop Stop/Shutdown Arthas server and exit the console.

该输出覆盖了 Arthas 日常诊断的绝大多数场景:类与方法检索(sc/sm/jad)、方法执行观测(watch/trace/stack/monitor/tt)、JVM 信息(jvm/dashboard/thread/vmoption)、类增强与重置(redefine/retransform/reset)、文件与管道工具(cat/base64/echo/grep/tee)以及会话管理(auth/keymap/history/session/stop)等。

从源码结构看,该列表并非硬编码,而是动态收集当前会话中所有CommandResolver提供的命令后生成的(见 HelpCommand.java 的allCommands方法),并在createHelpModel中过滤掉cli == null或标记为隐藏(isHidden())的命令(HelpCommand.java)。这意味着列表会随 Arthas 版本及动态加载的命令(如外部命令)变化,以实际输出为准。

带命令名:查看单个命令的详细用法

当需要了解某个命令的具体参数时,使用help 命令名,例如:

$ help dashboard USAGE: dashboard [-h] [-i <value>] [-n <value>] SUMMARY: Overview of target jvm's thread, memory, gc, vm, tomcat info. EXAMPLES: dashboard dashboard -n 10 dashboard -i 2000 WIKI: https://arthas.aliyun.com/doc/dashboard OPTIONS: -h, --help this help -i, --interval <value> The interval (in ms) between two executions, default is 5000 ms. -n, --number-of-execution <value> The number of times this command will be executed.

dashboard为例,输出分为六部分:

  • USAGE:命令的标准调用形式,[-i <value>][-n <value>]为可选参数,[-h]为帮助选项;
  • SUMMARY:一句话描述命令用途;
  • EXAMPLES:可直接复制运行的示例,如dashboard -n 10(执行 10 次)、dashboard -i 2000(采样间隔 2000ms);
  • WIKI:该命令对应的在线文档地址;
  • OPTIONS:逐一列出选项的短名(-i)、长名(--interval)、参数占位符(<value>)与说明,包括默认值(dashboard默认采样间隔为 5000ms)。

如何读懂 OPTIONS 中的默认值

help dashboard可以看到-i, --interval <value>的说明明确标注 "default is 5000 ms",这表示dashboard默认每 5000ms 采样一次。理解这些默认值有助于在实战中判断输出节奏,例如dashboard -i 2000可加快刷新频率,便于快速观察指标变化。

其他命令的用法示例

help同样适用于其他命令,例如:

$ help watch

会展示watch的表达式书写、-x展开级别、-n执行次数等完整参数说明;help schelp smhelp trace同理。在 HelpCommand.java 的@Description中即给出了helphelp schelp smhelp watch的典型用法示例。

help 与 -help / --help / -h 的关系

Arthas 中查看单个命令用法的途径有两种,效果等价:

  • help 命令名,例如help dashboard
  • 命令 -help,例如dashboard -help

此外,几乎每个命令都支持-h/--help选项。从源码看,这一行为由命令基类统一保证:在 AnnotatedCommandImpl.java 中,每个命令的 CLI 定义都会统一追加一个Option,其短名为-h、长名为--help、描述为 "this help"、并标记为setHelp(true)。因此,所有基于注解实现的 Arthas 命令都天然具备-h/--help能力,无需各自重复实现。

底层实现原理:帮助信息是如何生成的

1. 命令收集

help执行时,先从当前会话的CommandResolver列表中收集全部命令(allCommands),然后分两种分支处理(HelpCommand.java):

  • 未找到匹配命令(findCommand返回 null)→ 输出命令清单;
  • 找到匹配命令 → 输出该命令的详细帮助模型(createHelpDetailModel)。

2. 帮助信息的数据来源

命令的元信息(摘要、用法、选项、参数)并非硬编码在help里,而是取自每个命令自身的CLI定义:

  • SUMMARY来自命令注解@Summary
  • USAGEStyledUsageFormatter.computeUsageLine根据 CLI 选项与参数计算得出;
  • OPTIONS遍历cli.getOptions()ARGUMENTS遍历cli.getArguments(),并过滤隐藏项(见 HelpCommand.java)。

3. 输出模型

帮助结果的载体是HelpModel(HelpModel.java),其typehelp:无参数时承载List<CommandVO>(命令清单),有参数时承载单个detailCommand(命令详情)。该模型同样服务于 Arthas 的 HTTP API 与 Web Console,意味着你在浏览器控制台里看到的帮助输出与终端一致。

4. Tab 键补全支持

help还实现了命令名补全:在输入help后按 Tab 键,会基于当前所有可见命令名进行自动补全(见 HelpCommand.java 的complete方法)。同样会过滤隐藏命令,因此补全列表与help输出保持严格一致。

实战建议

  • 初入陌生环境先敲help:确认当前 Arthas 版本支持的命令范围,避免使用不存在的命令;
  • help 命令名代替翻文档:单个命令的 USAGE / OPTIONS / 默认值一目了然,且与当前运行版本严格匹配,不会出现文档与版本脱节的问题;
  • 借助 Tab 补全提速:记不清命令全名时,输入help后按 Tab 即可在候选命令间补全;
  • 脚本化场景同样可用help输出的是结构化文本(NAME/DESCRIPTION、USAGE/OPTIONS),在批量脚本或自动化巡检中可用于校验命令可用性;HTTP API 模式下help同样可用,便于远程/网页端查看命令用法。

小结

help是 Arthas 中最基础也最常用的导航命令:无参数时给出当前服务器支持的完整命令清单,带命令名时给出单个命令的完整用法,且与命令 -help(以及-h/--help)等价。其实现完全基于命令自身元数据动态生成,并支持 Tab 补全,是理解 Arthas 命令体系、快速上手的入口。相关实现可进一步阅读 HelpCommand.java、AnnotatedCommandImpl.java 与 HelpModel.java。

【免费下载链接】arthasAlibaba Java Diagnostic Tool Arthas/Alibaba Java诊断利器Arthas项目地址: https://gitcode.com/gh_mirrors/ar/arthas

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询