Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/aix/classes/sun/tools/attach/AixVirtualMachine.java
38892 views
/*1* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.2* Copyright 2015 SAP AG. All rights reserved.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation. Oracle designates this8* particular file as subject to the "Classpath" exception as provided9* by Oracle in the LICENSE file that accompanied this code.10*11* This code is distributed in the hope that it will be useful, but WITHOUT12* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or13* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License14* version 2 for more details (a copy is included in the LICENSE file that15* accompanied this code).16*17* You should have received a copy of the GNU General Public License version18* 2 along with this work; if not, write to the Free Software Foundation,19* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.20*21* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA22* or visit www.oracle.com if you need additional information or have any23* questions.24*/25package sun.tools.attach;2627import com.sun.tools.attach.AttachOperationFailedException;28import com.sun.tools.attach.AgentLoadException;29import com.sun.tools.attach.AttachNotSupportedException;30import com.sun.tools.attach.spi.AttachProvider;3132import java.io.InputStream;33import java.io.IOException;34import java.io.File;3536// Based on 'LinuxVirtualMachine.java'. All occurrences of the string37// "Linux" have been textually replaced by "Aix" to avoid confusion.3839/*40* Aix implementation of HotSpotVirtualMachine41*/42public class AixVirtualMachine extends HotSpotVirtualMachine {43// "/tmp" is used as a global well-known location for the files44// .java_pid<pid>. and .attach_pid<pid>. It is important that this45// location is the same for all processes, otherwise the tools46// will not be able to find all Hotspot processes.47// Any changes to this needs to be synchronized with HotSpot.48private static final String tmpdir = "/tmp";4950// The patch to the socket file created by the target VM51String path;5253/**54* Attaches to the target VM55*/56AixVirtualMachine(AttachProvider provider, String vmid)57throws AttachNotSupportedException, IOException58{59super(provider, vmid);6061// This provider only understands pids62int pid;63try {64pid = Integer.parseInt(vmid);65} catch (NumberFormatException x) {66throw new AttachNotSupportedException("Invalid process identifier");67}6869// Find the socket file. If not found then we attempt to start the70// attach mechanism in the target VM by sending it a QUIT signal.71// Then we attempt to find the socket file again.72path = findSocketFile(pid);73if (path == null) {74File f = createAttachFile(pid);75try {76sendQuitTo(pid);7778// give the target VM time to start the attach mechanism79int i = 0;80long delay = 200;81int retries = (int)(attachTimeout() / delay);82do {83try {84Thread.sleep(delay);85} catch (InterruptedException x) { }86path = findSocketFile(pid);87i++;88} while (i <= retries && path == null);89if (path == null) {90throw new AttachNotSupportedException(91"Unable to open socket file: target process not responding " +92"or HotSpot VM not loaded");93}94} finally {95f.delete();96}97}9899// Check that the file owner/permission to avoid attaching to100// bogus process101checkPermissions(path);102103// Check that we can connect to the process104// - this ensures we throw the permission denied error now rather than105// later when we attempt to enqueue a command.106int s = socket();107try {108connect(s, path);109} finally {110close(s);111}112}113114/**115* Detach from the target VM116*/117public void detach() throws IOException {118synchronized (this) {119if (this.path != null) {120this.path = null;121}122}123}124125// protocol version126private final static String PROTOCOL_VERSION = "1";127128// known errors129private final static int ATTACH_ERROR_BADVERSION = 101;130131/**132* Execute the given command in the target VM.133*/134InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOException {135assert args.length <= 3; // includes null136137// did we detach?138String p;139synchronized (this) {140if (this.path == null) {141throw new IOException("Detached from target VM");142}143p = this.path;144}145146// create UNIX socket147int s = socket();148149// connect to target VM150try {151connect(s, p);152} catch (IOException x) {153close(s);154throw x;155}156157IOException ioe = null;158159// connected - write request160// <ver> <cmd> <args...>161try {162writeString(s, PROTOCOL_VERSION);163writeString(s, cmd);164165for (int i=0; i<3; i++) {166if (i < args.length && args[i] != null) {167writeString(s, (String)args[i]);168} else {169writeString(s, "");170}171}172} catch (IOException x) {173ioe = x;174}175176177// Create an input stream to read reply178SocketInputStream sis = new SocketInputStream(s);179180// Read the command completion status181int completionStatus;182try {183completionStatus = readInt(sis);184} catch (IOException x) {185sis.close();186if (ioe != null) {187throw ioe;188} else {189throw x;190}191}192193if (completionStatus != 0) {194// read from the stream and use that as the error message195String message = readErrorMessage(sis);196sis.close();197198// In the event of a protocol mismatch then the target VM199// returns a known error so that we can throw a reasonable200// error.201if (completionStatus == ATTACH_ERROR_BADVERSION) {202throw new IOException("Protocol mismatch with target VM");203}204205// Special-case the "load" command so that the right exception is206// thrown.207if (cmd.equals("load")) {208throw new AgentLoadException("Failed to load agent library");209} else {210if (message == null) {211throw new AttachOperationFailedException("Command failed in target VM");212} else {213throw new AttachOperationFailedException(message);214}215}216}217218// Return the input stream so that the command output can be read219return sis;220}221222/*223* InputStream for the socket connection to get target VM224*/225private class SocketInputStream extends InputStream {226int s;227228public SocketInputStream(int s) {229this.s = s;230}231232public synchronized int read() throws IOException {233byte b[] = new byte[1];234int n = this.read(b, 0, 1);235if (n == 1) {236return b[0] & 0xff;237} else {238return -1;239}240}241242public synchronized int read(byte[] bs, int off, int len) throws IOException {243if ((off < 0) || (off > bs.length) || (len < 0) ||244((off + len) > bs.length) || ((off + len) < 0)) {245throw new IndexOutOfBoundsException();246} else if (len == 0)247return 0;248249return AixVirtualMachine.read(s, bs, off, len);250}251252public void close() throws IOException {253AixVirtualMachine.close(s);254}255}256257// Return the socket file for the given process.258private String findSocketFile(int pid) {259File f = new File(tmpdir, ".java_pid" + pid);260if (!f.exists()) {261return null;262}263return f.getPath();264}265266// On Solaris/Linux/Aix a simple handshake is used to start the attach mechanism267// if not already started. The client creates a .attach_pid<pid> file in the268// target VM's working directory (or temp directory), and the SIGQUIT handler269// checks for the file.270private File createAttachFile(int pid) throws IOException {271String fn = ".attach_pid" + pid;272String path = "/proc/" + pid + "/cwd/" + fn;273File f = new File(path);274try {275f.createNewFile();276} catch (IOException x) {277f = new File(tmpdir, fn);278f.createNewFile();279}280return f;281}282283/*284* Write/sends the given to the target VM. String is transmitted in285* UTF-8 encoding.286*/287private void writeString(int fd, String s) throws IOException {288if (s.length() > 0) {289byte b[];290try {291b = s.getBytes("UTF-8");292} catch (java.io.UnsupportedEncodingException x) {293throw new InternalError(x);294}295AixVirtualMachine.write(fd, b, 0, b.length);296}297byte b[] = new byte[1];298b[0] = 0;299write(fd, b, 0, 1);300}301302303//-- native methods304305static native void sendQuitTo(int pid) throws IOException;306307static native void checkPermissions(String path) throws IOException;308309static native int socket() throws IOException;310311static native void connect(int fd, String path) throws IOException;312313static native void close(int fd) throws IOException;314315static native int read(int fd, byte buf[], int off, int bufLen) throws IOException;316317static native void write(int fd, byte buf[], int off, int bufLen) throws IOException;318319static {320System.loadLibrary("attach");321}322}323324325