Перейти до вмісту

Цю сторінку ще не перекладено, тому її показано англійською.

Insight for JVM programs

Trace Java and Kotlin project runs with elide run --insights: JavaScript Insight scripts or a Java insight class, method filters, frames, and what differs from Insight on JavaScript.

На цій сторінці
Open in ChatGPTOpen in Claude

elide run --insights=<VALUE> in a Java or Kotlin project attaches an instrumentation agent to the program’s JVM. The value is either a JavaScript Insight script or a Java class:

ValueRunsRepeatable
trace.jsA JavaScript script that calls insight.onYes
class:app.TraceA class on the program’s classpath with public static void insightmain(String, JvmInsight)No; cannot be mixed with scripts

--insights applies to project runs (elide run with a jvm.main) and single-file Java or Kotlin runs (elide run Main.java).

The examples trace this program:

// elide.pkl
amends "elide:project.pkl"

name = "insight-demo"

jvm {
  main = "app.Main"
}
// src/main/java/app/Main.java
package app;

public class Main {
  static int add(int a, int b) {
    return a + b;
  }

  public static void main(String[] args) {
    System.out.println(add(1, 2));
  }
}

Scripts

// trace.js
insight.on("enter", (ctx, frame) => {
  console.log("enter " + ctx.name + " a=" + frame.a + " b=" + frame.b);
}, { roots: true, rootNameFilter: "Lapp/Main;\\.add.*" });

insight.on("return", (ctx, frame) => {
  console.log("return " + ctx.name);
}, { roots: true, rootNameFilter: "Lapp/Main;\\.add.*" });
elide run --insights=trace.js
enter Lapp/Main;.add(II)I a=1 b=2
return Lapp/Main;.add(II)I
3

The first run downloads the JavaScript engine for the agent (about 67 MB of JARs) into the Maven cache. JVM warnings and agent log lines go to stderr.

Script APIOn a JVM program
Eventsenter and return; no source
rootNameFilterRequired; without it the handler never runs. A regular expression that must match the whole method name, L<class path>;.<method><descriptor>
roots, statementsMatch method entries, or each statement; no expressions
sourceFilterNot supported
ctx.nameThe method name in the form above
ctx.lineSource line for statements; -1 for roots
frameArguments and locals by name (frame.a)
ctx.returnValueNot available

Handlers see only the program’s own classes: a broad filter such as .* skips JDK classes and the insight engine itself.

Classes

A class on the program’s classpath receives the agent’s API and registers handlers with onMethod. The API is in the bundled org.apidesign.jvm:jvm-insight:0.2, which a project declares as compileOnly:

dependencies {
  maven {
    compileOnly { "org.apidesign.jvm:jvm-insight:0.2" }
  }
}
// src/main/java/app/Trace.java
package app;

import org.apidesign.jvm.insight.JvmInsight;

public class Trace {
  public static void insightmain(String args, JvmInsight insight) {
    insight.onMethod((method, builder) -> {
      if (method.clazz().name().equals("app.Main") && method.name().equals("add")) {
        builder.when(JvmInsight.When.ENTER).roots(true)
            .call((at, frame) -> System.out.println("enter " + at.method().name() + " " + frame));
      }
    });
  }
}
elide run --insights=class:app.Trace
enter add {a=1, b=2}
3
JvmInsight APIUse
onMethod((method, builder) -> …)Called for each method as its class loads; method.clazz().name(), method.name(), method.descriptor() identify it
builder.when(When.ENTER or When.RETURN)When the handler runs
builder.roots(true), builder.statements(true)Method entry, or each statement
builder.call((at, frame) -> …)The handler; at.method(), at.line(), and frame as a Map of arguments and locals

Class handlers need no download.

Чи була ця сторінка корисною?