Btrace tool usage
Case Description
Use the test method to receive an int type parameter, and add two strings in map according to the parameter.
If no logs are printed in the modern code, the generated int value cannot be determined and the execution path cannot be determined. In this case, BTrace can be used to track and analyze the program path.
Java code
- Import java. util. hashmap;
- Import java. util. Map;
- Import java. util. Random;
- Public class testhello
- {
- Public static void main (String [] args) throws InterruptedException
- {
- Thread. sleep (2000*10 );
- TestHello th = new TestHello ();
- Random random = new Random (47 );
- Th. test (random. nextInt (9) + 1 );
- }
- Private Map <String, String> model = new HashMap <String, String> ();
- Public boolean test (int age)
- {
- String variable1 = "default prompt: The parameter you entered is:" + age;
- String variable2 = "welcome to the online Park ";
- Model. put ("variable1", variable1 );
- Model. put ("variable2", variable2 );
- If (age> = 1 & age <= 5)
- {
- Variable1 = "your input parameters are between 1 and 5 ";
- Variable2 = "Welcome to Yizhi games ";
- Model. put ("variable1", variable1 );
- Model. put ("variable2", variable2 );
- Return true;
- }
- Else if (age> 5 & age <= 10)
- {
- Variable1 = "your input parameters are between 5 and 10 ";
- Variable2 = "Welcome to English Playground ";
- Model. Put ("variable1", variable1 );
- Model. Put ("variable2", variable2 );
- Return false;
- }
- Else
- {
- Return true;
- }
- }
- }
Import Java. util. hashmap; import Java. util. map; import Java. util. random; public class testhello {public static void main (string [] ARGs) throws interruptedexception {thread. sleep (2000*10); testhello th = new testhello (); random = new random (47); th. test (random. nextint (9) + 1);} private Map <string, string> model = new hashmap <string, string> (); Public Boolean test (INT age) {string variable1 = "default prompt: The parameter you entered is:" + age; string variable2 = "welcome to the online Park"; model. put ("variable1", variable1); Model. put ("variable2", variable2); If (age> = 1 & age <= 5) {variable1 = "your input parameters are between 1 and 5 "; variable2 = "Welcome to Yizhi games"; model. put ("variable1", variable1); Model. put ("variable2", variable2); Return true;} else if (age> 5 & age <= 10) {variable1 = "your input parameter is between 5 and 10"; variable2 = "Welcome to English Playground"; model. put ("variable1", variable1); Model. put ("variable2", variable2); Return false ;}else {return true ;}}}
BTrace script:
Java code
- Import com. Sun. btrace. Annotations .*;
- Import static com. Sun. btrace. btraceutils .*;
- Import java. util. hashmap;
- @ Btrace public class btracefield {
- @ Onmethod (clazz = "Java. util. hashmap", method = "put ")
- Public static void m (@ Self HashMap map, Object Key, Object value) {// all callto the methods with signature "()"
- Println ("============================= ");
- Print (Key );
- Print (":");
- Println (value );
- }
- @ Onmethod (clazz = "testhello", method = "test ")
- Public static void D (@ self object OBJ, int age ){
- Print ("test method is called, input age = ");
- Println (AGE );
- }
- }
Import com. sun. btrace. annotations. *; import static com. sun. btrace. BTraceUtils. *; import java. util. hashMap; @ BTrace public class BTraceField {@ OnMethod (clazz = "java. util. hashMap ", method =" put ") public static void m (@ Self HashMap map, Object Key, Object value) {// all callto the methods with signature "() "println (" ========================== "); print (Key); print (":"); println (value) ;}@ OnMethod (clazz = "TestHello", method = "test") public static void d (@ Self Object obj, int age) {print ("test method is called, input age ="); println (age );}}
First Run TestHello
Use JPs to find the PID of testhello and enter btrace <pid> btracefield. Java
Output:
Test method is called, input age = 3
================================
Variable1: the default message is: 3.
================================
Variable2: Welcome to the online Park
================================
Variable1: Your input parameters are between 1 and 5.
================================
Variable2: Welcome to Yizhi games
Other related blog posts
One of the btrace series: overview
Btrace Series II: simple example
Btrace Series 3: actual cases
Btrace Series 4: cracking