|
| 1 | +/* |
| 2 | + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.InputStream; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.regex.Matcher; |
| 30 | +import java.util.regex.Pattern; |
| 31 | + |
| 32 | +import jdk.testlibrary.JDKToolFinder; |
| 33 | +import jdk.testlibrary.OutputAnalyzer; |
| 34 | +import jdk.testlibrary.ProcessTools; |
| 35 | + |
| 36 | +import com.sun.jdi.Bootstrap; |
| 37 | +import com.sun.jdi.ThreadReference; |
| 38 | +import com.sun.jdi.VirtualMachine; |
| 39 | +import com.sun.jdi.connect.AttachingConnector; |
| 40 | +import com.sun.jdi.connect.Connector; |
| 41 | +import com.sun.jdi.connect.IllegalConnectorArgumentsException; |
| 42 | + |
| 43 | +/** |
| 44 | + * @test |
| 45 | + * @bug 4527279 |
| 46 | + * @summary Unit test for ProcessAttachingConnector |
| 47 | + * |
| 48 | + * @library /lib/testlibrary |
| 49 | + * @build jdk.testlibrary.* ProcessAttachTest |
| 50 | + * @run driver ProcessAttachTest |
| 51 | + */ |
| 52 | + |
| 53 | +class ProcessAttachTestTarg { |
| 54 | + public static void main(String args[]) throws Exception { |
| 55 | + // Write something that can be read by the driver |
| 56 | + System.out.println("Debuggee started"); |
| 57 | + System.out.flush(); |
| 58 | + for (;;) { |
| 59 | + Thread.sleep(100); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +public class ProcessAttachTest { |
| 65 | + |
| 66 | + public static final String TESTCLASSES = System.getProperty("test.classes"); |
| 67 | + |
| 68 | + public static void main(String[] args) throws Throwable { |
| 69 | + |
| 70 | + System.out.println("Test 1: Debuggee start with suspend=n"); |
| 71 | + runTest("-agentlib:jdwp=transport=dt_socket,server=y,suspend=n"); |
| 72 | + |
| 73 | + System.out.println("Test 2: Debuggee start with suspend=y"); |
| 74 | + runTest("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y"); |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + private static void runTest(String jdwpArg) throws Throwable { |
| 79 | + ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( |
| 80 | + jdwpArg, |
| 81 | + "-classpath", TESTCLASSES, |
| 82 | + "ProcessAttachTestTarg"); |
| 83 | + Process p = null; |
| 84 | + try { |
| 85 | + // Get pid |
| 86 | + String jps = JDKToolFinder.getJDKTool("jps"); |
| 87 | + OutputAnalyzer output = ProcessTools.executeProcess(jps); |
| 88 | + Pattern ptn1 = Pattern.compile("(\\d+) ProcessAttachTestTarg"); |
| 89 | + Pattern ptn2 = Pattern.compile("(\\d+) -- main class information unavailable"); // The target process is displayed as such in Test 2. |
| 90 | + Matcher m1 = ptn1.matcher(output.getOutput()); |
| 91 | + Matcher m2 = ptn2.matcher(output.getOutput()); |
| 92 | + List<String> pids1 = new ArrayList<>(); |
| 93 | + while (m1.find()) { |
| 94 | + pids1.add(m1.group(1)); |
| 95 | + } |
| 96 | + while (m2.find()) { |
| 97 | + pids1.add(m2.group(1)); |
| 98 | + } |
| 99 | + |
| 100 | + p = pb.start(); |
| 101 | + |
| 102 | + // Wait for the process to start |
| 103 | + InputStream is = p.getInputStream(); |
| 104 | + is.read(); |
| 105 | + |
| 106 | + // Get pid |
| 107 | + output = ProcessTools.executeProcess(jps); |
| 108 | + m1 = ptn1.matcher(output.getOutput()); |
| 109 | + m2 = ptn2.matcher(output.getOutput()); |
| 110 | + List<String> pids2 = new ArrayList<>(); |
| 111 | + while (m1.find()) { |
| 112 | + pids2.add(m1.group(1)); |
| 113 | + } |
| 114 | + while (m2.find()) { |
| 115 | + pids2.add(m2.group(1)); |
| 116 | + } |
| 117 | + pids2.removeAll(pids1); |
| 118 | + if (pids2.size() != 1) { |
| 119 | + throw new RuntimeException("Did not find pid"); |
| 120 | + } |
| 121 | + |
| 122 | + // Attach a debugger |
| 123 | + tryDebug(Long.parseLong(pids2.get(0))); |
| 124 | + } finally { |
| 125 | + p.destroyForcibly(); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private static void tryDebug(long pid) throws IOException, |
| 130 | + IllegalConnectorArgumentsException { |
| 131 | + AttachingConnector ac = Bootstrap.virtualMachineManager().attachingConnectors() |
| 132 | + .stream() |
| 133 | + .filter(c -> c.name().equals("com.sun.jdi.ProcessAttach")) |
| 134 | + .findFirst() |
| 135 | + .orElseThrow(() -> new RuntimeException("Unable to locate ProcessAttachingConnector")); |
| 136 | + |
| 137 | + Map<String, Connector.Argument> args = ac.defaultArguments(); |
| 138 | + Connector.StringArgument arg = (Connector.StringArgument) args |
| 139 | + .get("pid"); |
| 140 | + arg.setValue("" + pid); |
| 141 | + |
| 142 | + System.out.println("Debugger is attaching to: " + pid + " ..."); |
| 143 | + VirtualMachine vm = ac.attach(args); |
| 144 | + |
| 145 | + // list all threads |
| 146 | + System.out.println("Attached! Now listing threads ..."); |
| 147 | + vm.allThreads().stream().forEach(System.out::println); |
| 148 | + |
| 149 | + System.out.println("Debugger done."); |
| 150 | + vm.dispose(); |
| 151 | + } |
| 152 | +} |
0 commit comments