Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/tools/attach/WindowsVirtualMachine.java
32288 views
/*1* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.tools.attach;2526import com.sun.tools.attach.AttachOperationFailedException;27import com.sun.tools.attach.AgentLoadException;28import com.sun.tools.attach.AttachNotSupportedException;29import com.sun.tools.attach.spi.AttachProvider;3031import sun.tools.attach.HotSpotVirtualMachine;3233import java.io.IOException;34import java.io.InputStream;35import java.util.Random;3637public class WindowsVirtualMachine extends HotSpotVirtualMachine {3839// the enqueue code stub (copied into each target VM)40private static byte[] stub;4142private volatile long hProcess; // handle to the process4344WindowsVirtualMachine(AttachProvider provider, String id)45throws AttachNotSupportedException, IOException46{47super(provider, id);4849int pid;50try {51pid = Integer.parseInt(id);52} catch (NumberFormatException x) {53throw new AttachNotSupportedException("Invalid process identifier");54}55hProcess = openProcess(pid);5657// The target VM might be a pre-6.0 VM so we enqueue a "null" command58// which minimally tests that the enqueue function exists in the target59// VM.60try {61enqueue(hProcess, stub, null, null);62} catch (IOException x) {63throw new AttachNotSupportedException(x.getMessage());64}65}6667public void detach() throws IOException {68synchronized (this) {69if (hProcess != -1) {70closeProcess(hProcess);71hProcess = -1;72}73}74}7576InputStream execute(String cmd, Object ... args)77throws AgentLoadException, IOException78{79assert args.length <= 3; // includes null8081// create a pipe using a random name82int r = (new Random()).nextInt();83String pipename = "\\\\.\\pipe\\javatool" + r;84long hPipe = createPipe(pipename);8586// check if we are detached - in theory it's possible that detach is invoked87// after this check but before we enqueue the command.88if (hProcess == -1) {89closePipe(hPipe);90throw new IOException("Detached from target VM");91}9293try {94// enqueue the command to the process95enqueue(hProcess, stub, cmd, pipename, args);9697// wait for command to complete - process will connect with the98// completion status99connectPipe(hPipe);100101// create an input stream for the pipe102PipedInputStream is = new PipedInputStream(hPipe);103104// read completion status105int status = readInt(is);106if (status != 0) {107// read from the stream and use that as the error message108String message = readErrorMessage(is);109// special case the load command so that the right exception is thrown110if (cmd.equals("load")) {111throw new AgentLoadException("Failed to load agent library");112} else {113if (message == null) {114throw new AttachOperationFailedException("Command failed in target VM");115} else {116throw new AttachOperationFailedException(message);117}118}119}120121// return the input stream122return is;123124} catch (IOException ioe) {125closePipe(hPipe);126throw ioe;127}128}129130// An InputStream based on a pipe to the target VM131private class PipedInputStream extends InputStream {132133private long hPipe;134135public PipedInputStream(long hPipe) {136this.hPipe = hPipe;137}138139public synchronized int read() throws IOException {140byte b[] = new byte[1];141int n = this.read(b, 0, 1);142if (n == 1) {143return b[0] & 0xff;144} else {145return -1;146}147}148149public synchronized int read(byte[] bs, int off, int len) throws IOException {150if ((off < 0) || (off > bs.length) || (len < 0) ||151((off + len) > bs.length) || ((off + len) < 0)) {152throw new IndexOutOfBoundsException();153} else if (len == 0)154return 0;155156return WindowsVirtualMachine.readPipe(hPipe, bs, off, len);157}158159public void close() throws IOException {160if (hPipe != -1) {161WindowsVirtualMachine.closePipe(hPipe);162hPipe = -1;163}164}165}166167168//-- native methods169170static native void init();171172static native byte[] generateStub();173174static native long openProcess(int pid) throws IOException;175176static native void closeProcess(long hProcess) throws IOException;177178static native long createPipe(String name) throws IOException;179180static native void closePipe(long hPipe) throws IOException;181182static native void connectPipe(long hPipe) throws IOException;183184static native int readPipe(long hPipe, byte buf[], int off, int buflen) throws IOException;185186static native void enqueue(long hProcess, byte[] stub,187String cmd, String pipename, Object ... args) throws IOException;188189static {190System.loadLibrary("attach");191init(); // native initialization192stub = generateStub(); // generate stub to copy into target process193}194}195196197