use of com.sun.tools.attach.AttachOperationFailedException in project jdk8u_jdk by JetBrains.
the class BsdVirtualMachine 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;
}
use of com.sun.tools.attach.AttachOperationFailedException in project jdk8u_jdk by JetBrains.
the class AixVirtualMachine 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;
}
use of com.sun.tools.attach.AttachOperationFailedException in project openj9 by eclipse.
the class OpenJ9VirtualMachine method parseResponse.
@SuppressWarnings("boxing")
private static boolean parseResponse(String response) throws IOException, AgentInitializationException, AgentLoadException, IllegalArgumentException, AttachOperationFailedException {
if (response.startsWith(ERROR)) {
int responseLength = response.indexOf('\0');
String trimmedResponse;
if (-1 == responseLength) {
trimmedResponse = response;
} else {
trimmedResponse = response.substring(0, responseLength);
}
if (response.contains(EXCEPTION_IOEXCEPTION)) {
// $NON-NLS-1$
throw new IOException(getString("K0576", trimmedResponse));
} else if (response.contains(EXCEPTION_AGENT_INITIALIZATION_EXCEPTION)) {
Integer status = getStatusValue(trimmedResponse);
if (null == status) {
throw new AgentInitializationException(trimmedResponse);
} else {
throw new AgentInitializationException(trimmedResponse, status);
}
} else if (response.contains(EXCEPTION_AGENT_LOAD_EXCEPTION)) {
throw new AgentLoadException(trimmedResponse);
} else if (response.contains(EXCEPTION_IOEXCEPTION)) {
// $NON-NLS-1$
throw new IOException(getString("K0576", trimmedResponse));
} else if (response.contains(EXCEPTION_ILLEGAL_ARGUMENT_EXCEPTION)) {
// $NON-NLS-1$
throw new IllegalArgumentException(getString("K05de", trimmedResponse));
} else if (response.contains(EXCEPTION_ATTACH_OPERATION_FAILED_EXCEPTION)) {
// $NON-NLS-1$
throw new AttachOperationFailedException(getString("k05dc", trimmedResponse));
}
return false;
} else if (response.startsWith(ACK) || response.startsWith(ATTACH_RESULT)) {
return true;
} else {
return false;
}
}
use of com.sun.tools.attach.AttachOperationFailedException in project jdk8u_jdk by JetBrains.
the class JCmd method main.
public static void main(String[] args) {
Arguments arg = null;
try {
arg = new Arguments(args);
} catch (IllegalArgumentException ex) {
System.err.println("Error parsing arguments: " + ex.getMessage() + "\n");
Arguments.usage();
System.exit(1);
}
if (arg.isShowUsage()) {
Arguments.usage();
System.exit(1);
}
if (arg.isListProcesses()) {
List<VirtualMachineDescriptor> vmds = VirtualMachine.list();
for (VirtualMachineDescriptor vmd : vmds) {
System.out.println(vmd.id() + " " + vmd.displayName());
}
System.exit(0);
}
List<String> pids = new ArrayList<String>();
if (arg.getPid() == 0) {
// find all VMs
List<VirtualMachineDescriptor> vmds = VirtualMachine.list();
for (VirtualMachineDescriptor vmd : vmds) {
if (!isJCmdProcess(vmd)) {
pids.add(vmd.id());
}
}
} else if (arg.getProcessSubstring() != null) {
// use the partial class-name match
List<VirtualMachineDescriptor> vmds = VirtualMachine.list();
for (VirtualMachineDescriptor vmd : vmds) {
if (isJCmdProcess(vmd)) {
continue;
}
try {
String mainClass = getMainClass(vmd);
if (mainClass != null && mainClass.indexOf(arg.getProcessSubstring()) != -1) {
pids.add(vmd.id());
}
} catch (MonitorException | URISyntaxException e) {
if (e.getMessage() != null) {
System.err.println(e.getMessage());
} else {
Throwable cause = e.getCause();
if ((cause != null) && (cause.getMessage() != null)) {
System.err.println(cause.getMessage());
} else {
e.printStackTrace();
}
}
}
}
if (pids.isEmpty()) {
System.err.println("Could not find any processes matching : '" + arg.getProcessSubstring() + "'");
System.exit(1);
}
} else if (arg.getPid() == -1) {
System.err.println("Invalid pid specified");
System.exit(1);
} else {
// Use the found pid
pids.add(arg.getPid() + "");
}
boolean success = true;
for (String pid : pids) {
System.out.println(pid + ":");
if (arg.isListCounters()) {
listCounters(pid);
} else {
try {
executeCommandForPid(pid, arg.getCommand());
} catch (AttachOperationFailedException ex) {
System.err.println(ex.getMessage());
success = false;
} catch (Exception ex) {
ex.printStackTrace();
success = false;
}
}
}
System.exit(success ? 0 : 1);
}
use of com.sun.tools.attach.AttachOperationFailedException in project jdk8u_jdk by JetBrains.
the class StartManagementAgent method testRemoteAgent.
public static void testRemoteAgent(VirtualMachine vm) throws Exception {
int port = Utils.getFreePort();
// try to connect - should fail
tryConnect(port, false);
// start agent
System.out.println("Starting agent on port: " + port);
Properties mgmtProps = new Properties();
mgmtProps.put("com.sun.management.jmxremote.port", port);
mgmtProps.put("com.sun.management.jmxremote.authenticate", "false");
mgmtProps.put("com.sun.management.jmxremote.ssl", "false");
vm.startManagementAgent(mgmtProps);
// try to connect - should work
tryConnect(port, true);
// try to start again - should fail
boolean exception = false;
try {
vm.startManagementAgent(mgmtProps);
} catch (AttachOperationFailedException ex) {
// expected
exception = true;
}
if (!exception) {
throw new Exception("Expected the second call to vm.startManagementAgent() to fail");
}
}
Aggregations