Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -48,6 +48,7 @@ public class VirtualMachineImpl extends HotSpotVirtualMachine {
// Any changes to this needs to be synchronized with HotSpot.
private static final String tmpdir;
String socket_path;
private int ver = VERSION_1; // updated in ctor depending on detectVersion result

/**
* Attaches to the target VM
Expand Down Expand Up @@ -107,14 +108,18 @@ public class VirtualMachineImpl extends HotSpotVirtualMachine {
// bogus process
checkPermissions(socket_path);

// Check that we can connect to the process
// - this ensures we throw the permission denied error now rather than
// later when we attempt to enqueue a command.
int s = socket();
try {
connect(s, socket_path);
} finally {
close(s);
if (isAPIv2Enabled()) {
ver = detectVersion();
} else {
// Check that we can connect to the process
// - this ensures we throw the permission denied error now rather than
// later when we attempt to enqueue a command.
int s = socket();
try {
connect(s, socket_path);
} finally {
close(s);
}
}
}

Expand All @@ -129,14 +134,10 @@ public void detach() throws IOException {
}
}

// protocol version
private static final String PROTOCOL_VERSION = "1";

/**
* Execute the given command in the target VM.
*/
InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOException {
assert args.length <= 3; // includes null
checkNulls(args);

// did we detach?
Expand All @@ -162,16 +163,8 @@ InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOEx
// connected - write request
// <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, "");
}
}
SocketOutputStream writer = new SocketOutputStream(s);
writeCommand(writer, ver, cmd, args);
} catch (IOException x) {
ioe = x;
}
Expand All @@ -187,6 +180,16 @@ InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOEx
return sis;
}

private static class SocketOutputStream implements AttachOutputStream {
private int fd;
public SocketOutputStream(int fd) {
this.fd = fd;
}
@Override
public void write(byte[] buffer, int offset, int length) throws IOException {
VirtualMachineImpl.write(fd, buffer, offset, length);
}
}
/*
* InputStream for the socket connection to get target VM
*/
Expand All @@ -206,20 +209,6 @@ protected void close(long fd) throws IOException {
}
}

/*
* Write/sends the given to the target VM. String is transmitted in
* UTF-8 encoding.
*/
private void writeString(int fd, String s) throws IOException {
if (s.length() > 0) {
byte[] b = s.getBytes(UTF_8);
VirtualMachineImpl.write(fd, b, 0, b.length);
}
byte b[] = new byte[1];
b[0] = 0;
write(fd, b, 0, 1);
}

private File createAttachFile(int pid) throws IOException {
File f = new File(tmpdir, ".attach_pid" + pid);
createAttachFile0(f.getPath());
Expand Down