Tracking Java code with btrace

Source: Internet
Author: User

Btrace is an open source project under sun Kenai cloud computing development platform. It is designed to provide Java with a secure and reliable dynamic tracking and analysis tool.

Btrace tracks and replaces Java programs at runtime Based on Dynamic bytecode Modification Technology (hotswap.
Btrace scripts are written in pure Java. Based on a set of officially provided annotation, the tracking logic is very simple to implement. To put it simply, you can monitor the execution status of the Java program at runtime without changing the current program, for example, monitor the memory status and method call. Detailed descriptions on the website

Http://kenai.com/projects/btrace/pages/UserGuide

 

First from the http://kenai.com/projects/btrace/downloads/directory/releases%252Frelease-1.1 according to their own operating system under the corresponding Binary Package, the package is very small not more than 1 m. My computer is windows, and there are several BAT files under the bin directory. I add the bin directory to the path, which makes compilation and execution easier in the future. The content of BAT is very simple. You can open it and see why you can run it without configuring classpath. Use the bluedavy example to describe it.

// Import all btrace annotations <br/> Import COM. sun. btrace. annotations. *; <br/> // import statics from btraceutils class <br/> Import static COM. sun. btrace. btraceutils. *; </P> <p> @ btrace <br/> public class methodresponsetime {</P> <p> @ TLS <br/> Private Static long starttime; </P> <p> @ onmethod (clazz = "net. zdsoft. EIS. stusys. student. action. studentinfoaction ", <br/> method =" list ") <br/> Public static void o Ncall () {<br/> println ("enter this method"); <br/> starttime = timemillis (); <br/>}</P> <p> @ onmethod (clazz = "net. zdsoft. EIS. stusys. student. action. studentinfoaction ", method =" list ", location = @ location (kind. return) <br/> Public static void onreturn () {<br/> println ("method end! "); <Br/> println (strcat (" time taken ms ", STR (timemillis ()-starttime ))); <br/>}</P> <p>}

 

The above Code monitors the execution time of the List Method in the net. zdsoft. EIS. stusys. Student. Action. studentinfoaction class.

Next let's take a look at how to monitor

Start tomcat to run your service. Use btracec methodresponsetime. Java to compile this file on the command line, and then run btrace 2300 methodresponsetime. Java to start monitoring. Note that 2300 is the PID of the Java program you want to monitor (Tomcat service here), which can be seen in the task manager in windows.

Now click to trigger the list method function in the Action configured above, and you will be able to see the output result in the command line.

E:/btrace_demo> btrace 2300 methodresponsetime. Java
Enter this method
Method end!
Time taken ms32
Enter this method
Method end!
Time taken ms32
Enter this method
Method end!
Time taken ms15
Enter this method

 

 

For other applications, refer to the introduction on the official website and examples in the samples folder under the directory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.