/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* Linux implementation of HotSpotVirtualMachine
*/
// "/tmp" is used as a global well-known location for the files
// .java_pid<pid>. and .attach_pid<pid>. It is important that this
// location is the same for all processes, otherwise the tools
// will not be able to find all Hotspot processes.
// Any changes to this needs to be synchronized with HotSpot.
// Indicates if this machine uses the old LinuxThreads
static boolean isLinuxThreads;
// The patch to the socket file created by the target VM
/**
* Attaches to the target VM
*/
{
// This provider only understands pids
int pid;
try {
} catch (NumberFormatException x) {
throw new AttachNotSupportedException("Invalid process identifier");
}
// Find the socket file. If not found then we attempt to start the
// attach mechanism in the target VM by sending it a QUIT signal.
// Then we attempt to find the socket file again.
try {
// On LinuxThreads each thread is a process and we don't have the
// pid of the VMThread which has SIGQUIT unblocked. To workaround
// this we get the pid of the "manager thread" that is created
// by the first call to pthread_create. This is parent of all
// threads (except the initial thread).
if (isLinuxThreads) {
int mpid;
try {
} catch (IOException x) {
throw new AttachNotSupportedException(x.getMessage());
}
assert(mpid >= 1);
} else {
}
// give the target VM time to start the attach mechanism
int i = 0;
long delay = 200;
do {
try {
} catch (InterruptedException x) { }
i++;
throw new AttachNotSupportedException(
"Unable to open socket file: target process not responding " +
"or HotSpot VM not loaded");
}
} finally {
f.delete();
}
}
// Check that the file owner/permission to avoid attaching to
// bogus process
// 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 {
} finally {
close(s);
}
}
/**
* Detach from the target VM
*/
synchronized (this) {
}
}
}
// protocol version
// known errors
/**
* Execute the given command in the target VM.
*/
// did we detach?
String p;
synchronized (this) {
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;
}
// connected - write request
// <ver> <cmd> <args...>
try {
writeString(s, cmd);
for (int i=0; i<3; i++) {
} else {
writeString(s, "");
}
}
} catch (IOException x) {
ioe = x;
}
// Create an input stream to read reply
// Read the command completion status
int completionStatus;
try {
} catch (IOException x) {
throw ioe;
} else {
throw x;
}
}
if (completionStatus != 0) {
// In the event of a protocol mismatch then the target VM
// returns a known error so that we can throw a reasonable
// error.
if (completionStatus == ATTACH_ERROR_BADVERSION) {
throw new IOException("Protocol mismatch with target VM");
}
// Special-case the "load" command so that the right exception is
// thrown.
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;
}
/*
* InputStream for the socket connection to get target VM
*/
int s;
public SocketInputStream(int s) {
this.s = s;
}
byte b[] = new byte[1];
if (n == 1) {
return b[0] & 0xff;
} else {
return -1;
}
}
throw new IndexOutOfBoundsException();
} else if (len == 0)
return 0;
}
}
}
// Return the socket file for the given process.
if (!f.exists()) {
return null;
}
return f.getPath();
}
// if not already started. The client creates a .attach_pid<pid> file in the
// target VM's working directory (or temp directory), and the SIGQUIT handler
// checks for the file.
try {
f.createNewFile();
} catch (IOException x) {
f.createNewFile();
}
return f;
}
/*
* UTF-8 encoding.
*/
if (s.length() > 0) {
byte b[];
try {
b = s.getBytes("UTF-8");
throw new InternalError();
}
}
byte b[] = new byte[1];
b[0] = 0;
}
//-- native methods
static native boolean isLinuxThreads();
static {
}
}