src/jdk.attach/share/classes/sun/tools/attach/HotSpotVirtualMachine.java
author gromero
Tue, 04 Sep 2018 11:46:23 -0400
changeset 51629 78c7f0c7827d
parent 48629 a58c1924e037
permissions -rw-r--r--
8210320: PPC64: Fix uninitialized variable in C1 LIR assembler code Reviewed-by: mbaesken, shade, mdoerr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48629
a58c1924e037 6640188: Methods com.cun.attach.VirtualMachine.load... don't throw NullPointerxception
gadams
parents: 48150
diff changeset
     2
 * Copyright (c) 2005, 2018, 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
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    28
import com.sun.tools.attach.AttachNotSupportedException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.tools.attach.VirtualMachine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.tools.attach.AgentLoadException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.attach.AgentInitializationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.attach.spi.AttachProvider;
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    33
import jdk.internal.misc.VM;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
26460
68a3e0abedfd 8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents: 25859
diff changeset
    35
import java.io.BufferedReader;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.IOException;
26460
68a3e0abedfd 8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents: 25859
diff changeset
    38
import java.io.InputStreamReader;
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    39
import java.security.AccessController;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    40
import java.security.PrivilegedAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Properties;
24870
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
    42
import java.util.stream.Collectors;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * The HotSpot implementation of com.sun.tools.attach.VirtualMachine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public abstract class HotSpotVirtualMachine extends VirtualMachine {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    50
    private static final long CURRENT_PID;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    51
    private static final boolean ALLOW_ATTACH_SELF;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    52
    static {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    53
        PrivilegedAction<ProcessHandle> pa = ProcessHandle::current;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    54
        CURRENT_PID = AccessController.doPrivileged(pa).pid();
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    55
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    56
        String s = VM.getSavedProperty("jdk.attach.allowAttachSelf");
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    57
        ALLOW_ATTACH_SELF = "".equals(s) || Boolean.parseBoolean(s);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    58
    }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    59
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    60
    HotSpotVirtualMachine(AttachProvider provider, String id)
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    61
        throws AttachNotSupportedException, IOException
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    62
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        super(provider, id);
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    64
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    65
        int pid;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    66
        try {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    67
            pid = Integer.parseInt(id);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    68
        } catch (NumberFormatException e) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    69
            throw new AttachNotSupportedException("Invalid process identifier");
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    70
        }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    71
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    72
        // The tool should be a different VM to the target. This check will
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    73
        // eventually be enforced by the target VM.
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    74
        if (!ALLOW_ATTACH_SELF && (pid == 0 || pid == CURRENT_PID)) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    75
            throw new IOException("Can not attach to current VM");
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
    76
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Load agent library
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * If isAbsolute is true then the agent library is the absolute path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * to the library and thus will not be expanded in the target VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * if isAbsolute is false then the agent library is just a library
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * name and it will be expended in the target VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private void loadAgentLibrary(String agentLibrary, boolean isAbsolute, String options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        throws AgentLoadException, AgentInitializationException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    {
48629
a58c1924e037 6640188: Methods com.cun.attach.VirtualMachine.load... don't throw NullPointerxception
gadams
parents: 48150
diff changeset
    89
        if (agentLibrary == null) {
a58c1924e037 6640188: Methods com.cun.attach.VirtualMachine.load... don't throw NullPointerxception
gadams
parents: 48150
diff changeset
    90
            throw new NullPointerException("agentLibrary cannot be null");
a58c1924e037 6640188: Methods com.cun.attach.VirtualMachine.load... don't throw NullPointerxception
gadams
parents: 48150
diff changeset
    91
        }
a58c1924e037 6640188: Methods com.cun.attach.VirtualMachine.load... don't throw NullPointerxception
gadams
parents: 48150
diff changeset
    92
48150
bc1cffa26561 8165736: Error message should be shown when JVMTI agent cannot be attached
ysuenaga
parents: 47216
diff changeset
    93
        String msgPrefix = "return code: ";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        InputStream in = execute("load",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                 agentLibrary,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                 isAbsolute ? "true" : "false",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                 options);
48150
bc1cffa26561 8165736: Error message should be shown when JVMTI agent cannot be attached
ysuenaga
parents: 47216
diff changeset
    98
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
bc1cffa26561 8165736: Error message should be shown when JVMTI agent cannot be attached
ysuenaga
parents: 47216
diff changeset
    99
            String result = reader.readLine();
bc1cffa26561 8165736: Error message should be shown when JVMTI agent cannot be attached
ysuenaga
parents: 47216
diff changeset
   100
            if (result == null) {
bc1cffa26561 8165736: Error message should be shown when JVMTI agent cannot be attached
ysuenaga
parents: 47216
diff changeset
   101
                throw new AgentLoadException("Target VM did not respond");
bc1cffa26561 8165736: Error message should be shown when JVMTI agent cannot be attached
ysuenaga
parents: 47216
diff changeset
   102
            } else if (result.startsWith(msgPrefix)) {
bc1cffa26561 8165736: Error message should be shown when JVMTI agent cannot be attached
ysuenaga
parents: 47216
diff changeset
   103
                int retCode = Integer.parseInt(result.substring(msgPrefix.length()));
bc1cffa26561 8165736: Error message should be shown when JVMTI agent cannot be attached
ysuenaga
parents: 47216
diff changeset
   104
                if (retCode != 0) {
bc1cffa26561 8165736: Error message should be shown when JVMTI agent cannot be attached
ysuenaga
parents: 47216
diff changeset
   105
                    throw new AgentInitializationException("Agent_OnAttach failed", retCode);
bc1cffa26561 8165736: Error message should be shown when JVMTI agent cannot be attached
ysuenaga
parents: 47216
diff changeset
   106
                }
bc1cffa26561 8165736: Error message should be shown when JVMTI agent cannot be attached
ysuenaga
parents: 47216
diff changeset
   107
            } else {
bc1cffa26561 8165736: Error message should be shown when JVMTI agent cannot be attached
ysuenaga
parents: 47216
diff changeset
   108
                throw new AgentLoadException(result);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Load agent library - library name will be expanded in target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public void loadAgentLibrary(String agentLibrary, String options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        throws AgentLoadException, AgentInitializationException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        loadAgentLibrary(agentLibrary, false, options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Load agent - absolute path of library provided to target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public void loadAgentPath(String agentLibrary, String options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        throws AgentLoadException, AgentInitializationException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        loadAgentLibrary(agentLibrary, true, options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Load JPLIS agent which will load the agent JAR file and invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * the agentmain method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public void loadAgent(String agent, String options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        throws AgentLoadException, AgentInitializationException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    {
48629
a58c1924e037 6640188: Methods com.cun.attach.VirtualMachine.load... don't throw NullPointerxception
gadams
parents: 48150
diff changeset
   138
        if (agent == null) {
a58c1924e037 6640188: Methods com.cun.attach.VirtualMachine.load... don't throw NullPointerxception
gadams
parents: 48150
diff changeset
   139
            throw new NullPointerException("agent cannot be null");
a58c1924e037 6640188: Methods com.cun.attach.VirtualMachine.load... don't throw NullPointerxception
gadams
parents: 48150
diff changeset
   140
        }
a58c1924e037 6640188: Methods com.cun.attach.VirtualMachine.load... don't throw NullPointerxception
gadams
parents: 48150
diff changeset
   141
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        String args = agent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (options != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            args = args + "=" + options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            loadAgentLibrary("instrument", args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } catch (AgentInitializationException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
             * Translate interesting errors into the right exception and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
             * message (FIXME: create a better interface to the instrument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
             * implementation so this isn't necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            int rc = x.returnValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            switch (rc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                case JNI_ENOMEM:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    throw new AgentLoadException("Insuffient memory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                case ATTACH_ERROR_BADJAR:
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
   159
                    throw new AgentLoadException(
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
   160
                        "Agent JAR not found or no Agent-Class attribute");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                case ATTACH_ERROR_NOTONCP:
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
   162
                    throw new AgentLoadException(
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
   163
                        "Unable to add JAR file to system class path");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                case ATTACH_ERROR_STARTFAIL:
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
   165
                    throw new AgentInitializationException(
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
   166
                        "Agent JAR loaded but agent failed to initialize");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                default :
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
   168
                    throw new AgentLoadException("" +
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
   169
                        "Failed to load agent - unknown reason: " + rc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * The possible errors returned by JPLIS's agentmain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    private static final int JNI_ENOMEM                 = -4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private static final int ATTACH_ERROR_BADJAR        = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    private static final int ATTACH_ERROR_NOTONCP       = 101;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private static final int ATTACH_ERROR_STARTFAIL     = 102;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Send "properties" command to target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public Properties getSystemProperties() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        InputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        Properties props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            in = executeCommand("properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            props.load(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            if (in != null) in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return props;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public Properties getAgentProperties() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        InputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        Properties props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            in = executeCommand("agentProperties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            props.load(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if (in != null) in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return props;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
   210
    private static final String MANAGEMENT_PREFIX = "com.sun.management.";
24870
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   211
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   212
    private static boolean checkedKeyName(Object key) {
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   213
        if (!(key instanceof String)) {
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   214
            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
   215
        }
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
   216
        if (!((String)key).startsWith(MANAGEMENT_PREFIX)) {
24870
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   217
            throw new IllegalArgumentException("Invalid option: "+key);
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   218
        }
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   219
        return true;
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   220
    }
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   221
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   222
    private static String stripKeyName(Object key) {
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
   223
        return ((String)key).substring(MANAGEMENT_PREFIX.length());
24870
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   224
    }
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   225
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   226
    @Override
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   227
    public void startManagementAgent(Properties agentProperties) throws IOException {
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   228
        if (agentProperties == null) {
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   229
            throw new NullPointerException("agentProperties cannot be null");
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   230
        }
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   231
        // 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
   232
        // "ManagementAgent.start jmxremote.port=5555 jmxremote.authenticate=false"
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   233
        String args = agentProperties.entrySet().stream()
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   234
            .filter(entry -> checkedKeyName(entry.getKey()))
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   235
            .map(entry -> stripKeyName(entry.getKey()) + "=" + escape(entry.getValue()))
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   236
            .collect(Collectors.joining(" "));
26460
68a3e0abedfd 8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents: 25859
diff changeset
   237
        executeJCmd("ManagementAgent.start " + args).close();
24870
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   238
    }
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   239
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   240
    private String escape(Object arg) {
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   241
        String value = arg.toString();
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   242
        if (value.contains(" ")) {
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   243
            return "'" + value + "'";
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   244
        }
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   245
        return value;
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   246
    }
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   247
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   248
    @Override
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   249
    public String startLocalManagementAgent() throws IOException {
26460
68a3e0abedfd 8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents: 25859
diff changeset
   250
        executeJCmd("ManagementAgent.start_local").close();
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
   251
        String prop = MANAGEMENT_PREFIX + "jmxremote.localConnectorAddress";
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
   252
        return getAgentProperties().getProperty(prop);
24870
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   253
    }
5d567113d043 8044135: Add API to start JMX agent from attach framework
sla
parents: 24363
diff changeset
   254
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
   255
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    // --- HotSpot specific methods ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    // same as SIGQUIT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public void localDataDump() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        executeCommand("datadump").close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    // Remote ctrl-break. The output of the ctrl-break actions can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    // be read from the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public InputStream remoteDataDump(Object ... args) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return executeCommand("threaddump", args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    // Remote heap dump. The output (error message) can be read from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    // returned input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public InputStream dumpHeap(Object ... args) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        return executeCommand("dumpheap", args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    // Heap histogram (heap inspection in HotSpot)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public InputStream heapHisto(Object ... args) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        return executeCommand("inspectheap", args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    // set JVM command line flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public InputStream setFlag(String name, String value) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        return executeCommand("setflag", name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    // print command line flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public InputStream printFlag(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        return executeCommand("printflag", name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
11365
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 10419
diff changeset
   290
    public InputStream executeJCmd(String command) throws IOException {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 10419
diff changeset
   291
        return executeCommand("jcmd", command);
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 10419
diff changeset
   292
    }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 10419
diff changeset
   293
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
   294
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    // -- Supporting methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Execute the given command in the target VM - specific platform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * implementation must implement this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    abstract InputStream execute(String cmd, Object ... args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        throws AgentLoadException, IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * Convenience method for simple commands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     */
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 26460
diff changeset
   307
    public InputStream executeCommand(String cmd, Object ... args) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            return execute(cmd, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        } catch (AgentLoadException x) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   311
            throw new InternalError("Should not get here", x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * Utility method to read an 'int' from the input stream. Ideally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * we should be using java.util.Scanner here but this implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * guarantees not to read ahead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    int readInt(InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        // read to \n or EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        byte buf[] = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            n = in.read(buf, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                char c = (char)buf[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                if (c == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    break;                  // EOL found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    sb.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        } while (n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if (sb.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            throw new IOException("Premature EOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            value = Integer.parseInt(sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        } catch (NumberFormatException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            throw new IOException("Non-numeric value found - int expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
24363
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   352
    /*
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   353
     * 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
   354
     */
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
   355
    String readErrorMessage(InputStream in) throws IOException {
26460
68a3e0abedfd 8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents: 25859
diff changeset
   356
        String s;
68a3e0abedfd 8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents: 25859
diff changeset
   357
        StringBuilder message = new StringBuilder();
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 40685
diff changeset
   358
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
26460
68a3e0abedfd 8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents: 25859
diff changeset
   359
        while ((s = br.readLine()) != null) {
68a3e0abedfd 8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents: 25859
diff changeset
   360
            message.append(s);
24363
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   361
        }
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   362
        return message.toString();
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   363
    }
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   364
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 14342
diff changeset
   365
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    // -- attach timeout support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
40685
e6f3a9fff607 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 38360
diff changeset
   368
    private static long defaultAttachTimeout = 10000;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    private volatile long attachTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Return attach timeout based on the value of the sun.tools.attach.attachTimeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * property, or the default timeout if the property is not set to a positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    long attachTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        if (attachTimeout == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                if (attachTimeout == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                        String s =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                            System.getProperty("sun.tools.attach.attachTimeout");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                        attachTimeout = Long.parseLong(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    } catch (NumberFormatException ne) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    if (attachTimeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                       attachTimeout = defaultAttachTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        return attachTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
}