|
| 1 | +<html> |
| 2 | +<head> |
| 3 | +<title> |
| 4 | +BTrace Developer's Guide |
| 5 | +</title> |
| 6 | +</head> |
| 7 | +<body> |
| 8 | + |
| 9 | +<h1>BTrace Developer's Guide</h1> |
| 10 | +<p> |
| 11 | +<b>BTrace</b> is a safe, dynamic tracing system for Java. Please |
| 12 | +refer to <a href="usersguide.html">user's guide</a> for BTrace usage |
| 13 | +information. |
| 14 | +</p> |
| 15 | + |
| 16 | +<h3>BTrace Development Tools</h3> |
| 17 | + |
| 18 | +<p> |
| 19 | +<ul> |
| 20 | +<li>BTrace requires JDK 6 or above. |
| 21 | +<li>BTrace can be built using <a href="http://ant.apache.org/">ant</a> or |
| 22 | +<a href="http://www.netbeans.org">NetBeans IDE</a>. |
| 23 | +</ul> |
| 24 | +</p> |
| 25 | + |
| 26 | +<h3>BTrace Components</h3> |
| 27 | + |
| 28 | +<p> |
| 29 | +BTrace accepts a tracing program written in (subset) of |
| 30 | +the Java programming language. BTrace compiles the trace class |
| 31 | +into bytecode and submits the same to a java.lang.instrument agent that |
| 32 | +runs inside the target program. The BTrace agent is dynamically |
| 33 | +loaded into the target program if it is not already loaded (using |
| 34 | +"attach-on-demand" API). |
| 35 | +</p> |
| 36 | +BTrace Components: |
| 37 | +<ul> |
| 38 | +<li><b>BTrace Client Tool</b> - compiles, validates and submits BTrace program to |
| 39 | +BTrace VM agent. And receives trace messages and prints to stdout. |
| 40 | +<li><b>BTrace java.lang.instrument Agent</b> to bytecode instrument classes |
| 41 | +and hotswap them. Also, this agent verifies the bytecodes of BTrace |
| 42 | +class for safety [read-only, boundedness] rules. This way we don't need |
| 43 | +to trust the client to enforce the safety rules at compile time. |
| 44 | +<li><b>Wire Protocol</b> between the client and the agent. |
| 45 | +</ul> |
| 46 | +<h3> |
| 47 | + |
| 48 | +<h3>BTrace Packages</h3> |
| 49 | +<ul> |
| 50 | +<li><b><code>com.sun.btrace.agent</code></b>. This package contains |
| 51 | +classes for BTrace's <b><code>java.lang.instrument</code></b> agent. |
| 52 | +This agent uses simple socket protocol to communicate with the client. |
| 53 | +Multiple BTrace clients are supported. For each client, an instance |
| 54 | +of com.sun.btrace.agent.Client is created. |
| 55 | +<li><b><code>com.sun.btrace.annotations</code></b>. This package |
| 56 | +contains annotations and enumeration classes used by BTrace author |
| 57 | +as well as agent to specify/infer "probed locations" of the traced |
| 58 | +program. These classes are loaded by bootstrap loader (agent |
| 59 | +adds classes containing these classes to bootstrap path). |
| 60 | +<li><b><code>com.sun.btrace.client</code></b>. This package contains |
| 61 | +BTrace client tool main class. |
| 62 | +<li><b><code>com.sun.btrace.dtrace</code></b>. This package contains |
| 63 | +BTrace and DTrace integration classes. The classes that wrap DTrace/Java |
| 64 | +API are here. Please refer to /use/share/lib/java/javadoc/dtrace for DTrace/Java |
| 65 | +API. |
| 66 | +<li><b><code>com.sun.btrace.comm</code></b>. This package contains |
| 67 | +wire protocol messages between BTrace agent and client tool. BTrace |
| 68 | +agent and client communicate by object serializing the instances of |
| 69 | +Message classes. |
| 70 | +<li><b><code>com.sun.btrace.compiler</code></b>. This package |
| 71 | +has classes for compiling a BTrace program into bytecode after |
| 72 | +safety verification. Because BTrace accepts subset of Java, it uses |
| 73 | +javac's APIs (JSR 199 - compiler tool API, JSR 269 - Annotation |
| 74 | +Processing API and javac Tree API to access AST of compiled Java |
| 75 | +program) to compile and enforce BTrace safety rules. |
| 76 | +<li><b><code>com.sun.btrace.resources</code></b>. This package contains |
| 77 | +error messages resource used by BTrace compiler and bytecode verifier. |
| 78 | +<li><b><code>com.sun.btrace.runtime</code></b>. This package contains |
| 79 | +various bytecode instrumentation classes used by BTrace. These |
| 80 | +instrumentation classes use <a href="http://asm.objectweb.org">Objectweb's ASM</a> |
| 81 | +package to do actual class file parsing and writing. ASM version 3.0 |
| 82 | +is used. This package contains BTrace bytecode verifier and jvmstat reader as well. |
| 83 | +<li><b><code>com.sun.btrace</code></b>. This package contains classes loaded |
| 84 | +by bootstrap loader (agent adds classes containing these classes to bootstrap path). |
| 85 | +<code>com.sun.btrace.BTraceUtils</code> class contains built-in "functions" that can be called |
| 86 | +by any BTrace program (these are read-only and bounded methods can |
| 87 | +be called by trace program). <code>com.sun.btrace.BTraceRuntime</code> class contains |
| 88 | +per-client state for each BTrace client and helps implementing certain methods |
| 89 | +of BTraceUtils. Also, BTraceRuntime makes sure that BTrace agent's own |
| 90 | +method invocations and BTrace built-in "function" calls are not |
| 91 | +traced [there by leading to infinite recursion!]. |
| 92 | +</ul> |
| 93 | + |
| 94 | +<h3>BTrace jar files</h3> |
| 95 | + |
| 96 | +<ul> |
| 97 | +<li><b>btrace-boot.jar</b> - loaded by bootstrap loader in the traced |
| 98 | +JVM. Contains BTrace annotation classes in <b><code>com.sun.btrace.annotations</code></b> |
| 99 | +package and classes in <b><code>com.sun.btrace</code></b> package. |
| 100 | +<li><b>btrace-agent.jar</b> - contains classes for java.lang.instrument |
| 101 | +agent and instrumentation classes. This uses <b><code>asm-3.0</code></b> |
| 102 | +jar for instrumentation and <b><code>tools.jar</code></b> for reading |
| 103 | +jvmstat counter values [<b><code>sun.jvmstat.monitor</code></b> classes]. |
| 104 | +<li><b>btrace-client.jar</b> - contains BTrace client classes. This |
| 105 | +uses <b><code>tools.jar</code></b> for javac's classes. |
| 106 | +</ul> |
| 107 | + |
| 108 | +<h3>BTrace "To Do"s</h3> |
| 109 | +<ul> |
| 110 | +<li>Remove instrumentation when a client leaves tracing session. |
| 111 | +Right now, we "disable" trace calls when a BTrace client leaves |
| 112 | +the session. It would be better to remove the instrumentation and |
| 113 | +re-hotswap the classes to avoid the "disabled" calls completely. |
| 114 | +</ul> |
| 115 | + |
| 116 | +<h3>Debugging BTrace</h3> |
| 117 | +<p> |
| 118 | +BTrace can be debugged by setting few System properties. All these properties are |
| 119 | +set at the BTrace client. |
| 120 | +<ul> |
| 121 | +<li><b><code>com.sun.btrace.debug</code></b> - this boolean valued property makes |
| 122 | +BTrace to print debug messages (set at client - but debug mode is propagated |
| 123 | +to BTrace agent as well). |
| 124 | +<li><b><code>com.sun.btrace.dumpClasses</code></b> - this boolean valued property |
| 125 | +may be set to force BTrace agent dump every .class that is |
| 126 | +intrumented. |
| 127 | +<li><b><code>com.sun.btrace.dumpDir</code></b> - this is a String valued property |
| 128 | +that sets the directory where the instrumened .class files are dumped. |
| 129 | +</ul> |
| 130 | +It is better to run the traced JVM with <b><code>-Xverify:all</code></b> to force |
| 131 | +bytecode verification of all classes. This is to make sure that BTrace does not produce |
| 132 | +bad classes thereby crashing the JVM. After dumping instrumeted classes, it is possible |
| 133 | +(offline) analyze those using <b>javap</b> tool. The BTrace action methods look like |
| 134 | +the form: "btrace$<trace-class-name>$<trace-action-method-name>" - where "."s |
| 135 | +in the trace class name are replaced by "$". |
| 136 | +</p> |
| 137 | + |
| 138 | +<h3>Known Issues and Limitations</h3> |
| 139 | + |
| 140 | +<ul> |
| 141 | +<li>BTrace <b>does not work when class sharing is on</b> - so run your traced program with |
| 142 | +<b>-Xshare:off</b> option or use <b>-server</b> option. When class sharing is |
| 143 | +used (atleast on Windows), the traced JVM crashes after instrumenting a |
| 144 | +class (for eg. a bootstrap class like java.awt.Component). |
| 145 | +<li>Certain <b><code>sun.misc.*</code></b> classes are used by BTrace. You get |
| 146 | +<b>compilation warnings</b> because of this. Also, <b><code>sun.jvmstat.monitor.*</code></b> |
| 147 | +classes are used to read jvmstat perf. counters. |
| 148 | +<li><b>BTrace requires JDK 6</b>. There are API dependencies [for example, javac's new APIs]. |
| 149 | +And BTrace uses certain recent changes with java.lang.instrument and hotswap. In particular, |
| 150 | +<b>BTrace adds private methods while hotswapping classes</b>. This feature (of |
| 151 | +adding private methods while hotswapping classes) is <b>not</b> available in earlier |
| 152 | +JDK versions. Also, retranformantion is used to avoid fetching .class bytes from |
| 153 | +the file system. Again, this is a new feature of java.lang.instrument API since JDK 6. |
| 154 | +</ul> |
| 155 | + |
| 156 | +</body> |
| 157 | +</html> |
0 commit comments