jdk/src/solaris/classes/sun/tools/attach/LinuxVirtualMachine.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.tools.attach;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import com.sun.tools.attach.VirtualMachine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.tools.attach.AgentLoadException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.tools.attach.AttachNotSupportedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.tools.attach.spi.AttachProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Properties;
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
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class LinuxVirtualMachine extends HotSpotVirtualMachine {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    // Indicates if this machine uses the old LinuxThreads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static boolean isLinuxThreads;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    // The patch to the socket file created by the target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    String path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * Attaches to the target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    LinuxVirtualMachine(AttachProvider provider, String vmid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        throws AttachNotSupportedException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        super(provider, vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        // This provider only understands pids
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        int pid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            pid = Integer.parseInt(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        } catch (NumberFormatException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            throw new AttachNotSupportedException("Invalid process identifier");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        // Find the socket file. If not found then we attempt to start the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        // attach mechanism in the target VM by sending it a QUIT signal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        // Then we attempt to find the socket file again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        path = findSocketFile(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (path == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            File f = createAttachFile(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                // On LinuxThreads each thread is a process and we don't have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                // pid of the VMThread which has SIGQUIT unblocked. To workaround
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                // this we get the pid of the "manager thread" that is created
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                // by the first call to pthread_create. This is parent of all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                // threads (except the initial thread).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                if (isLinuxThreads) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    int mpid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                        mpid = getLinuxThreadsManager(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                        throw new AttachNotSupportedException(x.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    assert(mpid >= 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    sendQuitToChildrenOf(mpid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    sendQuitTo(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                // give the target VM time to start the attach mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                long delay = 200;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                int retries = (int)(attachTimeout() / delay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                        Thread.sleep(delay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    } catch (InterruptedException x) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    path = findSocketFile(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                } while (i <= retries && path == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                if (path == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    throw new AttachNotSupportedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                        "Unable to open socket file: target process not responding " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        "or HotSpot VM not loaded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                f.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        // Check that the file owner/permission to avoid attaching to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // bogus process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        checkPermissions(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // Check that we can connect to the process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // - this ensures we throw the permission denied error now rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // later when we attempt to enqueue a command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        int s = socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            connect(s, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            close(s);
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
     * Detach from the target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public void detach() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            if (this.path != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                this.path = null;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    // protocol version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private final static String PROTOCOL_VERSION = "1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    // known errors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private final static int ATTACH_ERROR_BADVERSION = 101;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Execute the given command in the target VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        assert args.length <= 3;                // includes null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        // did we detach?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        String p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (this.path == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                throw new IOException("Detached from target VM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            p = this.path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // create UNIX socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        int s = socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        // connect to target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            connect(s, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            close(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        IOException ioe = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        // connected - write request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        // <ver> <cmd> <args...>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            writeString(s, PROTOCOL_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            writeString(s, cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            for (int i=0; i<3; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                if (i < args.length && args[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    writeString(s, (String)args[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    writeString(s, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            ioe = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        // Create an input stream to read reply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        SocketInputStream sis = new SocketInputStream(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        // Read the command completion status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        int completionStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            completionStatus = readInt(sis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            sis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            if (ioe != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (completionStatus != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            sis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            // In the event of a protocol mismatch then the target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            // returns a known error so that we can throw a reasonable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            // error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            if (completionStatus == ATTACH_ERROR_BADVERSION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                throw new IOException("Protocol mismatch with target VM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            // Special-case the "load" command so that the right exception is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            // thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            if (cmd.equals("load")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                throw new AgentLoadException("Failed to load agent library");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                throw new IOException("Command failed in target VM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        // Return the input stream so that the command output can be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return sis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * InputStream for the socket connection to get target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    private class SocketInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        int s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        public SocketInputStream(int s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            this.s = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        public synchronized int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            byte b[] = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            int n = this.read(b, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (n == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                return b[0] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        public synchronized int read(byte[] bs, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            if ((off < 0) || (off > bs.length) || (len < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                ((off + len) > bs.length) || ((off + len) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            } else if (len == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return LinuxVirtualMachine.read(s, bs, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            LinuxVirtualMachine.close(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    // Return the socket file for the given process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    // Checks working directory of process for .java_pid<pid>. If not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    // found it looks in /tmp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    private String findSocketFile(int pid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        // First check for a .java_pid<pid> file in the working directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        // of the target process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        String fn = ".java_pid" + pid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        String path = "/proc/" + pid + "/cwd/" + fn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        File f = new File(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (!f.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            // Not found, so try /tmp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            path = "/tmp/" + fn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            f = new File(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (!f.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                return null;            // not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    // On Solaris/Linux a simple handshake is used to start the attach mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    // if not already started. The client creates a .attach_pid<pid> file in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    // target VM's working directory (or /tmp), and the SIGQUIT handler checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    // for the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    private File createAttachFile(int pid) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        String fn = ".attach_pid" + pid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        String path = "/proc/" + pid + "/cwd/" + fn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        File f = new File(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            f.createNewFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            path = "/tmp/" + fn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            f = new File(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            f.createNewFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Write/sends the given to the target VM. String is transmitted in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * UTF-8 encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    private void writeString(int fd, String s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (s.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            byte b[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                b = s.getBytes("UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            } catch (java.io.UnsupportedEncodingException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            LinuxVirtualMachine.write(fd, b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        byte b[] = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        b[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        write(fd, b, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    //-- native methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    static native boolean isLinuxThreads();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    static native int getLinuxThreadsManager(int pid) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    static native void sendQuitToChildrenOf(int pid) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    static native void sendQuitTo(int pid) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    static native void checkPermissions(String path) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    static native int socket() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    static native void connect(int fd, String path) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    static native void close(int fd) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    static native int read(int fd, byte buf[], int off, int bufLen) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    static native void write(int fd, byte buf[], int off, int bufLen) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        System.loadLibrary("attach");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        isLinuxThreads = isLinuxThreads();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
}