Debugging

Debugging java agent can be a challenging task since some instrumentation code is directly inlined into target classes.

调试java代理可能是一项具有挑战性的任务,因为有些仪器代码直接内联到目标类中。

Advice methods

Breakpoints do not work in advice methods, because their code is directly inlined
by ByteBuddy into the target class. It is good to keep these methods as small as possible.
The advice methods are annotated with:

断点在Advice方法中不起作用,因为它们的代码是由ByteBuddy直接内联到目标类中。保持这些方法尽可能的小是好事。Advice方法的注释是:

  1. @net.bytebuddy.asm.Advice.OnMethodEnter
  2. @net.bytebuddy.asm.Advice.OnMethodExit

The best approach to debug advice methods and agent initialization is to use the following statements:

调试Advice方法和代理初始化的最佳方法是使用以下语句。

  1. System.out.println()
  2. Thread.dumpStack()

Agent initialization code

If you want to debug agent initialization code (e.g. OpenTelemetryAgent, AgentInitializer,
AgentInstaller, OpenTelemetryInstaller, etc.) then it’s important to specify the -agentlib: JVM arg
before the -javaagent: JVM arg and use suspend=y (see full example below).

如果你想调试agent初始化代码(如OpenTelemetryAgent、AgentInitializer、AgentInstaller、OpenTelemetryInstaller等),那么必须在 javaagent 参数之前添加-agentlib JVM参数并使用 suspend=y(见下面的完整例子)。

Enabling debugging

The following example shows remote debugger configuration. The breakpoints
should work in any code except ByteBuddy advice methods.

这里采用的是远程调试,可以在 idea 中启动 remote

  1. java -agentlib:jdwp="transport=dt_socket,server=y,suspend=y,address=5000" -javaagent:opentelemetry-javaagent-<version>-all.jar -jar app.jar