Skip to content

2 Usage

Frank edited this page Jul 9, 2024 · 3 revisions

1 First of ALL

Assuming you have completed this step.

If you get a different WEB UI, don't worry. Just a UI update, content is same.

2 Watch

Watch is a function, that enhances specific method, and record the input and output params and time consuming.

Once you exec the cmd as following, you will get a string Base64(123) = MTIz

$ curl 'http://localhost:8080/base64?str=123'
Base64(123) = MTIz

Becuase the demo project has code like this

@GetMapping("/base64")
public ResponseEntity<String> base64(String str) {
   return ResponseEntity.ok("Base64("+str+") = " 
          + new BASE64Encoder().encode(str.getBytes()));
}

Enter the className#MethodName which is com.example.demo.DemoApplication#base64 to the input of the Watch section in the Web UI. Then click the watch button, will get some logs in the right log window, which shows the transformer is added success. (Make minCost be 0, it's a filter to print the log)

Run the cmd curl 'http://localhost:8080/base64?str=123' again , and have a look at the log window. The time consuming as well as input and output params have been printed, that is the Watch.

chrome_BRKV3frzwP

By default, the watch will effect 100 times. If you want to remove the watch monitor, click the effected class button, all of the effected classes will be list in a dialog. You can input the uuid of the transformer to delete it. Well, if you want to delete all the effects, click the reset button.

chrome_NuMB87uYXM

Of course, I think you have noticed that the log showed from newest to oldest.

3 OuterWatch

Similar with Watch, OuterWatch enhances the outer method to record same things.

Watch change the code of the method self:

// watch
void mechod() {
    ...some code    // change the code, insert some code before and after original code
} 

==> 
void mechod() {
    ...record input params and start time
    ...some code    // change the code, insert some code before and after original code
    ...record output params and end time - start time
} 

OuterWatch change the code of outMethod:

void outMethod() {
    ...some code
    innerMethod();   // change this line, insert some code before and after this line
    ...some code
}
==>
void outMethod() {
    ...some code
    ...record input params of innerMethod and start time
    innerMethod();   // change this line, insert some code before and after this line
    ...record output params and end time - start time
    ...some code
}

Outer watch the *#encode in the base64 as following, you noticed that, it is allowed that use a pattern * as the className of inner method. Well, the outer method is not.

chrome_bKbPEzsSpW

Run the curl command again, you will get the log in log window, line26 is the line number of the *#encode in base64, req, res belong to *#encode method.

$ curl http://localhost:8080/base64?str=123

ConEmu64_4WaY7FO9wF

Finally, remember to reset the effected classes.

4 Trace

Trace is similar to OuterWatch, OuterWatch effects the specific inner method, but Trace effects all inner methods. Well, Trace only record the time consuming, without the req and res. It can be used to analyze function time-consuming to know where the most time is spent. The method from java.lang.xx (exclude java.lang.Thread) will be ignore. In addtion, the method in lambda cannot be traced, because in actually, these methods belong to a anonymous class, instead of the current class.

chrome_Weno0aXQid

Rememer to reset the transformer.

5 ChangeBody

ChangeBody will change the body of specific method. Through the previous study, you should easily learn its usage, as follows:

ConEmu64_VM8flXN1bv

6 ChangeResult(OuterChangeBody)

The relationship between ChangeResult and ChangeBody is like OuterWatch and Watch.

You can change the return value of inner method using $_ = newReturnValue;. In addition, if you choose the javassist engine, you can also use the javassist built-in varibles, like $1, $args, $proceed and so on. Use $_ = 1 + $proceed($$); to execute the original method to get the result then plus 1. In the other hand if you choose the ASM engine, you can also use $_ $1 $proceed, exclude $$, use $_ = 1 + $proceed(); instead.

chrome_KSe2q3CxA0

7 Exec

Exec triggers the execution of a piece of code immediately~

What you need to know:

  • 1 In the class w.Global, I have prepare many useful methods for you code
    • info(Object) and error(Object, Throwable) print content to log window.
    • ognl("ognl expression") execute ognl expression.
    • reset() same with reset in web ui.
    • readFile(String) read file and return string lines.
    • getInstances(Class) get the top 100 instances of the specific class.
  • 2 For spring boot, you can use the built-in varible ctx, which is the SpringApplicationContext. Use ctx.getBean("beanName") to get the bean Object.
  • 3 In the editor area, including Exec and ChangeBody, int and Integer cannot auto box/unbox, because the javassist compiler is too simple to finish the job. Also, any new feature in jdk 4+ will not supported, including lambda, generic and so on. Become a primitive man, that is the price of powerful features.

chrome_JccjF2p1XS

8 ReplaceClass

Use a .class file to replace the class running in the jvm. Change the source code, compile to get the class file.

idea64_jU7T9SO3BK

Then upload this file, class will be changed.

ConEmu64_2i44lGTkSm

Clone this wiki locally