jdk/src/share/classes/sun/tools/attach/HotSpotVirtualMachine.java
author fparain
Wed, 04 Jan 2012 03:49:35 -0800
changeset 11365 05d995976571
parent 10419 12c063b39232
child 14342 8435a30053c1
permissions -rw-r--r--
7104647: Adding a diagnostic command framework Reviewed-by: mchung, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
11365
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 10419
diff changeset
     2
 * Copyright (c) 2005, 2011, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.attach;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.tools.attach.VirtualMachine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.tools.attach.AgentLoadException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.tools.attach.AgentInitializationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.attach.spi.AttachProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The HotSpot implementation of com.sun.tools.attach.VirtualMachine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public abstract class HotSpotVirtualMachine extends VirtualMachine {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    HotSpotVirtualMachine(AttachProvider provider, String id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        super(provider, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Load agent library
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * If isAbsolute is true then the agent library is the absolute path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * to the library and thus will not be expanded in the target VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * if isAbsolute is false then the agent library is just a library
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * name and it will be expended in the target VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private void loadAgentLibrary(String agentLibrary, boolean isAbsolute, String options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        throws AgentLoadException, AgentInitializationException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        InputStream in = execute("load",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                                 agentLibrary,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                                 isAbsolute ? "true" : "false",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                                 options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            int result = readInt(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            if (result != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                throw new AgentInitializationException("Agent_OnAttach failed", result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Load agent library - library name will be expanded in target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public void loadAgentLibrary(String agentLibrary, String options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        throws AgentLoadException, AgentInitializationException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        loadAgentLibrary(agentLibrary, false, options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Load agent - absolute path of library provided to target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public void loadAgentPath(String agentLibrary, String options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        throws AgentLoadException, AgentInitializationException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        loadAgentLibrary(agentLibrary, true, options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Load JPLIS agent which will load the agent JAR file and invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * the agentmain method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public void loadAgent(String agent, String options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        throws AgentLoadException, AgentInitializationException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        String args = agent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (options != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            args = args + "=" + options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            loadAgentLibrary("instrument", args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } catch (AgentLoadException x) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   105
            throw new InternalError("instrument library is missing in target VM", x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        } catch (AgentInitializationException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
             * Translate interesting errors into the right exception and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
             * message (FIXME: create a better interface to the instrument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
             * implementation so this isn't necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            int rc = x.returnValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            switch (rc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                case JNI_ENOMEM:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    throw new AgentLoadException("Insuffient memory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                case ATTACH_ERROR_BADJAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    throw new AgentLoadException("Agent JAR not found or no Agent-Class attribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                case ATTACH_ERROR_NOTONCP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    throw new AgentLoadException("Unable to add JAR file to system class path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                case ATTACH_ERROR_STARTFAIL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    throw new AgentInitializationException("Agent JAR loaded but agent failed to initialize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                default :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    throw new AgentLoadException("Failed to load agent - unknown reason: " + rc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * The possible errors returned by JPLIS's agentmain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private static final int JNI_ENOMEM                 = -4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private static final int ATTACH_ERROR_BADJAR        = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private static final int ATTACH_ERROR_NOTONCP       = 101;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private static final int ATTACH_ERROR_STARTFAIL     = 102;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Send "properties" command to target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public Properties getSystemProperties() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        InputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        Properties props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            in = executeCommand("properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            props.load(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if (in != null) in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return props;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public Properties getAgentProperties() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        InputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        Properties props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            in = executeCommand("agentProperties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            props.load(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if (in != null) in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return props;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    // --- HotSpot specific methods ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    // same as SIGQUIT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public void localDataDump() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        executeCommand("datadump").close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    // Remote ctrl-break. The output of the ctrl-break actions can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    // be read from the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public InputStream remoteDataDump(Object ... args) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return executeCommand("threaddump", args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    // Remote heap dump. The output (error message) can be read from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    // returned input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public InputStream dumpHeap(Object ... args) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return executeCommand("dumpheap", args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    // Heap histogram (heap inspection in HotSpot)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public InputStream heapHisto(Object ... args) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return executeCommand("inspectheap", args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    // set JVM command line flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public InputStream setFlag(String name, String value) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return executeCommand("setflag", name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    // print command line flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public InputStream printFlag(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return executeCommand("printflag", name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
11365
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 10419
diff changeset
   198
    public InputStream executeJCmd(String command) throws IOException {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 10419
diff changeset
   199
        return executeCommand("jcmd", command);
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 10419
diff changeset
   200
    }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 10419
diff changeset
   201
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    // -- Supporting methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Execute the given command in the target VM - specific platform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * implementation must implement this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    abstract InputStream execute(String cmd, Object ... args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        throws AgentLoadException, IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Convenience method for simple commands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    private InputStream executeCommand(String cmd, Object ... args) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            return execute(cmd, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        } catch (AgentLoadException x) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   219
            throw new InternalError("Should not get here", x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
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
     * Utility method to read an 'int' from the input stream. Ideally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * we should be using java.util.Scanner here but this implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * guarantees not to read ahead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    int readInt(InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        // read to \n or EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        byte buf[] = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            n = in.read(buf, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                char c = (char)buf[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                if (c == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    break;                  // EOL found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    sb.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        } while (n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (sb.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            throw new IOException("Premature EOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            value = Integer.parseInt(sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        } catch (NumberFormatException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            throw new IOException("Non-numeric value found - int expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    // -- attach timeout support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    private static long defaultAttachTimeout = 5000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    private volatile long attachTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Return attach timeout based on the value of the sun.tools.attach.attachTimeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * property, or the default timeout if the property is not set to a positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    long attachTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (attachTimeout == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                if (attachTimeout == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                        String s =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                            System.getProperty("sun.tools.attach.attachTimeout");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                        attachTimeout = Long.parseLong(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    } catch (NumberFormatException ne) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    if (attachTimeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                       attachTimeout = defaultAttachTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        return attachTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
}