66import dev .aikido .agent_api .helpers .logging .LogManager ;
77import dev .aikido .agent_api .helpers .logging .Logger ;
88import net .bytebuddy .agent .builder .AgentBuilder ;
9+ import net .bytebuddy .asm .Advice ;
10+ import net .bytebuddy .description .method .MethodDescription ;
11+ import net .bytebuddy .description .type .TypeDescription ;
12+ import net .bytebuddy .implementation .Implementation ;
13+ import net .bytebuddy .implementation .bytecode .StackManipulation ;
14+ import net .bytebuddy .jar .asm .MethodVisitor ;
915import net .bytebuddy .matcher .ElementMatcher ;
1016import net .bytebuddy .matcher .ElementMatchers ;
1117
1218import java .io .File ;
19+ import java .io .IOException ;
1320import java .lang .instrument .Instrumentation ;
21+ import java .util .jar .JarFile ;
1422
1523import static dev .aikido .agent .ByteBuddyInitializer .createAgentBuilder ;
1624import static dev .aikido .agent .DaemonStarter .startDaemon ;
1927
2028public class Agent {
2129 private static final Logger logger = LogManager .getLogger (Agent .class );
30+
2231 public static void premain (String agentArgs , Instrumentation inst ) {
2332 // Check for 'AIKIDO_DISABLE' :
2433 if (new BooleanEnv ("AIKIDO_DISABLE" , /*default value*/ false ).getValue ()) {
2534 return ; // AIKIDO_DISABLE is true, so we will not be wrapping anything.
2635 }
2736 logger .info ("Zen by Aikido v%s starting." , Config .pkgVersion );
28- setAikidoSysProperties ();
37+ try {
38+ setAikidoSysProperties (inst );
39+ } catch (Exception e ) {
40+ logger .error (e .getMessage ());
41+ }
42+
43+ // Modify bootstrap class path (includes the core Java classes), so we can have some shared
44+ // code for core java classes when wrapping.
45+ try {
46+ inst .appendToBootstrapClassLoaderSearch (new JarFile (getPathToAikidoDirectory () + "/agent_bootstrap.jar" ));
47+ } catch (IOException e ) {
48+ logger .error (e .getMessage ());
49+ }
2950
3051 // Test loading of zen binaries :
3152 loadLibrary ();
3253
54+
3355 ElementMatcher .Junction wrapperTypeDescriptors = ElementMatchers .none ();
3456 for (Wrapper wrapper : WRAPPERS ) {
3557 wrapperTypeDescriptors = wrapperTypeDescriptors .or (wrapper .getTypeMatcher ());
@@ -56,11 +78,14 @@ public static AgentBuilder.Transformer get() {
5678 return adviceAgentBuilder ;
5779 }
5880 }
59- private static void setAikidoSysProperties () {
81+ private static String getPathToAikidoDirectory () {
6082 String pathToAgentJar = Agent .class .getProtectionDomain ().getCodeSource ().getLocation ().getPath ();
61- String pathToAikidoDirectory = new File (pathToAgentJar ).getParent ();
62- String jarPath = "file:" + pathToAikidoDirectory + "/agent_api.jar" ;
63- System .setProperty ("AIK_agent_dir" , pathToAikidoDirectory );
83+ return new File (pathToAgentJar ).getParent ();
84+ }
85+ private static void setAikidoSysProperties (Instrumentation inst ) throws IOException {
86+
87+ String jarPath = "file:" + getPathToAikidoDirectory () + "/agent_api.jar" ;
88+ System .setProperty ("AIK_agent_dir" , getPathToAikidoDirectory ());
6489 System .setProperty ("AIK_agent_api_jar" , jarPath );
6590 }
66- }
91+ }
0 commit comments