Dynamic Java Tracing with Instrumentation, BTrace, and ASM

The article explains how Java developers can dynamically trace and modify program behavior at runtime using the Instrumentation API, ASM bytecode manipulation, and the safe, annotation‑driven BTrace tool, illustrating techniques such as class redefinition, method interception, and examples like JSP recompilation and Arthas diagnostics.

Meituan Technology Team
Meituan Technology Team
Meituan Technology Team
Dynamic Java Tracing with Instrumentation, BTrace, and ASM

The article explores Java runtime tracing techniques that allow modifying class behavior without restarting the JVM.

It starts with a dialogue illustrating common debugging challenges and introduces JSP as an example where code changes take effect instantly because the web container recompiles the JSP into a new servlet class using a fresh ClassLoader.

It then discusses how Java objects consist of attributes (per‑instance) and behavior (shared methods) stored in the JVM's method area, and how changing behavior at runtime requires redefining classes.

The java.lang.instrument.Instrumentation API provides redefineClasses and retransformClasses methods to replace or transform existing class bytecode, albeit with safety restrictions.

public class Person {

  private int age;

  private String name;

  public void speak(String str) {
    System.out.println(str);
  }

  public Person(int age, String name) {
    this.age = age;
    this.name = name;
  }
}

Direct bytecode manipulation can be performed with frameworks such as ASM, which underlies many tools like cglib and Spring AOP.

BTrace is presented as a safe, annotation‑driven tracing tool built on ASM, the Attach API, and Instrumentation. Sample BTrace scripts are shown that intercept methods in java.io and count thread creations.

package com.sun.btrace.samples;

import com.sun.btrace.annotations.*;
import com.sun.btrace.AnyType;
import static com.sun.btrace.BTraceUtils.*;

@BTrace
public class ArgArray {
    @OnMethod(clazz="/java\\.io\\..*/", method="/read.*/")
    public static void anyRead(@ProbeClassName String pcn, @ProbeMethodName String pmn, AnyType[] args) {
        println(pcn);
        println(pmn);
        printArray(args);
    }
}

Arthas, an Alibaba open‑source diagnostic tool, is mentioned as a higher‑level solution that wraps similar instrumentation capabilities.

The article concludes that, despite limitations, runtime instrumentation has become a powerful means for developers to diagnose and monitor Java applications.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaJVMInstrumentationbytecodedynamic tracingBTrace
Meituan Technology Team
Written by

Meituan Technology Team

Over 10,000 engineers powering China’s leading lifestyle services e‑commerce platform. Supporting hundreds of millions of consumers, millions of merchants across 2,000+ industries. This is the public channel for the tech teams behind Meituan, Dianping, Meituan Waimai, Meituan Select, and related services.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.