4 Replies Latest reply on Apr 30, 2015 6:50 AM by Wang Shuai

    How can I use "for ,while and if " in the script file?

    Wang Shuai Newbie

      Dear all,

       

      I want to inject some "for,while and if" instructions into myapp  . Could anyone tell  me how I can do it ?

       

      Thank all!

        • 1. Re: How can I use "for ,while and if " in the script file?
          Andrew Dinn Master

          Hi Wang Shuai,

          I want to inject some "for,while and if" instructions into myapp  . Could anyone tell  me how I can do it ?

           

          You cannot use for, while and if in the IF or DO clauses of a Byteman rule. This is a deliberate choice. If you want to use complex control flow then you need to implement a helper class and call a method of the helper class from your rule. For example, here is a rule which uses a helper class to print all elements of a list

           

          RULE dump list data
          CLASS org.my.app.MyClass
          METHODmyMethod(List)
          HELPER org.my.helper.MyHelper
          AT ENTRY
          IF TRUE
          DO dumpList($1)
          ENDRULE
          

          The helper class extends the default helper provided by Byteman and provides one extra public method which can be called from the rule. Notice that the new method uses the existing builtin methods trace() and traceln().

           

          package org.my.helper;
          import org.jboss.byteman.rule.helper.Helper;
          
          class MyHelper extends Helper
          {
            public void dumpList(List<?> list)
            {
              String prefix = "{";
              for (int i = 0; i < list.size(); i++) {
                trace(prefix);
                trace(list.get(i).toString());
                prefix = ", "
              }
             traceln("}";
            }
          }
          
          • 2. Re: How can I use "for ,while and if " in the script file?
            Wang Shuai Newbie

            Thanks! I will create the helper class.  Meanwhile could you public some material about the fundamental and architecture of ByteMan. Thanks again!

            • 3. Re: How can I use "for ,while and if " in the script file?
              Andrew Dinn Master

              Hi Wang Shuai

              Meanwhile could you public some material about the fundamental and architecture of ByteMan

               

              The User's Guide explains all that there is to know about using Byteman.

               

              There is not a lot of material about how Byteman is architected/designed.

               

              I suggest you look at the FOSDEM 2012 tutorial (video and slides are available on the Byteman documentation page) which explains a little bit about the JVMTI Java Agent API that Byteman relies on and how Byteman uses that API to change method code at runtime. The JavaSE documentation provides full details of this API (in particular look under Instrumentation at package java.lang.instrument).

               

              The AOSD paper (also on the documentation page) also provides some details of Byteman's architecture.

               

              After that you probably need to study the source code :-)