src/jdk.attach/windows/classes/sun/tools/attach/VirtualMachineImpl.java
author rschmelter
Tue, 16 Jul 2019 13:57:13 +0200
changeset 55685 56b96d234f26
parent 54065 f984aca565c1
permissions -rw-r--r--
8227440: Implement fix to JDK-8224642 on all platforms Reviewed-by: sspitsyn, clanger
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
53858
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 49610
diff changeset
     2
 * Copyright (c) 2005, 2019, 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
    {
54065
f984aca565c1 8219721: jcmd from earlier release will hang attaching to VM with JDK-8215622 applied
dholmes
parents: 53858
diff changeset
    80
        assert args.length <= 3;        // includes null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // create a pipe using a random name
49610
6790b1077a3f 8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents: 47216
diff changeset
    83
        Random rnd = new Random();
6790b1077a3f 8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents: 47216
diff changeset
    84
        int r = rnd.nextInt();
6790b1077a3f 8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents: 47216
diff changeset
    85
        String pipeprefix = "\\\\.\\pipe\\javatool";
6790b1077a3f 8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents: 47216
diff changeset
    86
        String pipename = pipeprefix + r;
6790b1077a3f 8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents: 47216
diff changeset
    87
        long hPipe;
6790b1077a3f 8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents: 47216
diff changeset
    88
        try {
6790b1077a3f 8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents: 47216
diff changeset
    89
            hPipe = createPipe(pipename);
6790b1077a3f 8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents: 47216
diff changeset
    90
        } catch (IOException ce) {
6790b1077a3f 8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents: 47216
diff changeset
    91
            // Retry with another random pipe name.
6790b1077a3f 8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents: 47216
diff changeset
    92
            r = rnd.nextInt();
6790b1077a3f 8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents: 47216
diff changeset
    93
            pipename = pipeprefix + r;
6790b1077a3f 8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents: 47216
diff changeset
    94
            hPipe = createPipe(pipename);
6790b1077a3f 8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents: 47216
diff changeset
    95
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // check if we are detached - in theory it's possible that detach is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        // after this check but before we enqueue the command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (hProcess == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            closePipe(hPipe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            throw new IOException("Detached from target VM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            // enqueue the command to the process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            enqueue(hProcess, stub, cmd, pipename, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            // wait for command to complete - process will connect with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            // completion status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            connectPipe(hPipe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            // create an input stream for the pipe
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 26460
diff changeset
   113
            PipedInputStream in = new PipedInputStream(hPipe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            // read completion status
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 26460
diff changeset
   116
            int status = readInt(in);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (status != 0) {
24363
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 5506
diff changeset
   118
                // read from the stream and use that as the error message
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 26460
diff changeset
   119
                String message = readErrorMessage(in);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 26460
diff changeset
   120
                in.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                // special case the load command so that the right exception is thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                if (cmd.equals("load")) {
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 26460
diff changeset
   123
                    String msg = "Failed to load agent library";
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 26460
diff changeset
   124
                    if (!message.isEmpty())
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 26460
diff changeset
   125
                        msg += ": " + message;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 26460
diff changeset
   126
                    throw new AgentLoadException(msg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                } else {
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 26460
diff changeset
   128
                    if (message.isEmpty())
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 26460
diff changeset
   129
                        message = "Command failed in target VM";
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 26460
diff changeset
   130
                    throw new AttachOperationFailedException(message);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            // return the input stream
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 26460
diff changeset
   135
            return in;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            closePipe(hPipe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    // An InputStream based on a pipe to the target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private class PipedInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        private long hPipe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        public PipedInputStream(long hPipe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            this.hPipe = hPipe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        public synchronized int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            byte b[] = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            int n = this.read(b, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            if (n == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                return b[0] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                return -1;
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
        public synchronized int read(byte[] bs, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            if ((off < 0) || (off > bs.length) || (len < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                ((off + len) > bs.length) || ((off + len) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            } else if (len == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   169
            return VirtualMachineImpl.readPipe(hPipe, bs, off, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
55685
56b96d234f26 8227440: Implement fix to JDK-8224642 on all platforms
rschmelter
parents: 54065
diff changeset
   172
        public synchronized void close() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            if (hPipe != -1) {
55685
56b96d234f26 8227440: Implement fix to JDK-8224642 on all platforms
rschmelter
parents: 54065
diff changeset
   174
                long toClose = hPipe;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                hPipe = -1;
55685
56b96d234f26 8227440: Implement fix to JDK-8224642 on all platforms
rschmelter
parents: 54065
diff changeset
   176
                VirtualMachineImpl.closePipe(toClose);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    //-- native methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    static native void init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    static native byte[] generateStub();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    static native long openProcess(int pid) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    static native void closeProcess(long hProcess) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    static native long createPipe(String name) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    static native void closePipe(long hPipe) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    static native void connectPipe(long hPipe) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    static native int readPipe(long hPipe, byte buf[], int off, int buflen) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    static native void enqueue(long hProcess, byte[] stub,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        String cmd, String pipename, Object ... args) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        System.loadLibrary("attach");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        init();                                 // native initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        stub = generateStub();                  // generate stub to copy into target process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
}