jdk/src/jdk.attach/aix/classes/sun/tools/attach/VirtualMachineImpl.java
author mchung
Tue, 26 Aug 2014 14:35:33 -0700
changeset 26216 5e46c782b43c
parent 25859 jdk/src/jdk.attach/aix/classes/sun/tools/attach/AixVirtualMachine.java@3317bb8137f4
child 26454 014eafd6044d
permissions -rw-r--r--
8055230: Rename attach provider implementation class be platform neutral Reviewed-by: alanb, chegar, dfuchs, dholmes, erikj, sla
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
     1
/*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
     2
 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
     3
 * Copyright 2013 SAP AG. All rights reserved.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
     4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
     5
 *
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
     6
 * This code is free software; you can redistribute it and/or modify it
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
     7
 * under the terms of the GNU General Public License version 2 only, as
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
     8
 * published by the Free Software Foundation.  Oracle designates this
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
     9
 * particular file as subject to the "Classpath" exception as provided
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    10
 * by Oracle in the LICENSE file that accompanied this code.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    11
 *
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    12
 * This code is distributed in the hope that it will be useful, but WITHOUT
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    14
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    15
 * version 2 for more details (a copy is included in the LICENSE file that
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    16
 * accompanied this code).
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    17
 *
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    18
 * You should have received a copy of the GNU General Public License version
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    19
 * 2 along with this work; if not, write to the Free Software Foundation,
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    20
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    21
 *
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    22
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    23
 * or visit www.oracle.com if you need additional information or have any
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    24
 * questions.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    25
 */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    26
package sun.tools.attach;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    27
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    28
import com.sun.tools.attach.VirtualMachine;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    29
import com.sun.tools.attach.AgentLoadException;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    30
import com.sun.tools.attach.AttachNotSupportedException;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    31
import com.sun.tools.attach.spi.AttachProvider;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    32
import java.io.InputStream;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    33
import java.io.IOException;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    34
import java.io.File;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    35
import java.util.Properties;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    36
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
    37
// Based on linux/classes/sun/tools/attach/VirtualMachineImpl.java.
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    38
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    39
/*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    40
 * Aix implementation of HotSpotVirtualMachine
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    41
 */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
    42
public class VirtualMachineImpl extends HotSpotVirtualMachine {
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    43
    // "/tmp" is used as a global well-known location for the files
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    44
    // .java_pid<pid>. and .attach_pid<pid>. It is important that this
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    45
    // location is the same for all processes, otherwise the tools
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    46
    // will not be able to find all Hotspot processes.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    47
    // Any changes to this needs to be synchronized with HotSpot.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    48
    private static final String tmpdir = "/tmp";
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    49
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    50
    // The patch to the socket file created by the target VM
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    51
    String path;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    52
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    53
    /**
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    54
     * Attaches to the target VM
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    55
     */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
    56
    VirtualMachineImpl(AttachProvider provider, String vmid)
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    57
        throws AttachNotSupportedException, IOException
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    58
    {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    59
        super(provider, vmid);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    60
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    61
        // This provider only understands pids
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    62
        int pid;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    63
        try {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    64
            pid = Integer.parseInt(vmid);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    65
        } catch (NumberFormatException x) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    66
            throw new AttachNotSupportedException("Invalid process identifier");
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    67
        }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    68
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    69
        // Find the socket file. If not found then we attempt to start the
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    70
        // attach mechanism in the target VM by sending it a QUIT signal.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    71
        // Then we attempt to find the socket file again.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    72
        path = findSocketFile(pid);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    73
        if (path == null) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    74
            File f = createAttachFile(pid);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    75
            try {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    76
                sendQuitTo(pid);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    77
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    78
                // give the target VM time to start the attach mechanism
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    79
                int i = 0;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    80
                long delay = 200;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    81
                int retries = (int)(attachTimeout() / delay);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    82
                do {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    83
                    try {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    84
                        Thread.sleep(delay);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    85
                    } catch (InterruptedException x) { }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    86
                    path = findSocketFile(pid);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    87
                    i++;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    88
                } while (i <= retries && path == null);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    89
                if (path == null) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    90
                    throw new AttachNotSupportedException(
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    91
                        "Unable to open socket file: target process not responding " +
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    92
                        "or HotSpot VM not loaded");
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    93
                }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    94
            } finally {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    95
                f.delete();
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    96
            }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    97
        }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    98
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    99
        // Check that the file owner/permission to avoid attaching to
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   100
        // bogus process
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   101
        checkPermissions(path);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   102
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   103
        // Check that we can connect to the process
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   104
        // - this ensures we throw the permission denied error now rather than
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   105
        // later when we attempt to enqueue a command.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   106
        int s = socket();
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   107
        try {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   108
            connect(s, path);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   109
        } finally {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   110
            close(s);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   111
        }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   112
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   113
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   114
    /**
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   115
     * Detach from the target VM
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   116
     */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   117
    public void detach() throws IOException {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   118
        synchronized (this) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   119
            if (this.path != null) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   120
                this.path = null;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   121
            }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   122
        }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   123
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   124
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   125
    // protocol version
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   126
    private final static String PROTOCOL_VERSION = "1";
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   127
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   128
    // known errors
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   129
    private final static int ATTACH_ERROR_BADVERSION = 101;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   130
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   131
    /**
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   132
     * Execute the given command in the target VM.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   133
     */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   134
    InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOException {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   135
        assert args.length <= 3;            // includes null
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   136
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   137
        // did we detach?
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   138
        String p;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   139
        synchronized (this) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   140
            if (this.path == null) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   141
                throw new IOException("Detached from target VM");
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   142
            }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   143
            p = this.path;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   144
        }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   145
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   146
        // create UNIX socket
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   147
        int s = socket();
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   148
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   149
        // connect to target VM
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   150
        try {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   151
            connect(s, p);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   152
        } catch (IOException x) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   153
            close(s);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   154
            throw x;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   155
        }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   156
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   157
        IOException ioe = null;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   158
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   159
        // connected - write request
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   160
        // <ver> <cmd> <args...>
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   161
        try {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   162
            writeString(s, PROTOCOL_VERSION);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   163
            writeString(s, cmd);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   164
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   165
            for (int i=0; i<3; i++) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   166
                if (i < args.length && args[i] != null) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   167
                    writeString(s, (String)args[i]);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   168
                } else {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   169
                    writeString(s, "");
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   170
                }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   171
            }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   172
        } catch (IOException x) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   173
            ioe = x;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   174
        }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   175
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   176
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   177
        // Create an input stream to read reply
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   178
        SocketInputStream sis = new SocketInputStream(s);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   179
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   180
        // Read the command completion status
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   181
        int completionStatus;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   182
        try {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   183
            completionStatus = readInt(sis);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   184
        } catch (IOException x) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   185
            sis.close();
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   186
            if (ioe != null) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   187
                throw ioe;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   188
            } else {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   189
                throw x;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   190
            }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   191
        }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   192
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   193
        if (completionStatus != 0) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   194
            sis.close();
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   195
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   196
            // In the event of a protocol mismatch then the target VM
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   197
            // returns a known error so that we can throw a reasonable
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   198
            // error.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   199
            if (completionStatus == ATTACH_ERROR_BADVERSION) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   200
                throw new IOException("Protocol mismatch with target VM");
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   201
            }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   202
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   203
            // Special-case the "load" command so that the right exception is
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   204
            // thrown.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   205
            if (cmd.equals("load")) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   206
                throw new AgentLoadException("Failed to load agent library");
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   207
            } else {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   208
                throw new IOException("Command failed in target VM");
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   209
            }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   210
        }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   211
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   212
        // Return the input stream so that the command output can be read
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   213
        return sis;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   214
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   215
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   216
    /*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   217
     * InputStream for the socket connection to get target VM
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   218
     */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   219
    private class SocketInputStream extends InputStream {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   220
        int s;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   221
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   222
        public SocketInputStream(int s) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   223
            this.s = s;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   224
        }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   225
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   226
        public synchronized int read() throws IOException {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   227
            byte b[] = new byte[1];
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   228
            int n = this.read(b, 0, 1);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   229
            if (n == 1) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   230
                return b[0] & 0xff;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   231
            } else {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   232
                return -1;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   233
            }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   234
        }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   235
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   236
        public synchronized int read(byte[] bs, int off, int len) throws IOException {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   237
            if ((off < 0) || (off > bs.length) || (len < 0) ||
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   238
                ((off + len) > bs.length) || ((off + len) < 0)) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   239
                throw new IndexOutOfBoundsException();
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   240
            } else if (len == 0)
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   241
                return 0;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   242
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   243
            return VirtualMachineImpl.read(s, bs, off, len);
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   244
        }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   245
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   246
        public void close() throws IOException {
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   247
            VirtualMachineImpl.close(s);
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   248
        }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   249
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   250
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   251
    // Return the socket file for the given process.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   252
    private String findSocketFile(int pid) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   253
        File f = new File(tmpdir, ".java_pid" + pid);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   254
        if (!f.exists()) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   255
            return null;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   256
        }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   257
        return f.getPath();
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   258
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   259
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   260
    // On Solaris/Linux/Aix a simple handshake is used to start the attach mechanism
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   261
    // if not already started. The client creates a .attach_pid<pid> file in the
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   262
    // target VM's working directory (or temp directory), and the SIGQUIT handler
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   263
    // checks for the file.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   264
    private File createAttachFile(int pid) throws IOException {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   265
        String fn = ".attach_pid" + pid;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   266
        String path = "/proc/" + pid + "/cwd/" + fn;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   267
        File f = new File(path);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   268
        try {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   269
            f.createNewFile();
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   270
        } catch (IOException x) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   271
            f = new File(tmpdir, fn);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   272
            f.createNewFile();
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   273
        }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   274
        return f;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   275
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   276
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   277
    /*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   278
     * Write/sends the given to the target VM. String is transmitted in
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   279
     * UTF-8 encoding.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   280
     */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   281
    private void writeString(int fd, String s) throws IOException {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   282
        if (s.length() > 0) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   283
            byte b[];
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   284
            try {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   285
                b = s.getBytes("UTF-8");
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   286
            } catch (java.io.UnsupportedEncodingException x) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   287
                throw new InternalError(x);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   288
            }
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   289
            VirtualMachineImpl.write(fd, b, 0, b.length);
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   290
        }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   291
        byte b[] = new byte[1];
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   292
        b[0] = 0;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   293
        write(fd, b, 0, 1);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   294
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   295
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   296
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   297
    //-- native methods
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   298
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   299
    static native void sendQuitTo(int pid) throws IOException;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   300
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   301
    static native void checkPermissions(String path) throws IOException;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   302
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   303
    static native int socket() throws IOException;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   304
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   305
    static native void connect(int fd, String path) throws IOException;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   306
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   307
    static native void close(int fd) throws IOException;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   308
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   309
    static native int read(int fd, byte buf[], int off, int bufLen) throws IOException;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   310
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   311
    static native void write(int fd, byte buf[], int off, int bufLen) throws IOException;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   312
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   313
    static {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   314
        System.loadLibrary("attach");
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   315
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   316
}