Skip to content

Pr/olsajiri/java - #6130

Draft
olsajiri wants to merge 14 commits into
mainfrom
pr/olsajiri/java
Draft

olsajiri wants to merge 14 commits into
mainfrom
pr/olsajiri/java

Conversation

@olsajiri

Copy link
Copy Markdown
Contributor

wip

olsajiri and others added 14 commits November 22, 2026 21:33
Make sure we report al errors from unload path for
both map and prog.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Adding java section to the tracing policy spec, which holds
configuration for Java method monitoring in HotSpot JVMs:

  - targets selects the JVMs by executable path and process
    arguments, if omitted the policy applies to all JVMs
  - hooks select classes (exact class or package prefix) and
    methods to monitor

Also bumping the CRD schema version.

The generated changes are added in following commit.

AIL: 2
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Adding the ProcessJava event message and PROCESS_JAVA event type
to the gRPC API schema, to be regenerated in a follow-up commit.

The message carries the policy name, message and tags of the java
hook that produced the event, like other policy driven events.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
  $ make generate codegen
  $ make -C docs tracing-policy-docs

Plus CustomResourceDefinitionSchemaVersion update.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Adding a secondary index from PID to cached process entries alongside
the existing exec-ID index.

Adding GetParentProcessInternalByPID which looks up the newest cached
exec for a PID whose start time is not later than a given event's ktime.

It will be used by Java event handling to resolve process in following
chages.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Adding MsgJavaEvent grpc handling which turns a Java method entry
received from the jvmti agent into a tetragon.ProcessJava gRPC event.

The event carries already decoded fields: the JVM pid (taken by
tetragon from the agent socket peer credentials, not reported by
the JVM), thread id, ktime, method id and names, and the policy
name, message and tags of the java hook.

The process is resolved through the pid process cache index, with
eventcache retries when the process is not known yet.

Adding 'java' value to --enable-ancestors to enable ancestors for
the ProcessJava events.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Wire the ProcessJava case into the generic encoder and filter
paths so it exports and filters like any other event type for
gRPC clients such as tetra.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Adding SubmitEvent function that queues already decoded event to be
processed by the observer listeners.

It's for sensors that read events from other sources than the bpf
ring buffers (like the java events from jvmti agent ring buffers).

The submitted events are processed by the same goroutine that
processes the bpf events, so listeners are not called concurrently.
SubmitEvent does not block, the event is dropped (and accounted as
lost) when the queue is full.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Adding wire formats shared between the jvmti agent and tetragon:

  - control channel messages, sent over SOCK_SEQPACKET unix socket
    created by tetragon: HELLO, RING (memfd passed with SCM_RIGHTS),
    HOOK_ADD, POLICY_COMMIT, POLICY_REMOVE and ACK

  - data ring layout: header page followed by power of 2 data area
    holding variable size records (METHOD_DEF, METHOD_ENTRY, PAD);
    multiple producers (java threads) reserve space with CAS and
    commit records by storing their length, the consumer (tetragon)
    zeroes consumed records before advancing its position

The C header (jvmti/proto.h) is mirrored by pkg/javaagent/proto.go.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Adding TetragonHook class with native enter method. Methods
instrumented by the jvmti agent call TetragonHook.enter(key) at
their entry, the native method is implemented by the agent and
writes the method entry record into the data ring.

The agent defines the class in the bootstrap class loader from
the class bytes embedded in tetragonhook.h, which is generated
from TetragonHook.java (compiled with --release 8) by
gen-header.sh. The generated header is committed, so the agent
build does not need java compiler.

AIL: 2
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Adding native JVMTI agent that Tetragon loads into running HotSpot
JVMs, together with the Tetragon side of the agent protocol.

The javaattach package loads the agent through the HotSpot Attach
interface. It connects to the JVM's /tmp/.java_pid<pid> socket via
/proc/<pid>/root, and if the attach listener is not running yet, it
creates the .attach_pid<pid> trigger file and sends SIGQUIT to start
it. The namespace pid is taken from NSpid, so attaching works for
containerized JVMs.

The agent (pkg/javaattach/agent/jvmti_agent.c) connects back to the
control socket passed in the agent options and:
  - receives hooks (exact class or package prefix plus optional
    method name/descriptor) and policy commit/remove commands, and
    acknowledges each command
  - instruments matching methods from the ClassFileLoadHook, adding
    a call to TetragonHook at method entry, and retransforms already
    loaded classes when a policy is committed
  - leaves classes whose bytecode it cannot safely rewrite untouched,
    and never instruments java/, jdk/, sun/ and io/tetragon/ classes
  - reports method definitions and method entry records (method key,
    ktime, tid) to the shared ring buffer

The javaagent package implements the Tetragon side:
  - a SOCK_SEQPACKET control server that only accepts JVMs authorized
    by AllowProcess. The peer pid comes from SO_PEERCRED and is
    matched with the process start time, so a reused pid is rejected
  - a memfd-backed single producer/single consumer ring passed to
    the agent with SCM_RIGHTS, with futex based consumer wakeup
  - the wire format for control messages and ring records, mirroring
    jvmti/proto.h, with encode/decode tests
  - decoded method entry events delivered through an EventSink
    callback, together with the policy name, message and tags

AIL: 2
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Adding java sensor that turns the java section of a tracing policy
into JVMTI agent hooks and loads the agent into matching JVMs.

The policy handler translates java.hooks into agent hooks (exact
class or package prefix, optional method name/descriptor), keeping
each hook's message and tags as metadata for the emitted events.
The agent policy id is an FNV-1a hash of the policy name, so it is
unique and stays stable across Tetragon restarts. Java policies do
not support policy filtering yet.

When loaded, the sensor starts the agent control server and pushes
the policy to it, then scans /proc every second for JVMs matching
the policy:
  - targets.executables matches /proc/<pid>/exe exactly; without it
    any process with libjvm.so mapped is considered a JVM
  - targets.processArgsContains tokens must each match one of the
    process arguments

Each matching JVM is identified by pid and start time. The sensor
authorizes it on the control server, copies the agent library into
the process's /tmp (via /proc/<pid>/root) and loads it through the
HotSpot Attach interface. A JVM is attached only once, even when
several policies match it, and a failed attach is retried after
5 seconds. Processes that exit or whose pid gets reused are revoked.

Method entry records from the agent are submitted as ProcessJava
events.

Adding new options:
  --java-agent-path         path to the JVMTI agent library; if not
                            set, pkg/javaattach/agent/tetragon-jvmti.so
                            in the working directory is used, falling
                            back to /usr/lib/tetragon/tetragon-jvmti.so
  --java-agent-socket-path  control socket for the agent, defaults
                            to /var/run/tetragon/java.sock

AIL: 2
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Adding build and packaging support for the native JVMTI agent
library (tetragon-jvmti.so) that the java sensor loads into JVMs.

  - new tetragon-jvmti-agent make target that builds the agent in
    pkg/javaattach/agent, which the clean target also cleans up
  - the install target builds the agent and installs it into
    /usr/lib/tetragon/
  - the Dockerfile gets a java-agent-builder stage that builds the
    agent with the target architecture's C toolchain (cross gcc for
    arm64), and the image ships it in /usr/lib/tetragon/

/usr/lib/tetragon/tetragon-jvmti.so is the default location the java
sensor uses when --java-agent-path is not set and the agent is not
found in the source tree.

AIL: 2
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Adding example showing Java method monitoring with Tetragon.

  - TetragonJavaDemo.java is a small application calling its work
    method every 500ms
  - demo-tracingpolicy.yaml hooks TetragonJavaDemo.work, selecting
    the JVM by the tetragon-java-demo token in its arguments
  - run-demo.sh builds Tetragon, the BPF programs and the JVMTI
    agent, starts the demo application (as the invoking user when
    run through sudo) and runs Tetragon with the demo policy, which
    prints events for each TetragonJavaDemo.work call
  - README.md describes how to run the demo and how to adapt the
    policy to monitor another JVM

Run with:

  $ sudo ./examples/java/run-demo.sh

AIL: 2
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
@netlify

netlify Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for tetragon ready!

Name Link
🔨 Latest commit c6b55da
🔍 Latest deploy log https://app.netlify.com/projects/tetragon/deploys/6b035e16b531788a8071a0cc
😎 Deploy Preview https://deploy-preview-6130--tetragon.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

continue
} else if ok {
delete(s.active, target.pid)
server.RevokeProcess(s.policy.ID, uint32(target.pid))
continue
}
s.lastAttempt[target.pid] = time.Now()
server.AllowProcess(s.policy.ID, uint32(target.pid), target.startTicks)
}
if err := s.attach(target); err != nil {
attachedMu.Unlock()
server.RevokeProcess(s.policy.ID, uint32(target.pid))
current, err := readProcessIdentity(pid)
if err != nil || current.startTicks != target.startTicks {
delete(s.active, pid)
server.RevokeProcess(s.policy.ID, uint32(pid))

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants