hotspot/agent/test/jdi/sagclient.java
author cl
Thu, 26 Aug 2010 16:16:55 -0700
changeset 6236 4fc84aaaa7ab
parent 5547 f4b087cbb361
permissions -rw-r--r--
Added tag jdk7-b107 for changeset a8c1cf6a14b9
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) 2002, 2004, 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
public class sagclient {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
    static AttachingConnector myCoreConn;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
    static AttachingConnector myPIDConn;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
    static AttachingConnector myDbgSvrConn;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
    static VirtualMachine vm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
    static VirtualMachineManager vmmgr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
    public static void println(String msg) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
        System.out.println("jj: " + msg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
    public static void main(String args[]) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
        vmmgr = Bootstrap.virtualMachineManager();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
        List attachingConnectors = vmmgr.attachingConnectors();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
        if (attachingConnectors.isEmpty()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
            System.err.println( "ERROR: No attaching connectors");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
            return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
        Iterator myIt = attachingConnectors.iterator();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
        while (myIt.hasNext()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
            AttachingConnector tmpCon = (AttachingConnector)myIt.next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
            if (tmpCon.name().equals(
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
                "sun.jvm.hotspot.jdi.SACoreAttachingConnector")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
                myCoreConn = tmpCon;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
            } else if (tmpCon.name().equals(
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
                "sun.jvm.hotspot.jdi.SAPIDAttachingConnector")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
                myPIDConn = tmpCon;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
            } else if (tmpCon.name().equals(
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
                "sun.jvm.hotspot.jdi.SADebugServerAttachingConnector")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
                myDbgSvrConn = tmpCon;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
        String execPath = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
        String pidText = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
        String coreFilename = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
        String debugServer = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
        int pid = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
        switch (args.length) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
        case (0):
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
        case (1):
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
            // If all numbers, it is a PID to attach to
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
            // Else, it is a pathname to a .../bin/java for a core file.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
            try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
                pidText = args[0];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
                pid = Integer.parseInt(pidText);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
                System.out.println( "pid: " + pid);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
                vm = attachPID(pid);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
            } catch (NumberFormatException e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
                System.out.println("trying remote server ..");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
                debugServer = args[0];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
                System.out.println( "remote server: " + debugServer);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
                vm = attachDebugServer(debugServer);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
        case (2):
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
            execPath = args[0];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
            coreFilename = args[1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
            System.out.println( "jdk: " + execPath);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
            System.out.println( "core: " + coreFilename);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
            vm = attachCore(coreFilename, execPath);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
        if (vm != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
            System.out.println("sagclient: attached ok!");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
            sagdoit mine = new sagdoit(vm);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
            mine.doAll();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
            vm.dispose();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
    private static VirtualMachine attachCore(String coreFilename, String execPath) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
        Map connArgs = myCoreConn.defaultArguments();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
        System.out.println("connArgs = " + connArgs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
        VirtualMachine vm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
        Connector.StringArgument connArg = (Connector.StringArgument)connArgs.get("core");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
        connArg.setValue(coreFilename);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
        connArg =  (Connector.StringArgument)connArgs.get("javaExecutable");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
        connArg.setValue(execPath);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
            vm = myCoreConn.attach(connArgs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
        } catch (IOException ee) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
            System.err.println("ERROR: myCoreConn.attach got IO Exception:" + ee);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
            vm = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
        } catch (IllegalConnectorArgumentsException ee) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
            System.err.println("ERROR: myCoreConn.attach got illegal args exception:" + ee);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
            vm = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
        return vm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
   private static VirtualMachine attachPID(int pid) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
        Map connArgs = myPIDConn.defaultArguments();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
        System.out.println("connArgs = " + connArgs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
        VirtualMachine vm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
        Connector.StringArgument connArg = (Connector.StringArgument)connArgs.get("pid");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
        connArg.setValue(Integer.toString(pid));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
            vm = myPIDConn.attach(connArgs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
        } catch (IOException ee) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
            System.err.println("ERROR: myPIDConn.attach got IO Exception:" + ee);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
            vm = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
        } catch (IllegalConnectorArgumentsException ee) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
            System.err.println("ERROR: myPIDConn.attach got illegal args exception:" + ee);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
            vm = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
        return vm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
   private static VirtualMachine attachDebugServer(String debugServer) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
        Map connArgs = myDbgSvrConn.defaultArguments();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
        System.out.println("connArgs = " + connArgs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
        VirtualMachine vm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
        Connector.StringArgument connArg = (Connector.StringArgument)connArgs.get("debugServerName");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
        connArg.setValue(debugServer);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
            vm = myDbgSvrConn.attach(connArgs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
        } catch (IOException ee) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
            System.err.println("ERROR: myDbgSvrConn.attach got IO Exception:" + ee);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
            vm = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
        } catch (IllegalConnectorArgumentsException ee) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
            System.err.println("ERROR: myDbgSvrConn.attach got illegal args exception:" + ee);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
            vm = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
        return vm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
}