From 80a30351d3f196b47c35eab77dd98099ae1de9fd Mon Sep 17 00:00:00 2001 From: BlueT Date: Tue, 18 Sep 2018 00:11:33 +0800 Subject: [PATCH 1/2] add:local debug test --- core/src/test/java/LocalDebugTest.java | 79 ++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 core/src/test/java/LocalDebugTest.java diff --git a/core/src/test/java/LocalDebugTest.java b/core/src/test/java/LocalDebugTest.java new file mode 100644 index 000000000..07645a983 --- /dev/null +++ b/core/src/test/java/LocalDebugTest.java @@ -0,0 +1,79 @@ +import com.taobao.arthas.core.Arthas; +import sun.management.VMManagement; + +import java.lang.management.ManagementFactory; +import java.lang.management.RuntimeMXBean; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * 本机调试 + * @author BlueT + * 2018/9/17 23:34 + */ +public class LocalDebugTest { + + /** + * 首先执行./mvnw clean package -DskipTests打包,生成的zip在 packaging/target/ 下面,然后解压。 + * 以debug方式执行此方法, + * + * @param args + * @throws InterruptedException + */ + public static void main(String[] args) throws InterruptedException { + final int pid = getCurrentJVMPid(); + System.out.println("pid:"+pid); + String path = LocalDebugTest.class.getResource("/").getPath(); + final String npath = path.substring(1, path.indexOf("core")) + "packaging/target/"; + Thread thread = new Thread(new Runnable() { + @Override + public void run() { + Arthas.main(new String[]{ + "-jar", + npath + "arthas-core.jar", + "-pid", + pid + "", + "-target-ip", + "127.0.0.1", + //"-telnet-port", + //"3658", + //"-http-port", + //"8563", + "-core", + npath + "arthas-core.jar", + "-agent", + npath + "arthas-agent.jar" + }); + } + }); + thread.start(); + thread.join(); + System.out.println("代码植入成功"); + Thread.sleep(10000000); + } + + private static int getCurrentJVMPid() { + RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean(); + + try { + Field jvm = runtime.getClass().getDeclaredField("jvm"); + jvm.setAccessible(true); + VMManagement mgmt = (VMManagement) jvm.get(runtime); + Method pid_method = mgmt.getClass().getDeclaredMethod("getProcessId"); + pid_method.setAccessible(true); + return (int) (Integer) pid_method.invoke(mgmt); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + + throw new RuntimeException("Wow!"); + + } +} From 2526b84d97e5dfdb2302afc1e85586e921860423 Mon Sep 17 00:00:00 2001 From: ted-wq-x <736445126@qq.com> Date: Wed, 19 Sep 2018 08:24:42 +0800 Subject: [PATCH 2/2] fix:#62 --- bin/as.sh | 38 +++++++++++--------------------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/bin/as.sh b/bin/as.sh index 5857fbdd6..ba2920c1f 100755 --- a/bin/as.sh +++ b/bin/as.sh @@ -413,34 +413,18 @@ active_console() local arthas_version=$1 local arthas_lib_dir=${ARTHAS_LIB_DIR}/${arthas_version}/arthas - if [[ "${arthas_version}" > "3.0" ]]; then - if [ "${BATCH_MODE}" = "true" ]; then - ${JAVA_HOME}/bin/java ${ARTHAS_OPTS} ${JVM_OPTS} \ - -jar ${arthas_lib_dir}/arthas-client.jar \ - ${TARGET_IP} \ - -p ${TELNET_PORT} \ - -f ${BATCH_SCRIPT} - elif type telnet 2>&1 >> /dev/null; then - # use telnet - telnet ${TARGET_IP} ${TELNET_PORT} - else - echo "'telnet' is required." 1>&2 - return 1 - fi + if [ "${BATCH_MODE}" = "true" ]; then + ${JAVA_HOME}/bin/java ${ARTHAS_OPTS} ${JVM_OPTS} \ + -jar ${arthas_lib_dir}/arthas-client.jar \ + ${TARGET_IP} \ + -p ${TELNET_PORT} \ + -f ${BATCH_SCRIPT} + elif type telnet 2>&1 >> /dev/null; then + # use telnet + telnet ${TARGET_IP} ${TELNET_PORT} else - # for compatibility - # use default console - ARGS="${TARGET_IP} ${TELNET_PORT}" - if [ ${BATCH_MODE} = true ]; then - ARGS="$ARGS -b" - fi - if [ ! -z ${BATCH_SCRIPT} ]; then - ARGS="$ARGS -f $BATCH_SCRIPT" - fi - eval ${JAVA_HOME}/bin/java ${ARTHAS_OPTS} \ - -cp ${arthas_lib_dir}/arthas-core.jar \ - com.taobao.arthas.core.ArthasConsole \ - ${ARGS} + echo "'telnet' is required." 1>&2 + return 1 fi }