jdk/src/jdk.attach/share/classes/sun/tools/attach/HotSpotVirtualMachine.java
author dsamersoff
Mon, 09 May 2016 23:41:59 +0300
changeset 38360 fb63be22ffa6
parent 26460 68a3e0abedfd
child 40685 e6f3a9fff607
permissions -rw-r--r--
8155091: Remove SA related functions from tmtools Summary: Remove options that enables support for non-cooperative mode Reviewed-by: alanb, mchung, sla
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
26460
68a3e0abedfd 8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents: 25859
diff changeset
    33
import java.io.BufferedReader;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.IOException;
26460
68a3e0abedfd 8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents: 25859
diff changeset
    36
import java.io.InputStreamReader;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Properties;
24870
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
    38
import java.util.stream.Collectors;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * The HotSpot implementation of com.sun.tools.attach.VirtualMachine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public abstract class HotSpotVirtualMachine extends VirtualMachine {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    HotSpotVirtualMachine(AttachProvider provider, String id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        super(provider, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Load agent library
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * If isAbsolute is true then the agent library is the absolute path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * to the library and thus will not be expanded in the target VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * if isAbsolute is false then the agent library is just a library
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * name and it will be expended in the target VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private void loadAgentLibrary(String agentLibrary, boolean isAbsolute, String options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        throws AgentLoadException, AgentInitializationException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        InputStream in = execute("load",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                                 agentLibrary,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                                 isAbsolute ? "true" : "false",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                                 options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            int result = readInt(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            if (result != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                throw new AgentInitializationException("Agent_OnAttach failed", result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            in.close();
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Load agent library - library name will be expanded in target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public void loadAgentLibrary(String agentLibrary, String options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        throws AgentLoadException, AgentInitializationException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        loadAgentLibrary(agentLibrary, false, options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Load agent - absolute path of library provided to target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public void loadAgentPath(String agentLibrary, String options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        throws AgentLoadException, AgentInitializationException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        loadAgentLibrary(agentLibrary, true, options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Load JPLIS agent which will load the agent JAR file and invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * the agentmain method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public void loadAgent(String agent, String options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        throws AgentLoadException, AgentInitializationException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        String args = agent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (options != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            args = args + "=" + options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            loadAgentLibrary("instrument", args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        } catch (AgentLoadException x) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   107
            throw new InternalError("instrument library is missing in target VM", x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        } catch (AgentInitializationException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
             * Translate interesting errors into the right exception and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
             * message (FIXME: create a better interface to the instrument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
             * implementation so this isn't necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            int rc = x.returnValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            switch (rc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                case JNI_ENOMEM:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    throw new AgentLoadException("Insuffient memory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                case ATTACH_ERROR_BADJAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    throw new AgentLoadException("Agent JAR not found or no Agent-Class attribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                case ATTACH_ERROR_NOTONCP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    throw new AgentLoadException("Unable to add JAR file to system class path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                case ATTACH_ERROR_STARTFAIL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    throw new AgentInitializationException("Agent JAR loaded but agent failed to initialize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                default :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    throw new AgentLoadException("Failed to load agent - unknown reason: " + rc);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * The possible errors returned by JPLIS's agentmain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private static final int JNI_ENOMEM                 = -4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private static final int ATTACH_ERROR_BADJAR        = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private static final int ATTACH_ERROR_NOTONCP       = 101;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private static final int ATTACH_ERROR_STARTFAIL     = 102;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Send "properties" command to target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public Properties getSystemProperties() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        InputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        Properties props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            in = executeCommand("properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            props.load(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (in != null) in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return props;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public Properties getAgentProperties() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        InputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        Properties props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            in = executeCommand("agentProperties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            props.load(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            if (in != null) in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return props;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
24870
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   166
    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
   167
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   168
    private static boolean checkedKeyName(Object key) {
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   169
        if (!(key instanceof String)) {
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   170
            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
   171
        }
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   172
        if (!((String)key).startsWith(MANAGMENT_PREFIX)) {
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   173
            throw new IllegalArgumentException("Invalid option: "+key);
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
        return true;
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   176
    }
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   177
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   178
    private static String stripKeyName(Object key) {
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   179
        return ((String)key).substring(MANAGMENT_PREFIX.length());
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   180
    }
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   181
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   182
    @Override
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   183
    public void startManagementAgent(Properties agentProperties) throws IOException {
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   184
        if (agentProperties == null) {
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   185
            throw new NullPointerException("agentProperties cannot be null");
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   186
        }
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   187
        // 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
   188
        // "ManagementAgent.start jmxremote.port=5555 jmxremote.authenticate=false"
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   189
        String args = agentProperties.entrySet().stream()
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   190
            .filter(entry -> checkedKeyName(entry.getKey()))
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   191
            .map(entry -> stripKeyName(entry.getKey()) + "=" + escape(entry.getValue()))
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   192
            .collect(Collectors.joining(" "));
26460
68a3e0abedfd 8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents: 25859
diff changeset
   193
        executeJCmd("ManagementAgent.start " + args).close();
24870
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   194
    }
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   195
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   196
    private String escape(Object arg) {
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   197
        String value = arg.toString();
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   198
        if (value.contains(" ")) {
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
        return value;
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   202
    }
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   203
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   204
    @Override
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   205
    public String startLocalManagementAgent() throws IOException {
26460
68a3e0abedfd 8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents: 25859
diff changeset
   206
        executeJCmd("ManagementAgent.start_local").close();
24870
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   207
        return getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   208
    }
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   209
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    // --- HotSpot specific methods ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    // same as SIGQUIT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public void localDataDump() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        executeCommand("datadump").close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    // Remote ctrl-break. The output of the ctrl-break actions can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    // be read from the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public InputStream remoteDataDump(Object ... args) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return executeCommand("threaddump", args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    // Remote heap dump. The output (error message) can be read from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    // returned input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public InputStream dumpHeap(Object ... args) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return executeCommand("dumpheap", args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    // Heap histogram (heap inspection in HotSpot)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public InputStream heapHisto(Object ... args) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return executeCommand("inspectheap", args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    // set JVM command line flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public InputStream setFlag(String name, String value) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return executeCommand("setflag", name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    // print command line flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public InputStream printFlag(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return executeCommand("printflag", name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
11365
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 10419
diff changeset
   244
    public InputStream executeJCmd(String command) throws IOException {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 10419
diff changeset
   245
        return executeCommand("jcmd", command);
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 10419
diff changeset
   246
    }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 10419
diff changeset
   247
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    // -- Supporting methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Execute the given command in the target VM - specific platform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * implementation must implement this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    abstract InputStream execute(String cmd, Object ... args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        throws AgentLoadException, IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Convenience method for simple commands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 26460
diff changeset
   261
    public InputStream executeCommand(String cmd, Object ... args) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            return execute(cmd, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        } catch (AgentLoadException x) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   265
            throw new InternalError("Should not get here", x);
2
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Utility method to read an 'int' from the input stream. Ideally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * we should be using java.util.Scanner here but this implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * guarantees not to read ahead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    int readInt(InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        // read to \n or EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        byte buf[] = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            n = in.read(buf, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                char c = (char)buf[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                if (c == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    break;                  // EOL found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    sb.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        } while (n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (sb.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            throw new IOException("Premature EOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            value = Integer.parseInt(sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        } catch (NumberFormatException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            throw new IOException("Non-numeric value found - int expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
24363
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
     * 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
   308
     */
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   309
    String readErrorMessage(InputStream sis) throws IOException {
26460
68a3e0abedfd 8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents: 25859
diff changeset
   310
        String s;
68a3e0abedfd 8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents: 25859
diff changeset
   311
        StringBuilder message = new StringBuilder();
68a3e0abedfd 8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents: 25859
diff changeset
   312
        BufferedReader br = new BufferedReader(new InputStreamReader(sis));
68a3e0abedfd 8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents: 25859
diff changeset
   313
        while ((s = br.readLine()) != null) {
68a3e0abedfd 8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents: 25859
diff changeset
   314
            message.append(s);
24363
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
        return message.toString();
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   317
    }
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   318
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   319
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    // -- attach timeout support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    private static long defaultAttachTimeout = 5000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    private volatile long attachTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Return attach timeout based on the value of the sun.tools.attach.attachTimeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * property, or the default timeout if the property is not set to a positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    long attachTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if (attachTimeout == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                if (attachTimeout == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                        String s =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                            System.getProperty("sun.tools.attach.attachTimeout");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                        attachTimeout = Long.parseLong(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    } catch (NumberFormatException ne) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    if (attachTimeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                       attachTimeout = defaultAttachTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        return attachTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
}