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