Path: blob/master/src/jdk.jcmd/linux/classes/sun/tools/common/ProcessHelper.java
41159 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.common;2627import java.io.IOException;28import java.io.UncheckedIOException;29import java.nio.file.Files;30import java.nio.file.Paths;31import java.util.stream.Stream;3233/**34* A helper class that retrieves the main class name for35* a running Java process using the proc filesystem (procfs)36*/37final class ProcessHelper {3839private static final String CMD_PREFIX = "cmd:";4041/**42* Gets the main class name for the given Java process by parsing the43* process command line. If the application was started with the <em>-jar</em>44* option this method returns the name of the jar file. If the application45* was started with <em>-m</em> or <em>--module</em> option, the method returns46* the module name and the main class name.47* @param pid - process ID (pid)48* @return the main class name or null if the process no longer exists or49* was started with a native launcher (e.g. jcmd etc)50*/51static String getMainClass(String pid) {52String cmdLine = getCommandLine(pid);53if (cmdLine == null) {54return null;55}56if (cmdLine.startsWith(CMD_PREFIX)) {57cmdLine = cmdLine.substring(CMD_PREFIX.length());58}59String[] parts = cmdLine.split("\0");60String mainClass = null;6162if (parts.length == 0) {63return null;64}6566// Check the executable67String[] executablePath = parts[0].split("/");68if (executablePath.length > 0) {69String binaryName = executablePath[executablePath.length - 1];70if (!"java".equals(binaryName)) {71// Skip the process if it is not started with java launcher72return null;73}74}7576// To be consistent with the behavior on other platforms, if -jar, -m, or --module77// options are used then just return the value (the path to the jar file or module78// name with a main class). Otherwise, the main class name is the first part that79// is not a Java option (doesn't start with '-' and is not a classpath or a module80// whitespace option).8182for (int i = 1; i < parts.length && mainClass == null; i++) {83if (i < parts.length - 1) {84if (parts[i].equals("-m") || parts[i].equals("--module") || parts[i].equals("-jar")) {85return parts[i + 1];86}87}8889if (parts[i].startsWith("--module=")) {90return parts[i].substring("--module=".length());91}9293// If this is a classpath or a module whitespace option then skip the next part94// (the classpath or the option value itself)95if (parts[i].equals("-cp") || parts[i].equals("-classpath") || parts[i].equals("--class-path") ||96isModuleWhiteSpaceOption(parts[i])) {97i++;98continue;99}100// Skip all other Java options101if (parts[i].startsWith("-")) {102continue;103}104105// If it is a source-file mode then return null106if (parts[i].endsWith(".java")) {107return null;108}109110mainClass = parts[i];111}112return mainClass;113}114115private static String getCommandLine(String pid) {116try (Stream<String> lines =117Files.lines(Paths.get("/proc/" + pid + "/cmdline"))) {118return lines.findFirst().orElse(null);119} catch (IOException | UncheckedIOException e) {120return null;121}122}123124private static boolean isModuleWhiteSpaceOption(String option) {125return option.equals("-p") ||126option.equals("--module-path") ||127option.equals("--upgrade-module-path") ||128option.equals("--add-modules") ||129option.equals("--limit-modules") ||130option.equals("--add-exports") ||131option.equals("--add-opens") ||132option.equals("--add-reads") ||133option.equals("--patch-module");134}135}136137138