0N/A/*
3261N/A * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage sun.tools.attach;
0N/A
0N/Aimport com.sun.tools.attach.VirtualMachine;
0N/Aimport com.sun.tools.attach.AgentLoadException;
0N/Aimport com.sun.tools.attach.AttachNotSupportedException;
0N/Aimport com.sun.tools.attach.spi.AttachProvider;
0N/Aimport java.io.InputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.io.File;
0N/Aimport java.util.Properties;
0N/A
0N/A/*
0N/A * Linux implementation of HotSpotVirtualMachine
0N/A */
0N/Apublic class LinuxVirtualMachine extends HotSpotVirtualMachine {
4629N/A // "/tmp" is used as a global well-known location for the files
4629N/A // .java_pid<pid>. and .attach_pid<pid>. It is important that this
4629N/A // location is the same for all processes, otherwise the tools
4629N/A // will not be able to find all Hotspot processes.
4629N/A // Any changes to this needs to be synchronized with HotSpot.
4629N/A private static final String tmpdir = "/tmp";
0N/A
0N/A // Indicates if this machine uses the old LinuxThreads
0N/A static boolean isLinuxThreads;
0N/A
0N/A // The patch to the socket file created by the target VM
0N/A String path;
0N/A
0N/A /**
0N/A * Attaches to the target VM
0N/A */
0N/A LinuxVirtualMachine(AttachProvider provider, String vmid)
0N/A throws AttachNotSupportedException, IOException
0N/A {
0N/A super(provider, vmid);
0N/A
0N/A // This provider only understands pids
0N/A int pid;
0N/A try {
0N/A pid = Integer.parseInt(vmid);
0N/A } catch (NumberFormatException x) {
0N/A throw new AttachNotSupportedException("Invalid process identifier");
0N/A }
0N/A
0N/A // Find the socket file. If not found then we attempt to start the
0N/A // attach mechanism in the target VM by sending it a QUIT signal.
0N/A // Then we attempt to find the socket file again.
0N/A path = findSocketFile(pid);
0N/A if (path == null) {
0N/A File f = createAttachFile(pid);
0N/A try {
0N/A // On LinuxThreads each thread is a process and we don't have the
0N/A // pid of the VMThread which has SIGQUIT unblocked. To workaround
0N/A // this we get the pid of the "manager thread" that is created
0N/A // by the first call to pthread_create. This is parent of all
0N/A // threads (except the initial thread).
0N/A if (isLinuxThreads) {
0N/A int mpid;
0N/A try {
0N/A mpid = getLinuxThreadsManager(pid);
0N/A } catch (IOException x) {
0N/A throw new AttachNotSupportedException(x.getMessage());
0N/A }
0N/A assert(mpid >= 1);
0N/A sendQuitToChildrenOf(mpid);
0N/A } else {
0N/A sendQuitTo(pid);
0N/A }
0N/A
0N/A // give the target VM time to start the attach mechanism
0N/A int i = 0;
0N/A long delay = 200;
0N/A int retries = (int)(attachTimeout() / delay);
0N/A do {
0N/A try {
0N/A Thread.sleep(delay);
0N/A } catch (InterruptedException x) { }
0N/A path = findSocketFile(pid);
0N/A i++;
0N/A } while (i <= retries && path == null);
0N/A if (path == null) {
0N/A throw new AttachNotSupportedException(
0N/A "Unable to open socket file: target process not responding " +
0N/A "or HotSpot VM not loaded");
0N/A }
0N/A } finally {
0N/A f.delete();
0N/A }
0N/A }
0N/A
0N/A // Check that the file owner/permission to avoid attaching to
0N/A // bogus process
0N/A checkPermissions(path);
0N/A
0N/A // Check that we can connect to the process
0N/A // - this ensures we throw the permission denied error now rather than
0N/A // later when we attempt to enqueue a command.
0N/A int s = socket();
0N/A try {
0N/A connect(s, path);
0N/A } finally {
0N/A close(s);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Detach from the target VM
0N/A */
0N/A public void detach() throws IOException {
0N/A synchronized (this) {
0N/A if (this.path != null) {
0N/A this.path = null;
0N/A }
0N/A }
0N/A }
0N/A
0N/A // protocol version
0N/A private final static String PROTOCOL_VERSION = "1";
0N/A
0N/A // known errors
0N/A private final static int ATTACH_ERROR_BADVERSION = 101;
0N/A
0N/A /**
0N/A * Execute the given command in the target VM.
0N/A */
0N/A InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOException {
0N/A assert args.length <= 3; // includes null
0N/A
0N/A // did we detach?
0N/A String p;
0N/A synchronized (this) {
0N/A if (this.path == null) {
0N/A throw new IOException("Detached from target VM");
0N/A }
0N/A p = this.path;
0N/A }
0N/A
0N/A // create UNIX socket
0N/A int s = socket();
0N/A
0N/A // connect to target VM
0N/A try {
0N/A connect(s, p);
0N/A } catch (IOException x) {
0N/A close(s);
0N/A throw x;
0N/A }
0N/A
0N/A IOException ioe = null;
0N/A
0N/A // connected - write request
0N/A // <ver> <cmd> <args...>
0N/A try {
0N/A writeString(s, PROTOCOL_VERSION);
0N/A writeString(s, cmd);
0N/A
0N/A for (int i=0; i<3; i++) {
0N/A if (i < args.length && args[i] != null) {
0N/A writeString(s, (String)args[i]);
0N/A } else {
0N/A writeString(s, "");
0N/A }
0N/A }
0N/A } catch (IOException x) {
0N/A ioe = x;
0N/A }
0N/A
0N/A
0N/A // Create an input stream to read reply
0N/A SocketInputStream sis = new SocketInputStream(s);
0N/A
0N/A // Read the command completion status
0N/A int completionStatus;
0N/A try {
0N/A completionStatus = readInt(sis);
0N/A } catch (IOException x) {
0N/A sis.close();
0N/A if (ioe != null) {
0N/A throw ioe;
0N/A } else {
0N/A throw x;
0N/A }
0N/A }
0N/A
0N/A if (completionStatus != 0) {
0N/A sis.close();
0N/A
0N/A // In the event of a protocol mismatch then the target VM
0N/A // returns a known error so that we can throw a reasonable
0N/A // error.
0N/A if (completionStatus == ATTACH_ERROR_BADVERSION) {
0N/A throw new IOException("Protocol mismatch with target VM");
0N/A }
0N/A
0N/A // Special-case the "load" command so that the right exception is
0N/A // thrown.
0N/A if (cmd.equals("load")) {
0N/A throw new AgentLoadException("Failed to load agent library");
0N/A } else {
0N/A throw new IOException("Command failed in target VM");
0N/A }
0N/A }
0N/A
0N/A // Return the input stream so that the command output can be read
0N/A return sis;
0N/A }
0N/A
0N/A /*
0N/A * InputStream for the socket connection to get target VM
0N/A */
0N/A private class SocketInputStream extends InputStream {
0N/A int s;
0N/A
0N/A public SocketInputStream(int s) {
0N/A this.s = s;
0N/A }
0N/A
0N/A public synchronized int read() throws IOException {
0N/A byte b[] = new byte[1];
0N/A int n = this.read(b, 0, 1);
0N/A if (n == 1) {
0N/A return b[0] & 0xff;
0N/A } else {
0N/A return -1;
0N/A }
0N/A }
0N/A
0N/A public synchronized int read(byte[] bs, int off, int len) throws IOException {
0N/A if ((off < 0) || (off > bs.length) || (len < 0) ||
0N/A ((off + len) > bs.length) || ((off + len) < 0)) {
0N/A throw new IndexOutOfBoundsException();
0N/A } else if (len == 0)
0N/A return 0;
0N/A
0N/A return LinuxVirtualMachine.read(s, bs, off, len);
0N/A }
0N/A
0N/A public void close() throws IOException {
0N/A LinuxVirtualMachine.close(s);
0N/A }
0N/A }
0N/A
0N/A // Return the socket file for the given process.
0N/A private String findSocketFile(int pid) {
4629N/A File f = new File(tmpdir, ".java_pid" + pid);
0N/A if (!f.exists()) {
4629N/A return null;
0N/A }
4629N/A return f.getPath();
0N/A }
0N/A
0N/A // On Solaris/Linux a simple handshake is used to start the attach mechanism
0N/A // if not already started. The client creates a .attach_pid<pid> file in the
2458N/A // target VM's working directory (or temp directory), and the SIGQUIT handler
2458N/A // checks for the file.
0N/A private File createAttachFile(int pid) throws IOException {
0N/A String fn = ".attach_pid" + pid;
0N/A String path = "/proc/" + pid + "/cwd/" + fn;
0N/A File f = new File(path);
0N/A try {
0N/A f.createNewFile();
0N/A } catch (IOException x) {
2458N/A f = new File(tmpdir, fn);
0N/A f.createNewFile();
0N/A }
0N/A return f;
0N/A }
0N/A
0N/A /*
0N/A * Write/sends the given to the target VM. String is transmitted in
0N/A * UTF-8 encoding.
0N/A */
0N/A private void writeString(int fd, String s) throws IOException {
0N/A if (s.length() > 0) {
0N/A byte b[];
0N/A try {
0N/A b = s.getBytes("UTF-8");
0N/A } catch (java.io.UnsupportedEncodingException x) {
0N/A throw new InternalError();
0N/A }
0N/A LinuxVirtualMachine.write(fd, b, 0, b.length);
0N/A }
0N/A byte b[] = new byte[1];
0N/A b[0] = 0;
0N/A write(fd, b, 0, 1);
0N/A }
0N/A
0N/A
0N/A //-- native methods
0N/A
0N/A static native boolean isLinuxThreads();
0N/A
0N/A static native int getLinuxThreadsManager(int pid) throws IOException;
0N/A
0N/A static native void sendQuitToChildrenOf(int pid) throws IOException;
0N/A
0N/A static native void sendQuitTo(int pid) throws IOException;
0N/A
0N/A static native void checkPermissions(String path) throws IOException;
0N/A
0N/A static native int socket() throws IOException;
0N/A
0N/A static native void connect(int fd, String path) throws IOException;
0N/A
0N/A static native void close(int fd) throws IOException;
0N/A
0N/A static native int read(int fd, byte buf[], int off, int bufLen) throws IOException;
0N/A
0N/A static native void write(int fd, byte buf[], int off, int bufLen) throws IOException;
0N/A
0N/A static {
0N/A System.loadLibrary("attach");
0N/A isLinuxThreads = isLinuxThreads();
0N/A }
0N/A}