Java Clean Architecture Masterclass

Java Clean Architecture Masterclass28-29 May

Join

Dev build Download codecov.io huhu Join the chat at https://gitter.im/jbachorik/btrace Project Stats

BTrace

A safe, dynamic tracing tool for the Java platform

Overview

BTrace dynamically instruments running Java applications to inject tracing code at runtime, without stopping the application or recompiling code. Similar to DTrace for OpenSolaris, BTrace uses bytecode instrumentation to trace methods, monitor performance, and diagnose issues in production environments.

Credits

Building BTrace

Setup

You will need the following applications installed

Build

cd <btrace>
./gradlew :btrace-dist:build

Output locations:

Updating golden files: When instrumentor code changes, update test golden files with:

./gradlew test -PupdateTestData

Commit the regenerated golden files to Git.

How To Run Tests

For a fast local cycle, run unit tests and skip integration tests (which start forked JVMs):

export GRADLE_USER_HOME="$PWD/.gradle-home"   # keep caches inside the repo (optional)
./gradlew --no-daemon test -x integration-tests:test

Tips:

Integration tests (optional):

./gradlew --no-daemon integration-tests:test

These may exercise privileged extensions. If you run into permission denials, provide a policy file and pass it to the test JVMs via -Dbtrace.permissions=/path/to/permissions.properties.

Using BTrace

Installation

Use JBang to run BTrace without manual installation:

# Install JBang (one time)
curl -Ls https://sh.jbang.dev | bash -s - app setup

# Use BTrace immediately (replace <version> with desired version, e.g., 2.3.0)
jbang org.openjdk.btrace:btrace-client:<version> <PID> <script.java>

# After first run, use shorter alias
jbang btrace <PID> <script.java>

Note: Replace <version> with the desired BTrace version (e.g., 2.3.0). See releases for available versions.

Benefits: Zero installation, automatic version management, works everywhere (Windows/macOS/Linux/containers), perfect for CI/CD.

Extract agent JARs (if needed for --agent-jar/--boot-jar flags):

# Extract embedded JARs
jbang btrace --extract-agent ~/.btrace

# This creates ~/.btrace/btrace-agent.jar and ~/.btrace/btrace-boot.jar
# Then use them:
jbang btrace --agent-jar ~/.btrace/btrace-agent.jar --boot-jar ~/.btrace/btrace-boot.jar <PID> <script.java>

# Or find them in Maven local repository (after first jbang run):
# ~/.m2/repository/org/openjdk/btrace/btrace-agent/<version>/btrace-agent-<version>.jar
# ~/.m2/repository/org/openjdk/btrace/btrace-boot/<version>/btrace-boot-<version>.jar

See Getting Started Guide for complete JBang documentation and examples.

Binary Distribution

Download: Get the latest release from the release page

# Extract the archive
tar -xzf btrace-*.tar.gz
# or
unzip btrace-*.zip

# Set environment variables (optional but recommended)
export BTRACE_HOME=/path/to/btrace
export PATH=$BTRACE_HOME/bin:$PATH

Package Installation

# RPM-based systems
sudo rpm -i btrace-*.rpm

# Debian-based systems
sudo dpkg -i btrace-*.deb

Docker images:

# Copy BTrace into your application image
FROM btrace/btrace:latest AS btrace
FROM bellsoft/liberica-openjdk-debian:11-cds

COPY --from=btrace /opt/btrace /opt/btrace
ENV BTRACE_HOME=/opt/btrace PATH="${PATH}:${BTRACE_HOME}/bin"

# Your application...

Available variants:

See docker/README.md for complete Docker documentation.

Quick Start

With JBang (no installation required):

# Attach to running application
jbang btrace <PID> <trace_script.java>

# Extract agent JARs
jbang btrace --extract-agent ~/.btrace

With installed BTrace:

# Attach to running application
btrace <PID> <trace_script.java>

# Compile BTrace script
btracec <trace_script.java>

# Launch application with BTrace agent
btracer <compiled_script.class> <java-application-and-args>

Oneliner Quick Examples

BTrace now supports DTrace-style oneliners for quick debugging without writing full Java scripts:

# Trace method entry with arguments
btrace -n 'javax.swing.*::setText @entry { print method, args }' <PID>

# Find slow database queries (>100ms)
btrace -n 'java.sql.Statement::execute* @return if duration>100ms { print method, duration }' <PID>

# Count method invocations
btrace -n 'java.util.HashMap::get @entry { count }' <PID>

# Print stack trace on OutOfMemoryError
btrace -n 'java.lang.OutOfMemoryError::<init> @return { stack(10) }' <PID>

Supported features:

See Oneliner Guide for complete syntax and examples.

Documentation

For comprehensive documentation, tutorials, and guides:

Extensions and Permissions

BTrace supports extensions (like StatsdExtension) that provide additional functionality. Extensions require explicit permissions for security:

Permissions are enforced based on extension/service descriptors and agent grants specified at attach-time.

Grant permissions at runtime:

btrace --grant=NETWORK,THREADS <PID> MyProbe.class

If extensions fail to load, use -le to troubleshoot:

btrace -le <PID>

See the Tutorial for detailed documentation.

Extensions CLI: use btracex to inspect and manage extensions and the simplified permission policy:

Note: Extension “required permissions” are informational and help operators assess risk. Implementation linking is controlled by per‑extension allow/deny lists and the allowPrivileged flag; when blocked, APIs remain available and SHIMs are used so probes continue safely.

Agent Policy and Allow/Deny Lists

See docs/PermissionPolicy.md for details and examples.

btracex TUI (interactive)

Screenshot / demo (optional):

btracex TUI

btracex TUI demo

See also: docs/TUI.md for recording tips and an ASCII preview.

Keys

Maven Integration

The BTrace Maven Plugin enables:

Contributing

Important: Pull requests can only be accepted from signers of the Oracle Contributor Agreement.

Development

See CLAUDE.md for detailed development guidelines and project architecture.

Community

License

BTrace is licensed under GPLv2 with the Classpath Exception. See LICENSE for details.

Join libs.tech

...and unlock some superpowers

GitHub

We won't share your data with anyone else.