jdk/src/windows/classes/sun/tools/attach/WindowsVirtualMachine.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 24363 33b869a8806b
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.tools.attach;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import com.sun.tools.attach.VirtualMachine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.tools.attach.AgentLoadException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.tools.attach.AttachNotSupportedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.tools.attach.spi.AttachProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.tools.attach.HotSpotVirtualMachine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class WindowsVirtualMachine extends HotSpotVirtualMachine {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    // the enqueue code stub (copied into each target VM)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static byte[] stub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private volatile long hProcess;     // handle to the process
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    WindowsVirtualMachine(AttachProvider provider, String id)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        throws AttachNotSupportedException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        super(provider, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        int pid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            pid = Integer.parseInt(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        } catch (NumberFormatException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            throw new AttachNotSupportedException("Invalid process identifier");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        hProcess = openProcess(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        // The target VM might be a pre-6.0 VM so we enqueue a "null" command
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        // which minimally tests that the enqueue function exists in the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        // VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            enqueue(hProcess, stub, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            throw new AttachNotSupportedException(x.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public void detach() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            if (hProcess != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                closeProcess(hProcess);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                hProcess = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    InputStream execute(String cmd, Object ... args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        throws AgentLoadException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        assert args.length <= 3;        // includes null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // create a pipe using a random name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        int r = (new Random()).nextInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        String pipename = "\\\\.\\pipe\\javatool" + r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        long hPipe = createPipe(pipename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // check if we are detached - in theory it's possible that detach is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // after this check but before we enqueue the command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (hProcess == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            closePipe(hPipe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throw new IOException("Detached from target VM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            // enqueue the command to the process
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            enqueue(hProcess, stub, cmd, pipename, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            // wait for command to complete - process will connect with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            // completion status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            connectPipe(hPipe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            // create an input stream for the pipe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            PipedInputStream is = new PipedInputStream(hPipe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            // read completion status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            int status = readInt(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (status != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                // special case the load command so that the right exception is thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                if (cmd.equals("load")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    throw new AgentLoadException("Failed to load agent library");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    throw new IOException("Command failed in target VM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            // return the input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            return is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            closePipe(hPipe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    // An InputStream based on a pipe to the target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private class PipedInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        private long hPipe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        public PipedInputStream(long hPipe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            this.hPipe = hPipe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        public synchronized int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            byte b[] = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            int n = this.read(b, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            if (n == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                return b[0] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        public synchronized int read(byte[] bs, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if ((off < 0) || (off > bs.length) || (len < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                ((off + len) > bs.length) || ((off + len) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            } else if (len == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return WindowsVirtualMachine.readPipe(hPipe, bs, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            if (hPipe != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                WindowsVirtualMachine.closePipe(hPipe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                hPipe = -1;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    //-- native methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    static native void init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    static native byte[] generateStub();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    static native long openProcess(int pid) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    static native void closeProcess(long hProcess) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    static native long createPipe(String name) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    static native void closePipe(long hPipe) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    static native void connectPipe(long hPipe) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    static native int readPipe(long hPipe, byte buf[], int off, int buflen) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    static native void enqueue(long hProcess, byte[] stub,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        String cmd, String pipename, Object ... args) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        System.loadLibrary("attach");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        init();                                 // native initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        stub = generateStub();                  // generate stub to copy into target process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
}