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