IBM Java comes standard with a convenient function called method tracing (*). You can use method tracing to see the methods called at run time in chronological order. It's very easy to use, just specify the method you want to trace in the run-time options of the java command (you can also specify it in the properties file or the Java API). Wildcards can also be used to specify the method to be traced.
Let's use it right away. Let's execute the sample.HelloMT class with all the methods of all the classes of the sample package as the trace target. Specify run-time options with -Xtrace: ...
as follows:
java -Xtrace:methods={sample/*},print=mt sample.HelloMT
The Java program (HelloMT.java) to be executed is as follows.
HelloMT.java
package sample;
public class HelloMT {
private void say() {
System.out.println("Hello Method Trace.");
sayMore("Enjoy Method Trace!");
}
private void sayMore(String message) {
System.out.println(message);
}
public static void main(String args[]) {
HelloMT hello = new HelloMT();
hello.say();
}
}
The execution result is as follows. Method traces are printed to standard errors by default.
Standard output
Hello Method Trace. Enjoy Method Trace!
Standard error
12:15:33.618*0x2cb0500 mt.3 > sample/HelloMT.main([Ljava/lang/String;)V bytecode static method 12:15:33.618 0x2cb0500 mt.0 > sample/HelloMT.say()V bytecode method, this = 0xfff3b990 12:15:33.618 0x2cb0500 mt.0 > sample/HelloMT.sayMore(Ljava/lang/String;)V bytecode method, this = 0xfff3b990 12:15:33.618 0x2cb0500 mt.6 < sample/HelloMT.sayMore(Ljava/lang/String;)V bytecode method 12:15:33.618 0x2cb0500 mt.6 < sample/HelloMT.say()V bytecode method 12:15:33.618 0x2cb0500 mt.9 < sample/HelloMT.main([Ljava/lang/String;)V bytecode static method
For more information on options (-Xtrace: ...
), check the IBM Java online manuals listed in the reference links, and here are some practical options specification examples.
** Example 1) Specify multiple conditions for the method to be traced **
-Xtrace:methods={sample/*,test/*},print=mt
You can specify multiple conditions separated by commas.
** Example 2) Specifying trace conditions with limited class name / method name **
-Xtrace:methods={sample/HelloMT},print=mt
-Xtrace:methods={sample/HelloMT.say},print=mt
In the upper row, all methods of sample.HelloMT class are traced.
In the lower row, only the say method of the sample.HelloMT class is traced.
** Example 3) Trace including parameter information **
-Xtrace:methods={com/ibm/sample/HelloMT.say*()},print=mt
You can trace including parameter information by adding ()
to the end of the method specification.
If the argument is a primitive, you can also see the value in the trace. If the argument is an object, the hexadecimal address is output, so it is not possible to check the value of the contents, but if it is null, ʻarguments: (null)` is also output in the trace, so it is a clue to determine the problem. Will be.
** Example 4) Specifying trace exclusion conditions **
-Xtrace:methods={sample/*,!sample/HelloMT.sayMore},print=mt
If !
Is described at the beginning, the trace exclusion condition will be specified.
In the case of the above sample, if the exclusion specification is described first, it will be invalid and the sayMore method will also be output to the trace, so be careful about the order of description.
** Example 5) Output trace to file **
-Xtrace:none,methods={sample/HelloMT},maximal=mt,output=C:\MTrace\mtrace1.out
If you do not specify none, a large amount of logs will be output as the default trace point, so be careful!
The output file is in binary format and must be formatted for reference. Format using the IBM Java TraceFormat class as follows:
C:\MTrace>java com.ibm.jvm.format.TraceFormat mtrace1.out
Please see the reference link (** Running the trace formatter **) for details on the TraceFormat class.
For the method trace shown above (Examples 1 to 5), the method trace is valid from the start to the end of the JVM, but you may want to take a partial trace. You can use the suspend option and the Trace API (com.ibm.jvm.Trace class) to control the start and end of a trace. First, start the JVM with the trace paused with the suspend option specified as shown below.
-Xtrace:methods={*},print=mt,suspend
Then, call Trace.resume () from your Java program where you want to start tracing to start tracing. Finally, call Trace.suspend () to put the trace back into a paused state again. The following HelloMT2.java is a sample.
HelloMT2.java
package sample;
import com.ibm.jvm.Trace;
public class HelloMT2 {
private void say() {
System.out.println("Hello Method Trace.");
sayMore("Enjoy Method Trace!");
}
private void sayMore(String message) {
System.out.println(message);
}
public static void main(String args[]) {
HelloMT2 hello = new HelloMT2();
Trace.resume();
hello.say();
Trace.suspend();
}
}
that's all.
IBM SDK, Java Technology Edition, Version 8 IBM Java online manual. https://www.ibm.com/support/knowledgecenter/SSYKE2_8.0.0/welcome/welcome_javasdk_version.html
Using method trace This is the top page of Method Trace in the IBM Java Online Manual. https://www.ibm.com/support/knowledgecenter/SSYKE2_8.0.0/com.ibm.java.lnx.80.doc/diag/tools/method_trace.html
Controlling the trace This is the top page of Method Trace in the IBM Java Online Manual. https://www.ibm.com/support/knowledgecenter/SSYKE2_8.0.0/com.ibm.java.lnx.80.doc/diag/tools/trace_control.html
Detailed descriptions of trace options This page explains the method trace option details and syntax. https://www.ibm.com/support/knowledgecenter/SSYKE2_8.0.0/com.ibm.java.lnx.80.doc/diag/tools/trace_options_detail.html
Using the Java API This page explains how to use the Java API (com.ibm.jvm.Trace class). https://www.ibm.com/support/knowledgecenter/SSYKE2_8.0.0/com.ibm.java.lnx.80.doc/diag/tools/trace_app_api.html
Running the trace formatter This page explains how to use the formatter (com.ibm.jvm.format.TraceFormat) required when the trace is output to a file (see Example 5 above). https://www.ibm.com/support/knowledgecenter/SSYKE2_8.0.0/com.ibm.java.lnx.80.doc/diag/tools/trace_formatter.html
Here are some notes that I actually got into.
When using method tracing in the WebSphere Application Server Liberty Profile environment (hereinafter referred to as WAS Liberty environment), the option -Xtrace: methods = ...
is described in the jvm.options file, but the Windows version of WAS In the Liberty environment, the !
Symbol must be escaped with ^
(hat symbol) when optionally excluding it (because the WAS Liberty server startup command is implemented in the Windows batch file). .. A description example is shown below.
-Xtrace:methods={sample/*,^!sample/Hello.set*},print=mt
If you try to import com.ibm.jvm.Trace in an Eclipse IDE environment, an error may occur due to access restrictions (Note 1) and you may not be able to compile. Note 1) If you hover the mouse over the error marker, you can see the message "Access restriction: The type'Trace' is not API (restriction on required library ...".
This can be avoided by the following measures (Note 2). Note 2) The screenshot in the explanation is the operation in ** IBM Rational Application Developer V9.6.1 **.
Right-click on the project folder and select: From the context menu: ** Build path (B) **> ** Build path configuration (C) **
The "Java Build Path" dialog is displayed. Select ** Library tab (L) **. Click the triangle icon in ** JRE System Library ** to expand it Select ** Access Rule ** at the top of the expanded section and click the ** Edit ** button. The "Type Access Rule" dialog is displayed. Click the ** Add (A) ** button.
The "Add Access Rule" dialog is displayed.
-** Resolution (S) ** Select accessible
from the pull-down,
-Enter com / ibm / jvm / **
in ** Rule Pattern (R) **
Click the ** OK ** button.
After confirming that com / ibm / jvm
is accessible, click the OK button to close the open dialog.
The error marker that was displayed to the left of the import statement in the Java source should now disappear.
Recommended Posts