DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How does AI transform chaos engineering from an experiment into a critical capability? Learn how to effectively operationalize the chaos.

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Are you a front-end or full-stack developer frustrated by front-end distractions? Learn to move forward with tooling and clear boundaries.

Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

Related

  • Power BI Embedded Analytics — Part 3: Power BI Embedded Demo
  • DGS GraphQL and Spring Boot
  • Auto-Instrumentation in Azure Application Insights With AKS
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide

Trending

  • Secure IaC With a Shift-Left Approach
  • Privacy-Preserving AI: How Multimodal Models Are Changing Data Security
  • How to Create a Custom React Component in Vaadin Flow
  • Evaluating the Evaluators: Building Reliable LLM-as-a-Judge Systems

Introduction to BTrace

By 
Artiom Gourevitch user avatar
Artiom Gourevitch
·
Dec. 04, 11 · Interview
Likes (2)
Comment
Save
Tweet
Share
7.2K Views

Join the DZone community and get the full member experience.

Join For Free

 

The aim of this article is to learn how to dynamically trace/observe running Java applications (JDK 6+) using BTrace without changing the code and configuration params of the applications.

What is BTrace?


BTrace is an open source project that was started in 2007 and was originally owned by two people – A.Sundararajan and K. Balasubramanian. It gained fame thanks to Java One 2008 conference.

BTrace helps us to pinpoint complicated code problems in the application. The problems that can be solved using BTrace include code bugs, unpredictable flows, concurrency problems and performance problems that happen under specific and usually hard to reproduce circumstances. 

BTrace dynamically (without restarting application) instruments (changes) bytecode of an application in the way that programmer defines. The goal of the instrumentation is to see what happens in a specific area of the code. If used beyond this scope it may harm applications' flow and therefore is forbidden by a validator.

For example let's try to solve following problem – an important file gets deleted occasionally once a day. We want to find the code that does this. Therefore we would like to change "delete" method of java.io.File and print a stack trace of the calling thread if the file name fits. With BTrace we can do this by writing a short and straightforward Java code.

The following schema displays principles of BTrace work.


In Target JVM there is a dynamically inserted BTrace agent (using attach API). BTrace client (either BTrace Command Line or Visual VM with BTrace plugin) sends commands to the agent and gets responses. BTrace agent instruments classes that are being loaded into Target JVM and reloads classes that have already been loaded.

Writing a Tracing script



Writing BTrace scripts is pretty simple and straightforward and has concepts similar to Aspect Oriented Programming concepts.

 

import com.sun.btrace.annotations.*;
import com.sun.btrace.BTraceUtils;

@BTrace
public class HelloWorld {

@OnMethod(clazz="java.io.File",method="<init>")
public static void onNewFileCreated(String fileName) {
   BTraceUtils.println("New file is being created");
   BTraceUtils.println(fileName);
}


Each BTrace script consists of Probes (pointcuts in Aspects slang) and Actions (advices). A probe defines when the instrumentation should be executed. Action defines the instrumentation.

Probe can consist of the following:
  • Method entry/exit
  • Line number
  • Field updated/accessed
  • Method call/return (within specified method(s))
  • Exception throw (before)
  • Synchronization entry/exit
  • Timer
  • Constructor entry

 Abilities and limitations of BTrace


Abilities

  • Dynamically connect to any java6 + application and run any(*) code

Limitations
  • Must run with the same user as the traced application is running with - due to security concerns
  • Supports Hotspot JVM only
  • Is compiled separately from the target application – is not familiar with application classes
  • May crush target application - I ran BTrace on our longevity setup which was heavily loaded with many Tracing scripts. Target application didn't crush even once.

Advanced BTrace

BTrace community consists de facto of a single developer that works regularly on the project. Therefore don't expect too much improvement and many new features in the next releases. (This is a good opportunity to contribute to the project as each developer will enhance the development significantly).

Documentation of the project should be significantly improved - I found myself guessing many times while integrating this framework into the existing frameworks at my job. BTrace forum is a good way to get answers about the framework.

In order to understand how the magic of BTrace works, knowledge in three fields is required - "Java Agents Development", "Bytecode manipulation in java" and "Java attach api". I may write about some of these fields in the future.

References

Check out my blog
http://www.parleys.com/#st=5&id=1618&sl=1
http://kenai.com/projects/btrace
application

Opinions expressed by DZone contributors are their own.

Related

  • Power BI Embedded Analytics — Part 3: Power BI Embedded Demo
  • DGS GraphQL and Spring Boot
  • Auto-Instrumentation in Azure Application Insights With AKS
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: