blob: aabea2ca71efc68b42c4f1036a5c47754245407c [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 *
26 *
27 * Unit test for ProcessAttachingConnector - this "debugger" attaches to a debuggee
28 * given it's pid. Usage:
29 *
30 * java ProcessAttachDebugger <pid>
31 */
32
33import com.sun.jdi.Bootstrap;
34import com.sun.jdi.VirtualMachine;
35import com.sun.jdi.ThreadReference;
36import com.sun.jdi.connect.Connector;
37import com.sun.jdi.connect.AttachingConnector;
38
39import java.util.List;
40import java.util.Map;
41
42public class ProcessAttachDebugger {
43
44 public static void main(String main_args[]) throws Exception {
45 String pid = main_args[0];
46
47 // find ProcessAttachingConnector
48
49 List<AttachingConnector> l =
50 Bootstrap.virtualMachineManager().attachingConnectors();
51 AttachingConnector ac = null;
52 for (AttachingConnector c: l) {
53 if (c.name().equals("com.sun.jdi.ProcessAttach")) {
54 ac = c;
55 break;
56 }
57 }
58 if (ac == null) {
59 throw new RuntimeException("Unable to locate ProcessAttachingConnector");
60 }
61
62 Map<String,Connector.Argument> args = ac.defaultArguments();
63 Connector.StringArgument arg = (Connector.StringArgument)args.get("pid");
64 arg.setValue(pid);
65
66 System.out.println("Debugger is attaching to: " + pid + " ...");
67
68 VirtualMachine vm = ac.attach(args);
69
70 System.out.println("Attached! Now listing threads ...");
71
72 // list all threads
73
74 for (ThreadReference thr: vm.allThreads()) {
75 System.out.println(thr);
76 }
77
78 System.out.println("Debugger done.");
79 }
80
81}