jdk/src/share/classes/com/sun/tools/jdi/ProcessAttachingConnector.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.jdi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.UnsupportedEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import com.sun.jdi.Bootstrap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.jdi.VirtualMachine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.jdi.connect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.jdi.connect.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * An AttachingConnector that connects to a debuggee by specifying the process
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * id (pid) as the connector argument. If the process is a debuggee listening
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * on a transport address then this connector reads the transport address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * and attempts to attach to it using the appropriate transport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class ProcessAttachingConnector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        extends ConnectorImpl implements AttachingConnector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * The arguments that this connector supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static final String ARG_PID = "pid";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static final String ARG_TIMEOUT = "timeout";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    com.sun.tools.attach.VirtualMachine vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    Transport transport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public ProcessAttachingConnector() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        addStringArgument(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            ARG_PID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            getString("process_attaching.pid.label"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            getString("process_attaching.pid"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            "",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        addIntegerArgument(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            ARG_TIMEOUT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            getString("generic_attaching.timeout.label"),       // use generic keys to keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            getString("generic_attaching.timeout"),             // resource bundle small
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            "",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            0, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        transport = new Transport() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            public String name() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                return "local";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Attach to a target VM using the specified address and Connector arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public VirtualMachine attach(Map<String,? extends Connector.Argument> args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                throws IOException, IllegalConnectorArgumentsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        String pid = argument(ARG_PID, args).value();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        String t = argument(ARG_TIMEOUT, args).value();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        int timeout = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (t.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            timeout = Integer.decode(t).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // Use Attach API to attach to target VM and read value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        // sun.jdwp.listenAddress property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        String address = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        com.sun.tools.attach.VirtualMachine vm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            vm = com.sun.tools.attach.VirtualMachine.attach(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            Properties props = vm.getAgentProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            address = props.getProperty("sun.jdwp.listenerAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            throw new IOException(x.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            if (vm != null) vm.detach();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        // check that the property value is formatted correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (address == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new IOException("Not a debuggee, or not listening for debugger to attach");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        int pos = address.indexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (pos < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            throw new IOException("Unable to determine transport endpoint");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        // parse into transport library name and address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        final String lib = address.substring(0, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        address = address.substring(pos+1, address.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        TransportService ts = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (lib.equals("dt_socket")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            ts = new SocketTransportService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            if (lib.equals("dt_shmem")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    Class c = Class.forName("com.sun.tools.jdi.SharedMemoryTransportService");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    ts = (TransportService)c.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                } catch (Exception x) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (ts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            throw new IOException("Transport " + lib + " not recognized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        // connect to the debuggee
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        Connection connection = ts.attach(address, timeout, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return Bootstrap.virtualMachineManager().createVirtualMachine(connection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public String name() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return "com.sun.jdi.ProcessAttach";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public String description() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return getString("process_attaching.description");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public Transport transport() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (transport == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return new Transport() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                public String name() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    return "local";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return transport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
}