In btrace, how can I print a byte array in a readable format?

182 Views Asked by At

I want to use btrace to inspect the byte[] value of a method return use the @Return annotation.

The byte array is actually a normal string encoded using utf8.

The class is like below:

Class A {
  byte[] method1() {
    ...
  }
}

I have tried printArray, but it only accepts type of Objetc[], not working for type of byte[]. For print, it just outputs the internal object id like '[B@4fbc7b65'.

Is there any other way can solve the problem?

1

There are 1 best solutions below

2
JB- On

Yes, this is an omission in BTrace (https://github.com/btraceio/btrace/issues/322)

For now, use "trusted" mode where the safety checks will be turned off and you can do eg.

@BTrace(trusted = true)
public class TrustedTrace {
  @OnMethod(clazz = "MyClass", method = "m", location = Location(Kind.RETURN))
  public static void intercept(@Return byte[] data) {
    println(Arrays.toString(data));
  }
}