5
5
import net .bytebuddy .description .method .MethodDescription ;
6
6
import net .bytebuddy .description .type .TypeDescription ;
7
7
import net .bytebuddy .matcher .ElementMatcher ;
8
- import net .bytebuddy .matcher .ElementMatchers ;
9
8
10
9
import java .io .File ;
11
10
import java .io .FileInputStream ;
12
11
import java .io .IOException ;
13
12
import java .io .InputStreamReader ;
14
13
import java .lang .instrument .Instrumentation ;
15
- import java .util .ArrayList ;
16
- import java .util .List ;
17
14
import java .util .Set ;
18
15
19
16
import org .apache .logging .log4j .Logger ;
20
17
import org .apache .logging .log4j .core .LoggerContext ;
21
- import org .apache .logging .log4j .core .config .LoggerConfig ;
22
18
import org .yaml .snakeyaml .LoaderOptions ;
23
19
import org .yaml .snakeyaml .Yaml ;
24
20
import org .yaml .snakeyaml .constructor .Constructor ;
25
- import org .apache .logging .log4j .Level ;
26
21
import org .apache .logging .log4j .LogManager ;
27
22
28
23
import ca .polymtl .gigl .moose .kavehshahedi .bbi .helpers .MethodMatcherHelper ;
@@ -32,7 +27,7 @@ public class FunctionEntryAgent {
32
27
33
28
public static Logger logger ;
34
29
35
- public static boolean onlyCheckVisited = false ;
30
+ public static Configuration config ;
36
31
37
32
public static final long TIME_OFFSET ;
38
33
static {
@@ -41,74 +36,58 @@ public class FunctionEntryAgent {
41
36
TIME_OFFSET = (currentTimeMillis * 1_000_000 ) - currentNanoTime ;
42
37
}
43
38
44
- public static void premain (String args , Instrumentation inst ) throws SecurityException , IOException {
45
- Configuration config = new Configuration (new Configuration .Logging ("fine" , "logs.log" ),
46
- new Configuration .Instrumentation (
47
- "" , new Configuration .Instrumentation .TargetMethods (new ArrayList <>(), new ArrayList <>())));
39
+ public static void premain (String args , Instrumentation inst ) {
40
+ config = new Configuration ();
48
41
49
- String options [] ;
50
- if (args != null && args .length () > 0 ) {
42
+ String [] options ;
43
+ if (args != null && ! args .isEmpty () ) {
51
44
options = args .split ("," );
52
- if ( options . length > 0 ) {
53
- for ( String option : options ) {
54
- if ( option .contains ("=" )) {
55
- String key = option .split ("=" )[0 ];
56
- String value = option . split ( "=" )[ 1 ];
57
-
58
- switch ( key ) {
59
- case "config" :
60
- Constructor constructor = new Constructor ( Configuration . class , new LoaderOptions () );
61
- Yaml yaml = new Yaml ( constructor );
62
- try ( InputStreamReader reader = new InputStreamReader (
63
- new FileInputStream ( new File ( value )))) {
45
+ for ( String option : options ) {
46
+ if ( option . contains ( "=" ) ) {
47
+ String key = option .split ("=" )[ 0 ];
48
+ String value = option .split ("=" )[1 ];
49
+
50
+ switch ( key ) {
51
+ case "config" :
52
+ Constructor constructor = new Constructor ( Configuration . class , new LoaderOptions ());
53
+ Yaml yaml = new Yaml ( constructor );
54
+ try ( InputStreamReader reader = new InputStreamReader (
55
+ new FileInputStream ( new File ( value )))) {
56
+ try {
64
57
config = yaml .load (reader );
65
- } catch (IOException e ) {
66
- System .err .println ("Error reading the configuration file" );
58
+ } catch (Exception e ) {
59
+ System .err .println ("Error while parsing the configuration file" );
67
60
}
61
+ } catch (IOException e ) {
62
+ System .err .println ("Error while reading the configuration file" );
63
+ }
68
64
69
- break ;
70
- default :
71
- break ;
72
- }
65
+ break ;
66
+ default :
67
+ break ;
73
68
}
74
69
}
75
70
}
76
71
}
77
72
78
73
configureLoggerName (config .getLogging ());
79
74
80
- ElementMatcher .Junction <TypeDescription > targetPackageMatcher = ElementMatchers .any ();
81
- if (!config .getInstrumentation ().getTargetPackage ().isEmpty ()
82
- && !config .getInstrumentation ().getTargetPackage ().equals ("*" )) {
83
- targetPackageMatcher = ElementMatchers .nameStartsWith (config .getInstrumentation ().getTargetPackage ());
84
- }
85
-
86
- // Exclude the agent class from instrumentation (along with logging and other)
87
- targetPackageMatcher = targetPackageMatcher
88
- .and (ElementMatchers .not (ElementMatchers .nameStartsWith (FunctionEntryAgent .class .getPackageName ())));
89
- targetPackageMatcher = targetPackageMatcher
90
- .and (ElementMatchers .not (ElementMatchers .nameStartsWith ("org.apache.logging.log4j" )));
75
+ ElementMatcher .Junction <TypeDescription > targetPackageMatcher = MethodMatcherHelper .createTargetPackageMatcher (config .getInstrumentation ());
76
+ ElementMatcher .Junction <MethodDescription > methodMatcher = MethodMatcherHelper .createMethodMatcher (config .getInstrumentation ());
91
77
92
- List <String > instrumentMethodSignatures = config .getInstrumentation ().getTargetMethods ().getInstrument ();
93
- List <String > ignoreMethodSignatures = config .getInstrumentation ().getTargetMethods ().getIgnore ();
94
-
95
- ElementMatcher .Junction <MethodDescription > methodMatchers = MethodMatcherHelper
96
- .createMethodMatcher (instrumentMethodSignatures , ignoreMethodSignatures );
78
+ System .out .println (config );
97
79
98
80
new AgentBuilder .Default ()
99
81
.type (targetPackageMatcher )
100
82
.transform (new AgentBuilder .Transformer .ForAdvice ()
101
- .advice (methodMatchers , MethodExecutionTime .class .getName ()))
83
+ .advice (methodMatcher , MethodExecutionTime .class .getName ()))
102
84
.installOn (inst );
103
85
104
86
System .out .println ("-" .repeat (20 ));
105
87
}
106
88
107
89
public static void configureLoggerName (Configuration .Logging loggingInfo ) {
108
90
String logFileName = loggingInfo .getFile ();
109
- if (logFileName == null || logFileName .isEmpty ())
110
- logFileName = "logs.log" ;
111
-
112
91
System .setProperty ("logFilename" , logFileName );
113
92
114
93
LoggerContext ctx = (LoggerContext ) LogManager .getContext (false );
@@ -126,7 +105,7 @@ public static class MethodExecutionTime {
126
105
127
106
@ Advice .OnMethodEnter
128
107
static void enter (@ Advice .Origin String methodSignature ) {
129
- if (onlyCheckVisited ) {
108
+ if (config . getInstrumentation (). isOnlyCheckVisited () ) {
130
109
if (!visitedMethods .contains (methodSignature )) {
131
110
visitedMethods .add (methodSignature );
132
111
logTime (methodSignature , "ENTER" );
@@ -138,7 +117,7 @@ static void enter(@Advice.Origin String methodSignature) {
138
117
139
118
@ Advice .OnMethodExit
140
119
static void exit (@ Advice .Origin String methodSignature ) {
141
- if (onlyCheckVisited )
120
+ if (config . getInstrumentation (). isOnlyCheckVisited () )
142
121
return ;
143
122
144
123
logTime (methodSignature , "EXIT" );
0 commit comments