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