src/jdk.attach/solaris/classes/sun/tools/attach/VirtualMachineImpl.java
author ihse
Wed, 03 Oct 2018 16:02:24 +0200
branchJEP-230-microbenchmarks-branch
changeset 56921 69544f53b8bf
parent 49742 1196aa0be8be
child 52817 981eb3c1b90d
permissions -rw-r--r--
Merge from default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49742
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 48485
diff changeset
     2
 * Copyright (c) 2005, 2018, 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: 14342
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: 14342
diff changeset
    31
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.FileNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Solaris implementation of HotSpotVirtualMachine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
    40
public class VirtualMachineImpl extends HotSpotVirtualMachine {
11692
dd4289e8b9e3 7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents: 7668
diff changeset
    41
    // "/tmp" is used as a global well-known location for the files
dd4289e8b9e3 7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents: 7668
diff changeset
    42
    // .java_pid<pid>. and .attach_pid<pid>. It is important that this
dd4289e8b9e3 7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents: 7668
diff changeset
    43
    // location is the same for all processes, otherwise the tools
dd4289e8b9e3 7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents: 7668
diff changeset
    44
    // will not be able to find all Hotspot processes.
dd4289e8b9e3 7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents: 7668
diff changeset
    45
    // Any changes to this needs to be synchronized with HotSpot.
dd4289e8b9e3 7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents: 7668
diff changeset
    46
    private static final String tmpdir = "/tmp";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // door descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private int fd = -1;
48485
258a4dab74a7 8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents: 47216
diff changeset
    50
    String socket_path;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Attaches to the target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
    55
    VirtualMachineImpl(AttachProvider provider, String vmid)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        throws AttachNotSupportedException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        super(provider, vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        // This provider only understands process-ids (pids).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        int pid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            pid = Integer.parseInt(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        } catch (NumberFormatException x) {
48485
258a4dab74a7 8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents: 47216
diff changeset
    64
            throw new AttachNotSupportedException("Invalid process identifier");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        // Opens the door file to the target VM. If the file is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        // found it might mean that the attach mechanism isn't started in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        // target VM so we attempt to start it and retry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            fd = openDoor(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        } catch (FileNotFoundException fnf1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            File f = createAttachFile(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                sigquit(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                // give the target VM time to start the attach mechanism
40685
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
    78
                final int delay_step = 100;
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
    79
                final long timeout = attachTimeout();
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
    80
                long time_spend = 0;
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
    81
                long delay = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                do {
40685
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
    83
                    // Increase timeout on each attempt to reduce polling
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
    84
                    delay += delay_step;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                        Thread.sleep(delay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    } catch (InterruptedException x) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                        fd = openDoor(pid);
40685
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
    90
                    } catch (FileNotFoundException fnf2) {
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
    91
                        // pass
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
    92
                    }
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
    93
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
    94
                    time_spend += delay;
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
    95
                    if (time_spend > timeout/2 && fd == -1) {
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
    96
                        // Send QUIT again to give target VM the last chance to react
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
    97
                        sigquit(pid);
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
    98
                    }
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
    99
                } while (time_spend <= timeout && fd == -1);
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
   100
                if (fd  == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    throw new AttachNotSupportedException(
40685
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
   102
                        String.format("Unable to open door %s: " +
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
   103
                          "target process %d doesn't respond within %dms " +
48485
258a4dab74a7 8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents: 47216
diff changeset
   104
                          "or HotSpot VM not loaded", socket_path, pid, time_spend));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                f.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        assert fd >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Detach from the target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public void detach() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            if (fd != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                fd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Execute the given command in the target VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        assert args.length <= 3;                // includes null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        // first check that we are still attached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        int door;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                throw new IOException("Detached from target VM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            door = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // enqueue the command via a door call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        int s = enqueue(door, cmd, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        assert s >= 0;                          // valid file descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // The door call returns a file descriptor (one end of a socket pair).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        // Create an input stream around it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        SocketInputStream sis = new SocketInputStream(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // Read the command completion status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        int completionStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            completionStatus = readInt(sis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            sis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // If non-0 it means an error but we need to special-case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        // "load" command to ensure that the right exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (completionStatus != 0) {
24363
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   160
            // 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: 14342
diff changeset
   161
            String message = readErrorMessage(sis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            sis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            if (cmd.equals("load")) {
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
   164
                String msg = "Failed to load agent library";
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
   165
                if (!message.isEmpty())
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
   166
                    msg += ": " + message;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
   167
                throw new AgentLoadException(msg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            } else {
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
   169
                if (message.isEmpty())
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
   170
                    message = "Command failed in target VM";
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
   171
                throw new AttachOperationFailedException(message);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        // Return the input stream so that the command output can be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return sis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    // InputStream over a socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private class SocketInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        int s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        public SocketInputStream(int s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            this.s = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        public synchronized int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            byte b[] = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            int n = this.read(b, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            if (n == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                return b[0] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        public synchronized int read(byte[] bs, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if ((off < 0) || (off > bs.length) || (len < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                ((off + len) > bs.length) || ((off + len) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            } else if (len == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   204
            return VirtualMachineImpl.read(s, bs, off, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        public void close() throws IOException {
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   208
            VirtualMachineImpl.close(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
11692
dd4289e8b9e3 7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents: 7668
diff changeset
   212
    // The door is attached to .java_pid<pid> in the temporary directory.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private int openDoor(int pid) throws IOException {
48485
258a4dab74a7 8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents: 47216
diff changeset
   214
        socket_path = tmpdir + "/.java_pid" + pid;
258a4dab74a7 8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents: 47216
diff changeset
   215
        fd = open(socket_path);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        // Check that the file owner/permission to avoid attaching to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        // bogus process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        try {
48485
258a4dab74a7 8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents: 47216
diff changeset
   220
            checkPermissions(socket_path);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
49742
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 48485
diff changeset
   228
    // On Solaris a simple handshake is used to start the attach mechanism
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    // if not already started. The client creates a .attach_pid<pid> file in the
5771
1f415c7e277d 6950927: Testcase failure sun/management/jmxremote/bootstrap/JvmstatCountersTest.java
alanb
parents: 5506
diff changeset
   230
    // target VM's working directory (or temporary directory), and the SIGQUIT
1f415c7e277d 6950927: Testcase failure sun/management/jmxremote/bootstrap/JvmstatCountersTest.java
alanb
parents: 5506
diff changeset
   231
    // handler checks for the file.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    private File createAttachFile(int pid) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        String fn = ".attach_pid" + pid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        String path = "/proc/" + pid + "/cwd/" + fn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        File f = new File(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            f.createNewFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        } catch (IOException x) {
5771
1f415c7e277d 6950927: Testcase failure sun/management/jmxremote/bootstrap/JvmstatCountersTest.java
alanb
parents: 5506
diff changeset
   239
            f = new File(tmpdir, fn);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            f.createNewFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    //-- native methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    static native int open(String path) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    static native void close(int fd) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    static native int read(int fd, byte buf[], int off, int buflen) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    static native void checkPermissions(String path) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    static native void sigquit(int pid) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    // enqueue a command (and arguments) to the given door
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    static native int enqueue(int fd, String cmd, Object ... args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        System.loadLibrary("attach");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
}