Path: blob/master/test/hotspot/jtreg/serviceability/jdwp/DebuggeeLauncher.java
41149 views
/*1* Copyright (c) 2016, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.IOException;24import java.net.ServerSocket;25import java.util.StringTokenizer;26import jdk.test.lib.JDKToolFinder;27import jdk.test.lib.Utils;28import static jdk.test.lib.Asserts.assertFalse;2930/**31* Launches the debuggee with the necessary JDWP options and handles the output32*/33public class DebuggeeLauncher implements StreamHandler.Listener {3435public interface Listener {3637/**38* Callback to use when a module name is received from the debuggee39*40* @param modName module name reported by the debuggee41*/42void onDebuggeeModuleInfo(String modName);4344/**45* Callback to use when the debuggee completes sending out the info46*/47void onDebuggeeSendingCompleted();4849/**50* Callback to handle any debuggee error51*52* @param line line from the debuggee's stderr53*/54void onDebuggeeError(String line);55}5657private static int jdwpPort = -1;58private static final String CLS_DIR = System.getProperty("test.classes", "").trim();59private static final String DEBUGGEE = "AllModulesCommandTestDebuggee";60private Process p;61private final Listener listener;62private StreamHandler inputHandler;63private StreamHandler errorHandler;6465/**66* @param listener the listener we report the debuggee events to67*/68public DebuggeeLauncher(Listener listener) {69this.listener = listener;70}7172/**73* Starts the debuggee with the necessary JDWP options and handles the74* debuggee's stdout and stderr outputs75*76* @throws Throwable77*/78public void launchDebuggee() throws Throwable {7980ProcessBuilder pb = new ProcessBuilder(getCommand());81p = pb.start();82inputHandler = new StreamHandler(p.getInputStream(), this);83errorHandler = new StreamHandler(p.getErrorStream(), this);84inputHandler.start();85errorHandler.start();86}8788/**89* Command to start the debuggee with the JDWP options and using the JDK90* under test91*92* @return the command93*/94private String[] getCommand() {95return new String[]{96JDKToolFinder.getTestJDKTool("java"),97getJdwpOptions(),98"-cp",99CLS_DIR,100DEBUGGEE101};102}103104/**105* Terminates the debuggee106*/107public void terminateDebuggee() {108if (p.isAlive()) {109p.destroyForcibly();110}111}112113/**114* Debuggee JDWP options115*116* @return the JDWP options to start the debuggee with117*/118private static String getJdwpOptions() {119return "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=" + getJdwpPort();120}121122/**123* Find an available port for the JDWP session124*125* @return JDWP port126*/127public static int getJdwpPort() {128if (jdwpPort == -1) {129jdwpPort = findFreePort();130assertFalse(jdwpPort == -1, "Can not find vailbale port for JDWP");131}132return jdwpPort;133}134135private static int findFreePort() {136try (ServerSocket socket = new ServerSocket(0)) {137return socket.getLocalPort();138} catch (IOException e) {139}140return -1;141}142143@Override144public void onStringRead(StreamHandler handler, String line) {145if (handler.equals(errorHandler)) {146terminateDebuggee();147listener.onDebuggeeError(line);148} else {149processDebuggeeOutput(line);150}151}152153private void processDebuggeeOutput(String line) {154StringTokenizer st = new StringTokenizer(line);155String token = st.nextToken();156switch (token) {157case "module":158listener.onDebuggeeModuleInfo(st.nextToken());159break;160case "ready":161listener.onDebuggeeSendingCompleted();162break;163}164}165}166167168