Java Btrace Combat (1) Introduction and use of--btrace

Source: Internet
Author: User
Tags visualvm

Objective:
On-line Java services, often using logs for problem processing and analysis. What if the log lacks relevant information? remote debugging can affect the normal operation of the service, modify the code re-deployment of the scheme in fact, the time and flexibility is difficult to guarantee (the process of online service is cumbersome , the uncertainty of problem tracking ), there is no perfect solution ( does not affect the normal service, but also flexible/non-invasive)?
The answer is, it is a person to see people love, flowers see flowers open artifact btrace. That btrace is such a kind of artifact, why it just 998 (ah yuck ...,-_-bbb). All right, let's take a step-by-step to lift its red hijab. This series describes the use of Btrace/principles/techniques, I hope to help you, to yourself is also a learning experience.
This paper mainly explains the installation, principle and preliminary use of btrace.

Btrace's principle article:
Btrace's first letter, B, came from bytecode, revealing its principle to some extent. The hotswap technology of hotspot virtual machine can dynamically insert the non-existent debug code without affecting the running of the target program, which is based on the instumentation of JDK 6. The specific principles of the introduction, you can refer to the blog: Btrace Basic principles.

Installation of Btrace:
There are two ways to install Btrace.
  1). VISUALVM plug-in form exists
VISUALVM's Btrace plug-in installation is easy to install via its plugin manager.

Commentary: Specific in VISUALVM: Tools--Plug- in plug-in available, select BTrace WorkBench Installation
  2). Separate Btrace command-line tool
Btrace Download URL: https://kenai.com/projects/btrace/downloads/directory/releases
The directory structure of the Btrace-bin is as follows:
  
Commentary: Bin is a binary/script directory, Docs is an API document, samples contains various examples and descriptions of Btrace

Use of Btrace:
Let's construct a simple Java program as a test program.

public class Calculator {public int add (int a, int b) {return a + B;    } public static void Main (string[] args) {Scanner Scanner = new Scanner (system.in);    Calculator roboot = new Calculator ();      while (Scanner.hasnext ()) {int a = Scanner.nextint ();      int b = Scanner.nextint ();      int c = Roboot.add (A, b);    System.out.println (String.Format ("%d +%d =%d", A, B, c)); }  }}

Commentary: function add for the calculator class is used for a+b calculations, scanner is used for interaction, and input parameters are read from the command line.
We then write a btrace code that tracks the add function of the Calculator class.

Import Com.sun.btrace.annotations.*;import static com.sun.btrace.btraceutils.*; @BTracepublic class Tracingscript {    @OnMethod (        clazz= "Mmxf.btrace.test.Calculator",         method= "Add",         [email protected] (Kind.return)    )        public static void func (            int A, int b,             @Return int result) {        println ("Trace: =======================");        Jstack ();        println (Strcat ("A:", str (a)));        println (Strcat ("B:", str (b)));        println (strcat ("Result:", str (result)));}    }

Start with the VISUALVM Btrace plugin to complete the commissioning:
You can get the Java process from VISUALVM, then click on Trace application, import Btrace Script, click to run.
Results:
  
Of course, we can also use the Btrace command line tool to achieve:
  1). Determine the PID of the Java process
Jps-l can be used to achieve
  
Commentary: Select the process ID of PID 7547 as the target process
  2). Execute btrace command
Btrace <pid> <btrace_script>
  
Commentary: The stack information is printed, along with the input parameters of the Add method and the result of the function return

Summarize:
This article is only a beginning, the installation and use of Btrace has a preliminary understanding, the following article will be on the concept of btrace, use and practical examples to tell. Please look forward to it.

Java Btrace Combat (1) Introduction and use of--btrace

Related Article

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.