From dbf63013992d50e46ca9b0fa0f210fd9eba146ed Mon Sep 17 00:00:00 2001 From: hengyunabc Date: Thu, 10 Jun 2021 18:43:30 +0800 Subject: [PATCH] support disabledCommands part 2. #1729 --- bin/as.sh | 16 +++ .../core/command/BuiltinCommandPack.java | 116 ++++++++++-------- .../arthas/core/server/ArthasBootstrap.java | 13 +- 3 files changed, 93 insertions(+), 52 deletions(-) diff --git a/bin/as.sh b/bin/as.sh index 70edcfbd8d4..3ce707cd84c 100755 --- a/bin/as.sh +++ b/bin/as.sh @@ -151,6 +151,9 @@ USERNAME= # password PASSWORD= +# disabledCommands +DISABLED_COMMANDS= + ############ Command Arguments ############ # if arguments contains -c/--command or -f/--batch-file, BATCH_MODE will be true @@ -405,6 +408,7 @@ Usage: [--tunnel-server ] [--agent-id ] [--stat-url ] [--app-name ] [--username ] [--password ] + [--disabled-commands ] [--use-version ] [--repo-mirror ] [--versions] [--use-http] [--attach-only] [-c ] [-f ] [-v] [pid] @@ -427,6 +431,7 @@ Options and Arguments: --app-name Special app name --username Special username --password Special password + --disabled-commands Disable special commands --select select target process by classname or JARfilename -c,--command Command to execute, multiple commands separated by ; @@ -449,6 +454,7 @@ EXAMPLES: ./as.sh --use-version 3.5.1 ./as.sh --session-timeout 3600 ./as.sh --attach-only + ./as.sh --disabled-commands stop,dump ./as.sh --select math-game ./as.sh --repo-mirror aliyun --use-http WIKI: @@ -625,6 +631,11 @@ parse_arguments() shift # past argument shift # past value ;; + --disabled-commands) + DISABLED_COMMANDS="$2" + shift # past argument + shift # past value + ;; --use-http) USE_HTTP=true shift # past argument @@ -834,6 +845,11 @@ attach_jvm() tempArgs+=("${PASSWORD}") fi + if [ "${DISABLED_COMMANDS}" ]; then + tempArgs+=("-disabled-commands") + tempArgs+=("${DISABLED_COMMANDS}") + fi + if [ "${TARGET_IP}" ]; then tempArgs+=("-target-ip") tempArgs+=("${TARGET_IP}") diff --git a/core/src/main/java/com/taobao/arthas/core/command/BuiltinCommandPack.java b/core/src/main/java/com/taobao/arthas/core/command/BuiltinCommandPack.java index 0bdcd36eb43..dc55ad7c464 100644 --- a/core/src/main/java/com/taobao/arthas/core/command/BuiltinCommandPack.java +++ b/core/src/main/java/com/taobao/arthas/core/command/BuiltinCommandPack.java @@ -9,6 +9,7 @@ import com.taobao.arthas.core.command.basic1000.HistoryCommand; import com.taobao.arthas.core.command.basic1000.KeymapCommand; import com.taobao.arthas.core.command.basic1000.OptionsCommand; +import com.alibaba.bytekit.utils.AnnotationUtils; import com.taobao.arthas.core.command.basic1000.AuthCommand; import com.taobao.arthas.core.command.basic1000.PwdCommand; import com.taobao.arthas.core.command.basic1000.ResetCommand; @@ -46,8 +47,10 @@ import com.taobao.arthas.core.command.monitor200.TraceCommand; import com.taobao.arthas.core.command.monitor200.VmToolCommand; import com.taobao.arthas.core.command.monitor200.WatchCommand; +import com.taobao.arthas.core.shell.command.AnnotatedCommand; import com.taobao.arthas.core.shell.command.Command; import com.taobao.arthas.core.shell.command.CommandResolver; +import com.taobao.middleware.cli.annotations.Name; import java.util.ArrayList; import java.util.List; @@ -58,10 +61,10 @@ */ public class BuiltinCommandPack implements CommandResolver { - private static List commands = new ArrayList(); + private List commands = new ArrayList(); - static { - initCommands(); + public BuiltinCommandPack(List disabledCommands) { + initCommands(disabledCommands); } @Override @@ -69,53 +72,64 @@ public List commands() { return commands; } - private static void initCommands() { - commands.add(Command.create(HelpCommand.class)); - commands.add(Command.create(AuthCommand.class)); - commands.add(Command.create(KeymapCommand.class)); - commands.add(Command.create(SearchClassCommand.class)); - commands.add(Command.create(SearchMethodCommand.class)); - commands.add(Command.create(ClassLoaderCommand.class)); - commands.add(Command.create(JadCommand.class)); - commands.add(Command.create(GetStaticCommand.class)); - commands.add(Command.create(MonitorCommand.class)); - commands.add(Command.create(StackCommand.class)); - commands.add(Command.create(ThreadCommand.class)); - commands.add(Command.create(TraceCommand.class)); - commands.add(Command.create(WatchCommand.class)); - commands.add(Command.create(TimeTunnelCommand.class)); - commands.add(Command.create(JvmCommand.class)); - commands.add(Command.create(PerfCounterCommand.class)); - // commands.add(Command.create(GroovyScriptCommand.class)); - commands.add(Command.create(OgnlCommand.class)); - commands.add(Command.create(MemoryCompilerCommand.class)); - commands.add(Command.create(RedefineCommand.class)); - commands.add(Command.create(RetransformCommand.class)); - commands.add(Command.create(DashboardCommand.class)); - commands.add(Command.create(DumpClassCommand.class)); - commands.add(Command.create(HeapDumpCommand.class)); - commands.add(Command.create(JulyCommand.class)); - commands.add(Command.create(ThanksCommand.class)); - commands.add(Command.create(OptionsCommand.class)); - commands.add(Command.create(ClsCommand.class)); - commands.add(Command.create(ResetCommand.class)); - commands.add(Command.create(VersionCommand.class)); - commands.add(Command.create(SessionCommand.class)); - commands.add(Command.create(SystemPropertyCommand.class)); - commands.add(Command.create(SystemEnvCommand.class)); - commands.add(Command.create(VMOptionCommand.class)); - commands.add(Command.create(LoggerCommand.class)); - commands.add(Command.create(HistoryCommand.class)); - commands.add(Command.create(CatCommand.class)); - commands.add(Command.create(Base64Command.class)); - commands.add(Command.create(EchoCommand.class)); - commands.add(Command.create(PwdCommand.class)); - commands.add(Command.create(MBeanCommand.class)); - commands.add(Command.create(GrepCommand.class)); - commands.add(Command.create(TeeCommand.class)); - commands.add(Command.create(ProfilerCommand.class)); - commands.add(Command.create(VmToolCommand.class)); - commands.add(Command.create(ShutdownCommand.class)); - commands.add(Command.create(StopCommand.class)); + private void initCommands(List disabledCommands) { + List> commandClassList = new ArrayList>(32); + commandClassList.add(HelpCommand.class); + commandClassList.add(AuthCommand.class); + commandClassList.add(KeymapCommand.class); + commandClassList.add(SearchClassCommand.class); + commandClassList.add(SearchMethodCommand.class); + commandClassList.add(ClassLoaderCommand.class); + commandClassList.add(JadCommand.class); + commandClassList.add(GetStaticCommand.class); + commandClassList.add(MonitorCommand.class); + commandClassList.add(StackCommand.class); + commandClassList.add(ThreadCommand.class); + commandClassList.add(TraceCommand.class); + commandClassList.add(WatchCommand.class); + commandClassList.add(TimeTunnelCommand.class); + commandClassList.add(JvmCommand.class); + commandClassList.add(PerfCounterCommand.class); + // commandClassList.add(GroovyScriptCommand.class); + commandClassList.add(OgnlCommand.class); + commandClassList.add(MemoryCompilerCommand.class); + commandClassList.add(RedefineCommand.class); + commandClassList.add(RetransformCommand.class); + commandClassList.add(DashboardCommand.class); + commandClassList.add(DumpClassCommand.class); + commandClassList.add(HeapDumpCommand.class); + commandClassList.add(JulyCommand.class); + commandClassList.add(ThanksCommand.class); + commandClassList.add(OptionsCommand.class); + commandClassList.add(ClsCommand.class); + commandClassList.add(ResetCommand.class); + commandClassList.add(VersionCommand.class); + commandClassList.add(SessionCommand.class); + commandClassList.add(SystemPropertyCommand.class); + commandClassList.add(SystemEnvCommand.class); + commandClassList.add(VMOptionCommand.class); + commandClassList.add(LoggerCommand.class); + commandClassList.add(HistoryCommand.class); + commandClassList.add(CatCommand.class); + commandClassList.add(Base64Command.class); + commandClassList.add(EchoCommand.class); + commandClassList.add(PwdCommand.class); + commandClassList.add(MBeanCommand.class); + commandClassList.add(GrepCommand.class); + commandClassList.add(TeeCommand.class); + commandClassList.add(ProfilerCommand.class); + commandClassList.add(VmToolCommand.class); + commandClassList.add(ShutdownCommand.class); + commandClassList.add(StopCommand.class); + + for (Class clazz : commandClassList) { + Name name = clazz.getAnnotation(Name.class); + if (name != null && name.value() != null) { + if (disabledCommands.contains(name.value())) { + continue; + } + } + commands.add(Command.create(clazz)); + } } } diff --git a/core/src/main/java/com/taobao/arthas/core/server/ArthasBootstrap.java b/core/src/main/java/com/taobao/arthas/core/server/ArthasBootstrap.java index 7f1364fe16f..9988af0692c 100644 --- a/core/src/main/java/com/taobao/arthas/core/server/ArthasBootstrap.java +++ b/core/src/main/java/com/taobao/arthas/core/server/ArthasBootstrap.java @@ -70,6 +70,7 @@ import com.taobao.arthas.core.util.FileUtils; import com.taobao.arthas.core.util.InstrumentationUtils; import com.taobao.arthas.core.util.LogUtil; +import com.taobao.arthas.core.util.StringUtils; import com.taobao.arthas.core.util.UserStatUtil; import com.taobao.arthas.core.util.affect.EnhancerAffect; import com.taobao.arthas.core.util.matcher.WildcardMatcher; @@ -393,7 +394,17 @@ private void bind(Configure configure) throws Throwable { this.securityAuthenticator = new SecurityAuthenticatorImpl(configure.getUsername(), configure.getPassword()); shellServer = new ShellServerImpl(options); - BuiltinCommandPack builtinCommands = new BuiltinCommandPack(); + + List disabledCommands = new ArrayList(); + if (configure.getDisabledCommands() != null) { + String[] strings = StringUtils.tokenizeToStringArray(configure.getDisabledCommands(), ","); + if (strings != null) { + for (String s : strings) { + disabledCommands.add(s); + } + } + } + BuiltinCommandPack builtinCommands = new BuiltinCommandPack(disabledCommands); List resolvers = new ArrayList(); resolvers.add(builtinCommands);