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 package lib.jdb;
25
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.io.OutputStream;
29 import java.io.PrintWriter;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.LinkedList;
34 import java.util.List;
35 import java.util.concurrent.TimeUnit;
36 import java.util.regex.Pattern;
37 import java.util.stream.Collectors;
38 import jdk.test.lib.JDKToolFinder;
39 import jdk.test.lib.Utils;
40 import jdk.test.lib.process.StreamPumper;
41
42 public class Jdb implements AutoCloseable {
43 public Jdb(String... args) {
44 ProcessBuilder pb = new ProcessBuilder(JDKToolFinder.getTestJDKTool("jdb"));
45 pb.command().addAll(Arrays.asList(args));
46 try {
47 jdb = pb.start();
48 } catch (IOException ex) {
49 throw new RuntimeException("failed to launch pdb", ex);
50 }
51 try {
52 StreamPumper stdout = new StreamPumper(jdb.getInputStream());
53 StreamPumper stderr = new StreamPumper(jdb.getErrorStream());
54
55 stdout.addPump(new StreamPumper.StreamPump(outputHandler));
56 stderr.addPump(new StreamPumper.StreamPump(outputHandler));
57
58 stdout.process();
59 stderr.process();
60
61 inputWriter = new PrintWriter(jdb.getOutputStream(), true);
62 } catch (Throwable ex) {
63 // terminate jdb if something went wrong
64 jdb.destroy();
65 throw ex;
190 }
191 }
192 if (!jdb.isAlive()) {
193 // ensure we get the whole output
194 reply = outputHandler.reset();
195 logJdb(reply);
196 if (!allowExit) {
197 throw new RuntimeException("waitForPrompt timed out after " + (timeout/1000)
198 + " seconds, looking for '" + promptPattern + "', in " + lines + " lines");
199 }
200 return reply;
201 }
202 }
203 // timeout
204 logJdb(outputHandler.get());
205 throw new RuntimeException("waitForPrompt timed out after " + (timeout/1000)
206 + " seconds, looking for '" + promptPattern + "', in " + lines + " lines");
207 }
208
209 public List<String> command(JdbCommand cmd) {
210 if (!jdb.isAlive()) {
211 if (cmd.allowExit) {
212 // return remaining output
213 return outputHandler.reset();
214 }
215 throw new RuntimeException("Attempt to send command '" + cmd.cmd + "' to terminated jdb");
216 }
217
218 log("> " + cmd.cmd);
219
220 inputWriter.println(cmd.cmd);
221
222 if (inputWriter.checkError()) {
223 throw new RuntimeException("Unexpected IO error while writing command '" + cmd.cmd + "' to jdb stdin stream");
224 }
225
226 return waitForPrompt(1, cmd.allowExit);
227 }
228
229 public List<String> command(String cmd) {
230 return command(new JdbCommand(cmd));
231 }
232
233 // sends "cont" command up to maxTimes until debuggee exit
234 public void contToExit(int maxTimes) {
235 boolean exited = false;
236 JdbCommand cont = JdbCommand.cont().allowExit();
237 for (int i = 0; i < maxTimes && jdb.isAlive(); i++) {
238 String reply = command(cont).stream().collect(Collectors.joining(lineSeparator));
239 if (reply.contains(APPLICATION_EXIT)) {
240 exited = true;
241 break;
242 }
243 }
244 if (!exited && jdb.isAlive()) {
245 throw new RuntimeException("Debuggee did not exit after " + maxTimes + " <cont> commands");
246 }
|
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 package lib.jdb;
25
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.io.OutputStream;
29 import java.io.PrintWriter;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.Collections;
34 import java.util.LinkedList;
35 import java.util.List;
36 import java.util.concurrent.TimeUnit;
37 import java.util.regex.Pattern;
38 import java.util.stream.Collectors;
39 import jdk.test.lib.JDKToolFinder;
40 import jdk.test.lib.Utils;
41 import jdk.test.lib.process.StreamPumper;
42
43 public class Jdb implements AutoCloseable {
44
45 public Jdb(String... args) {
46 this(Arrays.asList(args));
47 }
48
49 public Jdb(Collection<String> args) {
50 ProcessBuilder pb = new ProcessBuilder(JDKToolFinder.getTestJDKTool("jdb"));
51 pb.command().addAll(args);
52 try {
53 jdb = pb.start();
54 } catch (IOException ex) {
55 throw new RuntimeException("failed to launch pdb", ex);
56 }
57 try {
58 StreamPumper stdout = new StreamPumper(jdb.getInputStream());
59 StreamPumper stderr = new StreamPumper(jdb.getErrorStream());
60
61 stdout.addPump(new StreamPumper.StreamPump(outputHandler));
62 stderr.addPump(new StreamPumper.StreamPump(outputHandler));
63
64 stdout.process();
65 stderr.process();
66
67 inputWriter = new PrintWriter(jdb.getOutputStream(), true);
68 } catch (Throwable ex) {
69 // terminate jdb if something went wrong
70 jdb.destroy();
71 throw ex;
196 }
197 }
198 if (!jdb.isAlive()) {
199 // ensure we get the whole output
200 reply = outputHandler.reset();
201 logJdb(reply);
202 if (!allowExit) {
203 throw new RuntimeException("waitForPrompt timed out after " + (timeout/1000)
204 + " seconds, looking for '" + promptPattern + "', in " + lines + " lines");
205 }
206 return reply;
207 }
208 }
209 // timeout
210 logJdb(outputHandler.get());
211 throw new RuntimeException("waitForPrompt timed out after " + (timeout/1000)
212 + " seconds, looking for '" + promptPattern + "', in " + lines + " lines");
213 }
214
215 public List<String> command(JdbCommand cmd) {
216 return command(cmd, false);
217 }
218
219 public List<String> command(JdbCommand cmd, boolean waitForSimplePrompt) {
220 if (!jdb.isAlive()) {
221 if (cmd.allowExit) {
222 // return remaining output
223 return outputHandler.reset();
224 }
225 throw new RuntimeException("Attempt to send command '" + cmd.cmd + "' to terminated jdb");
226 }
227
228 log("> " + cmd.cmd);
229
230 inputWriter.println(cmd.cmd);
231
232 if (inputWriter.checkError()) {
233 throw new RuntimeException("Unexpected IO error while writing command '" + cmd.cmd + "' to jdb stdin stream");
234 }
235
236 return waitForSimplePrompt ? waitForSimplePrompt(1, cmd.allowExit) : waitForPrompt(1, cmd.allowExit);
237 }
238
239 public List<String> command(String cmd) {
240 return command(new JdbCommand(cmd));
241 }
242
243 // sends "cont" command up to maxTimes until debuggee exit
244 public void contToExit(int maxTimes) {
245 boolean exited = false;
246 JdbCommand cont = JdbCommand.cont().allowExit();
247 for (int i = 0; i < maxTimes && jdb.isAlive(); i++) {
248 String reply = command(cont).stream().collect(Collectors.joining(lineSeparator));
249 if (reply.contains(APPLICATION_EXIT)) {
250 exited = true;
251 break;
252 }
253 }
254 if (!exited && jdb.isAlive()) {
255 throw new RuntimeException("Debuggee did not exit after " + maxTimes + " <cont> commands");
256 }
|