jdk/src/share/classes/sun/tools/attach/HotSpotVirtualMachine.java
author sherman
Tue, 30 Aug 2011 11:53:11 -0700
changeset 10419 12c063b39232
parent 5506 202f599c92aa
child 11365 05d995976571
permissions -rw-r--r--
7084245: Update usages of InternalError to use exception chaining Summary: to use new InternalError constructor with cause chainning Reviewed-by: alanb, ksrini, xuelei, neugens Contributed-by: sebastian.sickelmann@gmx.de
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, 2006, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    // -- Supporting methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Execute the given command in the target VM - specific platform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * implementation must implement this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    abstract InputStream execute(String cmd, Object ... args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        throws AgentLoadException, IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Convenience method for simple commands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    private InputStream executeCommand(String cmd, Object ... args) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            return execute(cmd, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        } catch (AgentLoadException x) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   215
            throw new InternalError("Should not get here", x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Utility method to read an 'int' from the input stream. Ideally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * we should be using java.util.Scanner here but this implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * guarantees not to read ahead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    int readInt(InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        // read to \n or EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        byte buf[] = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            n = in.read(buf, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                char c = (char)buf[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                if (c == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    break;                  // EOL found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    sb.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        } while (n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (sb.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            throw new IOException("Premature EOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            value = Integer.parseInt(sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        } catch (NumberFormatException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            throw new IOException("Non-numeric value found - int expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    // -- attach timeout support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    private static long defaultAttachTimeout = 5000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    private volatile long attachTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Return attach timeout based on the value of the sun.tools.attach.attachTimeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * property, or the default timeout if the property is not set to a positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    long attachTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (attachTimeout == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                if (attachTimeout == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                        String s =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                            System.getProperty("sun.tools.attach.attachTimeout");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                        attachTimeout = Long.parseLong(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    } catch (NumberFormatException ne) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    if (attachTimeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                       attachTimeout = defaultAttachTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return attachTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
}