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.
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.
}
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
Limitations
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
http://www.parleys.com/#st=5&id=1618&sl=1
http://kenai.com/projects/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.
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
http://www.parleys.com/#st=5&id=1618&sl=1
http://kenai.com/projects/btrace
I'm glad to see that BTrace is still under development and getting attention. I've used it in the past to successfully dive into new code bases.
ReplyDeleteI've spent the last year working on a tool that will trace and produce interactive visualizations of Java code. What was lacking from BTrace was dynamic configuration and the ability to visually see the executed code.
I'd love to hear your feedback on the tool. It is called Heatlamp. I'd be happy to send you a free license if you are interested in covering it. Just drop us an email from the website.
http://www.jmolly.com/heatlamp/
nice article.. thank you for sharing.. dotnet training in chennai
ReplyDeleteIt is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
ReplyDeleteCafe, Pub, Menu Consultants in Chennai | Hotel & Restaurant management services in chennai
This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.....
ReplyDeleteled and lcd tv screen replacement in chennai |
nearest tv repair and service center in chennai
Great post...Thanks for sharing.......
ReplyDeleteBouncers in chennai | Bouncers and bodyguards in chennai
I have read your blog its very attractive and impressive. I like it your blog.
ReplyDeleteFree brahmin matrimony in chennai |
Marriage matrimony in chennai |
Free Indian matrimony in chennai
Very good blog to enhance knowledge .Thanks for share.
ReplyDeleteRack Manufacturers in Chennai |
Racks in Chennai |
Steel Racks in Chennai
Thank you very much for sharing this post...keep sharing content...
ReplyDeleteHome security systems Chennai |
CCTV Dealers in Chennai |
Chennai CCTV Cameras
Thanks for sharing these effective tips. It was very helpful for me....
ReplyDeleteschool diaries printing services in chennai |
|
business cards printing press in chennai |
Hey, would you mind if I share your blog with my twitter group? There’s a lot of folks that I think would enjoy your content. Please let me know. Thank you.
ReplyDeleteJava Training in Chennai | J2EE Training in Chennai | Advanced Java Training in Chennai | Core Java Training in Chennai | Java Training institute in Chennai
I have to voice my passion for your kindness giving support to those people that should have guidance on this important matter.
ReplyDeleteAI training chennai | AI training class chennai
Cloud computing training | cloud computing class chennai
Appreciation for really being thoughtful and also for deciding on certain marvelous guides most people really want to be aware of....
ReplyDeleteData science training chennai | data science course chennai