This is the 27th day of my participation in Gwen Challenge

series

  • Basic Concept and Introduction of “BTrace” (1)

  • “BTrace” installation introduction and Working Principles (2) – not completed

  • “BTrace” field code for debugging using (3) – not completed

  • “BTrace” runtime exception cause analysis (4) – Not completed

Background (Pain point analysis)

  • Having problems online?
  • Service online problem, want to increase the print log how to do?
  • If I suspect an interface is slow online, what should I do if I want to print the interface?
  • An error is reported on an interface. What if you want to check the parameters of the call and who called it?
  • What if I want to see the data of an object?
  • What if I want to see some information about the JVM?
  • Not sure if a line of code executes on the line?

Traditional solutions

  • Modify source code -> add related print log -> Hotswap (hot load + hot refresh)
  • Thread.dumpstack () : Prints information data in the current heap
  • Beanshell can view data in memory
  • JVM information can be retrieved through JVM built-in commands: the main method is to place information in the thread heap on jStack

disadvantages

  • Code intrusion
  • inflexible
  • Source code redundancy
  • What if your service doesn’t support Hotswap?

BTrace application scenarios

  1. Service is slow, can you find out which step is slow, which function?
  2. Who called System.gc() and what about the call stack?
  3. Who constructed a huge ArrayList?
  4. An input parameter or object property that caused the exception to be thrown? You go into this processing branch, right?
  5. For cases where there is no exception stack, exceptions can be output in real time.

Basic introduction to BTrace

Btrace is a dynamic Trace tool developed by SUN Company. It is a secure and reliable dynamic Trace tool in Java. The way he works is throughinstrument + asmTo dynamically enhance class classes in running Java programs. It is safe and reliable because it is read-only to running programs. That is, he can insert trace statements to detect and analyze a running program without allowing it to be modified.

BTrace open source address

The biggest benefit of Btrace is that you can write your own scripts and apply the call information in real time without having to constantly reboot the system. Btrace is currently hosted on Github at Btrace -Github official address, Btrace official documentation. You can study it.

Alternatively, you can directly click Release to download the ZIP and decompress it for use.

BTrace restrictions

So he has some limitations:

  • Cannot create object
  • Cannot create an array
  • Exceptions cannot be thrown and caught
  • You cannot call any object methods or static methods
  • Class static properties and object properties in the target program cannot be assigned
  • There can be no external, inner, and nested classes
  • Cannot have synchronized blocks and synchronized methods
  • No loops (for, while, do.. while)
  • Cannot inherit any classes
  • Unable to implement interface
  • Assert assertion statements cannot be included

These restrictions can actually be circumvented using the Unsafe mode. Declare the @btrace (unsafe = true) annotation and run BTrace in unsafe mode -u

When tracing in unsafe mode is used, a problem is found. If a process is detected by bTrace in secure mode once, the unsafe mode does not take effect.

BTrace fundamentals

In general, BTrace is based on dynamic bytecode modification technology (Hotswap) to trace and replace Java programs at run time.

The general principle can be described by the following formula:

Client(Java compile API + Attach API) + Agent (script parsing engine + ASM + JDK6 Instumentation) + Socket

In fact, BTrace uses the Java Attach API to attach agent. Jar, and then uses the script parsing engine + ASM to rewrite the bytecode of the specified class, and then uses the instrument to replace the original class.

BTrace Precautions

The proper use of Btrace can make online problem locating much faster, but be careful to narrow it down to a test environment before executing Btrace scripts, or you may crash the JVM. So it’s best to use an IDE to write code when writing scripts.

BTrace concludes

BTrace is a killer that checks and resolves problems online. BTrace can get all the information about the execution of the program by writing a script and, notice, without restarting the service. Yes, without restarting the service. Write a good script, execute it directly with the command, without moving the code of the original program.