Johnson
Johnson

Reputation: 718

How to correctly inject the agent?

There is an application written in java that uses the 1.8 version. I do not know if it matters, but it comes and runs with its own runtime, and launches via own executable. It looks like a regular jre, nothing special.

I'm trying to write a loader and an agent to continue using javassist.

When trying to start, the loader reports an error of 100:

PID: 14644
VM attached. PID: 14644
Injecting: /D:/_CODE/Cartographer/build/libs/Cartographer-1.0-SNAPSHOT.jar
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 io.github.relvl.Main.printSystemProcessor(Main.java:88)
        at io.github.relvl.Main.main(Main.java:58)

My jar is okaish, have MANIFEST.MF with all attributes. This is from unzipped jar:

Manifest-Version: 1.0
PreMain-Class: io.github.relvl.Agent
Agent-Class: io.github.relvl.Agent
Main-Class: io.github.relvl.Main

Question: what's going wrong?

Loader code:

package io.github.relvl;

import com.sun.tools.attach.VirtualMachine;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) {
        String command = getExecCommand();
        try (InputStream cmdIs = Runtime.getRuntime().exec(command).getInputStream();
             InputStreamReader cmdIsr = new InputStreamReader(cmdIs);
             BufferedReader cmdBr = new BufferedReader(cmdIsr)) {
            String line = "";
            while ((line = cmdBr.readLine()) != null) {
                if (line.startsWith("Launcher")) {
                    String pid = line.replaceAll("^[a-zA-Z0-9.-_]*\\s*([0-9]*)\\s.*$", "$1");
                    System.out.println("PID: " + pid);
                    VirtualMachine vm = VirtualMachine.attach(pid);
                    System.out.println("VM attached. ID: " + vm.id());
                    String agent = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath();
                    System.out.println("Injecting: " + agent);
                    if (!agent.endsWith(".jar")) return;
                    System.out.println("Loading agent...");
                    vm.loadAgent(agent);
                    System.out.println("Done!");
                    vm.detach();
                }
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    public static String getExecCommand() {
        String command = "cmd.exe /c tasklist";
        String osName = System.getProperty("os.name");
        if (osName != null && !osName.contains("Windows")) {
            // TODO! Check result format? I have no linux installed...
            command = "ps aux";
        }
        return command;
    }
}

Agent code:

package io.github.relvl;

import java.io.File;
import java.io.IOException;
import java.lang.instrument.Instrumentation;

public class Agent {
    public static void premain(String args, Instrumentation inst) {
        System.out.println("Hello premain!"); // idunno if i can see this sout, so just create a file...
        try {
            new File("D:/test-premain.txt").createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void agentmain(String agentArgs, Instrumentation inst) {
        System.out.println("Hello agentmain!"); // idunno if i can see this sout, so just create a file...
        try {
            new File("D:/test-agent.txt").createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Upvotes: 0

Views: 60

Answers (0)

Related Questions