Btrace Series II: simple example

Source: Internet
Author: User

 

Btrace tool usage

"Hello World"

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
  1. Import java. util. hashmap;
  2. Import java. util. Map;
  3. Import java. util. Random;
  4. Public class testhello
  5. {
  6. Public static void main (String [] args) throws InterruptedException
  7. {
  8. Thread. sleep (2000*10 );
  9. TestHello th = new TestHello ();
  10. Random random = new Random (47 );
  11. Th. test (random. nextInt (9) + 1 );
  12. }
  13. Private Map <String, String> model = new HashMap <String, String> ();
  14. Public boolean test (int age)
  15. {
  16. String variable1 = "default prompt: The parameter you entered is:" + age;
  17. String variable2 = "welcome to the online Park ";
  18. Model. put ("variable1", variable1 );
  19. Model. put ("variable2", variable2 );
  20. If (age> = 1 & age <= 5)
  21. {
  22. Variable1 = "your input parameters are between 1 and 5 ";
  23. Variable2 = "Welcome to Yizhi games ";
  24. Model. put ("variable1", variable1 );
  25. Model. put ("variable2", variable2 );
  26. Return true;
  27. }
  28. Else if (age> 5 & age <= 10)
  29. {
  30. Variable1 = "your input parameters are between 5 and 10 ";
  31. Variable2 = "Welcome to English Playground ";
  32. Model. Put ("variable1", variable1 );
  33. Model. Put ("variable2", variable2 );
  34. Return false;
  35. }
  36. Else
  37. {
  38. Return true;
  39. }
  40. }
  41. }
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
  1. Import com. Sun. btrace. Annotations .*;
  2. Import static com. Sun. btrace. btraceutils .*;
  3. Import java. util. hashmap;
  4. @ Btrace public class btracefield {
  5. @ Onmethod (clazz = "Java. util. hashmap", method = "put ")
  6. Public static void m (@ Self HashMap map, Object Key, Object value) {// all callto the methods with signature "()"
  7. Println ("============================= ");
  8. Print (Key );
  9. Print (":");
  10. Println (value );
  11. }
  12. @ Onmethod (clazz = "testhello", method = "test ")
  13. Public static void D (@ self object OBJ, int age ){
  14. Print ("test method is called, input age = ");
  15. Println (AGE );
  16. }
  17. }
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

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.