From 416a4df0c66a2fe03c2d70407a26a427c971dd5a Mon Sep 17 00:00:00 2001 From: Adi Muraru Date: Thu, 18 Apr 2019 19:00:13 +0300 Subject: [PATCH] Added tool to attach at runtime the agent OpenJDK based, depends on: - JAVA_HOME set to a JDK instalation - OpenJDK tools.jar available locally for compilation --- attach-client.sh | 8 ++++++++ pom.xml | 8 +++++++- .../uber/profiling/tools/AttachClient.java | 20 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100755 attach-client.sh create mode 100644 src/main/java/com/uber/profiling/tools/AttachClient.java diff --git a/attach-client.sh b/attach-client.sh new file mode 100755 index 0000000..671dce2 --- /dev/null +++ b/attach-client.sh @@ -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 $* + diff --git a/pom.xml b/pom.xml index 5c84fbb..b03f83f 100644 --- a/pom.xml +++ b/pom.xml @@ -81,7 +81,13 @@ 4.8.1 test - + + + + com.github.olivergondza + maven-jdk-tools-wrapper + 0.1 + diff --git a/src/main/java/com/uber/profiling/tools/AttachClient.java b/src/main/java/com/uber/profiling/tools/AttachClient.java new file mode 100644 index 0000000..4feaf1e --- /dev/null +++ b/src/main/java/com/uber/profiling/tools/AttachClient.java @@ -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 "); + 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(); + } +}