Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/serviceability/jdwp/DebuggeeLauncher.java
41149 views
1
/*
2
* Copyright (c) 2016, Oracle and/or its affiliates. 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 it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.io.IOException;
25
import java.net.ServerSocket;
26
import java.util.StringTokenizer;
27
import jdk.test.lib.JDKToolFinder;
28
import jdk.test.lib.Utils;
29
import static jdk.test.lib.Asserts.assertFalse;
30
31
/**
32
* Launches the debuggee with the necessary JDWP options and handles the output
33
*/
34
public class DebuggeeLauncher implements StreamHandler.Listener {
35
36
public interface Listener {
37
38
/**
39
* Callback to use when a module name is received from the debuggee
40
*
41
* @param modName module name reported by the debuggee
42
*/
43
void onDebuggeeModuleInfo(String modName);
44
45
/**
46
* Callback to use when the debuggee completes sending out the info
47
*/
48
void onDebuggeeSendingCompleted();
49
50
/**
51
* Callback to handle any debuggee error
52
*
53
* @param line line from the debuggee's stderr
54
*/
55
void onDebuggeeError(String line);
56
}
57
58
private static int jdwpPort = -1;
59
private static final String CLS_DIR = System.getProperty("test.classes", "").trim();
60
private static final String DEBUGGEE = "AllModulesCommandTestDebuggee";
61
private Process p;
62
private final Listener listener;
63
private StreamHandler inputHandler;
64
private StreamHandler errorHandler;
65
66
/**
67
* @param listener the listener we report the debuggee events to
68
*/
69
public DebuggeeLauncher(Listener listener) {
70
this.listener = listener;
71
}
72
73
/**
74
* Starts the debuggee with the necessary JDWP options and handles the
75
* debuggee's stdout and stderr outputs
76
*
77
* @throws Throwable
78
*/
79
public void launchDebuggee() throws Throwable {
80
81
ProcessBuilder pb = new ProcessBuilder(getCommand());
82
p = pb.start();
83
inputHandler = new StreamHandler(p.getInputStream(), this);
84
errorHandler = new StreamHandler(p.getErrorStream(), this);
85
inputHandler.start();
86
errorHandler.start();
87
}
88
89
/**
90
* Command to start the debuggee with the JDWP options and using the JDK
91
* under test
92
*
93
* @return the command
94
*/
95
private String[] getCommand() {
96
return new String[]{
97
JDKToolFinder.getTestJDKTool("java"),
98
getJdwpOptions(),
99
"-cp",
100
CLS_DIR,
101
DEBUGGEE
102
};
103
}
104
105
/**
106
* Terminates the debuggee
107
*/
108
public void terminateDebuggee() {
109
if (p.isAlive()) {
110
p.destroyForcibly();
111
}
112
}
113
114
/**
115
* Debuggee JDWP options
116
*
117
* @return the JDWP options to start the debuggee with
118
*/
119
private static String getJdwpOptions() {
120
return "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=" + getJdwpPort();
121
}
122
123
/**
124
* Find an available port for the JDWP session
125
*
126
* @return JDWP port
127
*/
128
public static int getJdwpPort() {
129
if (jdwpPort == -1) {
130
jdwpPort = findFreePort();
131
assertFalse(jdwpPort == -1, "Can not find vailbale port for JDWP");
132
}
133
return jdwpPort;
134
}
135
136
private static int findFreePort() {
137
try (ServerSocket socket = new ServerSocket(0)) {
138
return socket.getLocalPort();
139
} catch (IOException e) {
140
}
141
return -1;
142
}
143
144
@Override
145
public void onStringRead(StreamHandler handler, String line) {
146
if (handler.equals(errorHandler)) {
147
terminateDebuggee();
148
listener.onDebuggeeError(line);
149
} else {
150
processDebuggeeOutput(line);
151
}
152
}
153
154
private void processDebuggeeOutput(String line) {
155
StringTokenizer st = new StringTokenizer(line);
156
String token = st.nextToken();
157
switch (token) {
158
case "module":
159
listener.onDebuggeeModuleInfo(st.nextToken());
160
break;
161
case "ready":
162
listener.onDebuggeeSendingCompleted();
163
break;
164
}
165
}
166
}
167
168