爆款云主机2核4G限时秒杀,88元/年起!
查看详情

活动

天翼云最新优惠活动,涵盖免费试用,产品折扣等,助您降本增效!
热门活动
  • 618智算钜惠季 爆款云主机2核4G限时秒杀,88元/年起!
  • 免费体验DeepSeek,上天翼云息壤 NEW 新老用户均可免费体验2500万Tokens,限时两周
  • 云上钜惠 HOT 爆款云主机全场特惠,更有万元锦鲤券等你来领!
  • 算力套餐 HOT 让算力触手可及
  • 天翼云脑AOne NEW 连接、保护、办公,All-in-One!
  • 中小企业应用上云专场 产品组合下单即享折上9折起,助力企业快速上云
  • 息壤高校钜惠活动 NEW 天翼云息壤杯高校AI大赛,数款产品享受线上订购超值特惠
  • 天翼云电脑专场 HOT 移动办公新选择,爆款4核8G畅享1年3.5折起,快来抢购!
  • 天翼云奖励推广计划 加入成为云推官,推荐新用户注册下单得现金奖励
免费活动
  • 免费试用中心 HOT 多款云产品免费试用,快来开启云上之旅
  • 天翼云用户体验官 NEW 您的洞察,重塑科技边界

智算服务

打造统一的产品能力,实现算网调度、训练推理、技术架构、资源管理一体化智算服务
智算云(DeepSeek专区)
科研助手
  • 算力商城
  • 应用商城
  • 开发机
  • 并行计算
算力互联调度平台
  • 应用市场
  • 算力市场
  • 算力调度推荐
一站式智算服务平台
  • 模型广场
  • 体验中心
  • 服务接入
智算一体机
  • 智算一体机
大模型
  • DeepSeek-R1-昇腾版(671B)
  • DeepSeek-R1-英伟达版(671B)
  • DeepSeek-V3-昇腾版(671B)
  • DeepSeek-R1-Distill-Llama-70B
  • DeepSeek-R1-Distill-Qwen-32B
  • Qwen2-72B-Instruct
  • StableDiffusion-V2.1
  • TeleChat-12B

应用商城

天翼云精选行业优秀合作伙伴及千余款商品,提供一站式云上应用服务
进入甄选商城进入云市场创新解决方案
办公协同
  • WPS云文档
  • 安全邮箱
  • EMM手机管家
  • 智能商业平台
财务管理
  • 工资条
  • 税务风控云
企业应用
  • 翼信息化运维服务
  • 翼视频云归档解决方案
工业能源
  • 智慧工厂_生产流程管理解决方案
  • 智慧工地
建站工具
  • SSL证书
  • 新域名服务
网络工具
  • 翼云加速
灾备迁移
  • 云管家2.0
  • 翼备份
资源管理
  • 全栈混合云敏捷版(软件)
  • 全栈混合云敏捷版(一体机)
行业应用
  • 翼电子教室
  • 翼智慧显示一体化解决方案

合作伙伴

天翼云携手合作伙伴,共创云上生态,合作共赢
天翼云生态合作中心
  • 天翼云生态合作中心
天翼云渠道合作伙伴
  • 天翼云代理渠道合作伙伴
天翼云服务合作伙伴
  • 天翼云集成商交付能力认证
天翼云应用合作伙伴
  • 天翼云云市场合作伙伴
  • 天翼云甄选商城合作伙伴
天翼云技术合作伙伴
  • 天翼云OpenAPI中心
  • 天翼云EasyCoding平台
天翼云培训认证
  • 天翼云学堂
  • 天翼云市场商学院
天翼云合作计划
  • 云汇计划
天翼云东升计划
  • 适配中心
  • 东升计划
  • 适配互认证

开发者

开发者相关功能入口汇聚
技术社区
  • 专栏文章
  • 互动问答
  • 技术视频
资源与工具
  • OpenAPI中心
开放能力
  • EasyCoding敏捷开发平台
培训与认证
  • 天翼云学堂
  • 天翼云认证
魔乐社区
  • 魔乐社区

支持与服务

为您提供全方位支持与服务,全流程技术保障,助您轻松上云,安全无忧
文档与工具
  • 文档中心
  • 新手上云
  • 自助服务
  • OpenAPI中心
定价
  • 价格计算器
  • 定价策略
基础服务
  • 售前咨询
  • 在线支持
  • 在线支持
  • 工单服务
  • 建议与反馈
  • 用户体验官
  • 服务保障
  • 客户公告
  • 会员中心
增值服务
  • 红心服务
  • 首保服务
  • 客户支持计划
  • 专家技术服务
  • 备案管家

了解天翼云

天翼云秉承央企使命,致力于成为数字经济主力军,投身科技强国伟大事业,为用户提供安全、普惠云服务
品牌介绍
  • 关于天翼云
  • 智算云
  • 天翼云4.0
  • 新闻资讯
  • 天翼云APP
基础设施
  • 全球基础设施
  • 信任中心
最佳实践
  • 精选案例
  • 超级探访
  • 云杂志
  • 分析师和白皮书
  • 天翼云·创新直播间
市场活动
  • 2025智能云生态大会
  • 2024智算云生态大会
  • 2023云生态大会
  • 2022云生态大会
  • 天翼云中国行
天翼云
  • 活动
  • 智算服务
  • 产品
  • 解决方案
  • 应用商城
  • 合作伙伴
  • 开发者
  • 支持与服务
  • 了解天翼云
      • 文档
      • 控制中心
      • 备案
      • 管理中心

      【java程序运行监控byteman】使用示例(运行中方法耗时监控、javaagent监控、jvm监控、规则的检查、是否运行以及查看等)

      首页 知识中心 软件开发 文章详情页

      【java程序运行监控byteman】使用示例(运行中方法耗时监控、javaagent监控、jvm监控、规则的检查、是否运行以及查看等)

      2023-07-06 08:31:52 阅读次数:431

      java,jvm,监控

      本文介绍了byteman的其他几种应用场景及示例,比如javaagent、监控jvm、bmjava命令、如何查看运行的规则、检查规则的正确性、检查规则是否在运行中等。 本文分为2个部分,即运行中方法耗时监控和其他示例。

      一、 统计方法耗时(程序运行中)

      该类是实时显示控制台输入的结果以及输入的是end时退出。 程序运行中是表示程序处于运行中进行某些方法的耗时监控。

      1、创建示例类

      package com.win.byteman;
       
      import java.io.BufferedReader;
      import java.io.DataInputStream;
      import java.io.IOException;
      import java.io.InputStreamReader;
       
      /**
       * @author alan 2018年12月26日
       */
      public class WorkerDemo {
         public static void main(String[] args) {
            WorkerDemo workerDemo = new WorkerDemo();
            DataInputStream dataInputStream = new DataInputStream(System.in);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(dataInputStream));
            try {
               String next;
               while ((next = bufferedReader.readLine()) != null) {
                  System.out.println(
                         "==========================================开始调用==========================================");
                  workerDemo.working(next);
                  System.out.println(
                         "==========================================结束调用==========================================");
                  if (next.contains("end")) {
                      break;
                  }
               }
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
       
         private void working(String input) {
            System.out.println("输入内容:" + input);
      //    try {
      //       TimeUnit.SECONDS.sleep(2);
      //    } catch (InterruptedException e) {
      //       e.printStackTrace();
      //    }
         }
      }
      

      2、建立监控

      1)、获取运行程序的进程号

      在cmd中输入jps,结果如下图: 2、【java程序运行监控byteman】使用示例(运行中方法耗时监控、javaagent监控、jvm监控、规则的检查、是否运行以及查看等)

      2)、建立监控

      在cmd中执行命令

      bminstall -b -Dorg.jboss.byteman.tranform.all 13456
      

      2、【java程序运行监控byteman】使用示例(运行中方法耗时监控、javaagent监控、jvm监控、规则的检查、是否运行以及查看等)

      3、编制脚本

      WorkerDemo.btm 文件存储在:D:\application\byteman-download-4.0.5\sample\testing 规则如下:

      RULE trace worker time start
       
      CLASS WorkerDemo
      METHOD working(String)
      AT ENTRY
      IF TRUE
      DO createTimer($0)
      ENDRULE
       
      RULE trace worker time stop
      CLASS WorkerDemo
      METHOD working(String)
      AT EXIT
      IF TRUE
      DO traceln("                           working method  spent [" + getElapsedTimeFromTimer($0) + "] ms                            ");
          deleteTimer($0);
      ENDRULE
      

      4、检测脚本

      脚本检测需要建立起脚本与需要监控的类之间的关系。 目录需要切换到classes所在的目录,btm文件所在的目录以实际的为准,本示例中所在的地址是D:\application\byteman-download-4.0.5\sample\testing。 在cmd中将目录cd到classes所在的目录,然后执行命令

      bmcheck -p com.win.byteman -cp . D:\application\byteman-download-4.0.5\sample\testing\WorkerDemo.btm
      

      如果执行成功则如下图所示 2、【java程序运行监控byteman】使用示例(运行中方法耗时监控、javaagent监控、jvm监控、规则的检查、是否运行以及查看等) 否则则可能有异常抛出,例如 2、【java程序运行监控byteman】使用示例(运行中方法耗时监控、javaagent监控、jvm监控、规则的检查、是否运行以及查看等) 该异常的原因是在btm所在的目录找不到classes对应的包目录。 当然,如果方便起见,也可以将btm放在与classes目录下,统一切换到classes目录下操作即可。

      5、提交脚本

      同样,需要在classes目录下提交脚本,执行命令

      bmsubmit -i D:\application\byteman-download-4.0.5\sample\testing\WorkerDemo.btm
      

      如果成功则如下图所示 2、【java程序运行监控byteman】使用示例(运行中方法耗时监控、javaagent监控、jvm监控、规则的检查、是否运行以及查看等)

      6、验证

      在运行程序控制台输入任意参数,查看其结果 2、【java程序运行监控byteman】使用示例(运行中方法耗时监控、javaagent监控、jvm监控、规则的检查、是否运行以及查看等)

      7、卸载脚本

      卸载脚本就是不再对程序进行监控。在cmd中执行命令

      bmsubmit -u D:\application\byteman-download-4.0.5\sample\testing\WorkerDemo.btm
      

      2、【java程序运行监控byteman】使用示例(运行中方法耗时监控、javaagent监控、jvm监控、规则的检查、是否运行以及查看等)

      二、其他示例

      1、javaagent示例(程序未运行)

      1)、创建示例类

      package com.win.byteman;
       
      class App1
      {
           public static void main(String[] args)
           {
               for (int i = 0; i < args.length; i++) {
                   System.out.println(args[i]);
               }
           }
      }
      

      2)、编译及测试

      通过cmd命令行进行编译。 cd到classes所在的目录(即包文件夹所在的目录)或者直接到类所在的目录进行编译,即 D:\application\byteman-download-4.0.5\sample\testing 或 D:\application\byteman-download-4.0.5\sample\testing\com\win\byteman 执行编译命令:

      Javac com/win/byteman/App1.java
      # 或
      Java App1.java
      

      完成上述步骤后,在cmd中运行编译后的classes文件,执行命令:

      Java com.win.byteman.App1 testing
      

      2、【java程序运行监控byteman】使用示例(运行中方法耗时监控、javaagent监控、jvm监控、规则的检查、是否运行以及查看等) 执行java命令时需要带上包名,同时需要切换目录至classes所在的目录

      3)、编制脚本

      该脚本放在与classes目录下,当然也可以放在其他地方,但是在执行脚本的时候需要加上脚本所在目录。 脚本中如果有中文,会出现乱码,具体不知道是规则脚本中不能有中文字符还是其他的原因。 脚本名称:app1.btm

      RULE trace main entry
      
      CLASS App1
      METHOD main
      AT ENTRY
      IF true
      DO traceln("entering main")
      ENDRULE
       
      RULE trace main exit
      CLASS App1
      METHOD main
      AT EXIT
      IF true
      DO traceln("exiting main")
      ENDRULE
      

      关于脚本说明: 该脚本有针对App1类的main方法两个规则。 第一个规则是进入该方法时输出:entering main 第二个规则是退出该方法时输出:exiting main

      This script contains two rules both of which specify code to be injected into METHOD main of CLASS AppMain. The first rule is injected AT ENTRY i.e. at the start of the method. The code to be injected is the IF DO part. In the first rule it calls builtin operation traceln(String) which prints its argument to System.out() followed by a newline . The second rule is injected AT EXIT i.e. at the point(s) where control returns from method main. Both rules specify condition IF true which means the DO part is always executed. You can supply a different Java expression if you want to compute whether or not the actions following DO are run.

      4)、运行脚本

      运行脚本的命令根据实际需要而定。 运行命令时需要关注btm文件所在的位置,本示例是放在classes所在的目录,如果不在该目录下则直接带上绝对地址即可。 当然在运行脚本前也可以通过bmcheck命令检查脚本是否合法,具体参考本文的第一个示例中的检测脚本章节。 The -javaagent option used to run the progarm with these Byteman rules is supported by all the main desktop and server JVMs. The syntax is

      -javaagent : path_to_agent_jar = agent_option1 , agent_option_2 , . . .
      

      The Linux command to run the program with Byteman and this rule set is

      java -javaagent:${BYTEMAN_HOME}/lib/byteman.jar=script:app1.btm com.win.byteman.App1 foo bar baz
      

      or on Windows

      java -javaagent:%BYTEMAN_HOME%\lib\byteman.jar=script:app1.btm com.win.byteman.App1 testing again
      

      命令说明: 告诉虚拟机在哪里找到agent的jar,本机是%BYTEMAN_HOME%\lib\byteman.jar 本示例中仅是the = sign,指定规则script:app1.btm。agent通过加载的btm文件读取操作,如果需要加载更多的btm文件则配置更多的btm文件即可。 -javaagent是java命令的一个参数,com.win.byteman.App1 foo bar baz 是java执行的具体的类及传递的参数。

      The value after the : tells the JVM where to find the Byteman agent jar, ${BYTEMAN_HOME}/lib/byteman.jar. We only supply one agent option following the = sign, script:app1.btm, specifying the location of the Byteman rule script. The Byteman agent reads this option then loads and injects the rules from file app1.btm. If we wanted to load more than one script we could provide multiple script:file agent options, separated by a comma.

      When you pass -javaagent on the command line the Byteman agent starts executing during JVM startup before the application is run. So, it will read the rules and inject side effects into App1.main() before this method gets called. The output should be as follows.

      5)、测试

      运行脚本的结果正常的情况如下图: 2、【java程序运行监控byteman】使用示例(运行中方法耗时监控、javaagent监控、jvm监控、规则的检查、是否运行以及查看等) 异常的结果(之一)如下图: 2、【java程序运行监控byteman】使用示例(运行中方法耗时监控、javaagent监控、jvm监控、规则的检查、是否运行以及查看等)

      2、监控jvm的类

      1)、创建示例类

      package com.win.byteman;
       
      class App2
      {
          public static void main(String[] args)
          {
              for (int i = 0; i < args.length; i++) {
                  final String arg = args[i];
                  Thread thread = new Thread(arg) {
                      public void run() {
                      System.out.println(arg);
                      }
                  };
       
                  thread.start();
                  try {
                      thread.join();
                  } catch (Exception e) {
                  }
              }
          }
      }
      

      2)、编制脚本

      该脚本放在与classes目录下,当然也可以放在其他地方,但是在执行脚本的时候需要加上脚本所在目录。 脚本名称:thread.btm

      RULE trace thread start
      CLASS java.lang.Thread
      METHOD start()
      IF true
      DO traceln("*** start for thread: "+ $0.getName())
      ENDRULE
      

      规则说明: 该规则是监控Thread的start方法,输出跟踪的内容。

      This rule is injected into METHOD start() of the JVM runtime CLASS java.lang.Thread. It prints a trace message pasted together using the String + operator. Now start() is an instance method so when it is called there is a specific Thread instance which is the target for the method call.

      The special variable $0 can be used to refer to this target object. start() has no arguments but in other cases where there are the arguments to the call which triggers the rule they can be referenced using $1, $2 etc.

      Byteman knows that $0 references a Thread object so it type checks the method invocation $0.getName() and verifies that the result type is String. The injected code makes a call this method, appends the result to the constant String and passes the result to method traceln() to be written to System.out.

      3)、运行脚本

      运行脚本,由于是监控jvm的类,所以需要按照如下的命令进行。 To run this program using the new script we modify the script: agent option to reference file thread.btm. But that's not enough if we want to inject code into JVM classes. We need to provide some extra options/arguments. On Linux:

      java -javaagent:${BYTEMAN_HOME}/lib/byteman.jar=script:thread.btm,boot:${BYTEMAN_HOME}/lib/byteman.jar -Dorg.jboss.byteman.transform.all com.win.byteman.App2 foo bar baz
      

      or on Windows:

      java -javaagent:%BYTEMAN_HOME%\lib\byteman.jar=script:thread.btm,boot:%BYTEMAN_HOME%\lib\byteman.jar -Dorg.jboss.byteman.transform.all com.win.byteman.App2 foo bar baz
      

      本示例是针对jvm的Thread进行监控,所以通过bootstarp classloader加载。

      Class Thread is a JVM class which means it gets loaded by the bootstrap classloader. Byteman can only inject code into this class if the Byteman agent classes are also loaded by the bootstrap classloader. The extra agent option boot:${BYTEMAN_HOME}/lib/byteman.jar is appended after the script option (note the comma used as a separator). This makes sure that the byteman agent jar is installed into the bootstrap classpath.

      Class Thread also happens to be in package java.lang. Normally Byteman is super-cautious and will not inject code into this package in case it beaks the JVM. If you really want to change methods of classes in this package you need to define the system property org.jboss.byteman.transform.all.

      4)、验证

      运行脚本的结果正常的情况如下图: 2、【java程序运行监控byteman】使用示例(运行中方法耗时监控、javaagent监控、jvm监控、规则的检查、是否运行以及查看等)

      3、Bmjava命令的应用

      例子参考监控jvm示例和本文中的统计方法耗时示例。 On Linux you can use the command script bmjava.sh to wrap up the -javaagent options for you. It looks very much like the java command but it accepts Byteman rule scripts on the command line and bundles them up as -javaagent script: options. It also automatically bundles in the byteman jar using the boot: agent option. So, using bmjava.sh the previous command line simplifies to

      bmjava.sh -i thread.btm com.win.byteman.App2 foo bar baz
      

      Notice that the flag used there is -i for load. On Windows if you are using byteman release 2.0.1 or later there is an equivalent script called bmjava.bat which you can execute using command bjava. So the previous command simplifies to

      bmjava -i thread.btm com.win.byteman.App2 foo bar baz
      

      Note that you need to add the installed bin directory ${BYTEMAN_HOME}/bin to your path in orderto be able to execute the script by name (on windows use %BYETMAN_HOME%/bin)

      4、注入正在运行的类(jvm类)

      本示例是监控运行中的Thread,监控普通的类是否可以使用本方式没有测试(理论上是可以的),同时,监控运行中的类也可以采用本文的统计方法耗时(程序运行中)部分的说明。

      1)、创建示例类

      package com.win.byteman;
       
      import java.io.DataInputStream;
       
      class App3
      {
          public static void main(String[] args)
          {
              try {
              DataInputStream in = new DataInputStream(System.in);
              String next = in.readLine();
              while (next != null && next.length() > 0 && !next.contains("end")) {
              final String arg = next;
              Thread thread = new Thread(arg) {
                  public void run() {
                      System.out.println(arg);
                  }
                  };
              thread.start();
              try {
                  thread.join();
              } catch (Exception e){
              }
              next = in.readLine();
              }
          } catch (Exception e) {
          }
          }
      }
      

      编译略。

      2)、编制脚本

      该文件在classes所在目录。 脚本名称:thread.btm

      RULE trace thread start
      CLASS java.lang.Thread
      METHOD start()
      IF true
      DO traceln("----------------------- start for thread: "+ $0.getName())
      ENDRULE
      

      3)、建立监控

      本示例中建立监控的方式,与本文中其他的方式有所不同,理论上应该都可以,但没有做测试。 The agent listener is enabled on the java command line using agent option listener:true. On Linux the command is this

      java -javaagent:${BYTEMAN_HOME}/lib/byteman.jar=listener:true,boot:${BYTEMAN_HOME}/lib/byteman.jar -Dorg.jboss.byteman.transform.all com.win.byteman.App3
      

      or on Windows

      java -javaagent:%BYTEMAN_HOME%\lib\byteman.jar=listener:true,boot:%BYTEMAN_HOME%\lib\byteman.jar -Dorg.jboss.byteman.transform.all com.win.byteman.App3
      

      建立监控成功后测试,如下图: 2、【java程序运行监控byteman】使用示例(运行中方法耗时监控、javaagent监控、jvm监控、规则的检查、是否运行以及查看等)

      4)、提交脚本

      java -classpath %BYTEMAN_HOME%\lib\byteman-submit.jar org.jboss.byteman.agent.submit.Submit -l thread.btm
      

      提交成功,则出现如下界面 2、【java程序运行监控byteman】使用示例(运行中方法耗时监控、javaagent监控、jvm监控、规则的检查、是否运行以及查看等)

      5)、验证

      在cmd中输入需要测试的内容,测试内容如下图红框中所示 2、【java程序运行监控byteman】使用示例(运行中方法耗时监控、javaagent监控、jvm监控、规则的检查、是否运行以及查看等)

      5、如何查看在运行的规则

      Running bmsubmit.sh again we can see that the rule has been successfully compiled. 执行命令:

      bmsubmit.sh
      

      输出:

      # File thread.btm line 4
      RULE trace thread start
      CLASS java.lang.Thread
      METHOD start()
      AT ENTRY
      IF true
      DO traceln("*** start for thread: "+ $0.getName())
      ENDRULE
      Transformed in:
      loader: sun.misc.Launcher$AppClassLoader@5acac268
      trigger method: java.lang.Thread.start() void
      compiled successfully
      

      This time there is an extra line at the end of the output saying compiled successfully. This means the rule has been type-checked and executed. Type checking only happens when the rule is first triggered -- in this case when Thread.start() is called after typing in the line containing the word baz. If type checking had failed then execution of the injected code would be inhibited and the listing would include details of the type error. Injected code is only removed when the rule is unloaded.

      6、如何在一个运行的程序中安装一个代理

      参考本文的使用示例中统计方法耗时(运行中)的例子。

      7、如何检查规则的正确性

      使用bmcheck命令进行检查。

      1)、示例规则-thread.btm

      由于本示例使用的是Thread,jvm的类,所以不需要。

      RULE trace thread start
      CLASS java.lang.Thread
      METHOD start()
      IF true
      DO traceln("----------------------- start for thread: "+ $0.getName())
      ENDRULE
      另外一种形式的规则设置
      RULE trace thread start
      CLASS Thread
      METHOD start()
      IF true
      DO traceln("----------------------- start for thread: "+ $0.getName())
      ENDRULE
      

      On Linux you run command script bmcheck.sh

      >bmcheck.sh thread.btm
      #或者
      >bmcheck.sh -p java.lang -cp . thread.btm
      

      checking rule trace thread start parsed rule "trace thread start" for class java.lang.Thread type checked rule "trace thread start"

      TestScript: no errors On Windows if you are usiing release 2.0.1 or later use the equivalent script bmcheck.bat.

      > bmcheck thread.btm
      # 或者
      > bmcheck  -p java.lang -cp . thread.btm
      

      checking rule trace thread start parsed rule "trace thread start" for class java.lang.Thread type checked rule "trace thread start"

      TestScript: no errors

      2)、示例规则-App1.btm

      示例类:

      package com.win.byteman;
       
      class App1
      {
           public static void main(String[] args)
           {
               for (int i = 0; i < args.length; i++) {
                   System.out.println(args[i]);
               }
           }
      }
      

      编译完成后,做下面的操作。

      This rule does not use the fully qualified name of the target class. That's ok because the agent notices when com.win.byteman.App1 gets loaded and realises that the rule applies to it. But when we are checking rules offline the application is not run so Byteman does not know which package to use to lookup the class mentioned in the rule. The -p flag can be used to suggest one or more package prefixes for the type checker to try.

      RULE trace main entry
      CLASS App1
      METHOD main
      AT ENTRY
      IF true
      DO traceln("进入 main")
      ENDRULE
       
      RULE trace main exit
      CLASS App1
      METHOD main
      AT EXIT
      IF true
      DO traceln("退出 main")
      ENDRULE
      或者另外一种形式
      RULE trace main entry
      CLASS com.win.byteman.App1
      METHOD main
      AT ENTRY
      IF true
      DO traceln("进入 main")
      ENDRULE
       
      RULE trace main exit
      CLASS com.win.byteman.App1
      METHOD main
      AT EXIT
      IF true
      DO traceln("退出 main")
      ENDRULE
      

      执行如下命令

      bmcheck -p com.win.byteman -cp . app1.btm
      #或者
      bmcheck app1.btm
      

      3)、验证

      略

      8、如何确定规则是否运行

      1)、示例类

      package com.win.byteman;
       
      class App1
      {
           public static void main(String[] args)
           {
               for (int i = 0; i < args.length; i++) {
                   System.out.println(args[i]);
               }
           }
      }
      

      2)、规则

      app1.btm

      RULE trace main entry
      CLASS com.win.byteman.App1
      METHOD main
      AT ENTRY
      IF true
      DO traceln("entry main")
      ENDRULE
       
      RULE trace main exit
      CLASS com.win.byteman.App1
      METHOD main
      AT EXIT
      IF true
      DO traceln("exit main")
      ENDRULE
      

      Sometimes you may be unsure whether your rules are being injected correctly. Perhaps the target method is never executed. Perhaps the rule condition is always false. Or maybe a parse or type error is stopping them being executed. If you run Byteman in verbose mode you will see trace output telling you what Byteman is doing. Verbose mode is enabled by setting a system property 执行命令:

      java -Dorg.jboss.byteman.verbose -javaagent:%BYTEMAN_HOME%/lib/byteman.jar=script:app1.btm com.win.byteman.App1 foo bar baz
      

      2、【java程序运行监控byteman】使用示例(运行中方法耗时监控、javaagent监控、jvm监控、规则的检查、是否运行以及查看等) The trace is quite verbose but all the information you need is there if you look for it.

      • When class AppMain is loaded the trigger locations for each rule is identified and the rule code is injected

      Next the main method gets called and you can see an execute message for the entry rule (you can ignore the messages about helper activation and rule installation just now or read the Ptrogrammer's Guide if you want them explained)

      • The execute message is followed by the the enter rule's trace message.
      • The main method prints all its its ouptut
      • Just before it returns there is another execute message for the exit rule (again, just ignore the messages about rule installation)
      • The second execute message is followed by the exit rule's trace message .

      以上,本文介绍了byteman的其他几种应用场景及示例,比如javaagent、监控jvm、bmjava命令、如何查看运行的规则、检查规则的正确性、检查规则是否在运行中等。

      版权声明:本文内容来自第三方投稿或授权转载,原文地址:https://blog.51cto.com/alanchan2win/6501275,作者:一瓢一瓢的饮,版权归原作者所有。本网站转在其作品的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如因作品内容、版权等问题需要同本网站联系,请发邮件至ctyunbbs@chinatelecom.cn沟通。

      上一篇:C++多文件编程介绍

      下一篇:javaWeb,web服务器

      相关文章

      2025-05-14 10:02:58

      java休眠到指定时间怎么写

      java休眠到指定时间怎么写

      2025-05-14 10:02:58
      java , sleep , Thread , util , 方法
      2025-05-14 10:02:58

      java项目多端数据同步解决方案

      多端数据同步是指在多个设备(例如桌面应用、移动应用、Web应用)之间保持数据的一致性。

      2025-05-14 10:02:58
      java , Spring , WebSocket , 同步 , 数据 , 版本号
      2025-05-14 09:51:15

      java怎么对线程池做监控

      对Java线程池进行监控是确保系统性能和稳定性的重要部分。监控线程池可以帮助我们了解线程池的状态,如当前活跃线程数、任务队列长度、已完成任务数等。

      2025-05-14 09:51:15
      Java , 方法 , 监控 , 示例 , 线程 , 队列
      2025-05-13 09:49:12

      Java学习(动态代理的思想详细分析与案例准备)(1)

      Java学习(动态代理的思想详细分析与案例准备)(1)

      2025-05-13 09:49:12
      java , 代理 , 代码 , 对象 , 接口 , 方法 , 需要
      2025-05-12 08:40:18

      C#目录文件监控

      C#目录文件监控

      2025-05-12 08:40:18
      控制台 , 源码 , 监控 , 目录 , 程序
      2025-05-09 08:51:09

      【Linux 从基础到进阶】系统资源监控与报警

      【Linux 从基础到进阶】系统资源监控与报警

      2025-05-09 08:51:09
      CPU , 工具 , 监控
      2025-05-09 08:20:32

      基于IDEA的Maven简单工程创建及结构分析

      通过一个 mvn 命令直接让我们创建一个 Maven 的脚手架。

      2025-05-09 08:20:32
      java , Maven , xml , 创建 , 文件 , 文件夹 , 项目
      2025-05-08 09:03:57

      前K个高频元素java

      给定一个非空的整数数组,返回其中出现频率前 前K个高频元素java 高的元素。

      2025-05-08 09:03:57
      java , 元素 , 样例 , 给定
      2025-05-08 09:03:21

      基于java Swing开发的学生成绩管理系统【项目源码+数据库脚本】

      基于java Swing开发的学生成绩管理系统【项目源码+数据库脚本】

      2025-05-08 09:03:21
      java , Swing , 学生 , 源码
      2025-05-08 09:03:21

      java Swing学生成绩管理系统【项目源码+数据库脚本】

      本项目是一套基于java Swing开发的学生成绩管理系统,主要针对计算机相关专业的正在做bishe的学生和需要项目实战练习的Java学习者。

      2025-05-08 09:03:21
      java , 学生 , 成绩 , 数据库 , 源码
      查看更多
      推荐标签

      作者介绍

      天翼云小翼
      天翼云用户

      文章

      33561

      阅读量

      5266835

      查看更多

      最新文章

      java项目多端数据同步解决方案

      2025-05-14 10:02:58

      java怎么对线程池做监控

      2025-05-14 09:51:15

      Java学习(动态代理的思想详细分析与案例准备)(1)

      2025-05-13 09:49:12

      C#目录文件监控

      2025-05-12 08:40:18

      前K个高频元素java

      2025-05-08 09:03:57

      java Swing学生成绩管理系统【项目源码+数据库脚本】

      2025-05-08 09:03:21

      查看更多

      热门文章

      JAVA__接口的作用

      2023-04-18 14:14:13

      Java学习之算术运算符两只老虎

      2023-04-19 09:23:13

      排序算法Java版-归并排序算法

      2023-04-24 11:25:19

      JAVA多线程学习笔记

      2023-05-11 06:05:48

      try...catch...finally java

      2023-03-29 09:40:26

      Java:apache.poi读写Excel文件

      2023-02-22 06:40:54

      查看更多

      热门标签

      java Java python 编程开发 代码 开发语言 算法 线程 Python html 数组 C++ 元素 javascript c++
      查看更多

      相关产品

      弹性云主机

      随时自助获取、弹性伸缩的云服务器资源

      天翼云电脑(公众版)

      便捷、安全、高效的云电脑服务

      对象存储

      高品质、低成本的云上存储服务

      云硬盘

      为云上计算资源提供持久性块存储

      查看更多

      随机文章

      有接口的CGLIB动态代理实现

      java并发编程-StampedLock高性能读写锁

      java编程:jedis连接redis数据库实例

      Java统计水仙花数

      java synchronized 锁住同一个类的静态方法和非静态方法会互斥吗?

      Nginx Lua 之Shared Dict get()方法在存入字符串超长的情况下不支持高并发

      • 7*24小时售后
      • 无忧退款
      • 免费备案
      • 专家服务
      售前咨询热线
      400-810-9889转1
      关注天翼云
      • 旗舰店
      • 天翼云APP
      • 天翼云微信公众号
      服务与支持
      • 备案中心
      • 售前咨询
      • 智能客服
      • 自助服务
      • 工单管理
      • 客户公告
      • 涉诈举报
      账户管理
      • 管理中心
      • 订单管理
      • 余额管理
      • 发票管理
      • 充值汇款
      • 续费管理
      快速入口
      • 天翼云旗舰店
      • 文档中心
      • 最新活动
      • 免费试用
      • 信任中心
      • 天翼云学堂
      云网生态
      • 甄选商城
      • 渠道合作
      • 云市场合作
      了解天翼云
      • 关于天翼云
      • 天翼云APP
      • 服务案例
      • 新闻资讯
      • 联系我们
      热门产品
      • 云电脑
      • 弹性云主机
      • 云电脑政企版
      • 天翼云手机
      • 云数据库
      • 对象存储
      • 云硬盘
      • Web应用防火墙
      • 服务器安全卫士
      • CDN加速
      热门推荐
      • 云服务备份
      • 边缘安全加速平台
      • 全站加速
      • 安全加速
      • 云服务器
      • 云主机
      • 智能边缘云
      • 应用编排服务
      • 微服务引擎
      • 共享流量包
      更多推荐
      • web应用防火墙
      • 密钥管理
      • 等保咨询
      • 安全专区
      • 应用运维管理
      • 云日志服务
      • 文档数据库服务
      • 云搜索服务
      • 数据湖探索
      • 数据仓库服务
      友情链接
      • 中国电信集团
      • 189邮箱
      • 天翼企业云盘
      • 天翼云盘
      ©2025 天翼云科技有限公司版权所有 增值电信业务经营许可证A2.B1.B2-20090001
      公司地址:北京市东城区青龙胡同甲1号、3号2幢2层205-32室
      • 用户协议
      • 隐私政策
      • 个人信息保护
      • 法律声明
      备案 京公网安备11010802043424号 京ICP备 2021034386号