Btrace Introduction and production environment examples

Source: Internet
Author: User

A/Brief introduction

BTrace is a reliable tool for dynamically tracking Java programs. It works by dynamically generating bytecode for a running Java program. Btrace inserts some trace operations into the class of the running Java program to hot-replace the tracked program.

BTrace noun

Probe points (Probe point)

Is the "place" or "event" in which a series of trace statements are executed. The probing point is the place or event where we want to execute some trace statements.

Trace action or short action (trace actions)

These are the trace statements that are executed when the probe point is triggered.

Action method

The Btrace trace statement must be defined in a static method of a class, and this static method is called an "action" method.

BTRACE Program Structure

A btrace program is an ordinary Java class that has at least one of these methods:

public static void * * *

Second, this method also needs to add btrace related annotations. These annotations are used to indicate the "location" of the tracked program (that is, the detection point mentioned earlier). The tracking action needs to be specified in the method body of this static method. These (note that there can be multiple) static methods are called "action" methods.

Limitations of Btrace

In order to ensure that the tracking action is "read-only" (that is, these actions can not modify the state of the tracked program) and limited (such as the end of a fixed time). A btrace program allows only some specified actions to be completed. Here are some things that btrace can not accomplish:

    • Unable to create new object

    • Cannot create a new array

    • Cannot throw an exception

    • Cannot catch exception

    • You cannot perform any instance functions or static functions--only static functions in the Com.sun.btrace.BTraceUtils class or Btrace programs declare themselves.

    • function can only be called by Btrace

    • For programs prior to version 1.2, the field and function cannot be used by instance level. Only functions that are statically exposed and have no return value are allowed to be used in the Btrace class. All

The field must be static.

    • You cannot assign a value to a class in the target program, or to a static or instance-level field of an object. However, the class of btrace itself can be assigned to its static field.

    • (meaning that you can change the status of a trace)

    • Cannot have outer,inner, nested, or local classes.

    • Cannot have synchronized code blocks or synchronized functions

    • Cannot have a looping statement (For,while, do: While

    • Cannot inherit other classes (parent class can only be Java.lang.Object)

    • Cannot implement Interface

    • Cannot contain assertion (ASSERT) statements

    • Can not use class literals (I don't understand what that means)

II./Specific examples

Current network encountered problems, the need to intercept the framework request, and Judge req in the StoreID field is 11008, output other parameters, convenient to further check the problem.

Class structure that needs to be intercepted

The match method needs to be blocked, there are several overloaded methods here, and only the first one needs to be intercepted at this time.

Btrace Code:

(add: In the Btrace code, there is no way to call a method other than Btraceutils, that is, to output the class field information is not through the ToString () method, Because calling the Instance.tostring () method is unsafe, compiling fail, you need to parse each field value yourself)

package btrace;import com.sun.btrace.anytype;import com.sun.btrace.annotations.*;import  Java.lang.reflect.field;import static com.sun.btrace.btraceutils.*;/** * created by  vernonzheng on 14-8-15. */@BTracepublic  class FrameFilterTrace {      @OnMethod (            clazz= " Com.skymobi.market.applist.service.imp.CustomizedFrameMatcher ",             method= "Match",             type =  "com.skymobi.market.applist.entity.framestoragemetadata  ( COM.SKYMOBI.MARKET.APPLIST.BEAN.COMMON.TLVCOMMONHEADERREQ) "    )      Public static void onmatch (anytype request) {         class framerequestcl = classof (requEST);         string storeid = getval ("StoreId", Framerequestcl, request);         class tlvcommentheaderreqcl  = getsuperclass (FRAMEREQUESTCL);        class  Networktrackcl = getsuperclass (TLVCOMMENTHEADERREQCL);         class terminalrequestcl = getsuperclass (NETWORKTRACKCL);         if (storeid!=null) {            if (Compare (StoreId, "10118")) {                //ashsman, ashstype,bizsource,sessionid,channelno,storeid,clientver,hsman,hstype,mnc,imsi,                 //networkType,capability      &Nbsp;          println (strcat ("Vernon----store_id=10118-- Output Parameters  : ,  str (request));                 println (Strcat ("StoreId:", storeId));                 println (Strcat ("Channelno:", Getval ("ChannelNo", framerequestcl,request));                 println (Strcat ("Ashsman:", Getval ("Ashsman", Framerequestcl,request)));                 println (Strcat ("AsHstype:", GetVal (" Ashstype ", framerequestcl,request)));                 println (Strcat ("Bizsource:", Getval ("Bizsource", Tlvcommentheaderreqcl,request)));        &nBsp;        println (Strcat ("SessionId:", Getval ("SessionId", tlvcommentheaderreqcl,request));                 println (Strcat ("Clientver:", Getval ("Clientver", Tlvcommentheaderreqcl,request)));                 println (Strcat (" Hsman: ", Getval (" Hsman ", Terminalrequestcl,request)));                 println (Strcat ("Hstype:", Getval ("Hstype", Terminalrequestcl,request)) );                 println ( Strcat ("MNC:", Getval ("MNC", Terminalrequestcl,request)));                 println (Strcat ("IMSI:", Getval ("Imsi", Terminalrequestcl,request)));       &nbsP;         println (Strcat ("Networktype:", Getval ("NetworkType", terminalrequestcl, request));                 println ("----------------------------------------------------------");             }        }     }    private static string getval (String  Filedname,class cl,anytype instance) {        field  Field = field (Classof (CL),  filedname, false);         if (field!=null)  {            return  str (Get (field, instance));        }else{             return  "Is null";        }     }}

Put Framefiltertrace.java and download good btrace-bin. tar.gz to the present online, decompression btrace-bin. tar.gz,

Copy the Framefiltertrace.java to the bin directory,

VI BTRACE script, add the header (export btrace_home= decompression path), if the java_home is not set, also export a bit.

Then JPS, find the JVM PID,

Perform btrace PID Framefiltertrace.java > Catch.log

Complete!

Output

Reference:

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

http://linmingren2003.blog.163.com/blog/static/56751003201121871725139/

Http://www.stacktrace.cn/?p=28

Btrace Introduction and production environment examples

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.