BTrace
Safe, dynamic tracing for Java applications
BTrace dynamically instruments running Java applications to inject tracing code at runtime. No restarts. No recompilation. Production-safe.
Quick links: Quick Reference · Step-by-Step Tutorial
Why BTrace?
- Zero downtime - Attach to running JVMs without restart
- Production safe - Verified scripts can't crash your application
- Flexible probes - Method entry/exit, timings, field access, allocations
- Low overhead - Bytecode injection with minimal performance impact
Supported Java Versions
BTrace 3.0 runs on Java 8–25+. Running BTrace against a JVM older than Java 17 is deprecated: it continues to work throughout 3.x but emits a deprecation warning. Support for Java with the desired BTrace version (e.g.,3.0.0`). See releases for available versions.
Extensions: The published artifact bundles the default extensions, so scripts that inject
services such as MetricsService or PrinterService work under jbang with nothing extra to
install.
Benefits: Zero installation, automatic version management, works everywhere (Windows/macOS/Linux/containers), perfect for CI/CD.
Agent JAR: The client automatically discovers the masked agent JAR (btrace.jar) on its classpath — no extraction step is needed. If you want to use the agent JAR directly (e.g., with -javaagent), find it in the Maven local repository after the first jbang run:
# ~/.m2/repository/io/btrace/btrace/<version>/btrace-<version>.jar
For launch-time use, review the startup-mode security boundary. Use noServer=true for startup scripts that do not need later client connections; BTrace 3.0 does not support an unauthenticated remote prepared-mode endpoint.
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:
btrace/btrace:latest- Debian-based (~25MB)btrace/btrace:latest-alpine- Alpine-based (~15MB)btrace/btrace:latest-distroless- Distroless (~10MB)
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>
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>
Extensions and Removed libs/profiles
Extensions add functionality via a stable API on bootstrap and an isolated implementation. See the extension development guide and examples.
Note: The legacy libs/profiles mechanism has been removed. Passing libs= logs an error and loads nothing, so custom classes that used to arrive that way will no longer resolve. Package integrations as extensions and use provided-style class loading patterns (object hand-off + TCCL). For migration guidance and examples, see:
docs/architecture/migrating-from-libs-profiles.mddocs/architecture/provided-style-extensions.mddocs/examples/README.md
As a last resort (discouraged), you may append a single jar to the system classpath: -Dbtrace.system.appendJar=/abs/path/lib.jar -Dbtrace.trusted=true.
Fat Agent JAR (Single-JAR Deployment)
For environments where managing multiple JARs is impractical (Spark, Hadoop, Kubernetes), BTrace provides a fat agent JAR with embedded extensions:
# Build fat agent with the default extensions
./gradlew :btrace-dist:fatAgentJar
# Build with specific extensions only
./gradlew :btrace-dist:fatAgentJar -PembedExtensions=btrace-metrics,btrace-statsd
# Use the fat agent
java -javaagent:btrace-agent-fat.jar <your-app>
The