simplify AgentBootstrap; use long to store pid; #990

This commit is contained in:
hengyunabc
2019-12-27 19:33:17 +08:00
parent f5270aba36
commit 7d186f87f0
18 changed files with 82 additions and 129 deletions
@@ -29,7 +29,7 @@ public class Arthas {
}
private Configure parse(String[] args) {
Option pid = new TypedOption<Integer>().setType(Integer.class).setShortName("pid").setRequired(true);
Option pid = new TypedOption<Long>().setType(Long.class).setShortName("pid").setRequired(true);
Option core = new TypedOption<String>().setType(String.class).setShortName("core").setRequired(true);
Option agent = new TypedOption<String>().setType(String.class).setShortName("agent").setRequired(true);
Option target = new TypedOption<String>().setType(String.class).setShortName("target-ip");
@@ -50,7 +50,7 @@ public class Arthas {
CommandLine commandLine = cli.parse(Arrays.asList(args));
Configure configure = new Configure();
configure.setJavaPid((Integer) commandLine.getOptionValue("pid"));
configure.setJavaPid((Long) commandLine.getOptionValue("pid"));
configure.setArthasAgent((String) commandLine.getOptionValue("agent"));
configure.setArthasCore((String) commandLine.getOptionValue("core"));
configure.setSessionTimeout((Integer)commandLine.getOptionValue("session-timeout"));
@@ -73,7 +73,7 @@ public class Arthas {
VirtualMachineDescriptor virtualMachineDescriptor = null;
for (VirtualMachineDescriptor descriptor : VirtualMachine.list()) {
String pid = descriptor.id();
if (pid.equals(Integer.toString(configure.getJavaPid()))) {
if (pid.equals(Long.toString(configure.getJavaPid()))) {
virtualMachineDescriptor = descriptor;
}
}
@@ -30,7 +30,13 @@ public class AdviceWeaver extends ClassVisitor implements Opcodes {
private final static Logger logger = LogUtil.getArthasLogger();
public static final String ON_BEFORE = "methodOnBegin";
public static final String ON_RETURN = "methodOnReturnEnd";
public static final String ON_THROWS = "methodOnThrowingEnd";
public static final String BEFORE_INVOKE = "methodOnInvokeBeforeTracing";
public static final String AFTER_INVOKE = "methodOnInvokeAfterTracing";
public static final String THROW_INVOKE = "methodOnInvokeThrowTracing";
public static final String RESET = "resetArthasClassLoader";
// 线程帧栈堆栈大小
private final static int FRAME_STACK_SIZE = 7;
@@ -20,7 +20,7 @@ public class Configure {
private String ip;
private int telnetPort;
private int httpPort;
private int javaPid;
private long javaPid;
private String arthasCore;
private String arthasAgent;
@@ -61,11 +61,11 @@ public class Configure {
return httpPort;
}
public int getJavaPid() {
public long getJavaPid() {
return javaPid;
}
public void setJavaPid(int javaPid) {
public void setJavaPid(long javaPid) {
this.javaPid = javaPid;
}
@@ -228,7 +228,6 @@ public class PropertyPlaceholderHelper {
* Strategy interface used to resolve replacement values for placeholders
* contained in Strings.
*/
@FunctionalInterface
public interface PlaceholderResolver {
/**
@@ -2,6 +2,8 @@ package com.taobao.arthas.core.server;
import com.taobao.arthas.core.config.Configure;
import com.alibaba.arthas.tunnel.client.TunnelClient;
import com.taobao.arthas.common.PidUtils;
import com.taobao.arthas.core.advisor.AdviceWeaver;
import com.taobao.arthas.core.command.BuiltinCommandPack;
import com.taobao.arthas.core.shell.ShellServer;
import com.taobao.arthas.core.shell.ShellServerOptions;
@@ -18,6 +20,7 @@ import com.taobao.middleware.logger.Logger;
import io.netty.channel.ChannelFuture;
import java.arthas.Spy;
import java.io.File;
import java.io.IOException;
import java.lang.instrument.Instrumentation;
@@ -43,7 +46,6 @@ public class ArthasBootstrap {
private static ArthasBootstrap arthasBootstrap;
private AtomicBoolean isBindRef = new AtomicBoolean(false);
private int pid;
private Instrumentation instrumentation;
private Thread shutdown;
private ShellServer shellServer;
@@ -52,8 +54,7 @@ public class ArthasBootstrap {
private File arthasOutputDir;
private ArthasBootstrap(int pid, Instrumentation instrumentation) {
this.pid = pid;
private ArthasBootstrap(Instrumentation instrumentation) {
this.instrumentation = instrumentation;
String outputPath = System.getProperty("arthas.output.dir", "arthas-output");
@@ -80,6 +81,24 @@ public class ArthasBootstrap {
Runtime.getRuntime().addShutdownHook(shutdown);
}
private static void initSpy() throws ClassNotFoundException, NoSuchMethodException {
Class<?> adviceWeaverClass = AdviceWeaver.class;
Method onBefore = adviceWeaverClass.getMethod(AdviceWeaver.ON_BEFORE, int.class, ClassLoader.class, String.class,
String.class, String.class, Object.class, Object[].class);
Method onReturn = adviceWeaverClass.getMethod(AdviceWeaver.ON_RETURN, Object.class);
Method onThrows = adviceWeaverClass.getMethod(AdviceWeaver.ON_THROWS, Throwable.class);
Method beforeInvoke = adviceWeaverClass.getMethod(AdviceWeaver.BEFORE_INVOKE, int.class, String.class, String.class, String.class, int.class);
Method afterInvoke = adviceWeaverClass.getMethod(AdviceWeaver.AFTER_INVOKE, int.class, String.class, String.class, String.class, int.class);
Method throwInvoke = adviceWeaverClass.getMethod(AdviceWeaver.THROW_INVOKE, int.class, String.class, String.class, String.class, int.class);
Spy.init(AdviceWeaver.class.getClassLoader(), onBefore, onReturn, onThrows, beforeInvoke, afterInvoke, throwInvoke);
}
public void bind(String args) throws Throwable {
initSpy();
Configure configure = Configure.toConfigure(args);
bind(configure);
}
/**
* Bootstrap arthas server
*
@@ -120,7 +139,7 @@ public class ArthasBootstrap {
try {
ShellServerOptions options = new ShellServerOptions()
.setInstrumentation(instrumentation)
.setPid(pid)
.setPid(PidUtils.currentLongPid())
.setSessionTimeout(configure.getSessionTimeout() * 1000);
if (agentId != null) {
@@ -208,9 +227,9 @@ public class ArthasBootstrap {
* @param instrumentation JVM增强
* @return ArthasServer单例
*/
public synchronized static ArthasBootstrap getInstance(int javaPid, Instrumentation instrumentation) {
public synchronized static ArthasBootstrap getInstance(Instrumentation instrumentation) {
if (arthasBootstrap == null) {
arthasBootstrap = new ArthasBootstrap(javaPid, instrumentation);
arthasBootstrap = new ArthasBootstrap(instrumentation);
}
return arthasBootstrap;
}
@@ -34,7 +34,7 @@ public class ShellServerOptions {
private long sessionTimeout;
private long reaperInterval;
private long connectionTimeout;
private int pid;
private long pid;
private Instrumentation instrumentation;
public ShellServerOptions() {
@@ -98,7 +98,7 @@ public class ShellServerOptions {
return this;
}
public ShellServerOptions setPid(int pid) {
public ShellServerOptions setPid(long pid) {
this.pid = pid;
return this;
}
@@ -108,7 +108,7 @@ public class ShellServerOptions {
return this;
}
public int getPid() {
public long getPid() {
return pid;
}
@@ -19,7 +19,6 @@ import com.taobao.arthas.core.shell.system.JobController;
import com.taobao.arthas.core.shell.system.impl.InternalCommandManager;
import com.taobao.arthas.core.shell.system.impl.JobControllerImpl;
import com.taobao.arthas.core.shell.term.Term;
import com.taobao.arthas.core.util.Constants;
import com.taobao.arthas.core.util.LogUtil;
import com.taobao.middleware.logger.Logger;
@@ -49,7 +48,7 @@ public class ShellImpl implements Shell {
private String prompt;
public ShellImpl(ShellServer server, Term term, InternalCommandManager commandManager,
Instrumentation instrumentation, int pid, JobControllerImpl jobController) {
Instrumentation instrumentation, long pid, JobControllerImpl jobController) {
session.put(Session.COMMAND_MANAGER, commandManager);
session.put(Session.INSTRUMENTATION, instrumentation);
session.put(Session.PID, pid);
@@ -50,7 +50,7 @@ public class ShellServerImpl extends ShellServer {
private String welcomeMessage;
private ArthasBootstrap bootstrap;
private Instrumentation instrumentation;
private int pid;
private long pid;
private boolean closed = true;
private final Map<String, ShellImpl> sessions;
private final Future<Void> sessionsClosed = Future.future();
@@ -92,7 +92,7 @@ public interface Session {
*
* @return java pid
*/
int getPid();
long getPid();
/**
* Get all registered command resolvers
@@ -75,8 +75,8 @@ public class SessionImpl implements Session {
}
@Override
public int getPid() {
return (Integer) data.get(PID);
public long getPid() {
return (Long) data.get(PID);
}
@Override
@@ -1,32 +0,0 @@
package com.taobao.arthas.core.util;
import java.lang.management.ManagementFactory;
/**
*
* @author hengyunabc 2018-09-30
*
*/
public class ApplicationUtils {
private static String PID = "-1";
static {
// https://stackoverflow.com/a/7690178
String jvmName = ManagementFactory.getRuntimeMXBean().getName();
int index = jvmName.indexOf('@');
if (index > 0) {
try {
PID = Long.toString(Long.parseLong(jvmName.substring(0, index)));
} catch (Throwable e) {
// ignore
}
}
}
public static String getPid() {
return PID;
}
}
@@ -2,6 +2,7 @@ package com.taobao.arthas.core.util;
import java.io.File;
import com.taobao.arthas.common.PidUtils;
import com.taobao.arthas.core.view.Ansi;
import static com.taobao.arthas.core.util.LogUtil.LOGS_DIR;
@@ -50,7 +51,7 @@ public class Constants {
/**
* 当前进程PID
*/
public static final String PID = ApplicationUtils.getPid();
public static final String PID = PidUtils.currentPid();
/**
* 缓存目录