agent/test/jdi/sagclient.java

Thu, 27 May 2010 19:08:38 -0700

author
trims
date
Thu, 27 May 2010 19:08:38 -0700
changeset 1907
c18cbe5936b8
parent 435
a61af66fc99e
child 6876
710a3c8b516e
permissions
-rw-r--r--

6941466: Oracle rebranding changes for Hotspot repositories
Summary: Change all the Sun copyrights to Oracle copyright
Reviewed-by: ohair

     1 /*
     2  * Copyright (c) 2002, 2004, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    25 import com.sun.jdi.*;
    26 import com.sun.jdi.connect.*;
    28 import java.util.Map;
    29 import java.util.List;
    30 import java.util.Iterator;
    31 import java.io.IOException;
    33 public class sagclient {
    34     static AttachingConnector myCoreConn;
    35     static AttachingConnector myPIDConn;
    36     static AttachingConnector myDbgSvrConn;
    37     static VirtualMachine vm;
    38     static VirtualMachineManager vmmgr;
    40     public static void println(String msg) {
    41         System.out.println("jj: " + msg);
    42     }
    45     public static void main(String args[]) {
    46         vmmgr = Bootstrap.virtualMachineManager();
    47         List attachingConnectors = vmmgr.attachingConnectors();
    48         if (attachingConnectors.isEmpty()) {
    49             System.err.println( "ERROR: No attaching connectors");
    50             return;
    51         }
    52         Iterator myIt = attachingConnectors.iterator();
    53         while (myIt.hasNext()) {
    54             AttachingConnector tmpCon = (AttachingConnector)myIt.next();
    55             if (tmpCon.name().equals(
    56                 "sun.jvm.hotspot.jdi.SACoreAttachingConnector")) {
    57                 myCoreConn = tmpCon;
    58             } else if (tmpCon.name().equals(
    59                 "sun.jvm.hotspot.jdi.SAPIDAttachingConnector")) {
    60                 myPIDConn = tmpCon;
    61             } else if (tmpCon.name().equals(
    62                 "sun.jvm.hotspot.jdi.SADebugServerAttachingConnector")) {
    63                 myDbgSvrConn = tmpCon;
    64             }
    65         }
    66         String execPath = null;
    67         String pidText = null;
    68         String coreFilename = null;
    69         String debugServer = null;
    70         int pid = 0;
    71         switch (args.length) {
    72         case (0):
    73             break;
    74         case (1):
    75             // If all numbers, it is a PID to attach to
    76             // Else, it is a pathname to a .../bin/java for a core file.
    77             try {
    78                 pidText = args[0];
    79                 pid = Integer.parseInt(pidText);
    80                 System.out.println( "pid: " + pid);
    81                 vm = attachPID(pid);
    82             } catch (NumberFormatException e) {
    83                 System.out.println("trying remote server ..");
    84                 debugServer = args[0];
    85                 System.out.println( "remote server: " + debugServer);
    86                 vm = attachDebugServer(debugServer);
    87             }
    88             break;
    90         case (2):
    91             execPath = args[0];
    92             coreFilename = args[1];
    93             System.out.println( "jdk: " + execPath);
    94             System.out.println( "core: " + coreFilename);
    95             vm = attachCore(coreFilename, execPath);
    96             break;
    97         }
   100         if (vm != null) {
   101             System.out.println("sagclient: attached ok!");
   102             sagdoit mine = new sagdoit(vm);
   103             mine.doAll();
   104             vm.dispose();
   105         }
   106     }
   108     private static VirtualMachine attachCore(String coreFilename, String execPath) {
   109         Map connArgs = myCoreConn.defaultArguments();
   110         System.out.println("connArgs = " + connArgs);
   111         VirtualMachine vm;
   112         Connector.StringArgument connArg = (Connector.StringArgument)connArgs.get("core");
   113         connArg.setValue(coreFilename);
   115         connArg =  (Connector.StringArgument)connArgs.get("javaExecutable");
   116         connArg.setValue(execPath);
   117         try {
   118             vm = myCoreConn.attach(connArgs);
   119         } catch (IOException ee) {
   120             System.err.println("ERROR: myCoreConn.attach got IO Exception:" + ee);
   121             vm = null;
   122         } catch (IllegalConnectorArgumentsException ee) {
   123             System.err.println("ERROR: myCoreConn.attach got illegal args exception:" + ee);
   124             vm = null;
   125         }
   126         return vm;
   127    }
   129    private static VirtualMachine attachPID(int pid) {
   130         Map connArgs = myPIDConn.defaultArguments();
   131         System.out.println("connArgs = " + connArgs);
   132         VirtualMachine vm;
   133         Connector.StringArgument connArg = (Connector.StringArgument)connArgs.get("pid");
   134         connArg.setValue(Integer.toString(pid));
   136         try {
   137             vm = myPIDConn.attach(connArgs);
   138         } catch (IOException ee) {
   139             System.err.println("ERROR: myPIDConn.attach got IO Exception:" + ee);
   140             vm = null;
   141         } catch (IllegalConnectorArgumentsException ee) {
   142             System.err.println("ERROR: myPIDConn.attach got illegal args exception:" + ee);
   143             vm = null;
   144         }
   145         return vm;
   146    }
   149    private static VirtualMachine attachDebugServer(String debugServer) {
   150         Map connArgs = myDbgSvrConn.defaultArguments();
   151         System.out.println("connArgs = " + connArgs);
   152         VirtualMachine vm;
   153         Connector.StringArgument connArg = (Connector.StringArgument)connArgs.get("debugServerName");
   154         connArg.setValue(debugServer);
   156         try {
   157             vm = myDbgSvrConn.attach(connArgs);
   158         } catch (IOException ee) {
   159             System.err.println("ERROR: myDbgSvrConn.attach got IO Exception:" + ee);
   160             vm = null;
   161         } catch (IllegalConnectorArgumentsException ee) {
   162             System.err.println("ERROR: myDbgSvrConn.attach got illegal args exception:" + ee);
   163             vm = null;
   164         }
   165         return vm;
   166    }
   167 }

mercurial