Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
DasBrain committed May 30, 2023
0 parents commit 51d4e3f
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*
/.project
/.classpath
/.settings
/target
28 changes: 28 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
BSD 3-Clause License

Copyright (c) 2023, Johannes Kuhn

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# lamarr
Attach JShell to an existing Java Process
35 changes: 35 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pw.dasbrain</groupId>
<artifactId>lamarr</artifactId>
<version>1.0</version>
<description>Attach JShell to existing Java Processes</description>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<index>true</index>
<manifestEntries>
<Agent-Class>pw.dasbrain.lamarr.agent.AgentMain</Agent-Class>
<Can-Redefine-Classes>true</Can-Redefine-Classes>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.release>9</maven.compiler.release>
<maven.compiler.target>9</maven.compiler.target>
<maven.compiler.source>9</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
</project>
12 changes: 12 additions & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module pw.dasbrain.lamarr {

requires transitive java.instrument;
requires jdk.jshell;
requires jdk.attach;

exports pw.dasbrain.lamarr.agent to java.instrument;
opens pw.dasbrain.lamarr.agent to java.instrument;

provides jdk.jshell.spi.ExecutionControlProvider with
pw.dasbrain.lamarr.InstrumentationExecutionControlProvider;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package pw.dasbrain.lamarr;

import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

import com.sun.tools.attach.AgentInitializationException;
import com.sun.tools.attach.AgentLoadException;
import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;

import jdk.jshell.execution.StreamingExecutionControl;
import jdk.jshell.spi.ExecutionControl;
import jdk.jshell.spi.ExecutionControlProvider;
import jdk.jshell.spi.ExecutionEnv;

public class InstrumentationExecutionControlProvider implements ExecutionControlProvider {

@Override
public String name() {
return "instrumentation";
}

@Override
public ExecutionControl generate(ExecutionEnv env, Map<String, String> parameters)
throws Throwable {
ServerSocket so = new ServerSocket(0);
so.setSoTimeout(5000);
attach(parameters.get("pid"), so.getLocalPort());
Socket s = so.accept();
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
out.flush();
ObjectInputStream in = new ObjectInputStream(s.getInputStream());
return new StreamingExecutionControl(out, in);
}

@Override
public Map<String, String> defaultParameters() {
Map<String, String> result = new HashMap<>();
result.put("pid", "");
return result;
}

private static void attach(String pid, int port) throws AttachNotSupportedException,
IOException, AgentLoadException, AgentInitializationException, URISyntaxException {
VirtualMachine vm = VirtualMachine.attach(pid);
try {
vm.loadAgent(new File(InstrumentationExecutionControlProvider.class
.getProtectionDomain().getCodeSource().getLocation().toURI()).toString(),
port + "");
} finally {
vm.detach();
}
}
}
12 changes: 12 additions & 0 deletions src/main/java/pw/dasbrain/lamarr/agent/AgentMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package pw.dasbrain.lamarr.agent;

import java.lang.instrument.Instrumentation;

public class AgentMain {

public static void agentmain(String arg, Instrumentation inst) {
Thread t = new Thread(new InstrumentationRemoteExecutionControl(inst, arg), "rjshell");
t.setDaemon(false);
t.start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package pw.dasbrain.lamarr.agent;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.instrument.ClassDefinition;
import java.lang.instrument.Instrumentation;
import java.lang.instrument.UnmodifiableClassException;
import java.net.InetAddress;
import java.net.Socket;

import jdk.jshell.execution.RemoteExecutionControl;
import jdk.jshell.execution.Util;

class InstrumentationRemoteExecutionControl extends RemoteExecutionControl implements Runnable {

private final Instrumentation inst;
private final String arg;

public InstrumentationRemoteExecutionControl(Instrumentation inst, String arg) {
this.inst = inst;
this.arg = arg;
}

@Override
public void redefine(ClassBytecodes[] cbcs)
throws ClassInstallException, NotImplementedException, EngineTerminationException {
try {
ClassDefinition[] defs = new ClassDefinition[cbcs.length];
for (int i = 0; i < cbcs.length; i++) {
defs[i] = new ClassDefinition(findClass(cbcs[i].name()), cbcs[i].bytecodes());
}
inst.redefineClasses(defs);
} catch (ClassNotFoundException | UnmodifiableClassException
| UnsupportedOperationException e) {
// In this case no classes have been redefined.
ClassInstallException cie = new ClassInstallException(e.getMessage(), new boolean[cbcs.length]);
cie.initCause(e);
throw cie;
}
super.redefine(cbcs);
}

@Override
public void run() {
try (Socket s = new Socket(InetAddress.getLoopbackAddress(), Integer.parseInt(arg));
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream in = new ObjectInputStream(s.getInputStream());) {
out.flush();
Util.forwardExecutionControl(this, in, out);
} catch (IOException e) {
// TODO: Better error handling
throw new InternalError(e);
}
}
}

0 comments on commit 51d4e3f

Please sign in to comment.