agent/test/jdi/serialvm.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
aoqi@0 34 /* This class is used to test multi VM connectivity feature of
aoqi@0 35 * SA/JDI. Accepts two PIDs as arguments. Connects to first VM
aoqi@0 36 *, disposes it, connects to second VM, disposes second VM.
aoqi@0 37 */
aoqi@0 38
aoqi@0 39
aoqi@0 40 public class serialvm {
aoqi@0 41 static AttachingConnector myPIDConn;
aoqi@0 42 static VirtualMachine vm1;
aoqi@0 43 static VirtualMachine vm2;
aoqi@0 44 static VirtualMachineManager vmmgr;
aoqi@0 45
aoqi@0 46 public static void println(String msg) {
aoqi@0 47 System.out.println(msg);
aoqi@0 48 }
aoqi@0 49
aoqi@0 50 private static void usage() {
aoqi@0 51 System.err.println("Usage: java serialvm <pid1> <pid2>");
aoqi@0 52 System.exit(1);
aoqi@0 53 }
aoqi@0 54
aoqi@0 55 public static void main(String args[]) {
aoqi@0 56 vmmgr = Bootstrap.virtualMachineManager();
aoqi@0 57 List attachingConnectors = vmmgr.attachingConnectors();
aoqi@0 58 if (attachingConnectors.isEmpty()) {
aoqi@0 59 System.err.println( "ERROR: No attaching connectors");
aoqi@0 60 return;
aoqi@0 61 }
aoqi@0 62 Iterator myIt = attachingConnectors.iterator();
aoqi@0 63 while (myIt.hasNext()) {
aoqi@0 64 AttachingConnector tmpCon = (AttachingConnector)myIt.next();
aoqi@0 65 if (tmpCon.name().equals(
aoqi@0 66 "sun.jvm.hotspot.jdi.SAPIDAttachingConnector")) {
aoqi@0 67 myPIDConn = tmpCon;
aoqi@0 68 break;
aoqi@0 69 }
aoqi@0 70 }
aoqi@0 71
aoqi@0 72 int pid1 = 0, pid2 = 0;
aoqi@0 73 String pidText = null;
aoqi@0 74 switch (args.length) {
aoqi@0 75 case (2):
aoqi@0 76 try {
aoqi@0 77 pidText = args[0];
aoqi@0 78 pid1 = Integer.parseInt(pidText);
aoqi@0 79 System.out.println( "pid1: " + pid1);
aoqi@0 80 pidText = args[1];
aoqi@0 81 pid2 = Integer.parseInt(pidText);
aoqi@0 82 System.out.println( "pid2: " + 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 // attach, dispose, attach2, dispose2 pattern
aoqi@0 93 // as opposed to attach1, attach2, dispose1, dispose2
aoqi@0 94 vm1 = attachPID(pid1);
aoqi@0 95 if (vm1 != null) {
aoqi@0 96 System.out.println("vm1: attached ok!");
aoqi@0 97 System.out.println(vm1.version());
aoqi@0 98 sagdoit mine = new sagdoit(vm1);
aoqi@0 99 mine.doAll();
aoqi@0 100 }
aoqi@0 101 if (vm1 != null) {
aoqi@0 102 vm1.dispose();
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 vm2 = attachPID(pid2);
aoqi@0 106 if (vm2 != null) {
aoqi@0 107 System.out.println("vm2: attached ok!");
aoqi@0 108 System.out.println(vm2.version());
aoqi@0 109 sagdoit mine = new sagdoit(vm2);
aoqi@0 110 mine.doAll();
aoqi@0 111 }
aoqi@0 112
aoqi@0 113
aoqi@0 114 if (vm2 != null) {
aoqi@0 115 vm2.dispose();
aoqi@0 116 }
aoqi@0 117 }
aoqi@0 118
aoqi@0 119 private static VirtualMachine attachPID(int pid) {
aoqi@0 120 Map connArgs = myPIDConn.defaultArguments();
aoqi@0 121 System.out.println("connArgs = " + connArgs);
aoqi@0 122 VirtualMachine vm;
aoqi@0 123 Connector.StringArgument connArg = (Connector.StringArgument)connArgs.get("pid");
aoqi@0 124 connArg.setValue(Integer.toString(pid));
aoqi@0 125
aoqi@0 126 try {
aoqi@0 127 vm = myPIDConn.attach(connArgs);
aoqi@0 128 } catch (IOException ee) {
aoqi@0 129 System.err.println("ERROR: myPIDConn.attach got IO Exception:" + ee);
aoqi@0 130 vm = null;
aoqi@0 131 } catch (IllegalConnectorArgumentsException ee) {
aoqi@0 132 System.err.println("ERROR: myPIDConn.attach got illegal args exception:" + ee);
aoqi@0 133 vm = null;
aoqi@0 134 }
aoqi@0 135 return vm;
aoqi@0 136 }
aoqi@0 137 }

mercurial