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