J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 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 | |
| 33 | import com.sun.jdi.Bootstrap; |
| 34 | import com.sun.jdi.VirtualMachine; |
| 35 | import com.sun.jdi.ThreadReference; |
| 36 | import com.sun.jdi.connect.Connector; |
| 37 | import com.sun.jdi.connect.AttachingConnector; |
| 38 | |
| 39 | import java.util.List; |
| 40 | import java.util.Map; |
| 41 | |
| 42 | public 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 | } |