|
1 | 1 | /******************************************************************************* |
2 | | -* Copyright (c) 2021 Microsoft Corporation and others. |
| 2 | +* Copyright (c) 2021-2022 Microsoft Corporation and others. |
3 | 3 | * All rights reserved. This program and the accompanying materials |
4 | 4 | * are made available under the terms of the Eclipse Public License v1.0 |
5 | 5 | * which accompanies this distribution, and is available at |
|
11 | 11 |
|
12 | 12 | package com.microsoft.java.debug.core.adapter.handler; |
13 | 13 |
|
| 14 | +import java.io.BufferedReader; |
14 | 15 | import java.io.File; |
15 | 16 | import java.io.FileOutputStream; |
16 | 17 | import java.io.IOException; |
| 18 | +import java.io.InputStreamReader; |
17 | 19 | import java.math.BigInteger; |
18 | 20 | import java.nio.file.Files; |
19 | 21 | import java.nio.file.Path; |
| 22 | +import java.nio.file.Paths; |
20 | 23 | import java.security.MessageDigest; |
21 | 24 | import java.security.NoSuchAlgorithmException; |
22 | 25 | import java.util.ArrayList; |
23 | 26 | import java.util.HashSet; |
24 | 27 | import java.util.List; |
| 28 | +import java.util.Objects; |
| 29 | +import java.util.Optional; |
25 | 30 | import java.util.Set; |
26 | 31 | import java.util.UUID; |
27 | 32 | import java.util.jar.Attributes; |
28 | 33 | import java.util.jar.JarOutputStream; |
29 | 34 | import java.util.jar.Manifest; |
| 35 | +import java.util.logging.Level; |
| 36 | +import java.util.logging.Logger; |
| 37 | +import java.util.stream.Collectors; |
30 | 38 |
|
| 39 | +import com.microsoft.java.debug.core.Configuration; |
31 | 40 | import com.microsoft.java.debug.core.adapter.AdapterUtils; |
32 | 41 |
|
33 | 42 | import org.apache.commons.lang3.ArrayUtils; |
| 43 | +import org.apache.commons.lang3.SystemUtils; |
34 | 44 |
|
35 | 45 | public class LaunchUtils { |
| 46 | + private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME); |
36 | 47 | private static Set<Path> tempFilesInUse = new HashSet<>(); |
37 | 48 |
|
38 | 49 | /** |
@@ -102,6 +113,171 @@ public static void releaseTempLaunchFile(Path tempFile) { |
102 | 113 | } |
103 | 114 | } |
104 | 115 |
|
| 116 | + public static ProcessHandle findJavaProcessInTerminalShell(long shellPid, String javaCommand, int timeout/*ms*/) { |
| 117 | + ProcessHandle shellProcess = ProcessHandle.of(shellPid).orElse(null); |
| 118 | + if (shellProcess != null) { |
| 119 | + int retry = 0; |
| 120 | + final int INTERVAL = 100; |
| 121 | + final int maxRetries = timeout / INTERVAL; |
| 122 | + final boolean isCygwinShell = isCygwinShell(shellProcess.info().command().orElse(null)); |
| 123 | + while (retry <= maxRetries) { |
| 124 | + Optional<ProcessHandle> subProcessHandle = shellProcess.descendants().filter(proc -> { |
| 125 | + String command = proc.info().command().orElse(""); |
| 126 | + return Objects.equals(command, javaCommand) || command.endsWith("\\java.exe") || command.endsWith("/java"); |
| 127 | + }).findFirst(); |
| 128 | + |
| 129 | + if (subProcessHandle.isPresent()) { |
| 130 | + logger.info("shellPid: " + shellPid + ", javaPid: " + subProcessHandle.get().pid()); |
| 131 | + return subProcessHandle.get(); |
| 132 | + } else if (isCygwinShell) { |
| 133 | + long javaPid = findJavaProcessByCygwinPsCommand(shellProcess, javaCommand); |
| 134 | + if (javaPid > 0) { |
| 135 | + logger.info("[Cygwin Shell] shellPid: " + shellPid + ", javaPid: " + javaPid); |
| 136 | + return ProcessHandle.of(javaPid).orElse(null); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + retry++; |
| 141 | + if (retry > maxRetries) { |
| 142 | + break; |
| 143 | + } |
| 144 | + |
| 145 | + try { |
| 146 | + Thread.sleep(INTERVAL); |
| 147 | + } catch (InterruptedException e) { |
| 148 | + // do nothing |
| 149 | + } |
| 150 | + logger.info("Retry to find Java subProcess of shell pid " + shellPid); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + return null; |
| 155 | + } |
| 156 | + |
| 157 | + private static long findJavaProcessByCygwinPsCommand(ProcessHandle shellProcess, String javaCommand) { |
| 158 | + String psCommand = detectPsCommandPath(shellProcess.info().command().orElse(null)); |
| 159 | + if (psCommand == null) { |
| 160 | + return -1; |
| 161 | + } |
| 162 | + |
| 163 | + BufferedReader psReader = null; |
| 164 | + List<PsProcess> psProcs = new ArrayList<>(); |
| 165 | + List<PsProcess> javaCandidates = new ArrayList<>(); |
| 166 | + try { |
| 167 | + String[] headers = null; |
| 168 | + int pidIndex = -1; |
| 169 | + int ppidIndex = -1; |
| 170 | + int winpidIndex = -1; |
| 171 | + String line; |
| 172 | + String javaExeName = Paths.get(javaCommand).toFile().getName().replaceFirst("\\.exe$", ""); |
| 173 | + |
| 174 | + Process p = Runtime.getRuntime().exec(new String[] {psCommand, "-l"}); |
| 175 | + psReader = new BufferedReader(new InputStreamReader(p.getInputStream())); |
| 176 | + /** |
| 177 | + * Here is a sample output when running ps command in Cygwin/MINGW64 shell. |
| 178 | + * PID PPID PGID WINPID TTY UID STIME COMMAND |
| 179 | + * 1869 1 1869 7852 cons2 4096 15:29:27 /usr/bin/bash |
| 180 | + * 2271 1 2271 30820 cons4 4096 19:38:30 /usr/bin/bash |
| 181 | + * 1812 1 1812 21540 cons1 4096 15:05:03 /usr/bin/bash |
| 182 | + * 2216 1 2216 11328 cons3 4096 19:38:18 /usr/bin/bash |
| 183 | + * 1720 1 1720 5404 cons0 4096 13:46:42 /usr/bin/bash |
| 184 | + * 2269 2216 2269 6676 cons3 4096 19:38:21 /c/Program Files/Microsoft/jdk-11.0.14.9-hotspot/bin/java |
| 185 | + * 1911 1869 1869 29708 cons2 4096 15:29:31 /c/Program Files/nodejs/node |
| 186 | + * 2315 2271 2315 18064 cons4 4096 19:38:34 /usr/bin/ps |
| 187 | + */ |
| 188 | + while ((line = psReader.readLine()) != null) { |
| 189 | + String[] cols = line.strip().split("\\s+"); |
| 190 | + if (headers == null) { |
| 191 | + headers = cols; |
| 192 | + pidIndex = ArrayUtils.indexOf(headers, "PID"); |
| 193 | + ppidIndex = ArrayUtils.indexOf(headers, "PPID"); |
| 194 | + winpidIndex = ArrayUtils.indexOf(headers, "WINPID"); |
| 195 | + if (pidIndex < 0 || ppidIndex < 0 || winpidIndex < 0) { |
| 196 | + logger.warning("Failed to find Java process because ps command is not the standard Cygwin ps command."); |
| 197 | + return -1; |
| 198 | + } |
| 199 | + } else if (cols.length >= headers.length) { |
| 200 | + long pid = Long.parseLong(cols[pidIndex]); |
| 201 | + long ppid = Long.parseLong(cols[ppidIndex]); |
| 202 | + long winpid = Long.parseLong(cols[winpidIndex]); |
| 203 | + PsProcess process = new PsProcess(pid, ppid, winpid); |
| 204 | + psProcs.add(process); |
| 205 | + if (cols[cols.length - 1].endsWith("/" + javaExeName) || cols[cols.length - 1].endsWith("/java")) { |
| 206 | + javaCandidates.add(process); |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + } catch (Exception err) { |
| 211 | + logger.log(Level.WARNING, "Failed to find Java process by Cygwin ps command.", err); |
| 212 | + } finally { |
| 213 | + if (psReader != null) { |
| 214 | + try { |
| 215 | + psReader.close(); |
| 216 | + } catch (IOException e) { |
| 217 | + // ignore |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + if (!javaCandidates.isEmpty()) { |
| 223 | + Set<Long> descendantWinpids = shellProcess.descendants().map(proc -> proc.pid()).collect(Collectors.toSet()); |
| 224 | + long shellWinpid = shellProcess.pid(); |
| 225 | + for (PsProcess javaCandidate: javaCandidates) { |
| 226 | + if (descendantWinpids.contains(javaCandidate.winpid)) { |
| 227 | + return javaCandidate.winpid; |
| 228 | + } |
| 229 | + |
| 230 | + for (PsProcess psProc : psProcs) { |
| 231 | + if (javaCandidate.ppid != psProc.pid) { |
| 232 | + continue; |
| 233 | + } |
| 234 | + |
| 235 | + if (descendantWinpids.contains(psProc.winpid) || psProc.winpid == shellWinpid) { |
| 236 | + return javaCandidate.winpid; |
| 237 | + } |
| 238 | + |
| 239 | + break; |
| 240 | + } |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + return -1; |
| 245 | + } |
| 246 | + |
| 247 | + private static boolean isCygwinShell(String shellPath) { |
| 248 | + if (!SystemUtils.IS_OS_WINDOWS || shellPath == null) { |
| 249 | + return false; |
| 250 | + } |
| 251 | + |
| 252 | + String lowerShellPath = shellPath.toLowerCase(); |
| 253 | + return lowerShellPath.endsWith("git\\bin\\bash.exe") |
| 254 | + || lowerShellPath.endsWith("git\\usr\\bin\\bash.exe") |
| 255 | + || lowerShellPath.endsWith("mintty.exe") |
| 256 | + || lowerShellPath.endsWith("cygwin64\\bin\\bash.exe") |
| 257 | + || (lowerShellPath.endsWith("bash.exe") && detectPsCommandPath(shellPath) != null) |
| 258 | + || (lowerShellPath.endsWith("sh.exe") && detectPsCommandPath(shellPath) != null); |
| 259 | + } |
| 260 | + |
| 261 | + private static String detectPsCommandPath(String shellPath) { |
| 262 | + if (shellPath == null) { |
| 263 | + return null; |
| 264 | + } |
| 265 | + |
| 266 | + Path psPath = Paths.get(shellPath, "..\\ps.exe"); |
| 267 | + if (!Files.exists(psPath)) { |
| 268 | + psPath = Paths.get(shellPath, "..\\..\\usr\\bin\\ps.exe"); |
| 269 | + if (!Files.exists(psPath)) { |
| 270 | + psPath = null; |
| 271 | + } |
| 272 | + } |
| 273 | + |
| 274 | + if (psPath == null) { |
| 275 | + return null; |
| 276 | + } |
| 277 | + |
| 278 | + return psPath.normalize().toString(); |
| 279 | + } |
| 280 | + |
105 | 281 | private static Path tmpdir = null; |
106 | 282 |
|
107 | 283 | private static synchronized Path getTmpDir() throws IOException { |
@@ -156,4 +332,16 @@ private static String getMd5(String input) { |
156 | 332 | return Integer.toString(input.hashCode(), Character.MAX_RADIX); |
157 | 333 | } |
158 | 334 | } |
| 335 | + |
| 336 | + private static class PsProcess { |
| 337 | + long pid; |
| 338 | + long ppid; |
| 339 | + long winpid; |
| 340 | + |
| 341 | + public PsProcess(long pid, long ppid, long winpid) { |
| 342 | + this.pid = pid; |
| 343 | + this.ppid = ppid; |
| 344 | + this.winpid = winpid; |
| 345 | + } |
| 346 | + } |
159 | 347 | } |
0 commit comments