【常见java错误】arthas的使用、常用命令

arthas的使用

arthas是一款强大的Java诊断工具,可以帮助开发者快速定位和解决Java应用程序中的问题。本文将介绍arthas的基本使用方法,包括安装、启动、基本命令和高级功能。

一、安装arthas

1.1 Windows系统

在Windows系统中,可以通过以下步骤安装arthas:

  1. 访问arthas的GitHub发布页面:https://github.com/alibaba/arthas/releases
  2. 下载最新版本的arthas(以arthas-boot.jar为例),并将其解压到一个目录中,例如:C:arthas
  3. 将arthas的bin目录添加到系统的PATH环境变量中,例如:C:\arthas\bin
  4. 打开命令提示符,输入arthas --version,如果显示arthas的版本信息,则表示安装成功。

1.2 Linux系统

在Linux系统中,可以通过以下步骤安装arthas:

  1. 访问arthas的GitHub发布页面:https://github.com/alibaba/arthas/releases
  2. 下载最新版本的arthas(以arthas-boot.jar为例),并将其解压到一个目录中,例如:/opt/arthas
  3. 将arthas的bin目录添加到系统的PATH环境变量中,例如:/opt/arthas/bin
  4. 打开终端,输入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

猜你喜欢

转载自blog.csdn.net/luansj/article/details/134134735