jdk/src/jdk.attach/windows/classes/sun/tools/attach/VirtualMachineImpl.java
author ctornqvi
Wed, 18 Feb 2015 19:28:08 -0800
changeset 29105 3927f8f0bf4b
parent 26460 68a3e0abedfd
child 45004 ea3137042a61
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
24363
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 5506
diff changeset
     2
 * Copyright (c) 2005, 2014, 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
24363
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 5506
diff changeset
    27
import com.sun.tools.attach.AttachOperationFailedException;
2
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;
24363
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 5506
diff changeset
    31
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.tools.attach.HotSpotVirtualMachine;
24363
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 5506
diff changeset
    33
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
    38
public class VirtualMachineImpl extends HotSpotVirtualMachine {
2
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
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
    45
    VirtualMachineImpl(AttachProvider provider, String id)
2
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) {
24363
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 5506
diff changeset
   108
                // read from the stream and use that as the error message
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 5506
diff changeset
   109
                String message = readErrorMessage(is);
26460
68a3e0abedfd 8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents: 26216
diff changeset
   110
                is.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                // special case the load command so that the right exception is thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                if (cmd.equals("load")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    throw new AgentLoadException("Failed to load agent library");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                } else {
24363
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 5506
diff changeset
   115
                    if (message == null) {
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 5506
diff changeset
   116
                        throw new AttachOperationFailedException("Command failed in target VM");
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 5506
diff changeset
   117
                    } else {
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 5506
diff changeset
   118
                        throw new AttachOperationFailedException(message);
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 5506
diff changeset
   119
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            // return the input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            closePipe(hPipe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    // An InputStream based on a pipe to the target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private class PipedInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        private long hPipe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        public PipedInputStream(long hPipe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            this.hPipe = hPipe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        public synchronized int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            byte b[] = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            int n = this.read(b, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (n == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                return b[0] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        public synchronized int read(byte[] bs, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            if ((off < 0) || (off > bs.length) || (len < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                ((off + len) > bs.length) || ((off + len) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            } else if (len == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   158
            return VirtualMachineImpl.readPipe(hPipe, bs, off, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (hPipe != -1) {
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   163
                VirtualMachineImpl.closePipe(hPipe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                hPipe = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    //-- native methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    static native void init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    static native byte[] generateStub();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    static native long openProcess(int pid) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    static native void closeProcess(long hProcess) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    static native long createPipe(String name) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    static native void closePipe(long hPipe) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    static native void connectPipe(long hPipe) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    static native int readPipe(long hPipe, byte buf[], int off, int buflen) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    static native void enqueue(long hProcess, byte[] stub,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        String cmd, String pipename, Object ... args) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        System.loadLibrary("attach");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        init();                                 // native initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        stub = generateStub();                  // generate stub to copy into target process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
}