Its my first time using agents, i went to task manager and picked the pid of my minecraft, when i do > jvm.attach(myAgent); i get AgentLoadException. loader:
File agentFile = new File("src/main/resources/jar/agent.jar");
VirtualMachine jvm = VirtualMachine.attach(pid);
jvm.loadAgent(agentFile.getAbsolutePath());
jvm.detach();
agent:
public static void premain(String agentArgs, Instrumentation inst) {
String className = "net.minecraft.client.Minecraft";
transformClass(className, inst);
}
public static void agentmain(String agentArgs, Instrumentation inst) {
String className = "net.minecraft.client.Minecraft";
transformClass(className, inst);
}
private static void transformClass(String className, Instrumentation instrumentation) {
Class<?> targetClass = null;
ClassLoader targetClassLoader = null;
try {
targetClass = Class.forName(className);
targetClassLoader = targetClass.getClassLoader();
tranform(targetClass, targetClassLoader, instrumentation);
return;
} catch (Exception exception) {
System.out.println("An error ocurred in transformClass()");
}
for (Class<?> clazz : instrumentation.getAllLoadedClasses()) {
if (clazz.getName().equals(className)) {
targetClass = clazz;
targetClassLoader = targetClass.getClassLoader();
tranform(targetClass, targetClassLoader, instrumentation);
return;
}
}
throw new RuntimeException("Failed to find class[" + className + "]");
}
private static void tranform(Class<?> clazz, ClassLoader classLoader, Instrumentation instrumentation) {
MinecraftTransformer dt = new MinecraftTransformer(clazz.getName(), classLoader);
instrumentation.addTransformer(dt, true);
try {
instrumentation.retransformClasses(clazz);
} catch (Exception ex) {
throw new RuntimeException("Transform failed for: [" + clazz.getName() + "]");
}
}
public static class MinecraftTransformer implements ClassFileTransformer {
private final String METHOD = "runGameLoop";
private String targetClassName;
private ClassLoader targetClassLoader;
public MinecraftTransformer(String targetClassName, ClassLoader classLoader) {
this.targetClassLoader = classLoader;
this.targetClassName = "net.minecraft.client.Minecraft";
}
@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
byte[] byteCode = classfileBuffer;
String finalTargetClassName = targetClassName.replaceAll("\\.", "/");
if (!className.equals(finalTargetClassName)) {
return byteCode;
}
if (className.equals(finalTargetClassName) && loader.equals(targetClassLoader)) {
System.out.println("Transforming the class");
try {
ClassPool cp = ClassPool.getDefault();
CtClass cc = cp.get(this.targetClassName);
CtMethod m = cc.getDeclaredMethod(METHOD);
StringBuilder startBlock = new StringBuilder();
startBlock.append("System.out.println(\"Agent works\");");
m.insertBefore(startBlock.toString());
byteCode = cc.toBytecode();
cc.detach();
} catch (NotFoundException | CannotCompileException | IOException exception) {
System.out.println("error en agent");
}
}
return byteCode;
}
}
I have been reading the docs about this exception and gives 0 information about the extra information ( im getting 100 ) can it be because some security things or because i did something wrong with my agent?
stacktrace:
com.sun.tools.attach.AgentLoadException: 100
at jdk.attach/sun.tools.attach.HotSpotVirtualMachine.loadAgentLibrary(HotSpotVirtualMachine.java:109)
at jdk.attach/sun.tools.attach.HotSpotVirtualMachine.loadAgentLibrary(HotSpotVirtualMachine.java:120)
at jdk.attach/sun.tools.attach.HotSpotVirtualMachine.loadAgent(HotSpotVirtualMachine.java:148)
at jdk.attach/com.sun.tools.attach.VirtualMachine.loadAgent(VirtualMachine.java:538)
at Launcher.main(Launcher.java:16)
pom.xml of the agent:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.3</version>
<configuration>
<archive>
<manifestFile>src/main/resources/custom/MANIFEST.MF</manifestFile>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
manifest file:
Agent-Class: Agent
Can-Redefine-Classes: true
Can-Retransform-Classes: true
Premain-Class: Agent
You are currently trying to create/set the manifest file with the maven-site-plugin
. This plugin is used to generate a website for your project.
See https://maven.apache.org/plugins/maven-site-plugin/
I think you either want the maven-archiver
https://maven.apache.org/shared/maven-archiver/ or the maven-jar-plugin
to customize your manifest: https://maven.apache.org/plugins/maven-jar-plugin/examples/manifest-customization.html
The default location for the manifest is also src/main/resources/META-INF/MANIFEST.MF
, you could try moving it from your current location src/main/resources/custom/MANIFEST.MF
You could also try any of the following tutorials on how to create your own Agent:
I would advice you to open the generated jar file of your project with a decompiler and check the contents of the Manifest file in that jar, for example with https://github.com/java-decompiler/jd-gui