Skip to content

Commit

Permalink
Added tool to attach at runtime the agent
Browse files Browse the repository at this point in the history
OpenJDK based, depends on:
- JAVA_HOME set to a JDK instalation
- OpenJDK tools.jar available locally for compilation
  • Loading branch information
amuraru committed Apr 23, 2019
1 parent 178e24d commit 5dd6680
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
8 changes: 8 additions & 0 deletions attach-client.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
AGENT_PATH=`ls -1 $DIR/target/jvm-profiler-*.jar`
echo $AGENT_PATH

java -cp $AGENT_PATH:$JAVA_HOME/lib/tools.jar com.uber.profiling.tools.AttachClient $AGENT_PATH $*

8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,13 @@
<version>4.8.1</version>
<scope>test</scope>
</dependency>


<!-- required for AttachClient build -->
<dependency>
<groupId>com.github.olivergondza</groupId>
<artifactId>maven-jdk-tools-wrapper</artifactId>
<version>0.1</version>
</dependency>
</dependencies>

<build>
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/uber/profiling/tools/AttachClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.uber.profiling.tools;

import com.sun.tools.attach.VirtualMachine;

public class AttachClient {

public static void main(String[] args) throws Exception {
if (args.length != 3) {
System.out.println("Usage: com.uber.profiling.tools.AttachClient <agent_jar_full_path> <agent_args> <vm_pid>");
return;
}
String agent_path = args[0];
String agent_args = args[1];
String pid = args[2];
VirtualMachine vm = VirtualMachine.attach(pid);
System.out.println("vm = " + vm);
vm.loadAgent(agent_path, agent_args);
vm.detach();
}
}

0 comments on commit 5dd6680

Please sign in to comment.