mirror of
https://github.com/alibaba/arthas.git
synced 2024-04-21 10:21:39 +00:00
check vm java version before attach. fixed #6
This commit is contained in:
@@ -3,6 +3,7 @@ package com.taobao.arthas.core;
|
||||
import com.sun.tools.attach.VirtualMachine;
|
||||
import com.sun.tools.attach.VirtualMachineDescriptor;
|
||||
import com.taobao.arthas.core.config.Configure;
|
||||
import com.taobao.arthas.core.util.AnsiLog;
|
||||
import com.taobao.middleware.cli.CLI;
|
||||
import com.taobao.middleware.cli.CLIs;
|
||||
import com.taobao.middleware.cli.CommandLine;
|
||||
@@ -10,6 +11,7 @@ import com.taobao.middleware.cli.Option;
|
||||
import com.taobao.middleware.cli.TypedOption;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* Arthas启动器
|
||||
@@ -59,7 +61,6 @@ public class Arthas {
|
||||
virtualMachineDescriptor = descriptor;
|
||||
}
|
||||
}
|
||||
|
||||
VirtualMachine virtualMachine = null;
|
||||
try {
|
||||
if (null == virtualMachineDescriptor) { // 使用 attach(String pid) 这种方式
|
||||
@@ -67,6 +68,19 @@ public class Arthas {
|
||||
} else {
|
||||
virtualMachine = VirtualMachine.attach(virtualMachineDescriptor);
|
||||
}
|
||||
|
||||
Properties targetSystemProperties = virtualMachine.getSystemProperties();
|
||||
String targetJavaVersion = targetSystemProperties.getProperty("java.specification.version");
|
||||
String currentJavaVersion = System.getProperty("java.specification.version");
|
||||
if (targetJavaVersion != null && currentJavaVersion != null) {
|
||||
if (!targetJavaVersion.equals(currentJavaVersion)) {
|
||||
AnsiLog.warn("Current VM java version: {} do not match target VM java version: {}, attach may fail.",
|
||||
currentJavaVersion, targetJavaVersion);
|
||||
AnsiLog.warn("Target VM JAVA_HOME is {}, try to set the same JAVA_HOME.",
|
||||
targetSystemProperties.getProperty("java.home"));
|
||||
}
|
||||
}
|
||||
|
||||
virtualMachine.loadAgent(configure.getArthasAgent(),
|
||||
configure.getArthasCore() + ";" + configure.toString());
|
||||
} finally {
|
||||
@@ -81,7 +95,7 @@ public class Arthas {
|
||||
try {
|
||||
new Arthas(args);
|
||||
} catch (Throwable t) {
|
||||
System.err.println("Start arthas failed, exception stack trace: ");
|
||||
AnsiLog.error("Start arthas failed, exception stack trace: ");
|
||||
t.printStackTrace();
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.taobao.arthas.core.util;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author hengyunabc 2017-05-03
|
||||
*
|
||||
*/
|
||||
public abstract class AnsiLog {
|
||||
|
||||
static boolean enable = System.console() != null;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param msg
|
||||
*/
|
||||
public static void red(String msg) {
|
||||
if (enable) {
|
||||
System.out.println("[31m" + msg + "[0m");
|
||||
} else {
|
||||
System.err.println(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void green(String msg) {
|
||||
if (enable) {
|
||||
System.out.println("[32m" + msg + "[0m");
|
||||
} else {
|
||||
System.out.println(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void yellow(String msg) {
|
||||
if (enable) {
|
||||
System.out.println("[33m" + msg + "[0m");
|
||||
} else {
|
||||
System.out.println(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void info(String msg) {
|
||||
green(msg);
|
||||
}
|
||||
|
||||
public static void info(String format, Object... arguments) {
|
||||
green(format(format, arguments));
|
||||
}
|
||||
|
||||
public static void warn(String msg) {
|
||||
yellow(msg);
|
||||
}
|
||||
|
||||
public static void warn(String format, Object... arguments) {
|
||||
yellow(format(format, arguments));
|
||||
}
|
||||
|
||||
public static void error(String msg) {
|
||||
red(msg);
|
||||
}
|
||||
|
||||
public static void error(String format, Object... arguments) {
|
||||
red(format(format, arguments));
|
||||
}
|
||||
|
||||
private static String format(String from, Object... arguments) {
|
||||
if (from != null) {
|
||||
String computed = from;
|
||||
if (arguments != null && arguments.length != 0) {
|
||||
for (Object argument : arguments) {
|
||||
computed = computed.replaceFirst("\\{\\}", Matcher.quoteReplacement(argument.toString()));
|
||||
}
|
||||
}
|
||||
return computed;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user