agent works

This commit is contained in:
Claudio Maggioni 2023-12-13 11:41:52 +01:00
parent 65bb0fd0b4
commit 28c2237b77
8 changed files with 70 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/out/
/agent/agent.jar

View File

@ -4,6 +4,7 @@
<modules>
<module fileurl="file://$PROJECT_DIR$/agent/agent.iml" filepath="$PROJECT_DIR$/agent/agent.iml" />
<module fileurl="file://$PROJECT_DIR$/application/application.iml" filepath="$PROJECT_DIR$/application/application.iml" />
<module fileurl="file://$PROJECT_DIR$/lab-6-maggicl.iml" filepath="$PROJECT_DIR$/lab-6-maggicl.iml" />
<module fileurl="file://$PROJECT_DIR$/profiler/profiler.iml" filepath="$PROJECT_DIR$/profiler/profiler.iml" />
</modules>
</component>

16
README.md Normal file
View File

@ -0,0 +1,16 @@
# Agent
### Compile the agent as JAR
```shell
cd agent/src
find . -name '*.java' -print -exec javac -d ../../out/production/agent \{\} \;
cd ..
jar cfm agent.jar manifest.txt -C ../out/production/agent .
```
### Run application with agent
```shell
java -javaagent:agent/agent.jar=hello -cp out/production/application ch.usi.inf.sp.dbi.Application
```

2
agent/manifest.txt Normal file
View File

@ -0,0 +1,2 @@
Manifest-Version: 1.0
Premain-Class: ch.usi.inf.sp.dbi.agent.Agent

View File

@ -0,0 +1,10 @@
package ch.usi.inf.sp.dbi.agent;
import java.lang.instrument.Instrumentation;
public class Agent {
public static void premain(String agentArgs, Instrumentation inst) {
System.out.println("Agent starting (arguments: '" + agentArgs + "')");
inst.addTransformer(new ClassTransformer());
}
}

View File

@ -0,0 +1,17 @@
package ch.usi.inf.sp.dbi.agent;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;
public class ClassTransformer implements ClassFileTransformer {
@Override
public byte[] transform(ClassLoader loader,
String className,
Class<?> classBeingRedefined,
ProtectionDomain protectionDomain,
byte[] classfileBuffer) throws IllegalClassFormatException {
System.out.println("About to transform class <" + loader + ", " + className + ">");
return classfileBuffer; // no changes
}
}

View File

@ -0,0 +1,11 @@
package ch.usi.inf.sp.dbi;
public class Application {
public static void main(String... args) {
sayHi();
}
public static void sayHi() {
System.out.println("Hello!");
}
}

11
lab-6-maggicl.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>