jdk/src/share/classes/sun/tools/attach/HotSpotVirtualMachine.java
author sla
Wed, 11 Jun 2014 15:47:53 +0200
changeset 24870 5d567113d043
parent 24363 33b869a8806b
permissions -rw-r--r--
8044135: Add API to start JMX agent from attach framework Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
24363
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
     2
 * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
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;
24870
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
    36
import java.util.stream.Collectors;
2
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
24870
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   164
    private static final String MANAGMENT_PREFIX = "com.sun.management.";
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   165
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   166
    private static boolean checkedKeyName(Object key) {
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   167
        if (!(key instanceof String)) {
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   168
            throw new IllegalArgumentException("Invalid option (not a String): "+key);
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   169
        }
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   170
        if (!((String)key).startsWith(MANAGMENT_PREFIX)) {
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   171
            throw new IllegalArgumentException("Invalid option: "+key);
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   172
        }
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   173
        return true;
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   174
    }
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   175
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   176
    private static String stripKeyName(Object key) {
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   177
        return ((String)key).substring(MANAGMENT_PREFIX.length());
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   178
    }
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   179
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   180
    @Override
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   181
    public void startManagementAgent(Properties agentProperties) throws IOException {
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   182
        if (agentProperties == null) {
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   183
            throw new NullPointerException("agentProperties cannot be null");
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   184
        }
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   185
        // Convert the arguments into arguments suitable for the Diagnostic Command:
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   186
        // "ManagementAgent.start jmxremote.port=5555 jmxremote.authenticate=false"
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   187
        String args = agentProperties.entrySet().stream()
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   188
            .filter(entry -> checkedKeyName(entry.getKey()))
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   189
            .map(entry -> stripKeyName(entry.getKey()) + "=" + escape(entry.getValue()))
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   190
            .collect(Collectors.joining(" "));
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   191
        executeJCmd("ManagementAgent.start " + args);
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   192
    }
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   193
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   194
    private String escape(Object arg) {
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   195
        String value = arg.toString();
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   196
        if (value.contains(" ")) {
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   197
            return "'" + value + "'";
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   198
        }
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   199
        return value;
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   200
    }
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   201
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   202
    @Override
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   203
    public String startLocalManagementAgent() throws IOException {
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   204
        executeJCmd("ManagementAgent.start_local");
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   205
        return getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   206
    }
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   207
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    // --- HotSpot specific methods ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    // same as SIGQUIT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public void localDataDump() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        executeCommand("datadump").close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    // Remote ctrl-break. The output of the ctrl-break actions can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    // be read from the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public InputStream remoteDataDump(Object ... args) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return executeCommand("threaddump", args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    // Remote heap dump. The output (error message) can be read from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    // returned input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public InputStream dumpHeap(Object ... args) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return executeCommand("dumpheap", args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    // Heap histogram (heap inspection in HotSpot)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public InputStream heapHisto(Object ... args) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return executeCommand("inspectheap", args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    // set JVM command line flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public InputStream setFlag(String name, String value) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return executeCommand("setflag", name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    // print command line flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public InputStream printFlag(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return executeCommand("printflag", name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
11365
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 10419
diff changeset
   242
    public InputStream executeJCmd(String command) throws IOException {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 10419
diff changeset
   243
        return executeCommand("jcmd", command);
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 10419
diff changeset
   244
    }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 10419
diff changeset
   245
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    // -- Supporting methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Execute the given command in the target VM - specific platform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * implementation must implement this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    abstract InputStream execute(String cmd, Object ... args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        throws AgentLoadException, IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Convenience method for simple commands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    private InputStream executeCommand(String cmd, Object ... args) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return execute(cmd, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        } catch (AgentLoadException x) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   263
            throw new InternalError("Should not get here", x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Utility method to read an 'int' from the input stream. Ideally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * we should be using java.util.Scanner here but this implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * guarantees not to read ahead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    int readInt(InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        // read to \n or EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        byte buf[] = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            n = in.read(buf, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                char c = (char)buf[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                if (c == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    break;                  // EOL found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    sb.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        } while (n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (sb.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            throw new IOException("Premature EOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            value = Integer.parseInt(sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        } catch (NumberFormatException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            throw new IOException("Non-numeric value found - int expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
24363
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   304
    /*
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   305
     * Utility method to read data into a String.
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   306
     */
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   307
    String readErrorMessage(InputStream sis) throws IOException {
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   308
        byte b[] = new byte[1024];
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   309
        int n;
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   310
        StringBuffer message = new StringBuffer();
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   311
        while ((n = sis.read(b)) != -1) {
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   312
            message.append(new String(b, 0, n, "UTF-8"));
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   313
        }
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   314
        return message.toString();
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   315
    }
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   316
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   317
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    // -- attach timeout support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    private static long defaultAttachTimeout = 5000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    private volatile long attachTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Return attach timeout based on the value of the sun.tools.attach.attachTimeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * property, or the default timeout if the property is not set to a positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    long attachTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (attachTimeout == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                if (attachTimeout == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                        String s =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                            System.getProperty("sun.tools.attach.attachTimeout");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                        attachTimeout = Long.parseLong(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    } catch (NumberFormatException ne) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    if (attachTimeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                       attachTimeout = defaultAttachTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return attachTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
}