1 /* 2 * Copyright (c) 2014, 2015, 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.List; 27 import java.util.Map; 28 29 import jdk.testlibrary.ProcessTools; 30 31 import com.sun.jdi.Bootstrap; 32 import com.sun.jdi.ThreadReference; 33 import com.sun.jdi.VirtualMachine; 34 import com.sun.jdi.connect.AttachingConnector; 35 import com.sun.jdi.connect.Connector; 36 import com.sun.jdi.connect.IllegalConnectorArgumentsException; 37 38 /** 39 * @test 40 * @bug 4527279 41 * @summary Unit test for ProcessAttachingConnector 42 * 43 * @library /lib/testlibrary 44 * @modules java.management 45 * jdk.jdi 46 * 47 * @build jdk.testlibrary.* ProcessAttachTest 48 * @run driver ProcessAttachTest 49 */ 50 51 class ProcessAttachTestTarg { 52 public static void main(String args[]) throws Exception { 53 // Write something that can be read by the driver 54 System.out.println("Debuggee started"); 55 System.out.flush(); 56 for (;;) { 57 Thread.sleep(100); 58 } 59 } 60 } 61 62 public class ProcessAttachTest { 63 64 public static final String TESTCLASSES = System.getProperty("test.classes"); 65 66 public static void main(String[] args) throws Exception { 67 68 System.out.println("Test 1: Debuggee start with suspend=n"); 69 runTest("-agentlib:jdwp=transport=dt_socket,server=y,suspend=n"); 70 71 System.out.println("Test 2: Debuggee start with suspend=y"); 72 runTest("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y"); 73 74 } 75 76 private static void runTest(String jdwpArg) throws Exception { 77 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( 78 jdwpArg, 79 "-classpath", TESTCLASSES, 80 "ProcessAttachTestTarg"); 81 Process p = null; 82 try { 83 p = pb.start(); 84 85 // Wait for the process to start 86 InputStream is = p.getInputStream(); 87 is.read(); 88 89 // Attach a debugger 90 tryDebug(p.getPid()); 91 } finally { 92 p.destroyForcibly(); 93 } 94 } 95 96 private static void tryDebug(long pid) throws IOException, 97 IllegalConnectorArgumentsException { 98 AttachingConnector ac = Bootstrap.virtualMachineManager().attachingConnectors() 99 .stream() 100 .filter(c -> c.name().equals("com.sun.jdi.ProcessAttach")) 101 .findFirst() 102 .orElseThrow(() -> new RuntimeException("Unable to locate ProcessAttachingConnector")); 103 104 Map<String, Connector.Argument> args = ac.defaultArguments(); 105 Connector.StringArgument arg = (Connector.StringArgument) args 106 .get("pid"); 107 arg.setValue("" + pid); 108 109 System.out.println("Debugger is attaching to: " + pid + " ..."); 110 VirtualMachine vm = ac.attach(args); 111 112 // list all threads 113 System.out.println("Attached! Now listing threads ..."); 114 vm.allThreads().stream().forEach(System.out::println); 115 116 System.out.println("Debugger done."); 117 vm.dispose(); 118 } 119 }