关于python:常见java错误arthas的使用常用命令

5次阅读

共计 5084 个字符,预计需要花费 13 分钟才能阅读完成。

arthas 的应用

arthas 是一款弱小的 Java 诊断工具,能够帮忙开发者疾速定位和解决 Java 应用程序中的问题。本文将介绍 arthas 的根本应用办法,包含装置、启动、根本命令和高级性能。

一、装置 arthas

1.1 Windows 零碎

在 Windows 零碎中,能够通过以下步骤装置 arthas:

  • 拜访 arthas 的 GitHub 发布页面:https://github.com/alibaba/arthas/releases
  • 下载最新版本的 arthas(以 arthas-boot.jar 为例),并将其解压到一个目录中,例如:C:arthas
  • 将 arthas 的 bin 目录增加到零碎的 PATH 环境变量中,例如:C:\arthas\bin
  • 关上命令提示符,输出arthas --version,如果显示 arthas 的版本信息,则示意装置胜利。

1.2 Linux 零碎

在 Linux 零碎中,能够通过以下步骤装置 arthas:

  • 拜访 arthas 的 GitHub 发布页面:https://github.com/alibaba/arthas/releases
  • 下载最新版本的 arthas(以 arthas-boot.jar 为例),并将其解压到一个目录中,例如:/opt/arthas
  • 将 arthas 的 bin 目录增加到零碎的 PATH 环境变量中,例如:/opt/arthas/bin
  • 关上终端,输出arthas --version,如果显示 arthas 的版本信息,则示意装置胜利。

二、启动 arthas

2.1 在线模式

在线模式下,arthas 会附加到指标 Java 过程上,实时监控和诊断 Java 应用程序。启动 arthas 的命令如下:

java -jar arthas-boot.jar < 指标过程 ID>

其中,< 指标过程 ID>是指标 Java 过程的 ID。能够通过 jps 命令查看以后运行的 Java 过程及其 ID。

2.2 离线模式

离线模式下,须要先导出指标 Java 过程的内存快照(core 文件),而后应用 arthas 剖析该快照。启动 arthas 的命令如下:

java -jar arthas-boot.jar --core <core 文件门路 >

其中,<core 文件门路 >是指标 Java 过程的内存快照文件门路。

三、根本命令

3.1 help 命令

help命令能够查看 arthas 反对的所有命令及其简要阐明。输出 help 后,会显示如下输入:

Usage: help [command] [options] -h | --help | --version | --description | --groups | --classloaders | --threads | --dependencies | --dashboard | --interceptors | --sc | --jad | --mc | --redefine | --watch | --option | --websocket | --telnet | --ssh | --local | --tunnel | --selectJvm | --selectClassLoader | --selectInstace | --attach | --detach | --quit | --close | --reset | --reload | --kill | --suspend | --resume | --interrupt | --interactive | --batch | --sampler [options] | --sample <pattern> [options] | --monitor <pattern> [options] | --trace <pattern> [options] | --stack <pattern> [options] | --tt <pattern> [options] | --info <pattern> [options] | --methods [options] | --params [options] | --bp <pattern> [options] | --jad <pattern> [options] | --mc <pattern> [options] | --redefine <pattern> [options] | --watch <pattern> [options] | --option <pattern> [options] | --dashboard [options] | --interceptors [options] | --sc <pattern> [options] | --jad <pattern> [options] | --mc <pattern> [options] | --redefine <pattern> [options] | --watch <pattern> [options] | --option <pattern> [options] | --dashboard [options] | --interceptors [options] | --sc <pattern> [options] | --jad <pattern> [options] | --mc <pattern> [options] | --redefine <pattern> [options] | --watch <pattern> [options] | --option <pattern> [options] | --dashboard [options]
br -b, -eBreakpoint, -nNew breakpoint, -cClear breakpoint, -lList breakpoints, -aAll breakpoints, -iInfo breakpoint, -xDisable breakpoint, -dDelete breakpoint, -hHelp, -vVersion, -tThread, -oOption, -fForce, -qQuick mode, -mMethod trace filter, -pPattern match, -rRange match, -uUser defined range match, -gGroup match, -LLine number match, -NNext line match, -CClass match, -EException match, -SSource match, -FField match, -DDebugger command match, -GGroup name match, -HHot swap match, -MMethod modifier match, -IInstance ID match, -RRemote IP match, -TThread CPU time threshold (ms), -WWaiting for matching thread to suspend, -XSkip matched method in stack trace, -YForce matched method in stack trace, -ZEnable class redefinition, -UUse agent built-in properties and variables as default values when parsing option arguments, -KKeep alive interval (ms), -PPlugin class name pattern match, -AAgent address match, -BBatch mode flag, -CConnect address and port of target JVM or agent process, -DConnect address and port of local JVM or agent process if tunnel is enabled, -EError message template for batch mode output, -FFile path pattern match for batch mode input and output files, -GGroup name pattern match for batch mode input and output files, -HHelp message template for batch mode output, IInterval between batch mode commands (ms), JNumber of times to repeat batch mode command execution, KKeep alive interval (ms), LLog level for batch mode output messages, MMessage template for batch mode output messages with placeholders for error codes and error messages from failed commands or scripts in batch mode output file(s), NNumber of lines to skip at the beginning of batch mode input file(s), OOutput file path for batch mode output messages and error messages from failed commands or scripts in batch mode output file(s), PPlugin class name pattern match for batch mode input and output files, QQuick mode flag for batch mode commands execution, RRemote IP pattern match for batch mode input and output files, SSource pattern match for batch mode input and output files, TTarget JVM or agent process ID or address pattern match for batch mode commands execution and input/output files selection if tunnel is enabled or not specified respectively

3.2 watch 参数的根本用法

watch 参数的根本用法非常简单,只须要在 arthas 命令行中输出 watch 关键字,前面跟上要监控的办法名即可。例如,咱们想要监控 com.example.demo.service.UserService 类的 addUser 办法,能够输出以下命令:

watch com.example.demo.service.UserService addUser

执行该命令后,arthas 会实时显示 addUser 办法的调用状况,包含入参、出参、异样等信息。如果须要退出 watch 模式,能够按 Ctrl+C 组合键。

watch 参数的高级用法

除了根本用法外,watch 参数还有一些高级用法,能够帮忙咱们更灵便地监控办法。以下是一些罕用的高级用法:

1. 指定类和办法

咱们能够应用通配符 * 来指定类和办法。例如,咱们想要监控所有以 com.example.demo.service. 结尾的服务类的所有办法,能够输出以下命令:

watch com.example.demo.service.* *

2. 指定返回值类型

咱们能够应用 -x 参数来指定返回值的类型。例如,咱们想要监控 com.example.demo.service.UserService 类的 getUserById 办法,只关怀返回值中的 idname字段,能够输出以下命令:

watch -x "{id: id, name: name}" com.example.demo.service.UserService getUserById

3. 指定条件表达式

咱们能够应用 -b 参数来指定条件表达式。例如,咱们想要监控 com.example.demo.service.UserService 类的 updateUser 办法,当更新胜利时才显示信息,能够输出以下命令:

watch -b "result == true" com.example.demo.service.UserService updateUser

4. 指定输入格局

咱们能够应用 -o 参数来指定输入格局。例如,咱们想要以 JSON 格局输入监控后果,能够输出以下命令:

watch -o json com.example.demo.service.UserService addUser

5. 指定日志级别

咱们能够应用 -l 参数来指定日志级别。例如,咱们想要以 debug 级别的日志输入监控后果,能够输出以下命令:

watch -l debug com.example.demo.service.UserService addUser
正文完
 0