agent/test/jdi/multivm.java

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 1907
c18cbe5936b8
parent 0
f90c822e73f8
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 import com.sun.jdi.*;
aoqi@0 26 import com.sun.jdi.connect.*;
aoqi@0 27
aoqi@0 28 import java.util.Map;
aoqi@0 29 import java.util.List;
aoqi@0 30 import java.util.Iterator;
aoqi@0 31 import java.io.IOException;
aoqi@0 32
aoqi@0 33 /* This class is used to test multi VM connectivity feature of
aoqi@0 34 * SA/JDI. Accepts two PIDs as arguments. Connects to first VM
aoqi@0 35 *, Connects to second VM and disposes them in that order.
aoqi@0 36 */
aoqi@0 37
aoqi@0 38 public class multivm {
aoqi@0 39 static AttachingConnector myPIDConn;
aoqi@0 40 static VirtualMachine vm1;
aoqi@0 41 static VirtualMachine vm2;
aoqi@0 42 static VirtualMachineManager vmmgr;
aoqi@0 43
aoqi@0 44 public static void println(String msg) {
aoqi@0 45 System.out.println(msg);
aoqi@0 46 }
aoqi@0 47
aoqi@0 48 private static void usage() {
aoqi@0 49 System.err.println("Usage: java multivm <pid1> <pid2>");
aoqi@0 50 System.exit(1);
aoqi@0 51 }
aoqi@0 52
aoqi@0 53 public static void main(String args[]) {
aoqi@0 54 vmmgr = Bootstrap.virtualMachineManager();
aoqi@0 55 List attachingConnectors = vmmgr.attachingConnectors();
aoqi@0 56 if (attachingConnectors.isEmpty()) {
aoqi@0 57 System.err.println( "ERROR: No attaching connectors");
aoqi@0 58 return;
aoqi@0 59 }
aoqi@0 60 Iterator myIt = attachingConnectors.iterator();
aoqi@0 61 while (myIt.hasNext()) {
aoqi@0 62 AttachingConnector tmpCon = (AttachingConnector)myIt.next();
aoqi@0 63 if (tmpCon.name().equals(
aoqi@0 64 "sun.jvm.hotspot.jdi.SAPIDAttachingConnector")) {
aoqi@0 65 myPIDConn = tmpCon;
aoqi@0 66 break;
aoqi@0 67 }
aoqi@0 68 }
aoqi@0 69
aoqi@0 70 int pid1 = 0, pid2 = 0;
aoqi@0 71 String pidText = null;
aoqi@0 72 switch (args.length) {
aoqi@0 73 case (2):
aoqi@0 74 try {
aoqi@0 75 pidText = args[0];
aoqi@0 76 pid1 = Integer.parseInt(pidText);
aoqi@0 77 System.out.println( "pid1: " + pid1);
aoqi@0 78 vm1 = attachPID(pid1);
aoqi@0 79 pidText = args[1];
aoqi@0 80 pid2 = Integer.parseInt(pidText);
aoqi@0 81 System.out.println( "pid2: " + pid2);
aoqi@0 82 vm2 = attachPID(pid2);
aoqi@0 83 } catch (NumberFormatException e) {
aoqi@0 84 println(e.getMessage());
aoqi@0 85 usage();
aoqi@0 86 }
aoqi@0 87 break;
aoqi@0 88 default:
aoqi@0 89 usage();
aoqi@0 90 }
aoqi@0 91
aoqi@0 92 if (vm1 != null) {
aoqi@0 93 System.out.println("vm1: attached ok!");
aoqi@0 94 System.out.println(vm1.version());
aoqi@0 95 sagdoit mine = new sagdoit(vm1);
aoqi@0 96 mine.doAll();
aoqi@0 97 }
aoqi@0 98
aoqi@0 99 if (vm2 != null) {
aoqi@0 100 System.out.println("vm2: attached ok!");
aoqi@0 101 System.out.println(vm2.version());
aoqi@0 102 sagdoit mine = new sagdoit(vm2);
aoqi@0 103 mine.doAll();
aoqi@0 104 }
aoqi@0 105
aoqi@0 106 if (vm1 != null) {
aoqi@0 107 vm1.dispose();
aoqi@0 108 }
aoqi@0 109
aoqi@0 110 if (vm2 != null) {
aoqi@0 111 vm2.dispose();
aoqi@0 112 }
aoqi@0 113 }
aoqi@0 114
aoqi@0 115 private static VirtualMachine attachPID(int pid) {
aoqi@0 116 Map connArgs = myPIDConn.defaultArguments();
aoqi@0 117 System.out.println("connArgs = " + connArgs);
aoqi@0 118 VirtualMachine vm;
aoqi@0 119 Connector.StringArgument connArg = (Connector.StringArgument)connArgs.get("pid");
aoqi@0 120 connArg.setValue(Integer.toString(pid));
aoqi@0 121
aoqi@0 122 try {
aoqi@0 123 vm = myPIDConn.attach(connArgs);
aoqi@0 124 } catch (IOException ee) {
aoqi@0 125 System.err.println("ERROR: myPIDConn.attach got IO Exception:" + ee);
aoqi@0 126 vm = null;
aoqi@0 127 } catch (IllegalConnectorArgumentsException ee) {
aoqi@0 128 System.err.println("ERROR: myPIDConn.attach got illegal args exception:" + ee);
aoqi@0 129 vm = null;
aoqi@0 130 }
aoqi@0 131 return vm;
aoqi@0 132 }
aoqi@0 133 }

mercurial