On Sun, 29 Nov 2026 03:19:20 GMT, Andrei Pangin <[email protected]> wrote:

> This PR aims to improve tracking of JVMTI agents, but honestly, I find it 
> very counterintuitive.
> 
> I suggest you start with defining what a JVMTI agent is (for the purpose of 
> JFR reporting). Since `jdk.NativeAgent` event is a user-facing API, it would 
> be useful to have a clear definition when the event is emitted and what it 
> means - ideally, something a user can easily recognize. E.g., previously, 
> each `JVMTI.agent_load` invocation produced a new event - this caused certain 
> problems, but at least, it was obvious and logical, and had practical 
> application as every profiler command was recorded. This PR obscures the 
> logic. It now depends on subtle implementation details of each particular 
> agent, whether it is reported in JFR or not. For example, the event is not 
> reported if an agent does not create `jvmtiEnv`, or if it creates `jvmtiEnv` 
> outside `Agent_OnAttach`, or if it creates `jvmtiEnv` in a separate thread.
> 
> I see two meaningful ways to identify an agent:
> 
>     * by library path (with special handling for libinstrument);
> 
>     * by `jvmtiEnv`, regardless of when an where the environment was created.
> 
> 
> There are pros and cons for both, but either way is more consistent to me 
> than the proposed PR. Trying to mix both approaches becomes problematic, 
> since an agent may create one environment, multiple environments (even in the 
> same invocation), or no environments at all. JVMTI environment can also be 
> created from a regular JNI library.
> 
> As an exercise, consider three versions of an agent:
> 
>     1. One allocates `jvmtiEnv` on the first invocation and reuses the 
> environment on subsequent invocations.
> 
>     2. Another one disposes the previous environment and allocates a new one 
> on each invocation.
> 
>     3. The third agent spawns a new thread from `Agent_OnAttach` and 
> allocates the environment from there.
>        Otherwise, all agents do the same thing. For the user, there is no 
> observable difference between three. But JFR will record one, many, or zero 
> `jdk.NativeAgent` events in these scenarios.

That is exactly what this PR addresses.

An "agent" is now equated with its jvmtiEnv, that is, its runtime identity, 
just like JVMTI handles agents internally.

An invocation of a JVMTI startup function is not an agent; it is a regular 
function call to a third-party library, just like any native method invocation.

The fact that not even the JVMTI specification can clarify what an "agent" is 
is how we ended up in the terrible situation in the first place.

That people abuse the Agent_OnAttach API as a control channel to communicate 
with their agents invalidates the statement in the JVMTI spec that "a startup 
function is called only once per agent" - this is clearly not the case.

I tried to also capture user-initiated jvmtiEnv creations, using the 
stackwalking primitives provided by the JVM, mostly the NMT specialization, so 
I could capture the caller and identify which library made the request, to 
reverse-lookup its agent identity - this proved impossible, since the JVM 
primitives handle inlining and tail-calls very badly.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/33700#issuecomment-7275712899

Reply via email to