2026/9/19 20:18:41

Arthas history 命令全解析:查看、检索与清理 Java 诊断会话的命令历史

Arthas history 命令全解析:查看、检索与清理 Java 诊断会话的命令历史 Arthas history 命令全解析查看、检索与清理 Java 诊断会话的命令历史【免费下载链接】arthasAlibaba Java Diagnostic Tool Arthas/Alibaba Java诊断利器Arthas项目地址: https://gitcode.com/gh_mirrors/ar/arthas本文以 Arthas 官方文档 history 命令说明 为主线结合 Arthas 开源仓库中 HistoryCommand.java、HistoryManagerImpl.java 等核心源码系统讲解 history 命令的用法、参数、持久化机制与底层实现原理。读完本文你将掌握如何查看、限制数量、清空 Arthas 服务器上的命令历史理解历史记录跨会话持久化的存储位置与安全处理逻辑并能通过 HTTP API 复用同一套历史管理能力。命令概览history是 Arthas 提供的基础命令之一其作用为打印命令历史view command history。在 Arthas 交互式终端中输入history即可看到当前 Arthas 服务器记录过的命令列表每行附带一个序号。命令入口定义在源码 HistoryCommand.java 中通过注解声明命令名、摘要与示例Name(history) Summary(Display command history) Description(Constants.EXAMPLE history\n history -c\n history 5\n) public class HistoryCommand extends AnnotatedCommand {命令的三种典型形态恰好对应三种使用场景history查看全部历史命令history 5查看最近 5 条历史命令history -c清空全部历史命令。核心特性历史是服务器级而非会话级官方文档中有一处关键提示tiphistory of commands will persisted in a file named history, so the history command can show all the history commands of current Arthas server, but not only history in current session.即命令历史会通过一个名为history的文件持久化因此history指令查看的是当前 Arthas 服务器的全部历史命令而不仅仅局限于当前这一轮会话中输入的命令。这意味着你反复 attach / 断开某 Java 进程只要 Arthas 服务器Bootstrap未重启之前会话输入过的命令依然能被检索到。参数说明history命令的参数极其精简官方文档给出的参数表如下参数名称参数说明[c:]clear all the history commands清空全部历史命令[n:]view the nearest 5 commands显示最近 n 条命令对照源码可以确认这两个参数的实现方式-c/--clear一个 flag 型选项不接收额外值设置后即进入清空分支Option(shortName c, longName clear, flag true, acceptValue false)n一个可选的位置参数Argument(index 0, argName n, required false)用于指定显示最近几条命令。文档表格中示例写的是 5实际传入任意正整数均可生效缺省时表示显示全部。从源码逻辑看n的默认值为-1处理时存在自动截断保护int size history.size(); if (n 0 || n size) { n size; // 超过总数或未指定时回退为显示全部 }也就是说即使你输入history 99999也只会输出实际存在的全部历史不会产生越界或异常。使用示例查看最近 N 条命令# 查看最近执行的 3 条命令 $ history 3 269 thread 270 cls 271 history 3输出格式为5 位右对齐的序号 两个空格 命令原文。序号是命令在全部历史中的全局编号history 3取的是全部历史中最后 3 条。源码中序号计算与输出逻辑如下for (int i 0; i n; i) { int[] line history.get(n - i - 1); sb.append(String.format(%5s , size - (n - i - 1))); Helper.appendCodePoints(line, sb); sb.append(\n); }注意其遍历方向是倒序的从最新的命令开始向前取n条但展示顺序仍是旧在上、新在下序号保持与历史中的全局编号一致。清空全部历史命令# 清空历史指令 $ history -c $ history 3 1 history 3清空后再次查询历史列表重新从序号 1 开始计数——示例中清空后再执行history 3输出里只有刚刚输入的这一条history 3命令本身。历史命令的持久化文件位置与生命周期官方文档特别强调历史被持久化到名为history的文件中。这个文件具体在哪里源码 Constants.java 给出了确切定义public static final String CMD_HISTORY_FILE System.getProperty(user.home) File.separator .arthas File.separator history;即历史文件路径为~/.arthas/historyuser.home为运行 Arthas 的 Java 进程所属用户的 home 目录。它是按用户维度存放的同一用户启动的 Arthas 服务器共享同一份历史文件。持久化的读写发生在两个环节启动加载Arthas 终端初始化时从文件恢复历史。TermImpl.java 中有readline.setHistory(FileUtils.loadCommandHistory(new File(Constants.CMD_HISTORY_FILE)));退出保存Arthas 服务器退出shutdown时把当前内存中的历史写回文件FileUtils.saveCommandHistory(readline.getHistory(), new File(Constants.CMD_HISTORY_FILE));文件读写由 FileUtils.java 中的saveCommandHistory/loadCommandHistory完成每行一条命令统一使用 UTF-8 编码保存时自动创建缺失的父目录openOutputStream会mkdirs。源码级原理终端与 HTTP API 的双通道实现history命令在 HistoryCommand.java 中根据当前会话的载体分为两条处理路径通道一交互式终端Telnet / WebConsole当会话携带Session.TTY且其实现是TermImpl时命令直接操作底层 termd 的ReadlineObject termObject session.get(Session.TTY); if (termObject instanceof TermImpl) { TermImpl term (TermImpl) termObject; Readline readline term.getReadline(); Listint[] history readline.getHistory(); if (clear) { readline.setHistory(new ArrayListint[]()); } else { // 倒序输出最近 n 条 } }这里的历史以int[]codepoint 数组形式保存在 termd 的Readline中输出时用io.termd.core.util.Helper.appendCodePoints还原为可读字符串。通道二HTTP API当通过 HTTP API如 http-api.md 描述的接口调用时会话中没有TermImpl则走HistoryManagerHistoryManager historyManager ArthasBootstrap.getInstance().getHistoryManager(); if (clear) { historyManager.clearHistory(); } else { ListString history historyManager.getHistory(); process.appendResult(new HistoryModel(new ArrayListString(history))); }HistoryManager由 Arthas 服务器单例 ArthasBootstrap.java 持有this.historyManager new HistoryManagerImpl();其默认实现 HistoryManagerImpl.java 在内存中维护一份ListString并提供saveHistory/loadHistory/clearHistory/addHistory/getHistory等方法同样与~/.arthas/history文件互通。HTTP 路径的结果通过 HistoryModel.java 返回getType()返回history便于客户端识别结果类型。从源码结构看HistoryManager与终端Readline目前仍是两套历史载体HistoryCommand中留有 TODO 注释计划统一收口到HistoryManager但二者最终都落盘到同一份history文件。容量上限与清空策略HistoryManagerImpl.java 中定义了内存中历史记录条数的上限private static final int MAX_HISTORY_SIZE 500;当历史数量达到 500 条后新增命令会从队首逐条淘汰history.remove(0)保证内存与文件不会无限膨胀。因此查看全部历史实际最多可见最近 500 条记录。执行history -c清空的是 Arthas 服务器内存中的历史终端路径下为readline.setHistory(...)HTTP 路径下为historyManager.clearHistory()由于保存动作发生在服务器退出时若清空后立即退出服务器落盘文件也将以清空后的状态被覆盖保存。安全细节auth 命令的脱敏处理历史记录中可能包含敏感命令FileUtils.java 对这类命令做了脱敏当保存的命令以auth开头时isAuthCommand判断写入文件的内容会被替换为仅保留auth关键字本身避免密码等敏感参数被明文落盘private static boolean isAuthCommand(String command) { return command ! null command.trim().startsWith(ArthasConstants.AUTH); }saveCommandHistory与saveCommandHistoryString两个保存路径均应用了该逻辑。这意味着~/.arthas/history中不会残留 auth 命令的完整明文参数从侧面保障了诊断会话的安全边界。小结与使用建议history命令虽然功能简单却是 Arthas 会话中高频使用的效率工具快速回看history n直接获取最近 n 条命令配合上下方向键翻查历史可大幅减少重复输入跨会话检索由于历史持久化在~/.arthas/history只要 Arthas 服务器未重启任何会话输入的指令都可通过history全局检索隐私清理history -c一键清空适合在共享环境或演示结束后清理操作痕迹自动化集成HTTP API 场景下可通过HistoryManager获得结构化的HistoryModel结果便于上层工具集成命令审计能力。如需查看更多 Arthas 命令的用法可继续阅读 commands.md若关心 HTTP API 如何调用history可参考 http-api.md。【免费下载链接】arthasAlibaba Java Diagnostic Tool Arthas/Alibaba Java诊断利器Arthas项目地址: https://gitcode.com/gh_mirrors/ar/arthas创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考