Skip to content

Commit

Permalink
support windows unicode file path. #571
Browse files Browse the repository at this point in the history
  • Loading branch information
kylixs authored and hengyunabc committed Mar 14, 2019
1 parent 6f990a2 commit 4641ec0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
12 changes: 11 additions & 1 deletion agent/src/main/java/com/taobao/arthas/agent/AgentBootstrap.java
Expand Up @@ -5,6 +5,7 @@
import java.lang.instrument.Instrumentation;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLDecoder;
import java.util.jar.JarFile;

/**
Expand Down Expand Up @@ -92,10 +93,11 @@ private static void initSpy(ClassLoader classLoader) throws ClassNotFoundExcepti
Spy.initForAgentLauncher(classLoader, onBefore, onReturn, onThrows, beforeInvoke, afterInvoke, throwInvoke, reset);
}

private static synchronized void main(final String args, final Instrumentation inst) {
private static synchronized void main(String args, final Instrumentation inst) {
try {
ps.println("Arthas server agent start...");
// 传递的args参数分两个部分:agentJar路径和agentArgs, 分别是Agent的JAR包路径和期望传递到服务端的参数
args = decodeArg(args);
int index = args.indexOf(';');
String agentJar = args.substring(0, index);
final String agentArgs = args.substring(index, args.length());
Expand Down Expand Up @@ -172,4 +174,12 @@ private static void bind(Instrumentation inst, ClassLoader agentLoader, String a
}
ps.println("Arthas server already bind.");
}

private static String decodeArg(String arg) {
try {
return URLDecoder.decode(arg, "utf-8");
} catch (UnsupportedEncodingException e) {
return arg;
}
}
}
3 changes: 3 additions & 0 deletions boot/src/main/java/com/taobao/arthas/boot/Bootstrap.java
Expand Up @@ -6,6 +6,7 @@
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLDecoder;
import java.security.CodeSource;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -340,6 +341,8 @@ public static void main(String[] args) throws ParserConfigurationException, SAXE
if (codeSource != null) {
String bootJarPath = codeSource.getLocation().getFile();
try {
//convert unicode path to normal path
bootJarPath = URLDecoder.decode(bootJarPath, "utf-8");
verifyArthasHome(new File(bootJarPath).getParent());
arthasHomeDir = new File(bootJarPath).getParentFile();
} catch (Exception e) {
Expand Down
17 changes: 15 additions & 2 deletions core/src/main/java/com/taobao/arthas/core/Arthas.java
Expand Up @@ -11,6 +11,8 @@
import com.taobao.middleware.cli.Option;
import com.taobao.middleware.cli.TypedOption;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Properties;

Expand Down Expand Up @@ -85,15 +87,26 @@ private void attachAgent(Configure configure) throws Exception {
}
}

virtualMachine.loadAgent(configure.getArthasAgent(),
configure.getArthasCore() + ";" + configure.toString());
String arthasAgentPath = configure.getArthasAgent();
//convert jar path to unicode string
configure.setArthasAgent(encodeArg(arthasAgentPath));
configure.setArthasCore(encodeArg(configure.getArthasCore()));
virtualMachine.loadAgent(arthasAgentPath,
configure.getArthasCore() + ";" + configure.toString());
} finally {
if (null != virtualMachine) {
virtualMachine.detach();
}
}
}

private static String encodeArg(String arg) {
try {
return URLEncoder.encode(arg, "utf-8");
} catch (UnsupportedEncodingException e) {
return arg;
}
}

public static void main(String[] args) {
try {
Expand Down

0 comments on commit 4641ec0

Please sign in to comment.