src/jdk.attach/linux/classes/sun/tools/attach/VirtualMachineImpl.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 45004 jdk/src/jdk.attach/linux/classes/sun/tools/attach/VirtualMachineImpl.java@ea3137042a61
child 47540 7242ab15b6a5
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
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: 14342
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: 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Linux implementation of HotSpotVirtualMachine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
    39
public class VirtualMachineImpl extends HotSpotVirtualMachine {
11692
dd4289e8b9e3 7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents: 10419
diff changeset
    40
    // "/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: 10419
diff changeset
    41
    // .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: 10419
diff changeset
    42
    // location is the same for all processes, otherwise the tools
dd4289e8b9e3 7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents: 10419
diff changeset
    43
    // will not be able to find all Hotspot processes.
dd4289e8b9e3 7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents: 10419
diff changeset
    44
    // Any changes to this needs to be synchronized with HotSpot.
dd4289e8b9e3 7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents: 10419
diff changeset
    45
    private static final String tmpdir = "/tmp";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // The patch to the socket file created by the target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    String path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Attaches to the target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
    53
    VirtualMachineImpl(AttachProvider provider, String vmid)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        throws AttachNotSupportedException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        super(provider, vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        // This provider only understands pids
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        int pid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            pid = Integer.parseInt(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        } catch (NumberFormatException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            throw new AttachNotSupportedException("Invalid process identifier");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        // Find the socket file. If not found then we attempt to start the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        // attach mechanism in the target VM by sending it a QUIT signal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        // Then we attempt to find the socket file again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        path = findSocketFile(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (path == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            File f = createAttachFile(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            try {
40685
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
    73
                sendQuitTo(pid);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                // 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
    76
                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
    77
                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
    78
                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
    79
                long delay = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                do {
40685
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
    81
                    // 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
    82
                    delay += delay_step;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                        Thread.sleep(delay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    } catch (InterruptedException x) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    path = findSocketFile(pid);
40685
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
    87
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
    88
                    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
    89
                    if (time_spend > timeout/2 && path == null) {
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
    90
                        // 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
    91
                        sendQuitTo(pid);
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
                } while (time_spend <= timeout && path == null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                if (path == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    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
    96
                        String.format("Unable to open socket file %s: " +
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
    97
                          "target process %d doesn't respond within %dms " +
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
    98
                          "or HotSpot VM not loaded", f.getPath(), pid, time_spend));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                f.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
40685
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 26216
diff changeset
   103
      }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        // Check that the file owner/permission to avoid attaching to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // bogus process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        checkPermissions(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        // Check that we can connect to the process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // - this ensures we throw the permission denied error now rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        // later when we attempt to enqueue a command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        int s = socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            connect(s, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            close(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Detach from the target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public void detach() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if (this.path != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                this.path = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    // protocol version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private final static String PROTOCOL_VERSION = "1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    // known errors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private final static int ATTACH_ERROR_BADVERSION = 101;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Execute the given command in the target VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        assert args.length <= 3;                // includes null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // did we detach?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        String p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (this.path == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                throw new IOException("Detached from target VM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            p = this.path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        // create UNIX socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        int s = socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // connect to target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            connect(s, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            close(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        IOException ioe = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // connected - write request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        // <ver> <cmd> <args...>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            writeString(s, PROTOCOL_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            writeString(s, cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            for (int i=0; i<3; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                if (i < args.length && args[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    writeString(s, (String)args[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    writeString(s, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            ioe = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // Create an input stream to read reply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        SocketInputStream sis = new SocketInputStream(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        // Read the command completion status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        int completionStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            completionStatus = readInt(sis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            sis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            if (ioe != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (completionStatus != 0) {
24363
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   200
            // 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
   201
            String message = readErrorMessage(sis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            sis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            // In the event of a protocol mismatch then the target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            // returns a known error so that we can throw a reasonable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            // error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            if (completionStatus == ATTACH_ERROR_BADVERSION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                throw new IOException("Protocol mismatch with target VM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            // Special-case the "load" command so that the right exception is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            // thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            if (cmd.equals("load")) {
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40943
diff changeset
   214
                String msg = "Failed to load agent library";
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40943
diff changeset
   215
                if (!message.isEmpty())
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40943
diff changeset
   216
                    msg += ": " + message;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40943
diff changeset
   217
                throw new AgentLoadException(msg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            } else {
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40943
diff changeset
   219
                if (message.isEmpty())
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40943
diff changeset
   220
                    message = "Command failed in target VM";
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40943
diff changeset
   221
                throw new AttachOperationFailedException(message);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        // Return the input stream so that the command output can be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return sis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * InputStream for the socket connection to get target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    private class SocketInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        int s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        public SocketInputStream(int s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            this.s = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        public synchronized int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            byte b[] = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            int n = this.read(b, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            if (n == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                return b[0] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        public synchronized int read(byte[] bs, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            if ((off < 0) || (off > bs.length) || (len < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                ((off + len) > bs.length) || ((off + len) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            } else if (len == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   256
            return VirtualMachineImpl.read(s, bs, off, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        public void close() throws IOException {
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   260
            VirtualMachineImpl.close(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    // Return the socket file for the given process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    private String findSocketFile(int pid) {
11692
dd4289e8b9e3 7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents: 10419
diff changeset
   266
        File f = new File(tmpdir, ".java_pid" + pid);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (!f.exists()) {
11692
dd4289e8b9e3 7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents: 10419
diff changeset
   268
            return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
11692
dd4289e8b9e3 7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents: 10419
diff changeset
   270
        return f.getPath();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    // On Solaris/Linux a simple handshake is used to start the attach mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    // 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
   275
    // target VM's working directory (or temp directory), and the SIGQUIT handler
1f415c7e277d 6950927: Testcase failure sun/management/jmxremote/bootstrap/JvmstatCountersTest.java
alanb
parents: 5506
diff changeset
   276
    // checks for the file.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    private File createAttachFile(int pid) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        String fn = ".attach_pid" + pid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        String path = "/proc/" + pid + "/cwd/" + fn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        File f = new File(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            f.createNewFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        } catch (IOException x) {
5771
1f415c7e277d 6950927: Testcase failure sun/management/jmxremote/bootstrap/JvmstatCountersTest.java
alanb
parents: 5506
diff changeset
   284
            f = new File(tmpdir, fn);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            f.createNewFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Write/sends the given to the target VM. String is transmitted in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * UTF-8 encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    private void writeString(int fd, String s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (s.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            byte b[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                b = s.getBytes("UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            } catch (java.io.UnsupportedEncodingException x) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 7668
diff changeset
   300
                throw new InternalError(x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            }
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   302
            VirtualMachineImpl.write(fd, b, 0, b.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        byte b[] = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        b[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        write(fd, b, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    //-- native methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    static native void sendQuitToChildrenOf(int pid) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    static native void sendQuitTo(int pid) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    static native void checkPermissions(String path) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    static native int socket() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    static native void connect(int fd, String path) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    static native void close(int fd) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    static native int read(int fd, byte buf[], int off, int bufLen) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    static native void write(int fd, byte buf[], int off, int bufLen) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        System.loadLibrary("attach");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
}