hotspot/agent/test/jdi/multivm.java
changeset 1 489c9b5090e2
child 5547 f4b087cbb361
equal deleted inserted replaced
0:fd16c54261b3 1:489c9b5090e2
       
     1 /*
       
     2  * Copyright 2003 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 import com.sun.jdi.*;
       
    26 import com.sun.jdi.connect.*;
       
    27 
       
    28 import java.util.Map;
       
    29 import java.util.List;
       
    30 import java.util.Iterator;
       
    31 import java.io.IOException;
       
    32 
       
    33 /* This class is used to test multi VM connectivity feature of
       
    34  * SA/JDI. Accepts two PIDs as arguments. Connects to first VM
       
    35  *, Connects to second VM and disposes them in that order.
       
    36  */
       
    37 
       
    38 public class multivm {
       
    39     static AttachingConnector myPIDConn;
       
    40     static VirtualMachine vm1;
       
    41     static VirtualMachine vm2;
       
    42     static VirtualMachineManager vmmgr;
       
    43 
       
    44     public static void println(String msg) {
       
    45         System.out.println(msg);
       
    46     }
       
    47 
       
    48     private static void usage() {
       
    49         System.err.println("Usage: java multivm <pid1> <pid2>");
       
    50         System.exit(1);
       
    51     }
       
    52 
       
    53     public static void main(String args[]) {
       
    54         vmmgr = Bootstrap.virtualMachineManager();
       
    55         List attachingConnectors = vmmgr.attachingConnectors();
       
    56         if (attachingConnectors.isEmpty()) {
       
    57             System.err.println( "ERROR: No attaching connectors");
       
    58             return;
       
    59         }
       
    60         Iterator myIt = attachingConnectors.iterator();
       
    61         while (myIt.hasNext()) {
       
    62             AttachingConnector tmpCon = (AttachingConnector)myIt.next();
       
    63             if (tmpCon.name().equals(
       
    64                 "sun.jvm.hotspot.jdi.SAPIDAttachingConnector")) {
       
    65                 myPIDConn = tmpCon;
       
    66                 break;
       
    67             }
       
    68         }
       
    69 
       
    70         int pid1 = 0, pid2 = 0;
       
    71         String pidText = null;
       
    72         switch (args.length) {
       
    73         case (2):
       
    74             try {
       
    75                 pidText = args[0];
       
    76                 pid1 = Integer.parseInt(pidText);
       
    77                 System.out.println( "pid1: " + pid1);
       
    78                 vm1 = attachPID(pid1);
       
    79                 pidText = args[1];
       
    80                 pid2 = Integer.parseInt(pidText);
       
    81                 System.out.println( "pid2: " + pid2);
       
    82                 vm2 = attachPID(pid2);
       
    83             } catch (NumberFormatException e) {
       
    84                 println(e.getMessage());
       
    85                 usage();
       
    86             }
       
    87             break;
       
    88         default:
       
    89             usage();
       
    90         }
       
    91 
       
    92         if (vm1 != null) {
       
    93             System.out.println("vm1: attached ok!");
       
    94             System.out.println(vm1.version());
       
    95             sagdoit mine = new sagdoit(vm1);
       
    96             mine.doAll();
       
    97         }
       
    98 
       
    99         if (vm2 != null) {
       
   100             System.out.println("vm2: attached ok!");
       
   101             System.out.println(vm2.version());
       
   102             sagdoit mine = new sagdoit(vm2);
       
   103             mine.doAll();
       
   104         }
       
   105 
       
   106         if (vm1 != null) {
       
   107             vm1.dispose();
       
   108         }
       
   109 
       
   110         if (vm2 != null) {
       
   111             vm2.dispose();
       
   112         }
       
   113     }
       
   114 
       
   115    private static VirtualMachine attachPID(int pid) {
       
   116         Map connArgs = myPIDConn.defaultArguments();
       
   117         System.out.println("connArgs = " + connArgs);
       
   118         VirtualMachine vm;
       
   119         Connector.StringArgument connArg = (Connector.StringArgument)connArgs.get("pid");
       
   120         connArg.setValue(Integer.toString(pid));
       
   121 
       
   122         try {
       
   123             vm = myPIDConn.attach(connArgs);
       
   124         } catch (IOException ee) {
       
   125             System.err.println("ERROR: myPIDConn.attach got IO Exception:" + ee);
       
   126             vm = null;
       
   127         } catch (IllegalConnectorArgumentsException ee) {
       
   128             System.err.println("ERROR: myPIDConn.attach got illegal args exception:" + ee);
       
   129             vm = null;
       
   130         }
       
   131         return vm;
       
   132    }
       
   133 }