From 69683d4cc04fbe821e8a61acc6fb5d6998bee49e Mon Sep 17 00:00:00 2001 From: hengyunabc Date: Thu, 13 Dec 2018 10:59:47 +0800 Subject: [PATCH] arthas-boot print more info --- .../com/taobao/arthas/boot/Bootstrap.java | 96 ++++++++++--------- .../com/taobao/arthas/boot/ProcessUtils.java | 3 +- 2 files changed, 54 insertions(+), 45 deletions(-) diff --git a/boot/src/main/java/com/taobao/arthas/boot/Bootstrap.java b/boot/src/main/java/com/taobao/arthas/boot/Bootstrap.java index bc8a91f16..c524f37e0 100644 --- a/boot/src/main/java/com/taobao/arthas/boot/Bootstrap.java +++ b/boot/src/main/java/com/taobao/arthas/boot/Bootstrap.java @@ -195,39 +195,47 @@ public class Bootstrap { public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + Package bootstrapPackage = Bootstrap.class.getPackage(); + if (bootstrapPackage != null) { + String arthasBootVersion = bootstrapPackage.getImplementationVersion(); + if (arthasBootVersion != null) { + AnsiLog.info("arthas-boot version: " + arthasBootVersion); + } + } + String mavenMetaData = null; - Bootstrap bootStrap = new Bootstrap(); + Bootstrap bootstrap = new Bootstrap(); CLI cli = CLIConfigurator.define(Bootstrap.class); CommandLine commandLine = cli.parse(Arrays.asList(args)); try { - CLIConfigurator.inject(commandLine, bootStrap); + CLIConfigurator.inject(commandLine, bootstrap); } catch (Throwable e) { e.printStackTrace(); System.out.println(usage(cli)); System.exit(1); } - if (bootStrap.isVerbose()) { + if (bootstrap.isVerbose()) { AnsiLog.level(Level.ALL); } - if (bootStrap.isHelp()) { + if (bootstrap.isHelp()) { System.out.println(usage(cli)); System.exit(0); } - if (bootStrap.isVersions()) { + if (bootstrap.isVersions()) { if (mavenMetaData == null) { - mavenMetaData = DownloadUtils.readMavenMetaData(bootStrap.getRepoMirror(), bootStrap.isuseHttp()); + mavenMetaData = DownloadUtils.readMavenMetaData(bootstrap.getRepoMirror(), bootstrap.isuseHttp()); } System.out.println(UsageRender.render(listVersions(mavenMetaData))); System.exit(0); } if (JavaVersionUtils.isJava6() || JavaVersionUtils.isJava7()) { - bootStrap.setuseHttp(true); + bootstrap.setuseHttp(true); AnsiLog.debug("Java version is {}, only support http, set useHttp to true.", JavaVersionUtils.javaVersionStr()); } @@ -235,24 +243,24 @@ public class Bootstrap { // check telnet/http port int telnetPortPid = -1; int httpPortPid = -1; - if (bootStrap.getTelnetPort() > 0) { - telnetPortPid = SocketUtils.findTcpListenProcess(bootStrap.getTelnetPort()); + if (bootstrap.getTelnetPort() > 0) { + telnetPortPid = SocketUtils.findTcpListenProcess(bootstrap.getTelnetPort()); if (telnetPortPid > 0) { - AnsiLog.info("Process {} already using port {}", telnetPortPid, bootStrap.getTelnetPort()); + AnsiLog.info("Process {} already using port {}", telnetPortPid, bootstrap.getTelnetPort()); } } - if (bootStrap.getHttpPort() > 0) { - httpPortPid = SocketUtils.findTcpListenProcess(bootStrap.getHttpPort()); + if (bootstrap.getHttpPort() > 0) { + httpPortPid = SocketUtils.findTcpListenProcess(bootstrap.getHttpPort()); if (httpPortPid > 0) { - AnsiLog.info("Process {} already using port {}", httpPortPid, bootStrap.getHttpPort()); + AnsiLog.info("Process {} already using port {}", httpPortPid, bootstrap.getHttpPort()); } } - int pid = bootStrap.getPid(); + int pid = bootstrap.getPid(); // select pid if (pid < 0) { try { - pid = ProcessUtils.select(bootStrap.isVerbose()); + pid = ProcessUtils.select(bootstrap.isVerbose()); } catch (InputMismatchException e) { System.out.println("Please input an integer to select pid."); System.exit(1); @@ -265,7 +273,7 @@ public class Bootstrap { if (telnetPortPid > 0 && pid != telnetPortPid) { AnsiLog.error("Target process {} is not the process using port {}, you will connect to an unexpected process.", - pid, bootStrap.getTelnetPort()); + pid, bootstrap.getTelnetPort()); AnsiLog.error("If you still want to attach target process {}, Try to set a different telnet port by using --telnet-port argument.", pid); AnsiLog.error("Or try to shutdown the process {} using the telnet port first.", telnetPortPid); @@ -274,7 +282,7 @@ public class Bootstrap { if (httpPortPid > 0 && pid != httpPortPid) { AnsiLog.error("Target process {} is not the process using port {}, you will connect to an unexpected process.", - pid, bootStrap.getHttpPort()); + pid, bootstrap.getHttpPort()); AnsiLog.error("If you still want to attach target process {}, Try to set a different http port by using --http-port argument.", pid); AnsiLog.error("Or try to shutdown the process {} using the http port first.", httpPortPid); @@ -283,18 +291,18 @@ public class Bootstrap { // find arthas home File arthasHomeDir = null; - if (bootStrap.getArthasHome() != null) { - verifyArthasHome(bootStrap.getArthasHome()); - arthasHomeDir = new File(bootStrap.getArthasHome()); + if (bootstrap.getArthasHome() != null) { + verifyArthasHome(bootstrap.getArthasHome()); + arthasHomeDir = new File(bootstrap.getArthasHome()); } - if (arthasHomeDir == null && bootStrap.getUseVersion() != null) { + if (arthasHomeDir == null && bootstrap.getUseVersion() != null) { // try to find from ~/.arthas/lib File specialVersionDir = new File(System.getProperty("user.home"), ".arthas" + File.separator + "lib" - + File.separator + bootStrap.getUseVersion() + File.separator + "arthas"); + + File.separator + bootstrap.getUseVersion() + File.separator + "arthas"); if (!specialVersionDir.exists()) { // try to download arthas from remote server. - DownloadUtils.downArthasPackaging(bootStrap.getRepoMirror(), bootStrap.isuseHttp(), - bootStrap.getUseVersion(), ARTHAS_LIB_DIR.getAbsolutePath()); + DownloadUtils.downArthasPackaging(bootstrap.getRepoMirror(), bootstrap.isuseHttp(), + bootstrap.getUseVersion(), ARTHAS_LIB_DIR.getAbsolutePath()); } verifyArthasHome(specialVersionDir.getAbsolutePath()); arthasHomeDir = specialVersionDir; @@ -304,10 +312,10 @@ public class Bootstrap { if (arthasHomeDir == null) { CodeSource codeSource = Bootstrap.class.getProtectionDomain().getCodeSource(); if (codeSource != null) { - String bootstrap = codeSource.getLocation().getFile(); + String bootJarPath = codeSource.getLocation().getFile(); try { - verifyArthasHome(new File(bootstrap).getParent()); - arthasHomeDir = new File(bootstrap).getParentFile(); + verifyArthasHome(new File(bootJarPath).getParent()); + arthasHomeDir = new File(bootJarPath).getParentFile(); } catch (Exception e) { // ignore } @@ -335,7 +343,7 @@ public class Bootstrap { } if (mavenMetaData == null) { - mavenMetaData = DownloadUtils.readMavenMetaData(bootStrap.getRepoMirror(), bootStrap.isuseHttp()); + mavenMetaData = DownloadUtils.readMavenMetaData(bootstrap.getRepoMirror(), bootstrap.isuseHttp()); } String remoteLastestVersion = DownloadUtils.readMavenReleaseVersion(mavenMetaData); @@ -345,7 +353,7 @@ public class Bootstrap { if (remoteLastestVersion == null) { // exit AnsiLog.error("Can not find Arthas under local: {} and remote: {}", ARTHAS_LIB_DIR, - bootStrap.getRepoMirror()); + bootstrap.getRepoMirror()); System.exit(1); } else { needDownload = true; @@ -361,7 +369,7 @@ public class Bootstrap { } if (needDownload) { // try to download arthas from remote server. - DownloadUtils.downArthasPackaging(bootStrap.getRepoMirror(), bootStrap.isuseHttp(), + DownloadUtils.downArthasPackaging(bootstrap.getRepoMirror(), bootstrap.isuseHttp(), remoteLastestVersion, ARTHAS_LIB_DIR.getAbsolutePath()); localLastestVersion = remoteLastestVersion; } @@ -375,7 +383,7 @@ public class Bootstrap { AnsiLog.info("arthas home: " + arthasHomeDir); if (telnetPortPid > 0 && pid == telnetPortPid) { - AnsiLog.info("The target process already listen port {}, skip attach.", bootStrap.getTelnetPort()); + AnsiLog.info("The target process already listen port {}, skip attach.", bootstrap.getTelnetPort()); } else { // start arthas-core.jar List attachArgs = new ArrayList(); @@ -384,18 +392,18 @@ public class Bootstrap { attachArgs.add("-pid"); attachArgs.add("" + pid); attachArgs.add("-target-ip"); - attachArgs.add(bootStrap.getTargetIp()); + attachArgs.add(bootstrap.getTargetIp()); attachArgs.add("-telnet-port"); - attachArgs.add("" + bootStrap.getTelnetPort()); + attachArgs.add("" + bootstrap.getTelnetPort()); attachArgs.add("-http-port"); - attachArgs.add("" + bootStrap.getHttpPort()); + attachArgs.add("" + bootstrap.getHttpPort()); attachArgs.add("-core"); attachArgs.add(new File(arthasHomeDir, "arthas-core.jar").getAbsolutePath()); attachArgs.add("-agent"); attachArgs.add(new File(arthasHomeDir, "arthas-agent.jar").getAbsolutePath()); - if (bootStrap.getSessionTimeout() != null) { + if (bootstrap.getSessionTimeout() != null) { attachArgs.add("-session-timeout"); - attachArgs.add("" + bootStrap.getSessionTimeout()); + attachArgs.add("" + bootstrap.getSessionTimeout()); } AnsiLog.info("Try to attach process " + pid); @@ -405,7 +413,7 @@ public class Bootstrap { AnsiLog.info("Attach process {} success.", pid); } - if (bootStrap.isAttachOnly()) { + if (bootstrap.isAttachOnly()) { System.exit(0); } @@ -417,20 +425,20 @@ public class Bootstrap { Method mainMethod = telnetConsoleClas.getMethod("main", String[].class); List telnetArgs = new ArrayList(); - if (bootStrap.getCommand() != null) { + if (bootstrap.getCommand() != null) { telnetArgs.add("-c"); - telnetArgs.add(bootStrap.getCommand()); + telnetArgs.add(bootstrap.getCommand()); } - if (bootStrap.getBatchFile() != null) { + if (bootstrap.getBatchFile() != null) { telnetArgs.add("-f"); - telnetArgs.add(bootStrap.getBatchFile()); + telnetArgs.add(bootstrap.getBatchFile()); } // telnet port ,ip - telnetArgs.add(bootStrap.getTargetIp()); - telnetArgs.add("" + bootStrap.getTelnetPort()); + telnetArgs.add(bootstrap.getTargetIp()); + telnetArgs.add("" + bootstrap.getTelnetPort()); - AnsiLog.info("arthas-client connect {} {}", bootStrap.getTargetIp(), bootStrap.getTelnetPort()); + AnsiLog.info("arthas-client connect {} {}", bootstrap.getTargetIp(), bootstrap.getTelnetPort()); AnsiLog.debug("Start arthas-client.jar args: " + telnetArgs); mainMethod.invoke(null, new Object[] { telnetArgs.toArray(new String[0]) }); } diff --git a/boot/src/main/java/com/taobao/arthas/boot/ProcessUtils.java b/boot/src/main/java/com/taobao/arthas/boot/ProcessUtils.java index 400d7dcce..417d96cea 100644 --- a/boot/src/main/java/com/taobao/arthas/boot/ProcessUtils.java +++ b/boot/src/main/java/com/taobao/arthas/boot/ProcessUtils.java @@ -51,10 +51,11 @@ public class ProcessUtils { Map processMap = listProcessByJps(v); if (processMap.isEmpty()) { - AnsiLog.info("Can not find java process. Try to pass pid in command line."); + AnsiLog.info("Can not find java process. Try to pass in command line."); return -1; } + AnsiLog.info("Found existing java process, please choose one and hit RETURN."); // print list int count = 1; for (String process : processMap.values()) {