Search in sources :

Example 6 with AgentLoadException

use of com.sun.tools.attach.AgentLoadException in project powermock by powermock.

the class LinuxVirtualMachine method execute.

/**
     * Execute the given command in the target VM.
     */
@Override
InputStream execute(String cmd, Object... args) throws AgentLoadException, IOException {
    // includes null
    assert args.length <= 3;
    // did we detach?
    String p;
    synchronized (this) {
        if (this.path == null) {
            throw new IOException("Detached from target VM");
        }
        p = this.path;
    }
    // create UNIX socket
    int s = socket();
    // connect to target VM
    try {
        connect(s, p);
    } catch (IOException x) {
        close(s);
        throw x;
    }
    IOException ioe = null;
    // <ver> <cmd> <args...>
    try {
        writeString(s, PROTOCOL_VERSION);
        writeString(s, cmd);
        for (int i = 0; i < 3; i++) {
            if (i < args.length && args[i] != null) {
                writeString(s, (String) args[i]);
            } else {
                writeString(s, "");
            }
        }
    } catch (IOException x) {
        ioe = x;
    }
    // Create an input stream to read reply
    SocketInputStream sis = new SocketInputStream(s);
    // Read the command completion status
    int completionStatus;
    try {
        completionStatus = readInt(sis);
    } catch (IOException x) {
        sis.close();
        if (ioe != null) {
            throw ioe;
        } else {
            throw x;
        }
    }
    if (completionStatus != 0) {
        sis.close();
        // error.
        if (completionStatus == ATTACH_ERROR_BADVERSION) {
            throw new IOException("Protocol mismatch with target VM");
        }
        // thrown.
        if (cmd.equals("load")) {
            throw new AgentLoadException("Failed to load agent library");
        } else {
            throw new IOException("Command failed in target VM");
        }
    }
    // Return the input stream so that the command output can be read
    return sis;
}
Also used : AgentLoadException(com.sun.tools.attach.AgentLoadException) IOException(java.io.IOException)

Example 7 with AgentLoadException

use of com.sun.tools.attach.AgentLoadException in project powermock by powermock.

the class BsdVirtualMachine method execute.

/**
     * Execute the given command in the target VM.
     */
@Override
InputStream execute(String cmd, Object... args) throws AgentLoadException, IOException {
    // includes null
    assert args.length <= 3;
    // did we detach?
    String p;
    synchronized (this) {
        if (this.path == null) {
            throw new IOException("Detached from target VM");
        }
        p = this.path;
    }
    // create UNIX socket
    int s = socket();
    // connect to target VM
    try {
        connect(s, p);
    } catch (IOException x) {
        close(s);
        throw x;
    }
    IOException ioe = null;
    // <ver> <cmd> <args...>
    try {
        writeString(s, PROTOCOL_VERSION);
        writeString(s, cmd);
        for (int i = 0; i < 3; i++) {
            if (i < args.length && args[i] != null) {
                writeString(s, (String) args[i]);
            } else {
                writeString(s, "");
            }
        }
    } catch (IOException x) {
        ioe = x;
    }
    // Create an input stream to read reply
    SocketInputStream sis = new SocketInputStream(s);
    // Read the command completion status
    int completionStatus;
    try {
        completionStatus = readInt(sis);
    } catch (IOException x) {
        sis.close();
        if (ioe != null) {
            throw ioe;
        } else {
            throw x;
        }
    }
    if (completionStatus != 0) {
        sis.close();
        // error.
        if (completionStatus == ATTACH_ERROR_BADVERSION) {
            throw new IOException("Protocol mismatch with target VM");
        }
        // thrown.
        if (cmd.equals("load")) {
            throw new AgentLoadException("Failed to load agent library");
        } else {
            throw new IOException("Command failed in target VM");
        }
    }
    // Return the input stream so that the command output can be read
    return sis;
}
Also used : AgentLoadException(com.sun.tools.attach.AgentLoadException) IOException(java.io.IOException)

Example 8 with AgentLoadException

use of com.sun.tools.attach.AgentLoadException in project jdk8u_jdk by JetBrains.

the class LinuxVirtualMachine method execute.

/**
     * Execute the given command in the target VM.
     */
InputStream execute(String cmd, Object... args) throws AgentLoadException, IOException {
    // includes null
    assert args.length <= 3;
    // did we detach?
    String p;
    synchronized (this) {
        if (this.path == null) {
            throw new IOException("Detached from target VM");
        }
        p = this.path;
    }
    // create UNIX socket
    int s = socket();
    // connect to target VM
    try {
        connect(s, p);
    } catch (IOException x) {
        close(s);
        throw x;
    }
    IOException ioe = null;
    // <ver> <cmd> <args...>
    try {
        writeString(s, PROTOCOL_VERSION);
        writeString(s, cmd);
        for (int i = 0; i < 3; i++) {
            if (i < args.length && args[i] != null) {
                writeString(s, (String) args[i]);
            } else {
                writeString(s, "");
            }
        }
    } catch (IOException x) {
        ioe = x;
    }
    // Create an input stream to read reply
    SocketInputStream sis = new SocketInputStream(s);
    // Read the command completion status
    int completionStatus;
    try {
        completionStatus = readInt(sis);
    } catch (IOException x) {
        sis.close();
        if (ioe != null) {
            throw ioe;
        } else {
            throw x;
        }
    }
    if (completionStatus != 0) {
        // read from the stream and use that as the error message
        String message = readErrorMessage(sis);
        sis.close();
        // error.
        if (completionStatus == ATTACH_ERROR_BADVERSION) {
            throw new IOException("Protocol mismatch with target VM");
        }
        // thrown.
        if (cmd.equals("load")) {
            throw new AgentLoadException("Failed to load agent library");
        } else {
            if (message == null) {
                throw new AttachOperationFailedException("Command failed in target VM");
            } else {
                throw new AttachOperationFailedException(message);
            }
        }
    }
    // Return the input stream so that the command output can be read
    return sis;
}
Also used : AttachOperationFailedException(com.sun.tools.attach.AttachOperationFailedException) AgentLoadException(com.sun.tools.attach.AgentLoadException) IOException(java.io.IOException)

Aggregations

AgentLoadException (com.sun.tools.attach.AgentLoadException)8 IOException (java.io.IOException)8 AgentInitializationException (com.sun.tools.attach.AgentInitializationException)3 AttachOperationFailedException (com.sun.tools.attach.AttachOperationFailedException)3 VirtualMachine (com.sun.tools.attach.VirtualMachine)3 File (java.io.File)3 Properties (java.util.Properties)3 JMXServiceURL (javax.management.remote.JMXServiceURL)2 AttachNotSupportedException (com.sun.tools.attach.AttachNotSupportedException)1